Graph (II) Shortest path, Minimum spanning tree GGuy 19-3-2011.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Single Source Shortest Paths
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
1 Paths in Graphs Oscar Miguel Alonso M. 2 Outline The problem to be solved Breadth first search Dijkstra's Algorithm Bellman-Ford Algorithm Shortest.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
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.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
Discrete Optimization Lecture 1
Minimum Spanning Trees and Shortest Paths
Minimum Spanning Trees
All-Pairs Shortest Paths (26.0/25)
Short paths and spanning trees
Graph Algorithm.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Data Structures – LECTURE 13 Minumum spanning trees
Minimum-Cost Spanning Tree
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Minimum Spanning Trees
The Bellman-Ford Shortest Path Algorithm Neil Tang 03/27/2008
Algorithms: Design and Analysis
Chapter 24: Single-Source Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Prim’s algorithm for minimum spanning trees
Winter 2019 Lecture 10 Minimum Spanning Trees
Single-Source Shortest Path & Minimum Spanning Trees
Topological Sorting Minimum Spanning Trees Shortest Path
Chapter 9: Graphs Spanning Trees
Minimum-Cost Spanning Tree
Presentation transcript:

Graph (II) Shortest path, Minimum spanning tree GGuy

Review DFS (Depth-First-Search) Graph traverse Topological sort BFS (Breadth-First-Search) Finding shortest path

Shortest Path Problem In a weighted graph G We want to find a path from s to t, such that the sum of edge weight is minimum

Shortest Path S T

S T

Algorithms Single source? All pairs? Negative weight edge? Negative cycles?

Dijkstra’s algorithm for-each v, d[v] ← ∞ Q.Insert(s,0) while not Q.Empty() do (u, w) = Q.ExtractMin() if (visited[u]) continue; d[u] ← w for-each v where (u, v) in E Q.Insert(v, d[u] + weight uv )

Dijkstra’s algorithm S T

0 T

0 2 T

0 2 T

0 3 2 T

0 3 2 T

T

T

T

Implement Q using binary heap (priority queue) At most E edges in the heap Time complexity: O(E log (E)) = O(E log (V)) Space complexity: O(V+E)

Bellman Ford for-each v, d[v] ← ∞ d[s] ← 0 Do V-1 times for each edge (u,v) if (d[u] + weight uv < d[v]) d[v] ← d[u] + weight uv

Bellman Ford’s algorithm S T

Bellman Ford’s algorithm S T

Bellman Ford’s algorithm S

Bellman Ford’s algorithm S

Bellman Ford’s algorithm Assume the shortest path from s->t is P 0 P 1 P 2 … P m-1 where m < |V| and s is fixed In the 1st iteration, d[P 1 ] is shortest In the 2nd iteration, d[P 2 ] is shortest … In the m-1 iteration, d[P m-1 ] is shortest

Bellman Ford’s algorithm After |V|-1 iterations, If there exists edge (u,v) where d[u] + weight uv < d[v] Negative cycle!

Bellman Ford’s algorithm Time complexity: O(VE) Space complexity: O(V)

Floyd Warshall’s algorithm for all pairs(i,j), d[i][j] ← ∞ for all edges(u,v),d[u][v] ← weight uv for k=1 to V for i=1 to V for j=1 to V d[i][j] = min(d[i][j], d[i][k]+d[k][j])

Floyd Warshall’s algorithm Assume the shortest path from s->t is P 0 P 1 P 2 … P m-1 for any s,t For example, 5->3->1->2->4->6 i=1: 5->1->6 i=2: 5->1->2->6 i=3: 5->3->1->2->6 i=4: 5->3->1->2->4->6 i=5: 5->3->1->2->4->6 i=6: 5->3->1->2->4->6 We won’t miss any shortest path!

Floyd Warshall’s algorithm Time complexity: O(V 3 ) Space complexity: O(V 2 )

Summary Negative edges?Negative cycle?Time complexity DijkstraSingle sourceNo O(E log V) Bellman FordSingle sourceYes O(VE) Floyd WarshallAll pairsYesMaybe (modified) O(V 3 )

Tree What is a tree? G is connected and acyclic G is connected and |E| = |V| - 1 G is acyclic and |E| = |V| - 1 For any pairs in G, there is a unique path

Spanning Tree

Minimum Spanning Tree

Kruskal’s algorithm T ← empty set of edges Sort the edges in increasing order For each edge e (in increasing order) if T + e does not contain a cycle add e to T

Kruskal’s algorithm How to detect a cycle? Disjoint Set

Kruskal’s algorithm If we choose an edge (u,v) Union(u, v)

Kruskal’s algorithm

Krustal’s algorithm Sorting: O(E log V) Select edge: O(E) Check union: O(α(V)) Overall: O(E log V + E α(V))

The Red Rule The Red Rule states that for any cycle in G, the largest weight edge will NOT be contained in any Minimum Spanning Tree.

Prim’s algorithm T ← node 1 while size of T < V choose a vertex u that is not in V and the cost adding it to V is minimum add u to V

Prim’s algorithm

Use a min heap to maintain Dijkstra like implement Time complexity: O(E log V)