Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree

Slides:



Advertisements
Similar presentations
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Advertisements

Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Shortest Paths Definitions Single Source Algorithms
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
SPANNING TREES Lecture 21 CS2110 – Spring
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 (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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Lecture 13 Shortest Path.
Single-Source Shortest Path
Graph Algorithms BFS, DFS, Dijkstra’s.
Minimum Spanning Trees
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Three Graph Algorithms
Minimum Spanning Trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Spanning Trees.
Minimum Spanning Trees
CS200: Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Shortest Paths C B A E D F Shortest Paths
Algorithms and Data Structures Lecture XII
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 13 Graph Algorithms
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Trees
Lecture 26 CSE 331 Nov 2, 2012.
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Autumn 2015 Lecture 10 Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Lecture 13 Algorithm Analysis
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Chapter 24: Single-Source Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Shortest Paths.
Chapter 24: Single-Source Shortest Paths
CSE 417: Algorithms and Computational Complexity
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Lecture 12 Shortest Path.
Winter 2019 Lecture 10 Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum Spanning Trees
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree

Shortest Path with negative edge length What is w(u,v) can be negative? Motivation: Arbitrage Image from wikipedia

Modeling arbitrage Suppose u, v are different currency, exchange rate is C(u,v) (1 unit of v is worth C(u,v) units of u) Let length of edge w(u,v) = log C(u,v) Length of a path = log of the total exchange rate Shortest path = best way to exchange money. Negative cycle = arbitrage.

Approach: dynamic programming with steps Bellman Ford algorithm: d(u, i) = length of shortest path to get to u with i steps 𝑑 𝑣,𝑖+1 = min 𝑢,𝑣 ∈𝐸 𝑤 𝑢,𝑣 +𝑑(𝑢,𝑖) Question: How many steps do we need to take? Length of last step Shortest Path to a Predecessor

Naïve implementation Slight modification: d(u, i) = length of shortest path to get to u with at most i steps 𝑑 𝑣,𝑖+1 = min {𝑑(𝑣,𝑖), min 𝑢,𝑣 ∈𝐸 𝑤 𝑢,𝑣 +𝑑(𝑢,𝑖) } Initialize d[s, 0] = 0, d[u, 0] = infinity for other u FOR i = 1 to n Initialize d[u, i] = d[u, i-1] for all i FOR all edges (u,v) IF w[u,v]+d[u,i-1] <= d[v,i] THEN d[v,i] = w[u,v]+d[u,i-1] IF there is a vertex such that d[u,n] <> d[u,n-1] THEN There is a negative cycle!

How to detect a negative cycle? When the graph has no negative cycle, a shortest path can only contain n-1 edges. (why?) If the graph has a negative cycle, then there is a vertex whose shortest path with n edges is shorter than all paths with at most n-1 edges.

Practical Implementation Running time: Each iteration takes O(m) time (O(1) per edge) There are n iterations. Total running time O(mn) In practice can be implemented to run efficiently for many graphs Only consider outgoing edges from u if d[u,i] < d[u,i-1] Use a queue to keep track of which vertices are “updated”

Minimum Spanning Tree

Minimum Spanning Tree Input: Undirected graph with edge weights Edge Weight: w[u,v] = cost of connecting two vertices u,v w[u,v] > 0 Goal: Select a subset of edges such that every pair of vertices can be connected by these edges. Minimize the total weight of the edges selected. Observation: Only need to select n-1 edges and form a tree.

Key Property? Recall: Any sub-path of a shortest path is still a shortest path.  Dynamic programming based algorithms. What properties does minimum spanning tree have?

Trees and cycles Adding any non-tree edge to a tree will introduce a cycle For a tree with a cycle, removing any edge in the cycle results in a tree. Basic operation: add an edge and remove another edge in the cycle created (swap).

Why swap? We will use this to give greedy algorithms for MST. Recall: Greedy algorithm, general recipe for proof Assume optimal solution is different from the current solution Try to find the first place the two solutions differ Try to modify OPT so that it looks similar to ALG (use swap here!)

Cuts in Graphs A cut is a subset of edges that separates the vertices into two parts A cut is usually specified by a subset of vertices 𝑆, 𝑆 , S is one part of the vertices, 𝑆 is the set of remaining vertices.

Question for the greedy algorithm Given a cut, which edge should we use? Idea: Use the shortest one (with minimum cost). (Note: Eventually we may use multiple edges in the cut, this only decides which is the first edge we use)

Key Property Key Lemma: Suppose F is a set of edges that is inside some MST T, if 𝑆, 𝑆 is a cut that does not contain any edge in F and e is the minimum cost edge in the cut, then it is safe to add e to F. 𝐹∪ 𝑒 ⊂𝑇′ for some MST T’

General Algorithm for MST Initialize the tree to be empty. (initially a subset of some MST) Repeat Find a cut that does not have any edge in the current tree Add the min cost edge of the cut to the tree (by Key Lemma: still a subset of some MST) Until the tree has n-1 edges (Now we already have a tree, so it must be a MST)