Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths.

Slides:



Advertisements
Similar presentations
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Shortest Paths1 C B A E D F
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.
CSC 213 Lecture 23: Shortest Path Algorithms. Weighted Graphs Each edge in weighted graph has numerical weight Weights can be distances, building costs,
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Lecture 22: Matrix Operations and All-pair Shortest Paths II Shang-Hua Teng.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Shortest Paths Definitions Single Source Algorithms
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Shortest Paths1 C B A E D F
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Shortest Paths C B A E D F
SPANNING TREES Lecture 21 CS2110 – Spring
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Lecture 16. Shortest Path Algorithms
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
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.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Contest Algorithms January 2016 Introduce the main kinds of graphs, discuss two implementation approaches, and remind you about trees 6. Intro. to Graphs.
7. Graph Traversal Describe and compare depth-first and breadth-first graph searching, and look at the creation of spanning trees Contest Algorithms: 7.
Discrete Maths 11. Graph Theory Objective
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington These slides are.
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
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.
Contest Algorithms January 2016 Describe the maxflow problem, explain the Ford-Fulkerson, Edmonds-Karp algorithms. Look at the mincut problem, bipartite.
Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some.
Shortest Paths.
Shortest Path 6/18/2018 4:22 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Eager Prim Dijkstra.
Shortest Paths C B A E D F
Eager Prim Dijkstra.
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Chapter 13 Graph Algorithms
Minimum Spanning Tree.
Fundamental Structures of Computer Science II
Algorithms: Design and Analysis
Algorithms: Design and Analysis
Shortest Paths.
Shortest Paths.
Chapter 9: Graphs Shortest Paths
Presentation transcript:

Contest Algorithms January 2016 Describe shortest path trees, SSSP, APSP, and three algorithms: Dijkstra, Bellman-Ford, Floyd-Warshall 9. Shortest Paths 1Contest Algorithms: 9. Shortest Paths

 The length of a path = the sum of the weights of the edges in the path.  The shortest path between two verticies = the path having the minimum length. 1. Shortest Paths 2 ORD PVD MIA DFW SFO LAX LGA HNL

 A shortest path tree spans all the nodes in the graph, using edges that give the shortest paths from the starting node to all the other vertices. Shortest Path Tree (SPT) SPT start node is 0

 SSSP: single source shortest path  find the shortest path from a given vertex to all the other nodes  for undirected, unweighted graphs use BFS  for directed, weighted graph use:  Dijkstra's algoritm, Bellman-Ford  APSP: all pairs shortest path  find the shortest path between all pairs of nodes in the graphs  popular contest algorithm  use Floyd-Warshall for small graphs Types of Shortest Path Problems Contest Algorithms: 8 MSTs4

 Build a shortest path tree (SPT) from a source node  No negative edge weights allowed.  Similar to Prim's algorithm (Greedy)  Grow a tree gradually, advancing from vertices taken from a priority queue of "distances"  Instead of a MST, we are building a SPT 2. Dijkstra’s Algorithm O(|E| * log |V|)

 Each vertex has a entry in a distTo[] array.  Initialize dist[start] to 0 and all other distTo[] entries to positive infinity (  )  During the execution, the distTo[] values will get smaller. This is called (edge) relaxing.  At each iteration, the SPT is grown by adding a vertex with the smallest distTo[] value. Algorithm in Words

What is Relaxing? a b c d distTo[a] = 3 distTo[b] = 13 distTo[c] = 13 distTo[d] = relax 'edges' linked to a a is in the SPT a b c d distTo[a] = 3 distTo[b] = 8 distTo[c] = 7 distTo[d] = c will be the next vertex to join SPT if (distTo[b] > distTo[a] + weight()) distTo[b] = distTo[a] + weight();

An Example (directed, weighted) continued Start Vertex: 0

Initialisation continued Inf = changing distTo 0

First Iteration continued 2 Inf 1 0 = final distTo

Second Iteration continued 2 Inf

Third Iteration Inf continued

Fourth Iteration Could have chosen ‘d’ instead. continued

Fifth Iteration Choosing ‘3’ is a waste of time. continued

Sixth Iteration Shortest path from node ‘0’ to ‘node 7’ is length 5.

Final Result 6 6

 no. of vertices, no. of edges, start vertex  many lines, each containing one triple (u v, weight)  e.g Data Input Format Contest Algorithms17

 Adjacency list for a directed, weighted graph.  A list of distances from the starting vertex to the other vertices; this data structure is computed by the algorithm.  A priority queue of (distances, vertex) pairs sorted based on increasing distances  i.e. smallest distance is first Dijkstra Data Structures Contest Algorithms: 8 MSTs18

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java Dijkstra "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); int vStart = sc.nextInt(); // adj list contains lists of (vertex, weight) pairs ArrayList > adjList = new ArrayList<>(); for (int i = 0; i < numVs; i++) { ArrayList neighbors = new ArrayList (); adjList.add(neighbors); // add neighbor list to Adjacency List } for (int i = 0; i < numEs; i++) { int u = sc.nextInt(); int v = sc.nextInt(); int weight = sc.nextInt(); adjList.get(u).add(new IPair(v, weight)); //add (v,weight) to list for u } : Code Contest Algorithms: 8 MSTs19 see Dijkstra.java

