UNIT V GRAPHS Representation of Graphs Breadth-first search Depth-first search Topological sort Minimum Spanning Trees Kruskal and Prim algorithm Shortest path algorithm Dijkstra s algorithm Bellman-Ford algorithm Floyd - Warshall algorithm. Two ways of representing a graph are: a. Adjacency matrix b. Adjacency list Graph Traversals 1. BFS 2. DFS St.Joseph s College of Engineering/St.Joseph s Institute of Technology 1
DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node; 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1. DFS(input: Graph G) { Stack S; Integer x, t; while (G has an unvisited node x){ visit(x); push(x,s); while (S is not empty){ t := peek(s); if (t has an unvisited neighbor y){ visit(y); push(y,s); else pop(s); BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node z in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1. BFS(input: graph G) { Queue Q; Integer x, z, y; while (G has an unvisited node x) { visit(x); Enqueue(x,Q); while (Q is not empty){ z := Dequeue(Q); for all (unvisited neighbor y of z){ visit(y); Enqueue(y,Q); St.Joseph s College of Engineering/St.Joseph s Institute of Technology 2
Explain topological sort with suitable algorithm and example (16) Definition: A topological sort is a linear ordering of vertices in a directed acyclic graph such that if there is a path from Vi to Vj, then Vj appears after Vi in the linear ordering. Topological ordering is not possible. If the graph has a cycle, since for two vertices v and w on the cycle, v precedes w and w precedes v. To implement the topological sort, perform the following steps. Step 1 : - Find the indegree for every vertex. Step 2 : - Place the vertices whose indegree is `0' on the empty queue. Step 3 : - Dequeue the vertex V and decrement the indegree's of all its adjacent vertices. Step 4 : - Enqueue the vertex on the queue, if its indegree falls to zero. Step 5 : - Repeat from step 3 until the queue becomes empty. Step 6 : - The topological ordering is the order in which the vertices dequeued. Routine to perform Topological Sort void topsort (graph g) { queue q ; int counter = 0; vertex v, w ; q = createqueue (numvertex); makeempty (q); for each vertex v if (indegree [v] = = 0) enqueue (v, q); St.Joseph s College of Engineering/St.Joseph s Institute of Technology 3
while (! isempty (q)) { v = dequeue (q); topnum [v] = + + counter; for each w adjacent to v if (--indegree [w] = = 0) enqueue (w, q); if (counter! = numvertex) error (" graph has a cycle"); disposequeue (q); /* free the memory */ Example 1: A B E C D Vertex In degree before Dequeue 5 A 0 0 0 0 0 B 1 0 0 0 0 C 2 1 1 0 0 D 2 1 0 0 0 E 2 2 1 0 0 Enqueue A B D C, E Dequeue A B D C E Dequeue A B D E C Example 2: Error St.Joseph s College of Engineering/St.Joseph s Institute of Technology 4
Example 3: A B C D E Vertex In degree before Dequeue 5 A 0 0 0 0 0 B 1 0 0 0 0 C 1 0 0 0 0 D 3 2 1 0 0 E 2 2 1 1 0 Enqueue A B, D E C Dequeue A B C D E Dequeue A C B D E Minimum Spanning Tree Algorithm: i. Prim s algorithm ii. Kruskals algorithm 1. Write and explain the prim s algorithm with an example (16) (MAY/JUNE 2012) (NOV/DEC 2011) Prim's Algorithm to Construct a Minimal Spanning Tree Input: A weighted, connected and undirected graph G = (V,E). Output: A minimal spanning tree of G. Step 1: Let x be any vertex in V. Let X = and Y = V Step 2: Select an edge (u,v) from E such that, and (u,v)has the smallest weight among edges between X and Y. Step 3: Connect u to v. Step 4: If Y is empty, terminate and the resulting tree is a minimal spanning tree. Otherwise, go to Step 2. Initialization a. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = null c. For all vertices v V, v r, set D(v) = d. Insert all vertices into priority queue P, using distances as the keys While P is not empty { 1. Select the next vertex u to add to the tree u = P.deleteMin() St.Joseph s College of Engineering/St.Joseph s Institute of Technology 5
P) 2. Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w 2. Explain Kruskal s algorithm with an example (16) Kruskal's Algorithm to Construct a Minimal Spanning Tree Input: A weighted, connected and undirected graph G = (V,E). Output: A minimal spanning tree of G. Step 1: T = Step 2: while T contains less than n-1edges do Choose an edge (v,w) from E of the smallest weight. Delete (v,w) from E. If the adding of (v,w)does not create cycle in T then Add (v,w) to T. Else Discard (v,w). end while A B C D E Edge Weight Action Comments (A,F) 10 Accepted An edge with minimum cost(from delete_min of heap (C,D) 12 Accepted (B,G) 14 Accepted (B,C) 16 Accepted (D,E) 22 Accepted (E,G) 24 Rejected Forms a cycle (F,E) 25 Accepted (A,B) 28 Rejected Forms a cycle St.Joseph s College of Engineering/St.Joseph s Institute of Technology 6
1. Single-source shortest paths in weighted graphs i. Dijkstra s Algorithm ii. Bellman-Ford Algorithm 2. All pair shortest path algorithm a. Floyd Warshall Algorithm Dijkstra(G,s) 01 for each vertex u Î G.V() 02 u.setd( ) 03 u.setparent(nil) 04 s.setd(0) 05 S Æ 06 Q.init(G.V()) 07 while not Q.isEmpty() 08 u Q.extractMin() 09 S S È {u 10 for each v Î u.adjacent() do 11 Relax(u, v, G) 12 Q.modifyKey(v) 13 Relax (u,v,g) 14 if v.d() > u.d()+g.w(u,v) then 15 v.setd(u.d()+g.w(u,v)) 16 v.setparent(u) Dijkstra s Algorithm St.Joseph s College of Engineering/St.Joseph s Institute of Technology 7
St.Joseph s College of Engineering/St.Joseph s Institute of Technology 8
Initial matrix a 0 0 - b 0 - c 0 - d 0 - e 0 - a is visited a 0 1 - b 10 0 a c 3 0 a d 0 - e 0 - c is visited a 0 1 - b 10,(3+4) 0 a,c c 3 1 a d,(3+8) 0 c e,(3+2) 0 -,c St.Joseph s College of Engineering/St.Joseph s Institute of Technology 9
e is visited a 0 1 - b 7 0 c c 3 1 a d 11,(5+9) 0 c,e e 5 1 c b is visited a 0 1 - b 7 1 c c 3,(7+1) 1 a,b d 11,(7+2) 0 c,b e 5 1 c d is visited a 0 1 - b 7 1 c c 3 1 a d 9 1 b e 5,(9+7) 1 C,d Final matrix a 0 1 - b 7 1 c c 3 1 a d 9 1 b e 5 1 c Dijkstra s doesn t work when there are negative edges: Intuition we can not be greedy any more on the assumption that the lengths of paths will only increase in the future Bellman-Ford algorithm detects negative cycles (returns false) or returns the shortest path-tree Bellman-Ford algorithm Bellman-Ford(G,s) 01 for each vertex u Î G.V() 02 u.setd( ) 03 u.setparent(nil) 04 s.setd(0) St.Joseph s College of Engineering/St.Joseph s Institute of Technology 10
05 for i 1 to G.V() -1 do 06 for each edge (u,v) Î G.E() do 07 Relax (u,v,g) 08 for each edge (u,v) Î G.E() do 09 if v.d() > u.d() + G.w(u,v) then 10 return false 11 return true 12 Relax (u,v,g) 13 if v.d() > u.d()+g.w(u,v) then 14 v.setd(u.d()+g.w(u,v)) 15 v.setparent(u) Eg.1 s 0 0 - t 0 - x 0 - y 0 - z 0 - S is Visited s 0 1 - t 6 0 s x 0 - y 7 0 s z 0 - St.Joseph s College of Engineering/St.Joseph s Institute of Technology 11
t is Visited (-ve cost) s 0 1 - t 6 1 s x 0 - y 7 0 s z (6-4) 0 t y is Visited (-ve cost) s 0 1 - t 6 1 s x,(7-3) 0 -,y y 7 1 s z 2 0 t s 0 1 - t 6 1 s x 4 0 y y 7 1 s z 2 0 t x is Visited (-ve cost) s 0 1 - t Min(6,(4-2)) 1 s,x x 4 1 y y 7 1 s z 2 0 t s 0 1 - t 2 1 x x 4 1 y y 7 1 s z 2 0 t St.Joseph s College of Engineering/St.Joseph s Institute of Technology 12
Update t s 0 1 - t 2 1 x x 4 1 y y 7 1 s z Min(2,(2-4)) 0 t The cost of moving from s to s is 0 s to t is 2 s to x is 4 s to y is 7 s to z is -2. Final matrix s 0 1 - t 2 1 x x 4 1 y y 7 1 s z -2 1 t Floyd Warshall Algorithm FloydWarshall(matrix W, integer n) for k 1 to n do for i 1 to n do for j 1 to n do d ij (k) min(d ij (k-1), d ik (k-1) + d kj (k-1) ) // D[i,j] = min(d[i,j],d[i,k]+d[k,j]) return D (n) The final D matrix will store all the shortest paths. Adjacency matrix for the given graph. 1 0-2 2 4 0 3 4-1 0 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 13
K=0 1 0-2 2 4 0 3 4-1 0 K i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 1 1 1 0 0 0 0 0 0 1 1 2 0 1 1 3-2 0-2 -2-2 -2 1 1 4 0 1 2 1 4 4 0 4 4 4 1 2 2 0 4 0 0 1 2 3 3 4-2 2 3 2 1 2 4 4 1 3 1 0 1 3 2 1 3 3 0-2 0 0 1 3 4 2 2 2 1 4 1 0 1 4 2-1 -1-1 1 4 3-2 1 4 4 0 0 0 K=1 1 0-2 2 4 0 2 4-1 0 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 14
K=1 1 0-2 2 4 0 2 4-1 0 k i j d[i,j] d[i,k] d[k,j] d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 2 1 1 0 4 0 0 2 1 2 0 2 1 3-2 2-2 -2 2 1 4 2 2 1 4 0 4 4 4 4 2 2 2 0 0 0 0 0 0 2 2 3 2 0 2 2 2 2 2 2 4 0 2 3 1 4 2 3 2 0 2 3 3 0 2 0 0 2 3 4 2 2 2 2 4 1-1 4 3 3 2 4 2-1 -1 0-1 -1-1 2 4 3-1 2 1 1 2 4 4 0-1 0 0 K=2 1 0-2 2 4 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 15
K=2 1 0-2 2 4 0 2 k i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 3 1 1 0-2 0 0 3 1 2-2 3 1 3-2 -2 0-2 -2-2 3 1 4-2 2 0 0 3 2 1 4 2 4 4 3 2 2 0 2 0 0 3 2 3 2 2 0 2 2 2 3 2 4 2 2 4 4 3 3 1 0 3 3 2 0 3 3 3 0 0 0 0 0 0 3 3 4 2 0 2 2 2 2 3 4 1 3 1 3 3 3 4 2-1 1-1 -1 3 4 3 1 1 0 1 1 1 3 4 4 0 1 2 3 0 0 K=3 1 0-2 0 2 4 0 2 4 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 16
K=3 1 0-2 0 2 4 0 2 4 k i j d[i,j] d[i,k] d[k,j] ) d[i,k]+ d[k,j] d[i,j] Min(d[i,j], d[i,k]+ d[k,j]) 4 1 1 0 0 3 3 0 0 4 1 2 0-1 -1-1 4 1 3-2 0 1 1-2 -2 4 1 4 0 0 0 0 0 0 4 2 1 4 4 3 7 4 4 4 2 2 0 4-1 3 0 0 4 2 3 2 4 1 5 2 2 4 2 4 4 4 0 4 4 4 4 3 1 2 3 5 5 4 3 2 2-1 1 1 4 3 3 0 2 1 3 0 0 4 3 4 2 2 0 2 2 2 4 4 1 3 0 3 3 3 3 4 4 2-1 0-1 -1-1 -1 4 4 3 1 0 1 1 1 1 4 4 4 0 0 0 0 0 0 K=4 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 17
K=0 1 0-2 2 4 0 3 4-1 0 K=1 1 0-2 2 4 0 2 4-1 0 K=2 1 0-2 2 4 0 2 K=3 1 0-2 0 2 4 0 2 4 K=4 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 18
1 0-1 -2 0 2 4 0 2 4 3 5 1 0 2 The cost of moving from 1 to 1, 2 to 2,... ie., self loop is 0. 1 to 2 is -1 1 to 3 is -2 1 to 4 is 0 2 to 1 is 4 2 to 3 is 2 2 to 4 is 4 3 to 1 is 5 3 to 2 is 1 3 to 4 is 2 4 to 1 is 3 4 to 2 is -1 4 to 3 is 1 St.Joseph s College of Engineering/St.Joseph s Institute of Technology 19