Based on slides of Dan Suciu

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Chapter 9: Graphs Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
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.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
1 CSE 326: Data Structures: Graphs Lecture 22: Monday, March 3 rd, 2003.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Dijkstra's algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
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
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
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.
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.
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.
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSE 373: Data Structures & Algorithms Graph Traversals: Dijkstra’s
Minimum Spanning Trees
Cse 373 May 8th – Dijkstras.
Cinda Heeren / Geoffrey Tien
Algorithms and Data Structures Lecture XIII
CS200: Algorithm Analysis
Why do Java programmers wear glasses?
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS
Hannah Tang and Brian Tjaden Summer Quarter 2002
Shortest Path Algorithms
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Section 7: Dijkstra’s & Midterm Postmortem
Shortest Paths and Minimum Spanning Trees
Lecture 13 Algorithm Analysis
Directed Graph Algorithms
Directed Graph Algorithms
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree.
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
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Richard Anderson Spring 2016
Chapter 9: Graphs Shortest Paths
Directed Graphs (Part II)
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Based on slides of Dan Suciu Graph Algorithms - 2 Based on slides of Dan Suciu

Single Source, Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s  V (called the source), find the shortest (lowest cost) path from s to every vertex in V Graph may be directed or undirected Graph may or may not contain cycles Weights may be all positive or not What is the problem if graph contains cycles whose total cost is negative? The problem that Dijkstra’s algorithm addresses is the single source, shortest path problem. Given a graph and a source vertex, find the shortest path from the source to every vertex. We can put a variety of limitations or spins on the problem. We’ll focus on weighted graphs with no negative weights. This is used in all sorts of optimization problems: minimum delay in a network, minimum cost flights for airplane routes, etc.

The Trouble with Negative Weighted Cycles 2 A B 10 -5 1 E You might wonder why we don’t allow negative weights. Here’s one reason, we’ll see another later. The shortest path here is undefined! We can always go once more around the cycle and get a lower cost path. 2 C D

Edsger Wybe Dijkstra (1930-2002) Invented concepts of structured programming, synchronization, weakest precondition, and "semaphores" for controlling computer processes. The Oxford English Dictionary cites his use of the words "vector" and "stack" in a computing context. Believed programming should be taught without computers 1972 Turing Award “Computer Science is no more about computers than astronomy is about telescopes.” To move to weighted graphs, we appeal to the mighty power of Dijkstra. This is one of those names in computer science you should just know! Like Turing or Knuth. So, here’s the super-brief bio of Dijkstra. Look him up if you’re interested in more.

Dijkstra’s Algorithm for Single Source Shortest Path Classic algorithm for solving shortest path in weighted graphs (with only positive edge weights) Similar to breadth-first search, but uses a priority queue instead of a FIFO queue: Always select (expand) the vertex that has a lowest-cost path to the start vertex a kind of “greedy” algorithm Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges Among the wide variety of things he has done, he created Dijkstra’s algorithm more than 30 years ago. Dijkstra’s algorithm is a greedy algorithm (like Huffman encoding), so it just makes the best local choice at each step. The choice it makes is which shortest path to declare known next. It starts by declaring the start node known to have a shortest path of length 0. Then, it updates neighboring node’s path costs according to the start node’s cost. Then, it just keeps picking the next shortest path and fixing that one until it has all the vertices.

void BFS(Node startNode) { Queue S = new Queue; for v in Nodes do v.dist =  ; startNode.dist = 0; S.enqueue(startNode); while (!S.empty()) { x = S.dequeue(); for y in x.children() do if (x.dist+1<y.dist) { y.dist = x.dist+1; S.enqueue(y); } void shortestPath(Node startNode) { Heap S = new Heap; for v in Nodes do v.dist = ; S.insert(v); startNode.dist = 0; S.decreaseKey(startNode); startNode.previous = null; while (!S.empty()) { x = S.deleteMin(); for y in x.children() do if (x.dist+c(x,y) < y.dist) { y.dist = x.dist+c(x,y); S.decreaseKey(y); y.previous = x; }

Dijkstra’s Algorithm: Correctness Proof Let Known be the set of nodes that were extracted from the heap S (through deleteMin) Thus at each moment S and Known are disjoint and their union is V. We show: At any stage, for every node x: (1) x.dist = the cost of the shortest path from source to x going only through nodes in Known (we call such a path a restricted path) (2) if x is in Known, then x.dist = the cost of the shortest path from source to x..

(1) and (2) are proved by induction on stage. Stage = 1 (when source enters Known): (1) and (2) are obvious. Stage k+1: Let u be the node that enters Known at this stage. We prove (2) for node u (since this is the only new node in Known). Suppose (2) is not true for u. i.e. there is a path from source to u shorter than u.dist. That path must go out of Known before reaching u. Let v be the first node out of Known on that path. Then u.dist > cost shorter path ≥ v.dist + shortest-cost(v to u). Since edges have positive cost, it follows that u.dist > v.dist, which is false. Proof of (1): if x is adjacent to u, the property follows from the way x.dist is updated. If x is not adjacent to u, the shortest restricted path from source to x cannot pass through u.

Dijkstra’s Algorithm in Action B D F H G E 2 3 1 4 10 8 9 7 OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action  9  2 2 3 B A F H 1  1 2 1 4 10 9 4 G C 2 8   D 1 E 7 8 OK, let’s do this a bit more carefully. next

Dijkstra’s Algorithm in Action  9  2 next 2 3 B A F H 1  1 2 1 4 10 9 4 G C 2 8 9 15 D 1 E 7 8 OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action 11 9  2 2 3 B A F H 1  1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 next OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action next Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1  1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action 11 next 9 11 2 2 3 B A F H 1  1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 OK, let’s do this a bit more carefully. next

Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 next OK, let’s do this a bit more carefully.

Dijkstra’s Algorithm in Action 11 9 11 2 2 3 B A F H 1 14 1 2 1 4 10 9 4 G C 2 8 9 13 D 1 E 7 8 OK, let’s do this a bit more carefully. Done

Data Structures for Dijkstra’s Algorithm |V| times: Select the unknown node with the lowest cost findMin/deleteMin O(log |V|) |E| times: y’s cost = min(y’s old cost, …) What data structures do we use to support these little snippets from Dijkstra’s? Priority Queue and a (VERY SIMPLE) dictionary. Initialization just takes O(|V|), so all the runtime is in these pq operations. O(E log V + V log V)… but, if we assume the graph is connected, this becomes: O(E log V) (Dijkstra’s will work fine with a disconnected graph, however.) decreaseKey O(log |V|) runtime: O(|E| log |V|)