// list of distances from vStart to the other vertices ArrayList distTo = new ArrayList<>(); distTo.addAll(Collections.nCopies(numVs, INF)); distTo.set(vStart, 0); // pri-queue of (distance, vertex) pairs PriorityQueue pq = new PriorityQueue (numVs); // sort based on increasing distances pq.offer(new IPair(0, vStart)); while (!pq.isEmpty()) { IPair nearest = pq.poll(); // pick shortest unvisited vertex int d = nearest.getX(); int u = nearest.getY(); if (d <= distTo.get(u)) // process vertex u only once relax(u, adjList, distTo, pq); } for (int i = 0; i < numVs; i++) System.out.println(" " + vStart + " --> " + i + " = " + distTo.get(i)); } // end of main() Contest Algorithms: 8 MSTs20

private static void relax(int u, ArrayList > adjList, ArrayList distTo, PriorityQueue pq) // examine all neighbors of u, trying to relax their distTo values { for (IPair neighbor : adjList.get(u)) { int v = neighbor.getX(); // for u-v edge int vWeight = neighbor.getY(); int uvDist = distTo.get(u) + vWeight; if (distTo.get(v) > uvDist) { // possibly relax distTo.set(v, uvDist); pq.offer(new IPair(uvDist, v)); } } // end of relax() Contest Algorithms: 8 MSTs21

Example (again) Contest Algorithms: 8 MSTs spData3.txt

Execution Contest Algorithms: 8 MSTs23

Example 2 (fig 4.17, CP) Contest Algorithms: 8 MSTs Start Vertex: spData1.txt

Execution Contest Algorithms: 8 MSTs

 Bellman-Ford algorithm can be used on graphs with negative edge weights  as long as the graph contains no negative cycles reachable from the source  Slower than Dijkstra's algorithm for the same problem  O(|V| * |E|) 3. Bellman-Ford Algorithm

Example This example can only be processed by Bellman-Ford since there are negative weights. There are no negative cycles

 Bellman-Ford relaxes all the edges |V | − 1 times.  The biggest path in the SPT is at most |V|-1 edges long. So |V|-1 loops is certain to process it Algorithm in Words

Example Execution distTo[] 00 edges distTo[] 067067 edges - 0->1 - 0->3 - The source node is 0 after each relax for-loop iteration

distTo[] edges - 0->1 3->2 0->3 1-> >3 3->2 0->3 1-> >3 3->2 0->3 1->4 Finished

 The same as for Dijkstra:  no. of vertices, no. of edges, start vertex  many lines, each containing one triple (u v, weight) Input Data Format Contest Algorithms: 8 MSTs31

 Adjacency list for a directed, weighted graph.  A list of distances from the starting vertex to the other vertices; this data structure is computed by the algorithm.  Unlike Dijkstra, there is no need for a priority queue of (distances, vertex) pairs  this makes the code a little simpler Bellman-Ford Data Structures Contest Algorithms: 8 MSTs32

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java BellmanFord "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); int vStart = sc.nextInt(); // adj list contains lists of (vertex, weight) pairs ArrayList > adjList = new ArrayList<>(); for (int i = 0; i < numVs; i++) { ArrayList neighbor = new ArrayList<>(); adjList.add(neighbor); // add neighbor list to Adjacency List } for (int i = 0; i < numEs; i++) { int u = sc.nextInt(); int v = sc.nextInt(); int weight = sc.nextInt(); adjList.get(u).add(new IPair(v, weight)); //add(v, weight) to list for u } : Code Contest Algorithms: 8 MSTs33 see BellmanFord.java

// list of distances to vertices from vStart ArrayList distTo = new ArrayList (); distTo.addAll(Collections.nCopies(numVs, INF)); distTo.set(vStart, 0); for (int i = 0; i < numVs-1; i++) // relax all edges numVs-1 times, O(numVs) for (int u = 0; u < numVs; u++) relax(u, adjList, distTo); // negative cycle detection- boolean hasNegCycle = false; for (int u = 0; u < numVs; u++) // one more pass to check for (IPair v : adjList.get(u)) { // relax these edges if (distTo.get(v.getX()) > (distTo.get(u)+v.getY())) // should be false hasNegCycle = true; } System.out.println("Negative Cycle Exist? " + hasNegCycle); if (!hasNegCycle) // print distances from start vertex for (int i = 0; i < numVs; i++) System.out.println(" " + vStart + " --> " + i + " = " + distTo.get(i)); } // end of main() Contest Algorithms: 8 MSTs34

private static void relax(int u, ArrayList > adjList, ArrayList distTo) // examine all neighbors of u, trying to relax their distTo values // same as Dijsktra's relax(), but without the pri-queue { for (IPair neighbor : adjList.get(u)) { // relax these edges int v = neighbor.getX(); // for u-v edge int vWeight = neighbor.getY(); int uvDist = distTo.get(u) + vWeight; if (distTo.get(v) > uvDist) // possibly relax distTo.set(v, uvDist); } } // end of relax() Contest Algorithms: 8 MSTs35

