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.

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

Greed is good. (Some of the time)
Minimum Spanning Tree CSE 331 Section 2 James Daly.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Introduction to Algorithms 6.046J/18.401J/SMA5503
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
1 Spanning Trees Lecture 20 CS2110 – Spring
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
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
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
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.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path 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.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
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
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
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.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
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 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.
F d a b c e g Prim’s Algorithm – an Example edge candidates choosen.
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
Lecture 13 Algorithm Analysis
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
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.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
Minimum Spanning Tree Shortest Paths
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Minimum-Cost Spanning Tree
Outline This topic covers Prim’s algorithm:
Minimum-Cost Spanning Tree
Minimum Spanning Tree Algorithms
CSC 413/513: Intro to Algorithms
Minimum Spanning Tree.
CS 3013: DS & Algorithms Shortest Paths.
Lecture 14 Minimum Spanning Tree (cont’d)
Minimum-Cost Spanning Tree
Presentation transcript:

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 A empty On each iteration, we add a suitable edge to A In the case of Kruskal’s algorithm, candidate edges are considered in order by increasing weight MST-Kruskal (G,w) A <- Ǿ For each vertex v  V[G] Make-Set(v) sort the edges(E) in increasing order by weight w for each edge (u,v)  E if FindSet(u)  FindSet(v) A = A U {(u,v)} UNION(u,v)

Kruskal’s Algorithm

Application: The Minimal- Connector Problem Given a connected graph in which each edge has a weight, find a spanning tree with minimum total cost.  Kruskal's Algorithm: Build tree by repeatedly adding min-weight edge that doesn't form a cycle  Prim's Algorithm: Build tree by adding vertices, adjacent to tree so far, for which new edges have min weight and don't form cycle

Properties of Kruskal’s and Prim’s Algorithms Both are greedy: They take the best immediate choice without considering future ramifications  Greedy algorithms don’t always work best, but these do. Prim always maintains a connected graph, while Kruskal may not.

Measuring Efficiency of Graph Algorithms Efficiency = Time complexity = (roughly) how many steps are needed as a function of N (number of vertices) and E (number of edges) For any graph, 0  E  N(N-1)/2 = O(N 2 ) For any connected graph, N-1  E  N(N- 1)/2, O(N)  E  O(N 2 )

Running time of Kruskal Algorithm Kruskal:  Sort E edges into increasing order: Best sorting algorithms take O(E log E) time  Keep track of number of components, and never add an edge with both ends in the same component: O(N)  Total is O(E log E), since O(N)  E If E = O(N) then Kruskal is faster If E = O(N 2 ) then Prim is faster

Shortest Path Problem in Graphs

Problem A motorist wishes to find the shortest possible route from Islamabad to Lahore. Route map is given where distance between each pair of adjacent intersections is marked. One possible way is to enumerate all the routes from Islamabad to Lahore, add up the distance on each route and select the shortest. There are hundreds and thousands of possibilities, most of them are simply not worth considering.

Contd… A route from Islamabad to Peshawar to Lahore is obviously a poor choice, as Peshawar is hundreds of miles out of the way. In this presentation, we show how to solve such problems efficiently.

Shortest path problem We are given a weighted, graph G=(V,E), with weight function w:E->R mapping edges to real-valued weights. The weight of path p= is the sum of the weights of its constituent edges.

Variants Assume that the graph is connected. The shortest path problem has several different forms: Given two nodes A and B, find the shortest path in the weighted graph from A to B. Given a node A, find the shortest path from A to every other node in the graph. (single- source shortest path problem) Find the shortest path between every pair of nodes in the graph. (all-pair shortest path problem)

Single-Source Shortest Path Problem: given a weighted graph G, find the minimum-weight path from a given source vertex s to another vertex v  “Shortest-path” = minimum weight  Weight of path is sum of edges

Dijkstra’s Algorithm Similar to breadth-first search  Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST  Use a priority queue keyed on d[v]

Dijkstra’s Algorithm The idea is to visit the nodes in order of their closeness to A; visit A first, then visit the closest node to A, then the next closest node to A, and so on. The closest node to A, say X, must be adjacent to A and the next closest node, say Y, must be either adjacent to A or X. The third closest node to A must be either adjacent to A or X or Y, and so on. (Otherwise, this node is closer to A than the third closest node.)

The next node to be visited must be adjacent to some visited node. We call the set of unvisited nodes that are adjacent to an already visited node the fringe. The algorithm then selects the node from the fringe closest to A, say B, then visits B and updates the fringe to include the nodes that are adjacent to B. This step is repeated until all the nodes of the graph have been visited and the fringe is empty. Dijkstra’s Algorithm

Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); Relaxation Step Note: this is really a call to Q->DecreaseKey()

Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is ExtractMin() called? How many times is DecreaseKey() called? What will be the total running time?

Dijkstra’s Algorithm Dijkstra(G) for each v  V d[v] =  ; d[s] = 0; S =  ; Q = V; while (Q   ) u = ExtractMin(Q); S = S U {u}; for each v  u->Adj[] if (d[v] > d[u]+w(u,v)) d[v] = d[u]+w(u,v); How many times is ExtractMin() called? How many times is DecreaseKey() called? A: O(E log V) using binary heap for Q

Step by Step operation of Dijkstra algorithm Step1. Given initial graph G=(V, E). All nodes have infinite cost except the source node, s, which has 0 cost. Step 2. First we choose the node, which is closest to the source node, s. We initialize d[s] to 0. Add it to S. Relax all nodes adjacent to source, s. Update predecessor (see red arrow in diagram below) for all nodes updated.

Step 3. Choose the closest node, x. Relax all nodes adjacent to node x. Update predecessors for nodes u, v and y (again notice red arrows in diagram below). Step 4. Now, node y is the closest node, so add it to S. Relax node v and adjust its predecessor (red arrows remember!).

Step 5. Now we have node u that is closest. Choose this node and adjust its neighbor node v. Step 6. Finally, add node v. The predecessor list now defines the shortest path from each node to the source node, s.

Analysis of Dijkstra Algorithm Q as a linear array EXTRACT_MIN takes O(V) time and there are |V| such operations. Therefore, a total time for EXTRACT_MIN in while-loop is O(V 2 ). Since the total number of edges in all the adjacency list is |E|. Therefore for-loop iterates |E| times with each iteration taking O(1) time. Hence, the running time of the algorithm with array implementation is O(V 2 + E) = O(V 2 ). Q as a binary heap ( If G is sparse) In this case, EXTRACT_MIN operations takes O(log V) time and there are |V| such operations. The binary heap can be build in O(V) time. Operation DECREASE (in the RELAX) takes O(log V) time and there are at most such operations. Hence, the running time of the algorithm with binary heap provided given graph is sparse is O((V + E) log V). Note that this time becomes O(E logV) if all vertices in the graph is reachable from the source vertices.