Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Outline Graph Algorithms (Chapter 9)

3 Kruskal’s Algorithm: UnionFind Kruskal’s Algorithm can be implemented with the UnionFind data structure. A set is first partitioned into singletons, i.e. single element sets. The subsets are repeatedly unified into larger subsets until some termination criterion is met. One termination criterion is that there are no more subsets to unify.

4 Review: UnionFind Operations MakeSet(x) - creates a one-element set {x}. Find(x) - returns a subset containing x. Union(x, y) - constructs the union of the disjoint sets S x and S y such that S x contains x and S y contains y. S x U S y replaces both S x and S y in the collection of subsets.

5 UnionFind: Two Implementations Use one element of each subset as a representative. Two implementation alternatives: –QuickFind: Find = O(1); Union = O(N). –QuickUnion: Union = O(1); Find = O(N).

6 QuickFind: Asymptotic Run Time When performing the union operation, always append the shorter list to the longer one (union by size). The worst case run time of any legitimate sequence of unions is O(nlogn). The worst case run time of a sequence of n-1 unions and m finds is O(nlogn + m).

7 QuickUnion: Asymptotic Run Time If union by size (number of elements in the tree) or union by rank (tree height) is used, the height of the tree is logn. The time efficiency of a sequence of n-1 unions and m finds is O(n + mlogn).

8 Kruskal’s Algorithm: Code Kruskal(V, E) { // The input is a graph G = (V, E) Sort E in non-decreasing order of edge weights; TreeEdges = new UnionFind(); k = 0; // number of processed edges // A min spanning tree has |V|-1 edges. for each (u, v) in E { if ( k == |V| - 1 ) break; T 1 = TreeEdges.Find(u); T 2 = TreeEdges.Find(v); if ( T 1 != T 2 ) { TreeEdges.Union(u, v); k = k + 1; }// end if }// end for return TreeEdges; }// end Kruskal

9 Kruskal’s Algorithm: Asymptotic Analysis Sorting the edges is O(|E|log(|E|)). In the worst case, the Union and Find operations are executed |E| times. If we use the QuickUnion implementation of UnionFind, we have O(|E| + |E|log(|E|)). If we use the QuickFind implementation of UnionFind, we also have O(|E| + |E|log(|E|)). Thus, we have O(|E|log(|E|)) + O(|E|+ |E|log(|E|)) = O(|E|log(|E|)).

10 Depth First Search (DFS) Each vertex V in G has a distance V.Dist associated with it. Each vertex V in G has a pointer to the vertex it was reached from (V.Prev). Initially, V.Dist = INF for every V. The start vertex S is the input.

11 Depth First Search DFS(G, S) { // G is a graph; S is the start vertex S.Dist = 0; S.Prev = NULL; DFS_REC(S); } // V is a vertex in G. DFS_REC(G, V) { For each U adjacent to V { If ( U.Dist == INF ) { U.Prev = V; U.Dist = V.Dist + 1; DFS_REC(G, U); } // end If } // end For } // end DFS_REC

12 DFS: Example V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2 Start Vertex

13 DFS: Example V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

14 V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

15 V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

16 V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

17 V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

18 V0V0 V6V6 V5V5 V3V3 V4V4 V1V1 V2V2

19 DFS: Asymptotic Analysis If the graph is represented with adjacency lists: O(|V|+|E|).


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

Similar presentations


Ads by Google