All Shortest Paths Questions from exercises and exams.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
Chapter 25: All-Pairs Shortest-Paths
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Chapter 23 Minimum Spanning Trees
1 Graphs: shortest paths & Minimum Spanning Tree(MST) Fundamental Data Structures and Algorithms Ananda Guna April 8, 2003.
Shortest Path Problems
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
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)
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.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Tirgul 11 Solutions for questions from T2, T3 DFS & BFS - reminder Some Hashing Reminder : don’t forget to run ~dast/bin/testEx3.csh on your jar file before.
All-Pairs Shortest Paths
Tirgul 13 Today we’ll solve two questions from last year’s exams.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
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.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Analysis of Algorithms
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Chapter 2 Graph Algorithms.
COSC 2007 Data Structures II Chapter 14 Graphs III.
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.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
Graphs.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
Introduction to Algorithms Jiafen Liu Sept
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.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
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.
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Graphs. Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A,
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Shortest Paths and Minimum Spanning Trees
Introduction to Graphs
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimum Spanning Trees
Short paths and spanning trees
Graphs.
CS 583 Analysis of Algorithms
Shortest Paths and Minimum Spanning Trees
Algorithms (2IL15) – Lecture 7
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Presentation transcript:

All Shortest Paths Questions from exercises and exams

The Problem: G = (V, E, w) is a weighted directed graph. We want to find the shortest path between any pair of vertices in G. Example: find the distance between cities on a road map. Can you use already known algorithms?

From every vertex in the graph Run –Dijkstra: O(|V||E|log|V|) = O(|V| 3 log|V|) –Run Bellman-Ford: O(|V| 2 |E|) = O(|V| 4 ) Can we do better?

Dynamic Programming Dynamic Programming is a technique for solving problems “bottom-up”: first, solve small problems, and then use the solutions to solve larger problems. What kind of problems can Dynamic Programming solve efficiently?

Dynamic Programming Optimal substructure: The optimal solution contains optimal solutions to sub-problems. What other algorithms can suit this kind of problems? Greedy algorithms Overlapping sub-problems: the number of different sub-problems is small, and a recursive algorithm might solve the same sub-problem a few times.

All Shortest Paths How can we define the size of sub-problems for the all shortest paths problem? (two way) Suggestion 1: according to the maximal number of edges participating in the shortest path (what algorithm uses this idea?) Suggestion 2: according to the set of vertices participating in the shortest paths (Floyd- Warshall)

All Shortest Paths - Suggestion 1 The algorithm uses the |V|x|V| matrix representation of a graph The result matrix - cell (j,k) contains the weight of the shortest path between vertex j and vertex k. Initialization: paths with 0 edges. What actual values are used? d i,k =  for i  k, d i,i = 0 In iteration m, we find the shortest paths between all vertices with no more then m edges and keep them in the matrix D (m). How many iterations are needed?

All Shortest Paths - Suggestion 1 no circles with negative weights - |V| -1 iterations. In iteration m: –For every (v,u), find the minimum of: The current shortest path v ~> u (maximum m-1 edges) For every w in Adj(u): The shortest path with maximum m edges trough w, which is the shortest path v~>w with maximum m-1 edges, plus the edge (w,u).

All Shortest Paths - Suggestion 1 Time complexity: –|V| iterations –In each iteration: going over O(|V| 2) pairs of vertices in –For each pair (u,v): going over O(|V|) possible neighbors –Total: O(|V| 4 )

All Shortest Paths - Suggestion 1 Improvement: If we know the shortest paths up to m edges long between every pair of vertices, we can find the shortest paths up to 2m edges in one iteration: For (v,u) - the minimal path through vertex w is v~>w~>u, when v~>w and w~>u have at most m edges. Time complexity: O(|V| 3 log |V|)

All Shortest Paths - Suggestion 1 Can we use this method to solve single- source-shortest-paths? Yes - we can update only the row vector that matches the single source, by using the results of previous iterations and the weights matrix. Note that this version is similar to Bellman- Ford.

