1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.

Slides:



Advertisements
Similar presentations
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Advertisements

Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Lecture 17 Path Algebra Matrix multiplication of adjacency matrices of directed graphs give important information about the graphs. Manipulating these.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
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.
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.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Shortest Paths Definitions Single Source Algorithms
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
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.
All-Pairs Shortest Paths
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
SINGLE-SOURCE SHORTEST PATHS. Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
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.
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.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
Lecture 13 Algorithm Analysis
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
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.
Graph Algorithm Hongfei Yan School of EECS, Peking University 5/21/2014.
CSE 326: Data Structures Lecture #23 Dijkstra and Kruskal (sittin’ in a graph) Steve Wolfman Winter Quarter 2000.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
Shortest Path Algorithms
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 17: Shortest Paths
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSC 380: Design and Analysis of Algorithms
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Chapter 9: Graphs Shortest Paths
Presentation transcript:

1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics

2 Shortest-Path Algorithms Izmir University of Economics The input is a weighted graph: associated with each edge (v i, v j ) is a cost c i,j. The cost of a path v 1 v 2...v N is ∑c i,i+1 for i in [1..N-1]  weighted path length The unweighted path length is merely the number of edges on the path, namely, N-1.

3 Shortest-Path Algorithms Izmir University of Economics Single-Source Shortest-Path Problem: Given as input a weighted graph G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

Negative Cost Cycles Izmir University of Economics4 In the graph to the left, the shortest path from v 1 to v 6 has a cost of 6 and the path itself is v 1 v 4 v 7 v 6. The shortest unweighted path has 2 edges. In the graph to the right, we have a negative cost. The path from v 5 to v 4 has cost 1, but a shorter path exists by following the loop v 5 v 4 v 2 v 5 v 4 which has cost -5. This path is still not the shortest, because we could stay in the loop arbitrarily long.

Shortest Path Lenght: Problems We will examine 4 algorithms to solve four versions of the problem 1.Unweighted shortest path  O(|E|+|V|) 2.Weighted shortest path without negative edges  O(|E|log|V|) using queues 3.Weighted shortest path with negative edges  O(|E|. |V|) 4.Weighted shortest path of acyclic graphs  linear time Izmir University of Economics5

Unweighted Shortest Paths Using some vertex, s, which is an input parameter, find the shortest path from s to all other vertices in an unweighted graph. Assume s=v 3. Izmir University of Economics6

Unweighted Shortest Paths Algorithm: find vertices that are at distance 1, 2,... N-1 by processing vertices in layers (breadth-first search) Izmir University of Economics7

Unweighted Shortest Paths Izmir University of Economics8

Unweighted Shortest Paths-Implementation For each vertex v 1.The distance from s in the entry  d v 2.The bookkeeping variable (path) which will allow us to print the actual path  p v 3.A boolean variable to check is the vertex is processed or not  known 9 class Vertex { List adj; bool known; int dist; Vertex path; }

Unweighted Shortest Paths Complexity O(|V| 2 ) Izmir University of Economics10

Unweighted Shortest Paths - Improvement 11

Unweighted Shortest Paths - Improvement At any point in time there are only two types of unknown vertices that have d v ≠∞. Some have d v = currDist and the rest have d v = currDist +1. We can make use of a queue data structure. O(|E|+|V|) 12

Weighted Shortest Path Dijkstra’s Algorithm With weighted shortest path,distance d v is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates. Greedy algorithm: proceeds in stages doing the best at each stage. Dijkstra’s algorithm selects a vertex v with smallest d v among all unknown vertices and declares it known. Remainder of the stage consists of updating the values d w for all edges (v, w). Izmir University of Economics13

Dijkstra’s Algorithm - Example Izmir University of Economics14 ► ► ►

Dijkstra’s Algorithm - Example A proof by contradiction will show that this algorithm always works as long as no edge has a negative cost. Izmir University of Economics15 ► ► ► ►

Dijkstra’s Algorithm Example – Stages I Izmir University of Economics16

Dijkstra’s Algorithm Example – Stages II Izmir University of Economics17

Dijkstra’s Algorithm - Pseudocode If the vertices are sequentially scanned to find minimum d v, each phase will take O(|V|) to find the minimum, thus O(|V| 2 ) over the course of the algorithm. The time for updates is constant and at most one update per edge for a total of O(|E|). Therefore the total time spent is O(|V| 2 +|E|). If the graph is dense, OPTIMAL. 18

Dijkstra’s Algorithm-What if the graph is sparse? If the graph is sparse |E|=θ(|V|), algorithm is too slow. The distances of vertices need to be kept in a priority queue. Selection of vertex with minimum distance via deleteMin, and updates via decreaseKey operation. Hence; O(|E|log|V|+|V|log|V|) find operations are not supported, so you need to be able to maintain locations of d i in the heap and update them as they change. Alternative: insert w and d w with every update. Izmir University of Economics19

Graphs with negative edge costs Dijkstra’s algorithm does not work with negative edge costs. Once a vertex u is known, it is possible that from some other unknown vertex v, there is a path back to u that is very negative. Algorithm: A combination of weighted and unweighted algorithms. Forget about the concept of known vertices. Izmir University of Economics20

Graphs with negative edge costs - I O(|E|*|V|) Each vertex can dequeue at most O(|V|) times. (Why? Algorithm computes shortest paths with at most 0, 1,..., |V|-1 edges in this order). Hence, the result! If negative cost cycles, then each vertex should be checked to have been dequeued at most |V| times. Izmir University of Economics21

Acyclic Graphs If the graph is known to be acyclic, the order in which vertices are declared known, can be set to be the topological order. Running time = O(|V|+|E|) This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological ordering rule it has no incoming edges emanating from unknown nodes. Izmir University of Economics22

Acyclic Graphs - Example Izmir University of Economics23