Skip to main content

CSC 207.01 2019S, Class 39: Shortest paths in graphs

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
  • Sample final
  • The shortest path problem
  • Shortest paths in unweighted graphs
  • Shortest paths in weighted graphs
  • Dijkstra’s algorithm
  • Lab

Preliminaries

News / Etc.

  • Please sit with your partner from Monday
  • While we do not have labs or readings for Friday, you are expected to show up on Friday (to fill out EOCE).
  • I’m working on getting grades to you as soon as I can. I’ll try to have all grades in by Tuesday. (May not include HW8.)
  • One of you did a much better job of printing than everyone else. I’d like to know what they used.
    • Hide unnecessary code in Eclipse; print from Eclipse
  • Thanks to G&G for ideas for representing paths.
  • Review sessions for final: TBD. Tentatively Wednesday.
  • DO NOT FILL IN THE END-OF-COURSE EVALUATIONS UNTIL FRIDAY’S CLASS
    • If you’ve done so already, please speak with me asap.

Upcoming work

  • No more readings.
  • No more lab writeups.
  • No more homework assignments.
  • Final exam: 9am or 2pm, Thursday or Friday of finals week.
    • Let me know which of the four times you plan to take the final.

Extra credit

Extra credit (Academic/Artistic)

  • Tonight Gridshock documentary, 7pm, Wednesday, Strand

Extra credit (Peer)

  • Today Wednesday, May 8, 2pm, Science 2022 - ECDLP: Frey-Ruck Attack
  • Friday, May 10, 2pm, Science 2022, An Exploration of Torsion of Elliptic Curves over Cubic and Quartic Fields.
  • Friday, May 10, 7:30pm, Voice Recital, Students of N. Miguel

Extra credit (Wellness)

Extra credit (Wellness, Regular)

  • Any organized exercise. (See previous eboards for a list.)
  • 60 minutes of some solitary self-care activities that are unrelated to academics or work. Your email reflection must explain how the activity contributed to your wellness.
  • 60 minutes of some shared self-care activity with friends. Your email reflection must explain how the activity contributed to your wellness.

Extra credit (Misc)

Other good things

  • Tonight Digital Music Making class presentation, 7:30 pm. Sebring-Lewis. (Rap, noise, pop, more.)
  • Today Choreography class presentations today at 4:30 pm.
  • Friday Dance MAP, 4:30 pm, Flanagan

Questions

Will we get an answer key for the sample final?

If I have time after I’ve done all the grading.

Sample final

The Sample Final is now posted. You can find it by going to Current > Exam. We’ll quickly talk through the policies.

The shortest path problem

Given a graph and two vertices, source and sink, find the shortest path from source to sink, where the length of a path is computed from the edges in the path.

If there is no path from source to sink, there is no shortest path. Different variants handle that issue in different ways.

Shortest paths in unweighted graphs

In an unweighted graph, the length of the path is the number of edges in the path.

Think/Pair/Share: How can you find the shortest path in a directed, unweighted graph? (Don’t say “Dijkstra’s algorithm”.)

  • BFS

Shortest paths in weighted graphs

In a weighted graph, the length of the path is the sum of the weights of the edges in the path.

In each of the following, if your answer is “Yes”, make an argument about why and if your answer is “No”, come up with a counter-example.

Think/Pair/Share: Can we use the algorithm we used for unweighted graphs? (or a variant thereof?) Why or why not?

  • We cannot use BFS directly.
  • If we take the weighted graph and replace each edge of weight W by a sequence of W edges, we now use BFS.
    • Won’t work naturally for non-integer weights.
    • Won’t work for negative weights.
    • May not work for zero weights.
    • Takes a lot of space.
    • Only works for the additive model for path length
    • If edge weights are large, this is no longer an O(m+n) algorithm

Think/Pair/Share: Can we use the obvious “greedy” algorithm: Take the smallest-weight edge from start, then the smallest-weight edge from there that does not cause a cycle, then the smallest-weight edge from there that does not cause a cycle, …. Why or why not?

Dijkstra’s algorithm

Key insight: Instead of finding the shortest path from source to sink, we find the shortest path from source to every vertex in the graph.

  • Precondition: All weights are positive.

We’ll do an example to think through it.

Key idea: We keep track of the best known path to each vertex and the cost of that path. (Initially, we have no known path to any vertex other than the source.) We repeatedly take the closest remaining vertex and update our knowledge of its neighbors.

We can take the closest remaining vertex because any other path would have to go through one of the neightbors, which suggests that the path would be longer.

This is the unordered graph we used in our discussions.

    B   C   D

A       F       E

    G   H   I
A B 4
A G 6
B C 9
B G 1
C D 1
C F 8
C I 2
D E 3
E I 5
F G 2
F H 1
G H 4
H I 2

Lab

Should we finish our implementations of Prim’s and Dijkstra’s?

It would be nice; you learn a lot when you have to work through the murky details. But it’s not required.

It’s a busy time of the semester.