CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.

Slides:



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

§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.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
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.
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.
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.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
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.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Minimum Spanning Trees
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.
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.
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.
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.
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.
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.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
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.
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.
Graphs – Part III CS 367 – Introduction to Data Structures.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Minimum Spanning Tree Neil Tang 3/25/2010
Graphs & Graph Algorithms 2
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
4-4 Graph Theory Trees.
Graphs.
Hannah Tang and Brian Tjaden Summer Quarter 2002
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
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
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
CSE 417: Algorithms and Computational Complexity
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
Richard Anderson Spring 2016
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 21: Graphs V 1

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: create a table of information about the currently known best way to reach each vertex (distance, previous vertex) and improve it until it reaches the best solution in a graph where: – vertices represent cities, – edge weights represent driving distances between pairs of cities connected by a direct road, Dijkstra's algorithm can be used to find the shortest route between one city and any other 2

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. 3

4 Example: Initialization A GF B ECD     Pick vertex in List with minimum distance.  Distance(source) = 0 Distance (all vertices but source) = 

5 Example: Update neighbors' distance A GF B ECD   1  Distance(B) = 2 Distance(D) = 1

6 Example: Remove vertex with minimum distance Pick vertex in List with minimum distance, i.e., D A GF B EC D   1 

7 Example: Update neighbors A GF B EC D Distance(C) = = 3 Distance(E) = = 3 Distance(F) = = 9 Distance(G) = = 5

8 Example: Continued... A GF B ECD Pick vertex in List with minimum distance (B) and update neighbors 95 Note : distance(D) not updated since D is already known and distance(E) not updated since it is larger than previously computed

9 Example: Continued... A GF B E CD No updating Pick vertex List with minimum distance (E) and update neighbors

10 Example: Continued... A GF B E C D Pick vertex List with minimum distance (C) and update neighbors Distance(F) = = 8

11 Example: Continued... A G F B ECD Distance(F) = min (8, 5+1) = 6 Previous distance Pick vertex List with minimum distance (G) and update neighbors

12 Example (end) A G F B ECD Pick vertex not in S with lowest cost (F) and update neighbors 65

13 Correctness Dijkstra’s algorithm is a greedy algorithm – make choices that currently seem the best – locally optimal does not always mean globally optimal 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

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

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. 15

16 Time Complexity: Using List The simplest implementation of the Dijkstra's algorithm stores vertices in an ordinary linked list or array – Good for dense graphs (many edges) |V| vertices and |E| edges Initialization O(|V|) While loop O(|V|) – Find and remove min distance vertices O(|V|) – Potentially |E| updates Update costs O(1) Reconstruct path O(|E|) Total time O(|V 2 | + |E|) = O(|V 2 | )

17 Time Complexity: Priority Queue For sparse graphs, (i.e. graphs with much less than |V 2 | edges) Dijkstra's implemented more efficiently by priority queue Initialization O(|V|) using O(|V|) buildHeap While loop O(|V|) – Find and remove min distance vertices O(log |V|) using O(log |V|) deleteMin – Potentially |E| 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

18 Dijkstra's Exercise A G F B E C D H 10 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.

Minimum spanning tree tree: a connected, directed acyclic graph spanning tree: a subgraph of a graph, which meets the constraints to be a tree (connected, acyclic) and connects every vertex of the original graph minimum spanning tree: a spanning tree with weight less than or equal to any other spanning tree for the given graph 19

Min. span. tree applications Consider a cable TV company laying cable to a new neighborhood... – If it is constrained to bury the cable only along certain paths, then there would be a graph representing which points are connected by those paths. – Some of those paths might be more expensive, because they are longer, or require the cable to be buried deeper. These paths would be represented by edges with larger weights. – A spanning tree for that graph would be a subset of those paths that has no cycles but still connects to every house. There might be several spanning trees possible. A minimum spanning tree would be one with the lowest total cost. 20