Floyd-Warshall Algorithm Intermediate vertices on path p = are all the vertices on p except the source v 1 and the destination v l. If we already know the all shortest paths whose intermediate vertices belong to the set {1,…,k-1}, how can we find all shortest paths with intermediate vertices {1,…,k}? Consider the shortest path p between (i, j), whose intermediate vertices belong to {1,…k}

Floyd-Warshall Algorithm If k is not an intermediate vertex in p, then p is the path found in the previous iteration. If k is in p, then we can write p as i~> k ~> j, where the intermediate vertices in i~> k and k~> j belong to {1,…,k-1}. The algorithm: –Initialize: D (0) =W –For k = 1…|V| For i = 1…|V| –For j = 1…|V| »d (k) i,j = min(d i,j (k-1), d ik (k-1) + d k,j (k-1) ) Time complexity: O(|V| 3 )

Johnson’s Algorithm We already wrote, debugged and developed emotional attachement to the Dijkstra and Bellman-Ford algorithms. How can we use them to efficiently find all-shortest-paths? Step 1: What should we do to successfully run Dijkstra if we are sure that there are no circles with negative weights?

Johnson’s Algorithm We can find a mapping from the graph’s weights to non-negative weights. The graph with the new weights must have the same shortest paths. Step 2: How can we be sure that there are no negative weighted circles? Simply run Bellman-Ford

Johnson’s Algorithm The algorithm: Add a dummy vertex, v, and an edge with weight 0 from v to every vertex in the graph. The modified graph has the same negative circles.

Johnson’s Algorithm Run Bellman-Ford from v to find negative circles, if any. Use the shortest paths from v to define non- negative weights: w’(s, t) = w(s,t) + h(s) - h(t) Is W’ non-negative? Yes, due to the fact that h(t)  w(s,t) + h(s)

Johnson’s Algorithm Do shortest paths remain shortest? Let p be a shortest path between v 0 and v l, then w’(p) =  w’(v i-1, v i ) =  [w(v i-1, v i ) + h(v i-1 ) - h(v i )] = w(p) + h(v 0 ) - h(v l ) The term h(v 0 ) - h(v l ) is common to all paths between v 0 and v l, so the minimal w’(p) matches the minimal w(p)

Johnson’s Algorithm So - now we can use W’ to run Dijkstra from each vertex in G. Time complexity: O(VE + |V| 2 |E| log|V|) Good for sparse graphs

a) Define Spanning Tree and Minimal Spanning Tree. Spanning Tree: Given a graph G=(V,E), a spanning tree T of G is a connected graph T=(V,E’) with no cycles (same vertices, a subset of the edges). For example, this graph has three spanning trees: {(a,b);(a,c)}, {(a,b);(b,c)}, {(a,c);(b,c)} a bc

Minimal Spanning Tree (MST): Given a weighted graph G=(V,E, w), define the weight of a spanning tree T as. Then a minimal spanning tree T is a spanning tree with minimal weight, i.e. T satisfies: For example, this graph has two minimal spanning trees: {(a,b);(b,c)}, {(a,c);(b,c)} a bc 22 1

b) Either prove or disprove the following claim: In a weighted (connected) graph, if every edge has a different weight then G has exactly one MST. First notice that if the edge weights are not distinct, then the claim is incorrect, for example the previous graph. So, can we come up with a counter-example when weights are distinct ? (no, but thinking about it for a few minutes sometimes helps...)

A useful feature of spanning trees Claim: Suppose T 1 and T 2 are two spanning trees of G. Then for any edge e 1 in T 1 \T 2 there exists an edge e 2 in T 2 \T 1 such that is also a spanning tree. To see this, consider the following partition of G: GvGv GuGu vu v’v’ u’u’ e1 e2

A useful feature of spanning trees Proof: Suppose e 1 = (v,u). Denote by G v and G u the two connected components of G when removing e 1 from T 1. Examine the path from v to u in T 2 : there must be an edge e 2 =(v’,u’) in T 2 such that v’ is in G v and u’ is in G u. Let. T’ is connected and has no cycles, thus it is a spanning tree, as claimed. Take two vertices x and y in G. If both are in G v or in G u then there is exactly one path from x to y since G v and G u are connected with no cycles. If x is in G v and y is in G u then there is also exactly one path between them: from x to v’, then to u’, and then to y.

