Many slides here are based on E. Demaine , D. Luebke slides

Slides:



Advertisements
Similar presentations
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Advertisements

Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Tirgul 11 DFS Properties of DFS Topological sort.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
1 Data Structures DFS, Topological Sort Dana Shapira.
Lecture 10 Topics Application of DFS Topological Sort
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Chapter 22: Elementary Graph Algorithms III. 2 About this lecture Topological Sort.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CS138A Elementary Graph Algorithms Peter Schröder.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
CS200: Algorithm Analysis
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graph Algorithms – 2 DAGs Topological order
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Many slides here are based on D. Luebke slides
CS6045: Advanced Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Analysis of Algorithms CS 477/677
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

Many slides here are based on E. Demaine , D. Luebke slides CS583 Lecture 09 Jana Kosecka Graph Algorithms Depth First Search Topological Sort Minimum Spanning Tree Many slides here are based on E. Demaine , D. Luebke slides

Depth-First Search Depth-first search is another strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

Depth-First Search Vertices initially colored white Then colored gray when discovered Then black when finished

DFS Example source vertex

Green in figure -> gray in code DFS Example source vertex 1 | | d f Green in figure -> gray in code

DFS Example source vertex d f 1 | | 2 |

DFS Example source vertex 1 | | 3 | 2 | d f

DFS Example 1 | | 3 | 4 2 | source vertex d f

DFS Example 1 | | 5 | 3 | 4 2 | source vertex d f

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What does u->d represent?

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What does u->f represent?

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; Will all vertices eventually be colored black?

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What will be the running time?

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?

Depth-First Search: The Code DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; So, running time of DFS = O(V+E)

Depth-First Sort Analysis This running time argument is an informal example of amortized analysis “Charge” the exploration of edge to the edge: Each loop in DFS_Visit can be attributed to an edge in the graph Runs once/edge if directed graph, twice if undirected Thus loop will run in O(E) time, algorithm O(V+E) Considered linear for graph, b/c adj list requires O(V+E) storage Important to be comfortable with this kind of reasoning and analysis

DFS Example source vertex

DFS Example source vertex d f 1 | |

DFS Example source vertex d f 1 | | 2 |

DFS Example 1 | | 3 | 2 | source vertex d f

DFS Example source vertex d f 1 | | 3 | 4 2 |

DFS Example source vertex d f 1 | | 5 | 3 | 4 2 |

DFS Example source vertex d f 1 | | 5 | 6 3 | 4 2 |

DFS Example source vertex d f 1 | 8 | | 5 | 6 3 | 4 2 | 7

DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 d f

What is the structure of the green vertices? What do they represent? DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 9 | d f What is the structure of the green vertices? What do they represent?

DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 9 |10 d f

DFS Example 1 | 8 |11 | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f

DFS Example source vertex 1 |12 8 |11 | 5 | 6 3 | 4 2 | 7 9 |10 d f

DFS Example d f 1 |12 8 |11 13| | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f 1 |12 8 |11 13| | 5 | 6 3 | 4 2 | 7 9 |10

DFS Example 1 |12 8 |11 13| 14| 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex 1 |12 8 |11 13| 14| 5 | 6 3 | 4 2 | 7 9 |10 d f

DFS Example 1 |12 8 |11 13| 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex d f

DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex d f

DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex The tree edges form a spanning forest Can tree edges form cycles? Why or why not?

DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex d f Tree edges

DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Encounter a grey vertex (grey to grey)

DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex d f Tree edges Back edges

DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Not a tree edge, though From grey node to black node

DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f source vertex d f Tree edges Back edges Forward edges

DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Cross edge: between a tree or subtrees From a grey node to a black node

DFS Example d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 Tree edges Back edges Forward edges Cross edges

DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Cross edge: between a tree or subtrees Note: tree & back edges are important; most algorithms don’t distinguish forward & cross

DFS: Kinds Of Edges Thm 23.9 (22.10 – in 3rd edition): If G is undirected, a DFS produces only tree and back edges Suppose you have u.d < v.d Then search discovered u before v, so first time v is discovered it is white – hence the edge (u,v) is a tree edge Otherwise the search already explored this edge in direction from v to u edge must actually be a back edge since u is still gray

DFS: Kinds Of Edges Thm 23.9: If G is undirected, a DFS produces only tree and back edges – cannot be a forward edge source F? source C?

