Docsity

Lecture 16 - Dijkstra's Algorithm.pdf, Exercises of Data Structures and Algorithms

Partial preview of the text

Download Lecture 16 - Dijkstra's Algorithm.pdf and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Instructor: Lilian de Greef Quarter: Summer 2017

CSE 373: Data Structures and Algorithms

Lecture 16: Dijkstra’s Algorithm (Graphs)

Today

  • Announcements
  • Graph Traversals Continued
    • Remarks on DFS & BFS
    • Shortest paths for weighted graphs:

Dijkstra’s Algorithm!

I will have the final exam quadruple-checked to avoid these situations! (I am so sorry)

Another midterm correction… ( & )

Bring your midterm to any

office hours to get your point

back.

Graphs: Traversals Continued

And introducing Dijkstra’s Algorithm for shortest paths!

Comparison (useful for Design Decisions!)

  • Which one finds shortest paths?
    • i.e. which is better for “what is the shortest path from x to y ” when there’s more than one possible path?
  • Which one can use less space in finding a path?
  • A third approach:
    • Iterative deepening (IDFS) :
      • Try DFS but disallow recursion more than K levels deep
      • If that fails, increment K and start the entire search over
    • Like BFS, finds shortest paths. Like DFS, less space.

Graph Traversal Uses

In addition to finding paths, we can use graph traversals to answer:
  • What are all the vertices reachable from a starting vertex?
  • Is an undirected graph connected?
  • Is a directed graph strongly connected?
  • But what if we want to actually output the path?
  • How to do it:
  • Instead of just “marking” a node, store the previous node along the path
  • When you reach the goal, follow path fields back to where you started (and then reverse the answer)
  • If just wanted path length , could put the integer distance at each node instead once

A Few Applications of Shortest Weighted Path

  • Driving directions
  • Cheap flight itineraries
  • Network routing
  • Critical paths in project management

Not as easy as BFS

Why BFS won’t work: Shortest path may not have the fewest edges

  • Annoying when this happens with costs of flights 500 100 100 100 100 We will assume there are no negative weights
  • Problem is ill-defined if there are negative-cost cycles
  • Today’s algorithm is wrong if edges can be negative
  • There are other, slower (but not terrible) algorithms 7 10 5
  • 11

Shortest Path Example # vertex known? cost path A B C D E F G H Known Set (in order added): A B D C F H E G 2 (^2 ) 10 2 1 3 11 1 7 1 9 2 4 5

(extra space in case you want/need it)

Dijkstra’s Algorithm (Pseudocode)

Dijkstra’s Algorithm – the following algorithm for finding single-source shortest paths in a weighted graph (directed or undirected) with no negative-weight edges:

  1. For each node v, set v.cost = ¥ and v.known = false
  2. Set source.cost = 0
  3. While there are unknown nodes in the graph a) Select the unknown node v with lowest cost b) Mark v as known c) For each edge (v,u) with weight w, c1 = v.cost + w // cost of best path through v to u c2 = u.cost // cost of best path to u previously known if(c1 < c2){ // if the path through v is better u.cost = c u.path = v // for computing actual paths }

Dijkstra’s Algorithm: Features

  • When a vertex is marked known, the cost of the shortest path to that node is known
    • The path is also known by following back-pointers
  • While a vertex is still not known, another shorter path to it might still be found Note: The “Order Added to Known Set” is not important
    • A detail about how the algorithm works (client doesn’t care)
    • Not used by the algorithm (implementation doesn’t care)
    • It is sorted by path-cost, resolving ties in some way
      • Helps give intuition of why the algorithm works

Dijkstra’s Algorithm: Practice Time!

A B C D F E G 2 1 2 5 1 1 1 2 6 5 3 10 vertex known? cost path A B C D E F G An order of adding vertices to the known set: A) A, D, C, E, F, B, G B) A, D, C, E, B, F, G C) A, D, E, C, B, G, F D) A, D, E, C, B, F, G

(space for scratch work)