Chapter 7: Greedy Algorithms 7.4 Finding the Shortest Path Dijkstra’s Algorithm pp. 295-301.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Chapter 9: Graphs 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.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
Management Science 461 Lecture 2b – Shortest Paths September 16, 2008.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Chapter 3 The Greedy Method 3.
1 Spanning Trees Lecture 20 CS2110 – Spring
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Vladimir Kulyukin Computer Science Department Utah State University
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Nondecreasing Paths in Weighted Graphs Or: How to optimally read a train schedule Virginia Vassilevska.
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.
Nondecreasing Paths in Weighted Graphs Or: How to optimally read a train schedule Virginia Vassilevska Carnegie Mellon UniversitySODA 2008.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Dijkstra’s Algorithm and Heuristic Graph Search David Johnson.
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.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
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
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.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity.
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.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
E E Module 5 © Wayne D. Grover 2002, (for non-negative edge weights only) Key concepts: “labelling”, “scanning” Label = {distance, predecessor}.
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
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.
COMP108 Algorithmic Foundations Greedy methods
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Lecture 13 Shortest Path.
Chapter 7: Greedy Algorithms
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
Shortest Path Problems
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 18: Dijkstra’s Algorithm
CSE 373: Data Structures and Algorithms
Outline This topic covers Prim’s algorithm:
Shortest Path Problems
Yan Shi CS/SE 2630 Lecture Notes
Weighted Graphs & Shortest Paths
Shortest Path Problems
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Graph Algorithms: Shortest Path
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Lecture 12 Shortest Path.
Lecture 14 Minimum Spanning Tree (cont’d)
Chapter 9: Graphs Shortest Paths
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Chapter 7: Greedy Algorithms 7.4 Finding the Shortest Path Dijkstra’s Algorithm pp

Finding Shortest Paths Many real world situations can be represented using Graphs. Many real world applications and problems require finding the shortest path in a Graph. Consider the following “real world” example...

“Real World” Example 1 Ninja’s often find themselves in precarious situations where they must escape. Like when they are being chased by a Pirate disguised as Johnny Depp. A good Ninja always knows the shortest path to the escape route

Problem 1 Location of Ninja being chased by Pirate The only way out crazy Pirate maze. PILF OAN UJD C TGBE

PILF OAN UJD C TGBE A  B  C  D  E  F  G  I  J  L  N  O  P  T  U  A  B  C  D  E  F  G  I  J  L  N  O  P  T  U   Keeps track of the shortest distance to each vertex and the path to get there. Initially, the distance is set to infinite and all the paths are null.

PILF OAN UJD C TGBE A  B  C  D  E  F 0 G  I  J  L  N  O  P  T  U  A  B  C  D  E  F 0 G  I  J  L  N  O  P  T  U   Initialization: Set the start vertex to zero. Represents that you can get to F with zero cost.

PILF OAN UJD C TGBE A    B    C    D    E    F  0  G    I    J    L  1 F N  14 F O    P    T    U    Step 1: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances. Where can you go from F.

PILF OAN UJD C TGBE A  14 L B    C    D  13 L E    F  0  G    I  3 L J    L  1 F N  14 F O  7 L P    T    U    Step 2: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances. Where can you go from F and L.

PILF OAN UJD C TGBE A  14 L B    C    D  13 L E    F  0  G    I  3 L J    L  1 F N  14 F O  7 L P  4 I T    U    Step 3: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances. Where can you go from F, L, and I

PILF OAN UJD C TGBE A  14 L B    C    D  13 L E    F  0  G    I  3 L J    L  1 F N  14 F O  6 P P  4 I T    U  8 P Step 4: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances. Where can you go from F, L, I and P

PILF OAN UJD C TGBE A  14 L B    C    D  13 L E    F  0  G    I  3 L J    L  1 F N  14 F O  6 P P  4 I T    U  7 O Step 5: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances. Where can you go from F, L, I, P, and O.

