Catch a single piece of cheese… with mud

Project – Session 3

  • Dijkstra’s algorithm
  • Min-heaps
  • Recap of the session

Dijkstra’s algorithm

Finding the shortest path in a weighted graph

Dijkstra’s algorithm

A direct application of the BFS algorithm

Dijkstra’s algorithm

The actual shortest path

Dijkstra’s algorithm

How does it work?

  • Initialize an array of $+\infty$ distances to the initial vertex $v_1$
  • Initialize a set to remember which vertices were explored

Dijkstra’s algorithm

How does it work?

  • Update the distances to accessible neighbors
  • We update $v_2$ (distance of $5 < +\infty$) and $v_3$ (distance of $3 < +\infty$)

Dijkstra’s algorithm

How does it work?

  • Explore the unvisited neighbor with minimum distance to $v_1$
  • We explore $v_3$

Dijkstra’s algorithm

How does it work?

  • Update the distances to accessible neighbors
  • We update $v_2$ (distance of $3+1 < 5$) and $v_7$ (distance of $3+2 < +\infty$)

Dijkstra’s algorithm

How does it work?

  • Explore the unvisited neighbor with minimum distance to $v_1$
  • We explore $v_2$

Dijkstra’s algorithm

How does it work?

  • Update the distances to accessible neighbors
  • We update $v_4$ (distance of $3+1+2 < +\infty$)

Dijkstra’s algorithm

How does it work?

  • Explore the unvisited neighbor with minimum distance to $v_1$
  • We explore $v_7$, no distances to update

Dijkstra’s algorithm

How does it work?

  • Explore the unvisited neighbor with minimum distance to $v_1$
  • We explore $v_5$, no distances to update, we are done!

Min-heaps

Using a min-heap

  • Just like FIFOs and LIFOs, min-heaps store elements

  • Two methods:

    • push stores an element in the structure
    • pop extracts the smallest element
  • “smallest” needs to be clarified

# Create a min-heap
heap = new_heap()
[]
# Add elements to the heap
push(heap, ("A", 50))
push(heap, ("B", 22))
push(heap, ("C", 10))
[('A', 50), ('B', 22), ('C', 10)]
# Extract smallest element from the heap
pop(heap)
('A', 50)

Min-heaps

Dijkstra’s algorithm and min-heaps

  • Explore the unvisited neighbor with minimum distance to $v_1$
  • Maintain a min-heap with elements (distance to $v_1$, $v_x$)

Recap of the session

Main elements to remember

  • Dijkstra’s algorithm extends BFS to weighted graph

  • Think of water flowing through the graph at a constant speed

  • We didn’t talk about routing tables but you will need one to retrieve the path

  • Min-heaps are data structure that pop the minimum element

  • Many implementations are possible, beware of details!

  • Dijkstra’s algorithm is a traversal that exploits the properties of a min-heap

  • Remember the relation between BFS/queue and DFS/stack

Recap of the session

What’s next?

Practical activity (~2h30)

Catch one cheese with mud

  • Program Dijkstra’s algorithm
  • Think carefully of data structures

After the session

  • Review the articles of the session
  • Check your understanding with the quiz
  • Complete the practical activity
  • Check next session’s “Before the class” section

Evaluation

  • Evaluated quiz to start next session!
  • Deliverable for session 3 (check session page for details)