560 likes | 694 Views
Objectives. In this session, you will learn to: Implement a graph Apply graphs to solve programming problems. Representing a Graph. To implement a graph, you need to first represent the given information in the form of a graph.
E N D
Objectives • In this session, you will learn to: • Implement a graph • Apply graphs to solve programming problems
Representing a Graph • 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
Adjacency Matrix Consider the following graph: • Adjacency Matrix Representation • v1 v2 v3 v4 • v1 0 1 0 0 • v2 0 0 1 0 • v3 0 0 0 0 • v4 1 0 1 0
Adjacency List Consider the following graph: • Adjacency List Representation
Traversing a Graph • 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)
DFS • Algorithm: DFS(v) • Push the starting vertex, v into the stack. • Repeat until the stack becomes empty: • Pop a vertex from the stack. • Visit the popped vertex. • Push all the unvisited vertices adjacent to the popped vertex into the stack.
DFS (Contd.) • Push the starting vertex, v1 into the stack v1
DFS (Contd.) • Pop a vertex, v1 from the stack • Visit v1 • Push all unvisited vertices adjacent to v1 into the stack v1 Visited: v1
DFS (Contd.) • Pop a vertex, v1 from the stack • Visit v1 • Push all unvisited vertices adjacent to v1 into the stack v2 v4 Visited: v1
DFS (Contd.) • Pop a vertex, v2 from the stack • Visit v2 • Push all unvisited vertices adjacent to v2 into the stack v2 v4 Visited: v1 v2
DFS (Contd.) • 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
DFS (Contd.) • Pop a vertex, v6 from the stack • Visit v6 • Push all unvisited vertices adjacent to v6 into the stack v6 v3 v4 There are no unvisited vertices adjacent to v6 Visited: v1 v2 v6
DFS (Contd.) • 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
DFS (Contd.) • 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
DFS (Contd.) • Pop a vertex, v5 from the stack • Visit v5 • Push all unvisited vertices adjacent to v5 into the stack v5 v4 There are no unvisited vertices adjacent to v5 Visited: v1 v2 v6 v3 v5
DFS (Contd.) • Pop a vertex, v4 from the stack • Visit v4 • Push all unvisited vertices adjacent to v4 into the stack v4 There are no unvisited vertices adjacent to v4 Visited: v1 v2 v6 v3 v5 v4
DFS (Contd.) • The stack is now empty • Therefore, traversal is complete Visited: v1 v2 v6 v3 v5 v4
DFS (Contd.) • 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.
DFS (Contd.) • To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph. • Repeat step 2 for each vertex, v in the graph • If v is not visited: • a. Call DFS(v)
BFS • Algorithm: BFS(v) • Visit the starting vertex, v and insert it into a queue. • Repeat step 3 until the queue becomes empty. • Delete the front vertex from the queue, visit all its unvisited adjacent vertices, and insert them into the queue.
BFS (Contd.) • Visit v1 • Insert v1 into the queue v1 v1
BFS (Contd.) • Remove a vertex v1 from the queue • Visit all unvisitedvertices adjacent to v1 and insert them in the queue v1 Visited: v1
BFS (Contd.) • Remove a vertex v1 from the queue • Visit all unvisitedvertices adjacent to v1 and insert them in the queue v2 v4 v1 v2 v4
BFS (Contd.) • Remove a vertex v2 from the queue • Visit all unvisitedvertices adjacent to v2 and insert them in the queue v2 v4 v1 v2 v4
BFS (Contd.) • Remove a vertex v2 from the queue • Visit all unvisitedvertices adjacent to v2 and insert them in the queue v4 v3 v6 v1 v2 v3 v6 v4
BFS (Contd.) • Remove a vertex v4 from the queue • Visit all unvisitedvertices adjacent to v4 and insert them in the queue v4 v3 v6 v5 v1 v2 v3 v6 v5 v4
BFS (Contd.) • Remove a vertex v3 from the queue • Visit all unvisited vertices adjacent to v3 and insert them in the queue v3 v6 v5 v3 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • Remove a vertex v6 from the queue • Visit all unvisitedvertices adjacent to v6 and insert them in the queue v6 v5 v3 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • Remove a vertex v6 from the queue • Visit all unvisitedvertices adjacent to v6 and insert them in the queue v5 v6 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • Remove a vertex v5 from the queue • Visit all unvisitedvertices adjacent to v5 and insert them in the queue v5 v6 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • Remove a vertex v5 from the queue • Visit all unvisitedvertices adjacent to v5 and insert them in the queue v5 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • The queue is now empty • Therefore, traversal is complete v5 does not have any unvisited adjacent vertices v1 v2 v3 v6 v5 v4
BFS (Contd.) • 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.
BFS (Contd.) • To solve this problem, you need to execute the preceding algorithm repeatedly for all unvisited vertices in the graph. • Repeat step 2 for each vertex, v in the graph • If v is not visited: • a. Call BFS(v)
Activity: Implementing a Graph by Using Adjacency Matrix Representation • 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.
Applications of Graphs • 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.
Solving the Shortest Path Problem • The shortest path problem can be solved by applying the Dijkstra’s algorithm on a graph • The Dijkstra’s algorithm is based on the greedy approach • The steps in the Dijkstra’s 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). • 4. If FINAL does not contain all the vertices, go to step 1.
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) Suppose you need to find the shortest distance of all the vertices from vertex v1. Add v1 to the FINAL array. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 ∞ ∞ 5 ∞ v1 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) In the DISTANCE array, vertex v4 has the shortest distance from vertex v1. Therefore, v4 is added to the FINAL array. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 ∞ ∞ 5 ∞ v1 v4 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 → v2 = 5 v1 → v4 → v2 = 3 + ∞ = ∞ ∞ > 5 Therefore, no change is made. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 ∞ ∞ 5 ∞ v1 v4 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v3 = ∞ v1 → v4 → v3 = 3 + 2 = 5 5 < ∞ Therefore, the entry corresponding to v3 in the DISTANCE array is changed to 5. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 ∞ ∞ 5 5 ∞ v1 v4 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v5 = ∞ v1 → v4 → v5 = 3 + 6 = 9 9 < ∞ Therefore, the entry corresponding to v5 in the DISTANCE array is changed to 9. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 ∞ ∞ 9 5 5 v1 v4 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v6 = ∞ v1 → v4 → v6 = 3 + ∞ = ∞ Both the values are equal. Therefore, no change is made. PASS 1 complete v1 v2 v3 v4 v5 v6 DISTANCE 0 3 9 ∞ 5 5 v1 v4 FINAL
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) 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 v2 v3 v4 v5 v6 DISTANCE 0 3 9 ∞ 5 5 v1 v4 FINAL v2
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v3 = 5 v1 → v2 → v3 = 5 + 4 = 9 9 > 5 Therefore, no change is made. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 9 ∞ 5 5 v1 v4 FINAL v2
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v5 = 9 v1 → v2 → v5 = 5 + ∞ = ∞ ∞ > 9 Therefore, no change is made. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 9 ∞ 5 5 v1 v4 FINAL v2
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) 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 v2 v3 v4 v5 v6 DISTANCE 0 3 9 ∞ 11 5 5 v1 v4 FINAL v2
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) 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. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 9 5 5 11 v1 v4 FINAL v2 v3
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) v1 →v5 = 9 v1 → v3 → v5 = 5 + 3 = 8 8 < 9 Therefore, the entry corresponding to v5 in the DISTANCE array is changed to 8. v1 v2 v3 v4 v5 v6 DISTANCE 0 3 9 8 5 5 11 v1 v4 FINAL v2 v3
5 3 4 6 2 3 6 3 Solving the Shortest Path Problem (Contd.) 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 v2 v3 v4 v5 v6 DISTANCE 0 3 8 5 5 11 8 v1 v4 FINAL v2 v3