PILF OAN UJD C TGBE A  14 L B    C    D  13 L E    F  0  G    I  3 L J  8 U L  1 F N  14 F O  6 P P  4 I T  9 U U  7 O Step 6: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  14 L B    C    D  11 J E    F  0  G    I  3 L J  8 U L  1 F N  14 F O  6 P P  4 I T  9 U U  7 O Step 7: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  14 L B    C    D  10 T E    F  0  G  12 T I  3 L J  8 U L  1 F N  14 F O  6 P P  4 I T  9 U U  7 O Step 8: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  17 D D  10 T E    F  0  G  12 T I  3 L J  8 U L  1 F N  14 F O  6 P P  4 I T  9 U U  7 O Step 9: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  17 D D  10 T E    F  0  G  12 T I  3 L J  8 U L  1 F N  14 F O  6 P P  4 I T  9 U U  7 O Step 10: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  17 D D  10 T E    F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Step 11: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  15 N D  10 T E    F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Step 12: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  15 N D  10 T E  16 C F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Step 13: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  15 N D  10 T E  16 C F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Step 14: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  15 N D  10 T E  16 C F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Step 15: Find the unmarked vertex with minimum distance (v). Mark it as visited Iterate over v’s adjacency list, and update the distances.

PILF OAN UJD C TGBE A  12 D B  15 D C  15 N D  10 T E  16 C F  0  G  12 T I  3 L J  8 U L  1 F N  13 A O  6 P P  4 I T  9 U U  7 O Final Step: This data structure can be used to compute the shortest path from F to all other vertices.

ABCDEFGIJLNOPTUABCDEFGIJLNOPTU L 14   13   3  1F 14 7    I 14   13   3L    P 14   13    I  8 PILF OAN UJD C TGBE F      0     1 14     O 14   13    14 6P  7 U 14   13   O J 14   11   8U 14 9 T 14   10  U D T  G  12T 14 A 12D  13 N  13A C 15 15N 16 B 15D 16 B 16C

“Real World” Lesson Learned Just do the Flip Out Dance to escape from the crazy Ninja maze with minimum damage.

Serious Lessons Learned Dijkstra’s algorithm is greedy. It always chooses the vertex with minimum distance from the source vertex. Optimization after each step Dijkstra’s algorithm updates the distances Shorter paths are discovered as minimum distances get updated.

Worst Case Finding the shortest path from a Start Vertex to a End Vertex could require marking every vertex. The Crazy Pirate Maze example showed us this fact. However, could we have stopped the algorithm after we marked the End Vertex as visited?

Example 2 PILF OAN UJD C TGBE F  0 

Example 2 PILF OAN UJD C TGBE F  0  L  1 F N  5 F

Example 2 PILF OAN UJD C TGBE F  0  L  1 F N  5 F I  10 L O  10 L D  13 L A  14 L

Example 2 PILF OAN UJD C TGBE F  0  L  1 F N  5 F A  6 N I  10 L O  10 L C  10 N D  13 L

Example 2 PILF OAN UJD C TGBE F  0  L  1 F N  5 F A  6 N D  8 A I  10 L O  10 L C  10 N

Example 2 PILF OAN UJD C TGBE F  0  L  1 F N  5 F A  6 N D  8 L C  9 D I  10 L O  10 L J  11 D G  11 D T  12 D B  13 D

Question: Is 9 the minimum distance from F to C? PILF OAN UJD C TGBE F  0  L  1 F N  5 F A  6 N D  8 L C  9 D I  10 L I  10 L J  11 D G  11 D E  11 C T  12 D B  13 D

Question: How do we know there isn’t a shorter path? PILF OAN UJD C TGBE F  0  L  1 F N  5 F A  6 N D  8 L C  9 D I  10 L I  10 L J  11 D G  11 D E  11 C T  12 D B  13 D

Technicality The textbook’s version of Dijkstra’s algorithm stops when all vertices are marked. which computes the shortest distance from a Start Vertex to all the other vertices. But, if you are only interested in the shortest path from A to B, you can stop the algorithm as soon as B is marked.

Worst Case Nonetheless, there are A to B problems that require all vertices to be marked. Thus, Dijkstra’s Algorithm takes O(N) steps. Now, in the worst case, how much work does one step take?

