CSC 413/513: Intro to Algorithms

Slides:



Advertisements
Similar presentations
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
Advertisements

David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
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.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
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”
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
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.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Spring 2015 Lecture 10: Elementary Graph Algorithms
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
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)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
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.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
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
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Chapter 22 Elementary Graph Algorithms
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.
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
Minimum Spanning Trees
CISC 235: Topic 10 Graph Algorithms.
CS200: Algorithm Analysis
Many slides here are based on E. Demaine , D. Luebke slides
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Many slides here are based on D. Luebke slides
CS6045: Advanced Algorithms
Advanced Algorithms Analysis and Design
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Analysis of Algorithms CS 477/677
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Finding Minimum Spanning Trees
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
CS 3013: DS & Algorithms Shortest Paths.
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Chapter 22: Elementary Graph Algorithms III
Presentation transcript:

CSC 413/513: Intro to Algorithms Topological Sort Minimum Spanning Trees

Review: DFS 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 = YELLOW; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

Review: DFS Example source vertex

Review: DFS Example source vertex d f 1 | | | | | | | |

Review: DFS Example source vertex d f 1 | | | 2 | | | | |

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 2 | 7 9 |10 3 | 4 5 | 6 source vertex d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15 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?

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: G has a cycle   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(v) So path from uv is yellowyellow, 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 Watch Pants Shoes Shirt Belt Tie Jacket

Topological Sort Algorithm { Run DFS When a vertex is finished, output it 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 When (u,v) is explored, u is yellow v = yellow  (u,v) is back edge. Contradiction (Why?) 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

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

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

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

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

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: Proof: in book (see Thm 23.1) 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) Greedy choice property

Greedy Choice Property V-A A A 6 4 5 9 H B C 14 2 10 15 G E D 3 8 F

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 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 4 9 5 14 2 10 15 3 8 Run on example graph

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);  6 4 9 5    14 2 10 15    3 8  Run on example graph

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);  6 4 9 5    14 2 10 15 r   3 8  Pick a start vertex r

Red vertices have been removed from Q 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);  6 4 9 5    14 2 10 15 u   3 8  Red vertices have been removed from Q

Red arrows indicate parent pointers 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);  6 4 9 5    14 2 10 15 u   3 8 3 Red arrows indicate parent pointers

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);  6 4 9 5 14   14 2 10 15 u   3 8 3

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);  6 4 9 5 14   14 2 10 15   3 8 3 u

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);  6 4 9 5 14   14 2 10 15 8  3 8 3 u

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);  6 4 9 5 10   14 2 10 15 8  3 8 3 u

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);  6 4 9 5 10   14 2 10 15 8  3 8 3 u

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);  6 4 9 5 10 2  14 2 10 15 8  3 8 3 u

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);  6 4 9 5 10 2  14 2 10 15 8 15 3 8 3 u

Prim’s Algorithm 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);  6 4 9 5 10 2  14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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);  6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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); 4 6 4 9 5 10 2 9 14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

Prim’s Algorithm 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); 4 6 4 9 5 5 2 9 14 2 10 15 8 15 3 8 3

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); 4 6 4 9 5 5 2 9 14 2 u 10 15 8 15 3 8 3

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 is the hidden cost in this code?

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)

Single-Source Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v “Shortest-path” = minimum weight Weight of path is sum of edges E.g., a road map: what is the shortest path from Hattiesburg to Nashville?