More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Implementing a Graph Implement a graph in three ways: –Adjacency List –Adjacency-Matrix –Pointers/memory for each node (actually a form of adjacency list)
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Greed is good. (Some of the time)
Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Shortest Paths Definitions Single Source Algorithms
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Dijkstra's algorithm.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
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/10/2015 ITCS 6114 Single-Source Shortest Path.
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
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)
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Single-Source Shortest Paths
Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
Minimum Spanning Trees
Kruskal’s Minimum Spanning Tree Algorithm
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms

Spanning Tree Definition –A spanning tree of a graph G is a tree (acyclic) that connects all the vertices of G once i.e. the tree “spans” every vertex in G –A Minimum Spanning Tree (MST) is a spanning tree on a weighted graph that has the minimum total weight Where might this be useful? Can also be used to approximate some NP-Complete problems

Sample MST Which links to make this a MST? Optimal substructure: A subtree of the MST must in turn be a MST of the nodes that it spans. Will use this idea more in dynamic programming.

MST Claim Claim: Say that M is a MST –If we remove any edge (u,v) from M then this results in two trees, T1 and T2. –T1 is a MST of its subgraph while T2 is a MST of its subgraph. –Then the MST of the entire graph is T1 + T2 + the smallest edge between T1 and T2 –If some other edge was used, we wouldn’t have the minimum spanning tree overall

Greedy Algorithm We can use a greedy algorithm to find the MST. –Skipping some terminology on the cut, the cut respects the edge –Two common algorithms Kruskal Prim

Kruskal’s MST Algorithm Idea: Greedily construct the MST –Go through the list of edges and make a forest that is a MST –At each vertex, sort the edges –Edges with smallest weights examined and possibly added to MST before edges with higher weights –Edges added must be “safe edges” that do not ruin the tree property.

Kruskal’s Algorithm

Kruskal’s Example A={ }, Make each element its own set. {a} {b} {c} {d} {e} {f} {g} {h} Sort edges. Look at smallest edge first: {c} and {f} not in same set, add it to A, union together. Now get {a} {b} {c f} {d} {e} {g} {h}

Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e} {g} {h} {e} <> {h}, add edge. Now get {a} {b} {c f} {d} {e h} {g}

Kruskal Example Keep going, checking next smallest edge. Had: {a} {b} {c f} {d} {e h} {g} {a} <> {c f}, add edge. Now get {b} {a c f} {d} {e h} {g}

Kruskal’s Example Keep going, checking next smallest edge. Had {b} {a c f} {d} {e h} {g} {b} <> {a c f}, add edge. Now get {a b c f} {d} {e h} {g}

Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {a b c f}, dont add it!

Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f} {d} {e h} {g} {a b c f} = {e h}, add it. Now get {a b c f e h} {d}{g}

Kruskal’s Example Keep going, checking next smallest edge. Had {a b c f e h} {d}{g} {d} <> {a b c e f h}, add it. Now get {a b c d e f h} {g}

Kruskal’s Example Keep going, check next two smallest edges. Had {a b c d e f h} {g} {a b c d e f h} = {a b c d e f h}, don’t add it a b cd e fg h

Kruskal’s Example Do add the last one: Had {a b c d e f h} {g}

Runtime of Kruskal’s Algo Runtime depends upon time to union set, find set, make set Idea: number each vertex and use an array –Use an array member[] : member[i] is a number j such that the ith vertex is a member of the jth set. –Example member[1,4,1,2,2] indicates the sets S1={1,3}, S2={4,5} and S4={2}; i.e. position in the array gives the set number. Idea similar to counting sort, up to number of edge members.

Set Operations Given the Member array Make-Set(v) member[v] = v Make-Set runs in constant running time for a single set. Find-Set(v) Return member[v] Find-Set runs in constant time. Union(u,v) for i=1 to n do if member[i] = u then member[i]=v Scan through the member array and update old members to be the new set. Running time O(n), length of member array member = [1,2,3]; {1} {2} {3} find-set(2) = 2 Union(2,3) member = [1,3,3] ; {1} {2 3}

