Chapter 14 Lafores Book Weighted Graphs Hwajung Lee
Chapter 14 (Lafore’s Book) Weighted Graphs Hwajung Lee ITEC 324 Principle of CS III
Minimum Spanning Trees with Weighted Graphs (1) To find the minimum cost to connect all the edges in a graph. 1 A 3 6 4 1 B 5 1 C 7 2 1 D E Weighted Graph B 1 1 D A 1 1 C E Minimum Cost = 4
MST (2) - Priority Queue Special type of queue It has a front and a rear The lowest key (or in some implementation the highest key) is always at the front. Items are inserted in the proper position to maintain the order. In some case, it might be implemented using a heap, a queue, or an array.
MST(3) - Outline of the Algorithm Start with a vertex and repeatedly do the following until all the vertices are in the tree: 1. Find all the edges from the newest vertex to other vertices that aren’t in the tree. Put these edges in the priority queue. 2. Pick the edge with the lowest weight, and this edge and its destination vertex to the tree. Note 1: Newest means most recently installed in the tree. Note 2: In order to have only one edge to the same end point, edges with duplicate destinations must be removed from the priority queue and keep one edge with the smallest weight.
MST (4) - Applet and Source Code To see the Applet, click here To see the source code, click here
Shortest Path Algorithm - Dijkstra Algorithm Closest node to s is 1 hop away Find shortest paths from source s to all other destinations 2 nd closest node to s is 1 hop away from s or w” 3 rd closest node to s is 1 hop away from s, w”, or x w' z w s x w" z' x'
Dijkstra’s algorithm N: set of nodes for which shortest path already found Initialization: (Start with source node s) N = {s}, Ds = 0, “s is distance zero from itself” Dj=Csj for all j s, distances of directly-connected neighbors Step A: (Find next closest node i) Find i N such that Di = min Dj for j N Add i to N If N contains all the nodes, stop Step B: (update minimum costs) For each node j N Dj = min (Dj, Di+Cij) Go to Step A Minimum distance from s to j through node i in N
Execution of Dijkstra’s algorithm 2 1 5 4 1 2 4 1 6 2 3 1 3 2 3 5 2 3 3 4 3 1 2 2 5 1 6 2 5 4 Iteration N D 2 D 3 D 4 D 5 D 6 Initial {1} 3 2 5 1 {1, 3} 3 2 4 3 2 {1, 2, 3} 3 2 4 7 3 3 {1, 2, 3, 6} 3 2 4 5 3 4 {1, 2, 3, 4, 6} 3 2 4 5 3 5 {1, 2, 3, 4, 5, 6} 3 2 4 5 3
Shortest Paths in Dijkstra’s Algorithm 1 3 2 2 1 3 2 3 5 2 4 1 1 1 3 2 5 4 1 2 3 2 1 3 2 5 4 4 2 3 5 2 1 3 2 5 4 3 5 2 2 1 3 2 5 4 1 4 6 2 3 4 1 6 2 5 1 2 5 4 1 3 6 3 4 1 6 4 1 2 1 3 2 5 3 5 2 2 1 6 2 3 4 1 3 2 6 2 3 5
Dijkstra’s Algorithm To see an applet, click here To see a source code, click here
Bellman-Ford Shortest Path Algorithm Do you know the way to San Jose? Sa n. J os e San Jose 392 29 4 Sa San Jose 596 n Jo se 25 0
Shortest Path to SJ Focus on how nodes find their shortest path to a given destination node, i. e. SJ Dj Cij i Di San Jos e j If Di is the shortest distance to SJ from i and if j is a neighbor on the shortest path, then Di = Cij + Dj
But we don’t know the shortest paths i only has local info from neighbors Dj' j' Cij' Di i j Cij” j" Dj Dj" Pick current shortest path
Why Bellman-Ford Algorithm Works SJ sends accurate info 3 Hops From SJ 2 Hops From SJ 1 Hop From SJ Accurate info about SJ ripples across network, Shortest Path Converges San Jos e Hop-1 nodes calculate current (next hop, dist), & send to neighbors
Bellman-Ford Algorithm Consider computations for one destination d Initialization Each node table has 1 row for destination d Distance of node d to itself is zero: Dd=0 Distance of other node j to d is infinite: Dj= , for j d Next hop node nj = -1 to indicate not yet defined for j d Send new distance vector to immediate neighbors across local link At node j, find the next hop that gives the minimum distance to d, ▪ Minj { Cij + Dj } ▪ Replace old (nj, Dj(d)) by new (nj*, Dj*(d)) if new next node or distance Go to send step Send Step Receive Step
Bellman-Ford Algorithm Now consider parallel computations for all destinations d Initialization Each node has 1 row for each destination d Distance of node d to itself is zero: Dd(d)=0 Distance of other node j to d is infinite: Dj(d)= , for j d Next node nj = -1 since not yet defined Send new distance vector to immediate neighbors across local link For each destination d, find the next hop that gives the minimum distance to d, ▪ Minj { Cij+ Dj(d) } ▪ Replace old (nj, Di(d)) by new (nj*, Dj*(d)) if new next node or distance found Go to send step Send Step Receive Step
Iteration Node 1 Node 2 Node 3 Node 4 Node 5 Initial (-1, ) (-1, ) 1 2 3 Table entry @ node 3 for dest SJ Table entry @ node 1 for dest SJ 2 1 3 5 San Jose 1 2 4 3 1 2 6 3 4 5 2
Iteration Node 1 Node 2 Node 3 Node 4 Node 5 Initial (-1, ) (-1, ) 1 (-1, ) (6, 1) (-1, ) (6, 2) 2 3 D 3=D 6+1 n 3=6 D 6=0 3 1 2 1 5 1 2 0 4 3 1 2 6 3 4 D 5=D 6+2 n 5=6 2 5 2 D 6=0 San Jose
Iteration Node 1 Node 2 Node 3 Node 4 Node 5 Initial (-1, ) (-1, ) 1 (-1, ) (6, 1) (-1, ) (6, 2) 2 (3, 3) (5, 6) (6, 1) (3, 3) (6, 2) 3 3 1 2 3 1 5 3 1 2 0 4 3 1 2 6 6 3 4 5 2 2 San Jose
Iteration Node 1 Node 2 Node 3 Node 4 Node 5 Initial (-1, ) (-1, ) 1 (-1, ) (6, 1) (-1, ) (6, 2) 2 (3, 3) (5, 6) (6, 1) (3, 3) (6, 2) 3 (3, 3) (4, 4) (6, 1) (3, 3) (6, 2) 3 1 2 3 1 5 3 1 2 0 4 3 1 2 6 4 6 3 4 5 2 2 San Jose
Reference Leon-Garcia, Widjaja, Communication Networks: Fundamental Concepts and Key Architectures, 2 nd Edition, Mc. Graw Hill
Kruskal's Algorithm Build priority Queue of all edges Edges_Accepted = 0; while (Edges_Accepted < Num. Vertex - 1) { E = Dequeue(); // E=(u, v) if (!Connected(E. u, E. v)) { Edges_Accepted ++; T[Action] = true; } }
Kruskal's Algorithm (cont. ) 1 1 2 1 4 2 3 1 1 10 1 2 2 4 3 5 5 1 4 8 6 6 7 1 Total path lengths: Edge causes aa V-1 Edges accepted Edge causes = 1 + 2 + so 2 +stop 2+4 cycle, so ignore = 12 1
Kruskal's Algorithm (cont. ) void kruskal() { int edges. Accepted =0; Disj. Set ds = new Disj. Sets(NUM-Vertices); PQ<Edge> pq = new Priority. Queue<Edge>(get. Edges()); Edge e; Vertix u, v; while (edges. Accepted < NUM_VERTICES -1) { e = pq. delete. Min(); //remove the smallest edge from pq E=(u, v); Set. Type uset = ds. find(u); Set. Type vset = ds. find(v); if (uset != vset){ edges. Accepted++; ds. union(uset, vset); } } }
Kruskal's Algorithm (cont. ) Typically the algorithm will maintain a forest of sets The concept would be to reduce the forest to just 1 set. A free tree This implies set operations such as: Union(Uset, Vset) ▪ Join 2 sets Uset & Vset Find(u) ▪ Return the set with vertex u in it. Compare(Uset, Vset) ▪ Is Uset equal Vset Set operations can become costly.
Kruskal's Algorithm (cont. ) Determining cycles without using sets Support an Adjacency list of accepted edges Want to add edge (u, v) Build a tree from u of currently accepted edges. ▪ If v in the tree, then (u, v) will cause a cycle. ▪ Else we add (u, v) as an accepted edge. Additional Data Structures are required Adjacency list of accepted edges Integer array size n used for book keeping ▪ Only enumerate a vertex once. If graph has directed edges then we need to determine: u contains v v contains u
Kruskal's Algorithm (cont. ) for (i==1; i <= n; i++) Known[i]=0; //Initialize Known to 0 boolean Find(u, v){ boolean cycle = false; Known[u]=1; //Mark u as processed if (u == v) return true; //We found a cycle for (each w adjacent to u) if (Known[w]==0) //Only search vertices w cycle = cycle || Find(w, v); //which are unprocessed return cycle; } If we ever find a cycle (u==v) then we return true. This value is passed back true the recursion.
Prim’s Algorithm Basic Idea 1. Build a tree starting at Vo=u. 2. Select vertex v which is closest to u and add (u, v) to tree. 3. Find next closes vertex v to tree and add. 4. Repeat 3 Until all v have been consumed.
Prim’s Algorithm Q. Enqueue(V 0, V 0); Vertices=1 while (Vertices++ < |V|) { do { E = Q. Dequeue(); while (T[v]. Known); } Where E = (u, v) PQ of edges c(u, v), where u is in the tree and v is not. T[v]. Known = true; for (Each w adjacent to v) if(T[w]. Dist > C(v, w) && !T[w]. known){ T[w]. Dist = C(v, w); T[w]. Path = v; Q. Enqueue(v, w); } Very Similar to Dijkstra’s Algorithm. dv now only holds the edge weight.
Prim’s Algorithm (cont. ) PQ C(7, 6) C(1, 4) C(1, 1) C(4, 3) C(2, 1) C(6, 7) C(3, 4) C(1, 2) C(2, 1) C(3, 1) C(7, 4) C(4, 2) C(1, 3) C(4, 3) C(3, 6) C(4, 2) C(2, 4) C(7, 5) C(1, 3) C(6, 3) C(4, 5) C(4, 7) C(7, 5) C(4, 6) C(3, 1) C(4, 5) C(2, 5) C(3, 6) C(4, 6) C(2, 5) C(4, 5) C(6, 4) C(2, 5) C(4, 6) C(2, 5) 2 1 4 2 3 1 10 7 2 4 3 5 5 4 8 6 6 7 1 1 1 1 V 1 2 2 1 6 V 4 V 1 V 7 1 4 V 7 V 4
Prim’s Algorithm (cont. ) The Algorithm stops when we have accepted |V| vertices. We can read the accepted edges from the table directly.
- Slides: 31