Professional Documents
Culture Documents
Objectives
Ver. 1.0
Session 17
To implement a graph, you need to first represent the given information in the form of a graph. The two most commonly used ways of representing a graph are as follows:
Adjacency Matrix Adjacency List
Ver. 1.0
Session 17
v2
1 0 0 0
v3
0 1 0 1
v4
0 0 0 0
Ver. 1.0
Session 17
Ver. 1.0
Session 17
Traversing a graph means visiting all the vertices in a graph. You can traverse a graph with the help of the following two methods:
Depth First Search (DFS) Breadth First Search (BFS)
Ver. 1.0
Session 17
Algorithm: DFS(v)
1. Push the starting vertex, v into the stack. 2. Repeat until the stack becomes empty:
a. Pop a vertex from the stack. b. Visit the popped vertex. c. Push all the unvisited vertices adjacent to the popped vertex into the stack.
Ver. 1.0
Session 17
v1
Ver. 1.0
Session 17
Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack
v1
Visited: v1
Ver. 1.0
Session 17
Pop a vertex, v1 from the stack Visit v1 Push all unvisited vertices adjacent to v1 into the stack
v2 v4
Visited:
v1
Ver. 1.0
Session 17
Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack
v2 v4
Visited: v1 v2
Ver. 1.0
Session 17
Pop a vertex, v2 from the stack Visit v2 Push all unvisited vertices adjacent to v2 into the stack
v6
v3 v4
Visited: v1 v2
Ver. 1.0
Session 17
Pop a vertex, v6 from the stack Visit v6 Push all unvisited vertices adjacent to v6 into the stack
v6
v3 v4
Visited: v1 v2 v6
Ver. 1.0
Session 17
Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack
v3 v4
Visited: v1 v2 v6 v3
Ver. 1.0
Session 17
Pop a vertex, v3 from the stack Visit v3 Push all unvisited vertices adjacent to v3 into the stack
v5 v4
Visited: v1 v2 v6 v3
Ver. 1.0
Session 17
Pop a vertex, v5 from the stack Visit v5 Push all unvisited vertices adjacent to v5 into the stack
v5 v4
Visited: v1 v2 v6 v3 v5
Ver. 1.0
Session 17
Pop a vertex, v4 from the stack Visit v4 Push all unvisited vertices adjacent to v4 into the stack
v4
Visited: v1 v2 v6 v3 v5 v4
Ver. 1.0
Session 17
Visited: v1 v2 v6 v3 v5 v4
Ver. 1.0
Session 17
Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. In such a case, you will not be able to traverse all the vertices from one single starting vertex.
Ver. 1.0
Session 17
To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph.
Ver. 1.0
Session 17
Algorithm: BFS(v)
1. Visit the starting vertex, v and insert it into a queue. 2. Repeat step 3 until the queue becomes empty. 3. Delete the front vertex from the queue, visit all its unvisited adjacent vertices, and insert them into the queue.
Ver. 1.0
Session 17
v1
v1
Ver. 1.0
Session 17
Remove a vertex v1 from the queue Visit all unvisited vertices adjacent to v1 and insert them in the queue
v1
Visited: v1
Ver. 1.0
Session 17
Remove a vertex v1 from the queue Visit all unvisited vertices adjacent to v1 and insert them in the queue
v2
v4
v1 v2 v4
Ver. 1.0
Session 17
Remove a vertex v2 from the queue Visit all unvisited vertices adjacent to v2 and insert them in the queue
v2
v4
v1 v2 v4
Ver. 1.0
Session 17
Remove a vertex v2 from the queue Visit all unvisited vertices adjacent to v2 and insert them in the queue
v4
v3
v6
v1 v2 v4 v3
Ver. 1.0
v6
Session 17
Remove a vertex v4 from the queue Visit all unvisited vertices adjacent to v4 and insert them in the queue
v4
v3
v6
v5
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Remove a vertex v3 from the queue Visit all unvisited vertices adjacent to v3 and insert them in the queue
v3
v6
v5
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Remove a vertex v6 from the queue Visit all unvisited vertices adjacent to v6 and insert them in the queue
v6
v5
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Remove a vertex v6 from the queue Visit all unvisited vertices adjacent to v6 and insert them in the queue
v5
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Remove a vertex v5 from the queue Visit all unvisited vertices adjacent to v5 and insert them in the queue
v5
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Remove a vertex v5 from the queue Visit all unvisited vertices adjacent to v5 and insert them in the queue
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
v1 v2 v4 v3
Ver. 1.0
v6 v5
Session 17
Although the preceding algorithm provides a simple and convenient method to traverse a graph, the algorithm will not work correctly if the graph is not connected. In such a case, you will not be able to traverse all the vertices from one single starting vertex.
Ver. 1.0
Session 17
To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph.
Ver. 1.0
Session 17
Problem Statement:
You have to represent a set of cities and the distances between them in the form of a graph. Write a program to represent the graph in the form of an adjacency matrix.
Ver. 1.0
Session 17
Many problems can be easily solved by reducing them in the form of a graph Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology.
Ver. 1.0
Session 17
The shortest path problem can be solved by applying the Dijkstras algorithm on a graph The Dijkstras algorithm is based on the greedy approach The steps in the Dijkstras algorithm are as follows:
1. Choose vertex v corresponding to the smallest distance recorded in the DISTANCE array such that v is not already in FINAL. 2. Add v to FINAL. 3. Repeat for each vertex w in the graph that is not in FINAL:
a. If the path from v1 to w via v is shorter than the previously recorded distance from v1 to w (If ((DISTANCE[v] + weight of edge(v,w)) < DISTANCE[w])):
i. Set DISTANCE[w]=DISTANCE[v] + weight of edge(v,w).
Ver. 1.0
Suppose you need to find the shortest distance of all the vertices from vertex v1.
4 6
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5
v3
v4 v5 3
v6
Session 17
In the DISTANCE array, vertex v4 has the shortest distance from vertex v1.
4 6
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3
v4 v5 3
v6
Session 17
v1 v2 = 5 v1 v4 v2 = 3 + =
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3
v4 v5 3
v6
Session 17
v1 v3 = v1 v4 v3 = 3 + 2 = 5
3 3
v3 5
DISTANCE
FINAL
Ver. 1.0
v4 v5 3
v6
v1 v5 = v1 v4 v5 = 3 + 6 = 9
3 3
v3 5
DISTANCE
FINAL
Ver. 1.0
v4 v5 3 9
v6
v1 v6 = v1 v4 v6 = 3 + =
3 3
Both the values are equal. Therefore, no change is made. PASS 1 complete v1 0 v1 v2 5 v4
Session 17
v3 5
DISTANCE
FINAL
Ver. 1.0
v4 v5 3 9
v6
From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. v2 and v3 have the shortest and the same distance from v1. Let us select v2 and add it to the FINAL array. v1 0 v1 v2 5 v4 v3 5 v2
Session 17
3 3
DISTANCE
FINAL
Ver. 1.0
v4 v5 3 9
v6
v1 v3 = 5 v1 v2 v3 = 5 + 4 = 9
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3 5 v2
v4 v5 3 9
v6
Session 17
v1 v5 = 9 v1 v2 v5 = 5 + =
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3 5 v2
v4 v5 3 9
v6
Session 17
v1 v6 = v1 v2 v6 = 5 + 6 = 11
11 < Therefore, the entry corresponding to v6 in the DISTANCE array is changed to 11. Pass 2 complete v1 0 v1 v2 5 v4 v3 5 v2
Session 17
3 3
DISTANCE
FINAL
Ver. 1.0
v4 v5 v6 3 9 11
From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. Let us select v3 and add it to the FINAL array.
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3 5 v2
v4 v5 v6 3 9 11 v3
Session 17
v1 v5 = 9 v1 v3 v5 = 5 + 3 = 8
3 3
DISTANCE
FINAL
Ver. 1.0
v1 v6 = 11 v1 v3 v6 = 5 + 3 = 8
8 < 11 Therefore, the entry corresponding to v6 in the DISTANCE array is changed to 8. Pass 3 complete v1 0 v1 v2 5 v4 v3 5 v2 v4 v5 v6 3 8 11 8 v3
Session 17
3 3
DISTANCE
FINAL
Ver. 1.0
From the DISTANCE array, select the vertex with the shortest distance from v1, such that the selected vertex is not in the FINAL array. Let us select v5 and add it to the FINAL array.
3 3
DISTANCE
FINAL
Ver. 1.0
v1 0 v1
v2 5 v4
v3 5 v2
v4 v5 3 8 v3 v5
v6 8
Session 17
v1 v6 = 8 v1 v5 v6 = 8 + =
3 3
v6 8
DISTANCE
FINAL
Ver. 1.0
Now add the only remaining vertex, v6 to the FINAL array. All vertices have been added to the FINAL array. This means that the DISTANCE array now contains the shortest distances from vertex v1 to all other vertices. v1 0 v1 v2 5 v4 v3 5 v2 v4 v5 3 8 v3 v5 v6 8 v6
Session 17
3 3
DISTANCE
FINAL
Ver. 1.0
Problem Statement:
In the previous activity, you created a program to represent a set of cities and the distances between them in the form of a graph. Extend the program to include the functionality for finding the shortest path from a given city to all the other cities.
Ver. 1.0
Session 17
Traversing a graph means visiting all the vertices in the graph. In a graph, there is no special vertex designated as the starting vertex. Therefore, traversal of the graph may start from any vertex. You can traverse a graph with the help of the following two methods:
DFS BFS
Ver. 1.0
Session 17
Graph theory has been instrumental in analyzing and solving problems in areas as diverse as computer network design, urban planning, finding shortest paths and molecular biology.
Ver. 1.0
Session 17