Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University.

Similar presentations


Presentation on theme: "CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University."— Presentation transcript:

1 CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University

2 Outline Graph Algorithms (Chapter 9)

3 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.

4 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.

5 Spanning Tree: Example A BC D A B C D

6 Spanning Trees: Examples AB CD 5 1 2 3 Original Graph AB DC 1 2 3 Tree T 1 Weight(T 1 ) = 6 AB CD 1 5 3 Tree T 2 Weight(T 2 ) = 9 AB CD 1 2 5 Tree T 3 Weight(T 3 ) = 8

7 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.

8 Min Spanning Tree Construction: Example AB CD 5 1 2 3 Original Graph AB D C 1 2 Step 1 AB 1 Step 2 AB D 1 2 3 Step 3

9 Min Spanning Tree Construction: Prim’s Algorithm G = (V, E) is a weighted connected graph Input: G = (V, E). Output: E T, the set of edges composing a minimum spanning tree.

10 Prim’s Algorithm: Insight v2v2 v3v3 v4v4 u* u is the vertex we have reached; next we choose the vertex closest to u* by comparing the weights: w 1, w 2, w 3, and w 4. v1v1 w1w1 w2w2 w3w3 w4w4

11 Prim’s Algorithm: Update After v is reached, two operations must be performed: –Mark v as visited, i.e. move it into V T ; –For each remaining vertex v not in V T 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.

12 Prim’s Algorithm: Example BC ADF E 3 1 6 8 6 2 44 55 TreeRemaining Vertices NULL,,,,,,,,,,,,,

13 Prim’s Algorithm: Example BC A D F E 3 1 6 8 6 2 44 55

14 BC A D F E 3 1 6 8 6 2 44 55

15 BC A D F E 3 1 6 8 6 2 44 55

16 BC A D F E 3 1 6 8 6 2 44 55

17 BC A D F E 3 1 6 8 6 2 44 55

18 Prim’s Algorithm: Result Spanning Tree BC A D F E 3 1 2 4 5

19 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.

20 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.


Download ppt "CS2420: Lecture 40 Vladimir Kulyukin Computer Science Department Utah State University."

Similar presentations


Ads by Google