Worst Case Step Dijkstra’s table will have N entries, one for each vertex. You have to pick the unmarked entry with minimum distance. Given an array of N numbers, how long does it take to find the minimum? What would be the overall worst case running time of Dijkstra’s algorithm consider it has to find a minimum N times? ABCDEFGIJLNOPTUABCDEFGIJLNOPTU 14   13  3  2  I  8 Find minimum

Worst Case Step N Steps Find minimum for each step O(N 2 ) so far... ABCDEFGIJLNOPTUABCDEFGIJLNOPTU 14   13  3  2  I  8 Find minimum

Worst Case Step Worse yet, once we find the closest vertex, we must update all the distances. It is possible that the closest vertex could be adjacent to all vertices. Thus, there could be up to N updates per step. Does this impact the Big-O?

F D A BC 900 E 440 Worst Case Example A  0 B  440 C  900 D  900 E  900 F  900

F D A BC 900 E 440 Worst Case Example A  0 B  440 C  630 D  880 E  880 F  880

F D A BC 900 E 440 Worst Case Example A  0 B  440 C  630 D  670 E  730 F 

F D A BC 900 E 440 Worst Case Example A  0 B  440 C  630 D  670 E  680 F 

F D A BC 900 E 440 Worst Case Example A  0 B  440 C  630 D  670 E  680 F 

D Worst Case Example A  0 B  440 C  630 D  670 E  680 F  685 F A BC 900 E Step 1: N-1 updates Step 2: N-2 updates Step 3: N-3 updates... Step i: N-i updates... Step N-1: 1 update Step N: 0 updates Worst case on number of updates: The Problem: An update requires finding a vertex in the table. Find is O(N) Each of the N 2 updates required N operations.

D Worst Case Example A  0 B  440 C  630 D  670 E  680 F  685 F A BC 900 E Step 1: N-1 updates Step 2: N-2 updates Step 3: N-3 updates... Step i: N-i updates... Step N-1: 1 update Step N: 0 updates Worst case on number of updates: Note: The worst case can only arise in a complete graph with N(N-1)/2 edges. The number of updates is really O(M), where M is the number of edges, which is bounded by O(N 2 )

Worst Case Algorithm while there are no un-marked vertices { find the vertex v with minimum weight mark it as visited for all of v’s edges { update the minimum distance and path to each vertex adjacent to v } N vertices * Find min: O(N) N-1 edges * 1 update: requires a find: O(N) N * (N + (N-1)*N) = O(N 3 )

Data Structures to the Rescue Is there a data structure we can use instead of an array. Goals: Find a vertex in constant time Update a distance in constant time Find minimum as efficiently as possible

Sorting & Binary Search: Sort the array by Distance while there are no un-marked vertices { find the vertex v with minimum weight mark it as visited for all of v’s edges { update the minimum distance and path to each vertex adjacent to v } re-sort the vertices by distance } N * (1 + (N-1)*N + N*log N) = O(N 3 ) N vertices * Find min: O(1) N-1 edges * 1 update: requires a find: O(N) Sort: O(N log N)

Sorting & Binary Search: Sort the array by Vertex Label while there are no un-marked vertices { find the vertex v with minimum weight mark it as visited for all of v’s edges { update the minimum distance and path to each vertex adjacent to v } N * (N + (N-1)*log N) = O(N 2 log N) N vertices * Find min: O(N) N-1 edges * 1 update: find: O(log N)

Hash Table: Constant time vertex lookup while there are no un-marked vertices { find the vertex v with minimum weight mark it as visited for all of v’s edges { update the minimum distance and path to each vertex adjacent to v } N * (N + (N-1)*1) = O(N 2 ) N vertices * Find min: O(N) N-1 edges * 1 update: find: O(1)

Binary Heap with Hash Index: Constant time vertex lookup Find min: O(log N) Updates: O(1) while there are no un-marked vertices { find the vertex v with minimum weight mark it as visited for all of v’s edges { update the minimum distance and path to each vertex adjacent to v } N * log N + M) = O(N log N + M) N vertices * Find min: O(log N) N-1 edges * 1 update: find: O(1)

Is there a difference?

PILF OAN UJD C TGBE