200 likes | 315 Views
CS2420: Lecture 40. Vladimir Kulyukin Computer Science Department Utah State University. Outline. Graph Algorithms (Chapter 9). Spanning Trees. An edge e in G is redundant if, after the removal of e , G remains connected.
E N D
CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University
Outline • Graph Algorithms (Chapter 9)
Spanning Trees • An edge e in G is redundant if, after the removal of e, G remains connected. • A spanning tree of G is a tree (acyclic graph) that contains all vertices of G and preserves G’s connectivity without any redundant edges.
Spanning Tree • A spanning tree of a connected graph is its connected acyclic sub-graph (a tree) that contains all vertices of the graph and preserves the graph’s connectivity. • In a weighted connected graph, the weight of a spanning tree is defined as the sum of the tree’s edges. • A minimal (minimum) spanning tree of a weighted connected graph is a spanning tree of the smallest weight.
Spanning Tree: Example A A B B C C D D
Spanning Trees: Examples 1 1 1 A B A B A B 2 2 5 5 3 3 3 C D C D C D Tree T1 Weight(T1) = 6 Tree T2 Weight(T2) = 9 Original Graph 1 A B 2 5 C D Tree T3 Weight(T3) = 8
Min Spanning Tree Construction • Construct a minimum spanning tree by expanding it with one vertex at each iteration. • The initial tree consists of one vertex, i.e. the root. • On each iteration, the tree so far is expanded by attaching to it the nearest vertex. • Stop when all the vertices are included.
Min Spanning Tree Construction: Example 1 1 1 A B A B A B 2 2 5 Step 1 3 D C D Step 2 Original Graph 1 A B 2 3 C D Step 3
Min Spanning Tree Construction: Prim’s Algorithm • G = (V, E) is a weighted connected graph • Input:G = (V, E). • Output:ET, the set of edges composing a minimum spanning tree.
Prim’s Algorithm: Insight u is the vertex we have reached; next we choose the vertex closest to u* by comparing the weights: w1, w2, w3, and w4. v1 w1 v2 w2 u* w3 v3 w4 v4
Prim’s Algorithm: Update • After v is reached, two operations must be performed: • Mark v as visited, i.e. move it into VT; • For each remaining vertex v not in VT and connected to u by a shorter edge than v’s current weight label, update v’s weight label (V.Wght) by the weight of (u, v) and set v’s previous label (V.Prev) to u.
Prim’s Algorithm: Example TreeRemaining Vertices NULL <A, _, INF>, <B, _, INF>, <C, _, INF>, <D, _, INF>, <E, _, INF>, <F, _, INF> <A, _, 0><B, A, 3> , <C,_,INF>, <D, _, INF>, <E, A, 6> , <F, A, 5> <B, A, 3><C, B, 1>, <D, _, INF>, <E, A, 6>, <F, B, 4> <C, B, 1> <D, C, 6>,<E, A, 6>, <F, B, 4> <F, B, 4><D, F, 5> , <E, F, 2> <E, F, 2> <D, F, 5> <D, F, 5> 1 B C 3 6 4 4 5 5 A F D 2 6 8 E
Prim’s Algorithm: Example 1 B C 6 3 4 4 5 5 D A F 2 6 8 E
Prim’s Algorithm: Example 1 B C 6 3 4 4 5 5 D A F 2 6 8 E
Prim’s Algorithm: Example 1 B C 6 3 4 4 5 5 D A F 2 6 8 E
Prim’s Algorithm: Example 1 B C 6 3 4 4 5 5 D A F 2 6 8 E
Prim’s Algorithm: Example 1 B C 6 3 4 4 5 5 D A F 2 6 8 E
Prim’s Algorithm: Result Spanning Tree 1 B C 3 4 5 D A F 2 E
Prim’s Algorithm: Implementation • A vertex v has four labels: • 1) The vertex’s name, i.e., V; • 2) The name of the vertex U from which V was reached (V.Prev); • 3) The weight of (u, v) (V.Wght). Remember V.Wght= weight(u,v); • 4) Visitation status of V (V.Visisted); this is a boolean variable, initially false, and set to true when V is visited. • Unreached vertices have NULL as the value of V.Prev and INF as the value of V.Wght.
Prim’s Algorithm: Implementation • We assume that we have implemented a dynamic priority queue data structure (DynamicPriorityQueue). • If Q is a dynamic priority queue, then we can perform the following operations on it: • Q.Insert(v); // inserts an element v into Q; • Q.UpdatePriority(v); // moves v up or down the minimal heap; • Q.Pop(); // removes the minimal element; • Q.Push(v); // inserts v into Q.