Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.

Similar presentations


Presentation on theme: "COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000."— Presentation transcript:

1 COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000

2 Proof(Prim’s Th.) Proof (Prim’s Th.) It is true for k=0 (single node) Assume: it is true for T(k) (i.e. T(k) is subtree of min.span.tr T). Prove it for T(k+1)!. -At step k+1 let e is the smallest weight-> T(k+1) = T(k)+e. There are 2 options-> if e T-> T(k+1) is subtree min.sp.tr. Of T. We are ready! Assume e isn’t edge of T and show this cannot be ( it leads to contradiction) If e  T-> (T+e) should have cycle! (otherwise T+e is a tree but T is max) -> Consider this cycle in T+e (see Figure) -> f is an edge in the cycle that connects to T(k). -> we know w(e)  w(f) [we choose so in the Algorithm ~always the smallest] ^T spanning tree: w(^T) = w(T)+w(e)-w(f)  w(T)-> but T is minimal. This is a CONTRADICTION unless w(e)=w(f). ->[eT it should be] g.e.d.. Define ^T= T+e-f

3 Complexity Analysis of Prim’s Algorithm
the algorithm has n-1 loops (for each covered vertex) the search in the loops for the minimal fronties edge O(n) For Large N This is Very Demanding -> There is ‘Better’ Algorithm Kruskal That can have -> O(e log e) (Kruskal) Time Complexity: -# of vertices This is better: If e<<n^2 (sparse graph) than Prim.

4 Algorithm 4.5.1 Prim’s Minimum Spanning Tree
Scheme Input: a weighted connected graph G. Output: a minimum spanning tree T. Choose an arbitrary vertex s of graph G. Initialize the Prim tree T as vertex as s. Initialize the set of frontier edges for tree T as empty. While Prim tree T does not yet span G. Update the set of frontier edges for T. Let e be a frontier edges for T. Let e be a frontier edge for T with the smallest edge-weight. Let v be the non-tree endpoint of edge e. Add the edge e (and vertex v) to tree T. Return Prim tree T.

5 Optimality of Spanning Trees
-> Prim (minimum spanning tree ~ weights) -> Dijkstra: Shortest Path: Shortest Path Problem: Let s and t two vertices of a (connected) weighted graph G. Find the path from s to t with minimum total weights Measuring of weight: distance (effort, time, …) Assume > 0 (positive) Idea: (similar to Prim’s) Grow a tree (Dijkstra tree D) from the vertex s At each iteration add a frontier edge which is as close as possible to s. -> dist[x] means: distance from node x to starting node s.

6 Theorem (Dijkstra) – Shortest Path
T(j)- Dijkstra tree after j iterations (0  j  |V(G)| - 1) on a connected graph G. ->    T(j) : the unique s-v path in T(j) is the shortest path in G. Proof: by induction: -true for T(0) -Assume it’s true for T(0)! -Prove it for T(j+1)! Per definition: Q is the shortest path from s to y in T(j+1)! We have to show: it is the shortest also in G. Let R is any other path s-y in G. We have to know: length (R)  length (Q). We know length (Q)  length. -> Length ( R ) = length/ s->v->f) + length (k)  length (Q) + length (k) *draw image

7 Complexity of Dijkstra Minimum Path Algorithm
- Efficiency : (O(n^2)) -we have n loops -each loop takes O(n) operations can be improved by adjusting the adjacency list O((e +n) log n) For sparse graphs: ~O(n log n)

8 Algorithm 4.5.2: Dijkstra’s Shortest Path
Input: a weighted connected graph G whose edge-weights are nonnegative; and a vertex s of G. Output: a spanning tree T of G, rooted at vertex s, whose path from s to each vertex v is a shortest path from s to v in G; and a vertex-labeling giving the distance from s to each vertex. Initialize the Dijkstra tree T as a vertex s. Initialize the set of frontier edges for tree T as empty. Dist[s]:= 0 Write label 0 on vertex s. While Dijkstra tree T does not yet span G For each frontier edge e for T Let x be the labeled endpoint of edge e. Let y be the unlabeled endpoint of edge e. Add edge e (and vertex y) to tree T. Dist[y]:= P(e) Write label dist[y] on vertex y. Return Dijkstra tree T and its vertex labels.


Download ppt "COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000."

Similar presentations


Ads by Google