DirectedEdge[] edgeTo; double[] distTo; void bellmanFord(Digraph graph, int start) { for (int v : graph.allVerts()) distTo[v] = Double.POSITIVE_INFINITY; distTo[start] = 0; for (int i=1; i < graph.numV(); i++) relax(graph); for (DirectedEdge e : graph.allEdges()) { int u = e.from(); // edge e is u --> v int v = e.to(); if (distTo[v] > distTo[u] + e.weight()) println("There's no solution"); } Code Initialize distTo[], which will reduce to shortest-path value Relaxation: Make |V|-1 passes, relaxing every edge Test for solution When do we not get a solution?

void relax(Digraph graph) // a simpler version than Dijkstra { for (DirectedEdge e : graph.allEdges()) { int u = e.from(); // edge e is u --> v int v = e.to(); if (distTo[v] > distTo[u] + e.weight()) { distTo[v] = distTo[u] + e.weight(); edgeTo[v] = e; }

Example Again Contest Algorithms: 8 MSTs spData4.txt Start Vertex: 0

Execution Contest Algorithms: 8 MSTs39 Start Vertex:

 Contains a negative cycle. Example 2 (fig 4.19, cp) Contest Algorithms: 8 MSTs Start Vertex: spData5.txt

4. Floyd-Warshall Algorithm  Used for APSP problems.  If you can get from A to B at cost c 1, and you can get from B to C with cost c 2, then you can get from A to C with at most cost c 1 +c 2  As the algorithm proceeds, if it finds a lower cost for A to C then it uses that instead.  Running time: O(|V| 3 ) A B C c1c1 c2c2 at most c 1 + c 2

Example from to D 0 = (d ij ) 0 d ij = shortest distance from i to j through nodes {0, …, k} k d ij = shortest distance from i to j through no nodes 0

D 0 = (d ij ) d ij = shortest distance from i to j through {0, …, k} k Before D 1 = (d ij ) After

D 0 = (d ij ) D 1 = (d ij ) 1 d ij = shortest distance from i to j through {0, …, k} k Before After ) d ij = k d ij k-1, d ik k-1 d kj k-1 min(

D 2 = (d ij ) D 3 = (d ij ) D 4 = (d ij ) 4 D 5 = (d ij ) 5 to store the path, another matrix can track the last intermediate vertex

 The input format is the same as for Dijkstra's and Bellman- Ford's algorithms:  no. of vertices, no. of edges, start vertex  each line is a triple (u v, weight)  Since Floyd-Warshall is for APSP, there's no need for a start vertex, but I've included it so that the same data files can be used by all three algorithms  the Floyd-Warshall code ignores the start vertex input Data Input Format Contest Algorithms46

 An adjacency matrix for a directed, weighted graph.  There's no need for a priority queue (as in Dijkstra) or a distances list (as in Dijkstra and Bellman-Ford).  A matrix is a bit easier to implement than an adj. list. Floyd-Warshall Data Structures Contest Algorithms: 8 MSTs47

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java FloydWarshall "); return; } Scanner sc = new Scanner(new File(args[0])); int numVs = sc.nextInt(); int numEs = sc.nextInt(); sc.nextInt(); // to skip start vertex int[][] adjMat = new int[numVs][]; for (int i = 0; i < numVs; i++) { adjMat[i] = new int[numVs]; for (int j = 0; j < numVs; j++) adjMat[i][j] = INF; adjMat[i][i] = 0; } for (int i = 0; i < numEs; i++) { int u = sc.nextInt(); int v = sc.nextInt(); int weight = sc.nextInt(); adjMat[u][v] = weight; } : Code Contest Algorithms: 8 MSTs48 see FloydWarshall.java

for (int k = 0; k < numVs; k++) for (int i = 0; i < numVs; i++) for (int j = 0; j < numVs; j++) if (adjMat[i][j] > (adjMat[i][k] + adjMat[k][j])) adjMat[i][j] = adjMat[i][k] + adjMat[k][j]; System.out.println("All-points Shortest Paths"); for (int i = 0; i < numVs; i++) System.out.println(" " + i + " --> " + Arrays.toString(adjMat[i])); } // end of main() Contest Algorithms: 8 MSTs49 short and simple!

Example Again Contest Algorithms: 8 MSTs spData6.txt not used by the code

Execution Contest Algorithms: 8 MSTs51

 Floyd-Warshall running time is best, O(|V| 3 ):  Dijkstra running time: |V| * O(|E| log |V|) = O(|V| 3 log|V|)  Bellman-Ford running time: |V| * O(|V|*|E|) = O(|V| 4 )  Easy to implement, but memory will become a problem for large matricies (|V| > 400)  For larger problems, use Dijkstra or Bellman-Ford by calling the algorithm repeatedly with every vertex as the start node Comparison with Dijkstra & Bellman-Ford Contest Algorithms52