CSE 373 Data Structures and Algorithms

Slides:



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

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CSE332: Data Abstractions Lecture 17: Shortest Paths Dan Grossman Spring 2010.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
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.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Dijkstra's algorithm.
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.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
CSE 326: Data Structures Lecture #20 Graphs I.5 Alon Halevy Spring Quarter 2001.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
CSE373: Data Structures & Algorithms Lecture 17: Dijkstra’s Algorithm Lauren Milne Summer 2015.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
CSE373: Data Structures & Algorithms Lecture 19: Dijkstra’s algorithm and Spanning Trees Catie Baker Spring 2015.
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.
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.
Graphs – Part III CS 367 – Introduction to Data Structures.
By Laksman Veeravagu and Luis Barrera
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Cinda Heeren / Geoffrey Tien
Introduction to Graphs
Shortest Path Problems
Shortest Path Problems
Minimum Spanning Tree Neil Tang 3/25/2010
Graphs & Graph Algorithms 2
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Hannah Tang and Brian Tjaden Summer Quarter 2002
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Dijkstra’s Shortest Path Algorithm Neil Tang 03/25/2008
Shortest Path Problems
Section 7: Dijkstra’s & Midterm Postmortem
Shortest Path Algorithms
Based on slides of Dan Suciu
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Minimum Spanning Tree Neil Tang 4/3/2008
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Shortest Path Problems
CSE332: Data Abstractions Lecture 17: Shortest Paths
Dijkstra’s Shortest Path Algorithm Neil Tang 3/2/2010
CE 221 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Shortest Path Problems
CSC 380: Design and Analysis of Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Prim’s Minimum Spanning Tree Algorithm Neil Tang 4/1/2008
Richard Anderson Spring 2016
Graphs: Shortest path and mst
Lecture 22: Implementing Dijkstra’s
Directed Graphs (Part II)
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 22: Graphs IV

Dijkstra's Algorithm Dijkstra's algorithm: finds shortest (minimum weight) path between a particular pair of vertices in a weighted directed graph with nonnegative edge weights Solves the "one vertex, shortest path" problem Basic algorithm concept: For each vertex, keep track of the currently known best way to reach it (distance, previous vertex) Iterate until best way is found

Example Application Dijkstra's algorithm can be used to find the shortest route between one city and any other vertices represent cities edge weights represent driving distances between pairs of cities connected by a direct road

Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialization v's distance := infinity. v's previous := none. v1's distance := 0. List := {all vertices}. while List is not empty: v := remove List vertex with minimum distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers.

Example: Initialization  Distance(source) = 0 Distance (all vertices but source) = A 2 B  4 1 3 10 C 2 D 2 E    5 8 4 6 F 1 G   Pick vertex in List with minimum distance.

Example: Update neighbors' distance 2 A 2 B 4 1 3 10 C 2 D 2 E   1 5 8 4 6 Distance(B) = 2 F 1 G Distance(D) = 1  

Example: Remove vertex with min. distance G F B E C D 4 1 2 10 3 6 8 5  Pick vertex in List with minimum distance, i.e., D

Example: Update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Distance(C) = 1 + 2 = 3 Distance(E) = 1 + 2 = 3 Distance(F) = 1 + 8 = 9 Distance(G) = 1 + 4 = 5 F 1 G 9 5

Example: Continued... Pick vertex in List with minimum distance (B) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed F 1 G 9 5

Example: Continued... Pick vertex List with minimum distance (E) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G No updating 9 5

Example: Continued... Pick vertex List with minimum distance (C) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 Distance(F) = 3 + 5 = 8 F 1 G 8 5

Example: Continued... Pick vertex List with minimum distance (G) and update neighbors 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G Previous distance 6 5 Distance(F) = min (8, 5+1) = 6

Example (end) 2 A 2 B 4 1 3 10 C 2 D 2 E 3 3 1 5 8 4 6 F 1 G 6 5 Pick vertex not in S with lowest cost (F) and update neighbors

Correctness Dijkstra’s algorithm is a greedy algorithm Makes choices that currently seem the best In general, locally optimal does not always mean globally optimal (think hill-climbing), but in this case, it is. Correct because maintains following two properties: For every known vertex, recorded distance is shortest distance to that vertex from source vertex For every unknown vertex v, its recorded distance is shortest path distance to v from source vertex, considering only currently known vertices and v

“Cloudy” Proof: The Idea Next shortest path from inside the known cloud Least cost node v THE KNOWN CLOUD competitor v' Source If the path to v is the next shortest path, the path to v' must be at least as long (if it were shorter, it would be picked over v). Therefore, any path through v' to v cannot be shorter!

Time Complexity: List The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or array Good if the graph is dense (lots of edges: |E| ~ O(|V2|)) Initialization (setting to infinity, unknown) O(|V|) While loop O(|V|) Find and remove min distance vertex O(|V|) Potentially O(|E|) distance updates Update costs O(1) Reconstruct path O(|E|) Total time O(|V2| + |E|) = O(|V2|)

Time Complexity: Priority Queue For sparse graphs (i.e. |E| ~ O(|V|)), Dijkstra's implemented more efficiently by priority queue Initialization O(|V|) using O(|V|) buildHeap While loop O(|V|) Find and remove min distance vertex O(log |V|) using deleteMin Potentially O(|E|) distance updates Update costs O(log |V|) using decreaseKey Reconstruct path O(|E|) Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|) |V| = O(|E|) assuming a connected graph

Exercise Use Dijkstra's algorithm to determine the lowest cost path from vertex A to all of the other vertices in the graph. Keep track of previous vertices so that you can reconstruct the path later. A G F B E C D 20 10 50 40 80 30 90 H