Claim: In a weighted (connected) graph, if every edge has a different weight, then G has exactly one MST. Proof: Suppose by contradiction that there are two MSTs, T 1 and T 2. Suppose also that the largest edge in T 1 \T 2 is larger than the largest edge in T 2 \T 1 (notice they can’t be equal). Let e 1 be the largest edge in T 1 \ T2. There is an edge e 2 in T 2 \T 1 such that is a spanning tree with weight: so T 1 is not an MST -> Contradiction. Back to the Question

Wrong proof for this claim A common (but wrong) argument from exams: “The Generic-MST algorithm always has a unique safe edge to add, thus it can create only one MST.” Why this is wrong? –There might be other ways to find an MST besides the Generic-MST algorithm. –It is not true that there is always one unique safe edge (!) For example, Prim and Kruskal might choose a different edge at the first step, although they are both Generic-MST variants

c) Write an algorithm that receives an undirected graph G=(V,E) and a sub-graph T=(V,E T ) and determines if T is a spanning tree of G (not necessarily minimal). What do we have to check? Cycles - run DFS on T and look for back edges Connectivity - if there are no cycles, it is enough to check that |E T |=|V|-1.

Question 2 a) Both in Dijkstra and in Prim we have a set of nodes S (that initially contains only s), and we add one additional node in each iteration. Prove or disprove that in both algorithms the nodes are added to S in the same order. The claim is not correct. A contradictory example: –Prim takes s,a,b,c –Dijkstra takes s,a,c,b s ab c 2 2 3

b) Consider a directed graph with positive weights. Give an algorithm that receives a node s and prints the shortest cycle that contains s. Suggestion 1: for every outgoing edge from s, (s, v), find the shortest path from v to s. Suggestion 2: Add a new node s’, and for every edge (s,v) add an edge (s’,v) with the same weight. Now find a shortest path from s’ to s. Question 2 - difficult

Question 3 An in-order tree walk can be implemented by finding the minimum element and then making n-1 calls to TREE-SUCCESSOR How many times at most do we pass through each edge?

Question 3 TREE-SUCCESSOR(x) if x.right==null y=x.parent while y!=null && x==y.right x=y y=y.parent else y=x.right while y.left!=null y=y.left return y going up (1) going up (2) going down (3) going down (4)

Question 3 Right edges: –A right edge n  n.right is passed downwards only at (3), which happens when we call TREE-SUCCESSOR(n) –Since we call TREE-SUCCESSOR once for each node, we go down each right edge once, at most Left edges: –After we pass a left edge n  n.left (at (1) or (2)), TREE- SUCCESSOR returns n –Since TREE-SUCCESSOR returns each node once, we go up each left edge once, at most Therefore, we pass each edge at most twice In-order walk takes O(n) steps

Question 4 You are in a square maze of n  n cells and you’ve got loads of coins in your pocket. How do you get out? The maze is a graph where –Each cell is a node –Each passage between cells is an edge Solve the maze by running DFS until the exit is found

DFS-VISIT(u) u.color=gray u.d=++time for each v  adj[u] if v.color=white v.prev=u DFS-VISIT(v) u.color=black u.f=++time DFS(G) for each u  V[G] u.color=white u.prev=nil time=0 for each u  V[G] if u.color=white DFS-VISIT(u) DFS - Reminder

Question 4 What does each color represent in the maze? –White - a cell without any coins –Gray - a cell with a coin lying with its head side up –Black -a cell with a coin lying with its tail side up An edge connecting a node to its parent is marked by a coin When visiting a cell, we color it gray If it has a white cell adjacent to it – visit it If there are no such cells, –Color the cell “black” by flipping the coin –backtrack by going to the cell marked as parent

Each node has one parent When backtracking, the parent will be the only adjacent “gray” cell that has a coin leading to it Can we solve it using BFS? No! In DFS we go between adjacent cells; in BFS, the nodes are in a queue, so the next cell could be anywhere Question 4