Overall Runtime O(V) O(ElgE) – using heapsort O(1) O(V) Total runtime: O(V)+O(ElgE)+O(E*(1+V)) = O(E*V) Book describes a version using trees for sets that runs in O(E*lgV) time O(E)

Prim’s MST Algorithm Also greedy, like Kruskal’s Will find a MST but may differ from Prim’s if multiple MST’s are possible

Prim’s Example

Prim’s Algorithm

Get spanning tree by connecting nodes with their parents:

Runtime for Prim’s Algorithm The inner loop takes O(E lg V) for the heap update inside the O(E) loop. This is over all executions, so it is not multiplied by O(V) for the while loop (this is included in the O(E) runtime through all edges. The Extract-Min requires O(V lg V) time. O(lg V) for the Extract-Min and O(V) for the while loop. Total runtime is then O(V lg V) + O(E lg V) which is O(E lg V) in a connected graph (a connected graph will always have at least V-1 edges). O(V) if using a heap O(V) O(lgV) if using a heap O(E) over entire while(Q<>NIL) loop O(lgV) to update if using a heap!

Shortest Path Algorithms

Shortest Path? Example: What is the shortest path from g to b?

Definitions Formal definition of problem –Input: Weighted directed graph G=(V,E) plus w: E  R, the weight function that maps from edges to weight values (real numbers). –Output: The shortest path between any source S and all other vertices. –The weight of a path is the sum of the weights of the edges on the path. w(u,v) is the weight of arc that connects vertex u and v; it is not necessarily the min. w(p) is the weight of some path p, where p is a list of vertices indicating the path from a source to destination. (u,v) is the weight of the shortest path that connects vertex u and v.

Shortest Path One way to address this problem is to shift edge values up to remove negative values

Properties of Shortest Paths

Relaxation Example

Dijkstra’s Algorithm

Dijkstra Example (0) a b cd e fg h 154

Dijkstra Example 1 Extract min, vertex f. S={f}. Update shorter paths a b cd e fg h 154 INF,NIL 0,NIL INF,NIL

Dijkstra Example 2 Extract min, vertex c. S={fc}. Update shorter paths a b cd e fg h ,f INF,NIL 0,NIL 2,f 4,f 15,f INF,NIL

Dijkstra Example 3 Extract min, vertex d. S={fcd}. Update shorter paths (None) a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example a b cd e fg h 154 7,c INF,NIL 0,NIL 2,f 3,c 15,f Extract min, vertex a. S={fcda}. Update shorter paths (None) Extract min, vertex b. S={fcdab}. Update shorter paths. 6,c

Dijkstra Example 5 Extract min, vertex h. S={fcdabh}. Update shorter paths a b cd e fg h 154 7,c INF,NIL 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example 6 Extract min, vertex g and h – nothing to update, done! a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra Example 7 Can follow parent “pointers” to get the path a b cd e fg h 154 7,c 16,h 13,b 0,NIL 2,f 3,c 15,f 6,c

Dijkstra vs. Prim’s MST Very similar to Prim’s MST algorithm, but the two compute different things. Not the shortest path in MST, but picks shortest edge overall to connect all edges –(what case would the MST pick a different edge than Dijkstra?)

Dijkstra’s Runtime Using a linear array for the queue, with the index as the vertex number, contents of array as it’s distance value O(V) O(V) to scan through array O(E) over entire while loop O(1) - direct access via array index Total runtime = O(V) + O(V 2 ) + O(E) = O(V 2 )

Dijkstra’s Runtime Using a min heap for the queue O(V) O(lgV) O(E) over entire while loop O(lgV) - must update heap key Total runtime = O(V) + O(VlgV) + O(ElgV) = O(ElgV) in a connected graph What if the graph is fully connected? O(V 2 lgV) Actually slower than the simple array-based queue for dense graphs!

All Pairs Shortest Path Finding the shortest path between all pairs of vertices One solution is to run Dijkstra’s Algorithm for every vertex. This will require time O(V)(Time-Dijkstra). –If using a linear array for Q, this is time O(V 3 ). –There are other ways to solve this problem using dynamic programming which we will examine next!