DFS And Graph Cycles Thm: An undirected graph is acyclic iff a DFS yields no back edges If acyclic, no back edges (because a back edge implies a cycle If no back edges, acyclic No back edges implies only tree edges (Why?) Only tree edges implies we have a tree or a forest Which by definition is acyclic Thus, can run DFS to find whether a graph has a cycle

DFS And Cycles How would you modify the code to detect cycles? DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

DFS And Cycles What will be the running time ? DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

DFS And Cycles What will be the running time? A: O(V+E) We can actually determine if cycles exist in O(V) time: In an undirected acyclic forest, |E|  |V| - 1 So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way

Review: Kinds Of Edges Thm: If G is undirected, a DFS produces only tree and back edges Thm: An undirected graph is acyclic iff a DFS yields no back edges Thus, can run DFS to find cycles

Review: Kinds of Edges d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 source vertex d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 Tree edges Back edges Forward edges Cross edges

DFS And Cycles Running time: O(V+E) We can actually determine if cycles exist in O(V) time: In an undirected acyclic forest, |E|  |V| - 1 So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way Why not just test if |E| <|V| and answer the question in constant time? We can have some isolated component nodes not connected by any edges to the rest of the graph

Directed Acyclic Graphs A directed acyclic graph or DAG is a directed graph with no directed cycles:

DFS and DAGs Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: Forward: if G is acyclic, will be no back edges Trivial: a back edge implies a cycle Backward: if no back edges, G is acyclic Argue contrapositive: Suppose G has a cycle  we will show that DFS will produce a back edge Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle When v discovered, whole cycle is white Must visit everything reachable from v before returning from DFS-Visit() So path from uv is descendant of v hence (graygray), thus (u, v) is a back edge

Topological Sort Topological sort of a DAG: Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G Real-world example: getting dressed

Getting Dressed Underwear Socks Shoes Pants Belt Shirt Watch Tie Jacket

Getting Dressed Underwear Socks Shoes Pants Belt Shirt Watch Tie Jacket Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

Topological Sort Algorithm { Run DFS When a vertex is finished, output it On the front of linked list Vertices are output in reverse topological order } Time: O(V+E) Correctness: Want to prove that (u,v)  G  uf > vf

Correctness of Topological Sort Claim: (u,v)  G  uf > vf Show that if there is an edge from u to v, finishing time of u is greater then v When (u,v) is explored, u is gray v = gray  (u,v) is back edge. Contradiction (Why?) hence v cannot be gray – since there are no cycles v = white  v becomes descendent of u  vf < uf (since must finish v before backtracking and finishing u) v = black  v already finished  vf < uf

Strongly Connected Components Application of DFS – find strongly connected components of the graph Strongly connected component C of the directed graph is a maximal set of vertices u and v such that for all u and v in C u is reachable from v and v is reachable from u

Strongly Connected Components 13|14 11|16 1|10 2|7 3 | 4 12|15 8|9 5|6

Strongly Connected Components Call DFS to compute finishing times f[u] of each vertex Create transpose graph (directions of edges reversed) Call DFS on the transpose, but in the main loop of DFS, consider vertices in the decreasing order of f[u] Output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component

Strongly Connected components SSC algorithm will induce component graph which is a DAG 13|14 11|16 1|10 8|9 12|15 3 | 4 2|7 5|6

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph: 14 10 3 6 4 5 2 9 15 8

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 14 10 3 6 4 5 2 9 15 8

Minimum Spanning Tree Which edges form the minimum spanning tree (MST) of the below graph? H B C G E D F A 14 10 3 6 4 5 2 9 15 8

Minimum Spanning Tree Answer: H B C G E D F A 14 10 3 6 4 5 2 9 15 8

Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees Let T be an MST of G with an edge (u,v) in the middle Removing (u,v) partitions T into two trees T1 and T2 Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?) Proof: w(T) = w(u,v) + w(T1) + w(T2) (There can’t be a better tree than T1 or T2, or T would be suboptimal)

Minimum Spanning Tree Thm: Let T be MST of G, and let A  T be subtree of T Let (u,v) be min-weight edge connecting A to V-A Then (u,v)  T

Minimum Spanning Tree Thm: Let T be MST of G, and let A  T be subtree of T Let (u,v) be min-weight edge connecting A to V-A Then (u,v)  T Proof: in book (see Thm 23.1)

Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q 14 10 3 6 4 5 2 9 15 8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

Prim’s Algorithm  r MST-Prim(G, w, r) Q = V[G]; for each u  Q 14 10 3 6 4 5 2 9 15 8 r MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Pick a start vertex r

Prim’s Algorithm  r MST-Prim(G, w, r) Q = V[G]; for each u  Q 14 10 3 6 4 5 2 9 15 8 Pick a start vertex r r MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Red vertices have been removed from Q Prim’s Algorithm  14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Red vertices have been removed from Q

Red arrows indicate parent pointers Prim’s Algorithm 3  14 10 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Red arrows indicate parent pointers

Prim’s Algorithm  3 u MST-Prim(G, w, r) Q = V[G]; 14 for each u  Q 10 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm  3 u MST-Prim(G, w, r) Q = V[G]; for each u  Q 10 14 6 4 5 2 9 15 8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

Prim’s Algorithm  8 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q 10 8 14 6 4 5 2 9 15 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm  8 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q 10 8 14 6 4 5 2 9 15 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6

Prim’s Algorithm  8 3 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10  8 14 6 4 5 2 9 15 u

Prim’s Algorithm 3 2  8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2  8 14 6 4 5 9 15

Prim’s Algorithm 3 2  8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2  8 15 14 6 4 5 9

Prim’s Algorithm 3 2 9 8  MST-Prim(G, w, r) Q = V[G]; 10 8 15  14 6 4 5 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2 9 8 15 4 14 6 5

Prim’s Algorithm 2 9 8 4 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q 5 2 9 8 15 4 14 10 3 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3

Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q 5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q 5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q 5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)