Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm...

22
Dijkstra’s Algorithm Problem Solving Club February 1, 2017

Transcript of Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm...

Page 1: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm

Problem Solving Club

February 1, 2017

Page 2: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s algorithm

I Dijkstra’s is a greedy algorithm that finds shortest pathsfrom a single source to every other vertex in the graph.

I As usually implemented:I Works for weighted graphs with non-negative weights.I Works for directed and undirected graphs.I Runs in O ((V + E ) logV ).

0 1

2 3

4

1

42

8

3

1

Page 3: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Binary heap (priority queue) data structure

I A binary heap is a data structure with two operations:I Insert: Insert an element into the heap.I Extract: Remove the max (or min) element from the heap.

I Both operations take at worst O (logN).

I C++ std::priority queue, Java PriorityQueue

Page 4: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm

Procedure:

1. Assign all nodes a (tentative) distance of infinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and set its distance to zero.

4. For the current node, consider all neighbours. If [distance tocurrent node + edge weight] is smaller than the currenttentative distance of that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited node with the smallesttentative distance, and go back to step 4 until there are nomore unvisited nodes.

Page 5: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 ∞

∞ ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 6: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

∞ ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 7: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

4 ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 8: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

4 ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 9: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

4 ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 10: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 ∞

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 11: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 ∞

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 12: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 ∞

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 13: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 ∞

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 14: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 15: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 16: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

9

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 17: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

7

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 18: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

7

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 19: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

7

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 20: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Dijkstra’s Algorithm: An Example

0 1

3 6

7

1

42

8

3

1

Procedure:

1. Assign all nodes a (tentative) distance ofinfinity.

2. Mark all nodes as unvisited.

3. Set the current node as start point and setits distance to zero.

4. For the current node, consider all neighbours.If [distance to current node + edge weight]is smaller than the current tentative distanceof that node, update its tentative distance.

5. Mark the current node as visited.

6. Set the current node to the unvisited nodewith the smallest tentative distance, and goback to step 4 until there are no moreunvisited nodes.

Page 21: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Example code

vector <edge > adj [100];

vector <int > dist (100, INF);

void dijkstra(int start) {

dist[start] = 0;

priority_queue <pair <int , int >,

vector <pair <int , int > >,

greater <pair <int , int > > > pq;

pq.push(make_pair(dist[start], start ));

while (!pq.empty ()) {

int u = pq.top(). second ,

d = pq.top(). first;

pq.pop();

if (d > dist[u]) continue;

for (int i = 0; i < adj[u].size (); i++) {

int v = adj[u][i].v,

w = adj[u][i]. weight;

if (w + dist[u] < dist[v]) {

dist[v] = w + dist[u];

pq.push(make_pair(dist[v], v));

}

}

}

}

Page 22: Dijkstra’s Algorithm - University of Calgary in Alberta · 2021. 2. 1. · Dijkstra’s Algorithm Procedure: 1.Assign all nodes a (tentative) distance of in nity. 2.Mark all nodes

Frequently asked questions

I How do I find the actual shortest paths?I Keep track of each vertex’s parent using a separate array.

I Can I use std::set or TreeSet instead of a binary heap?I Yes. Same asymptotic performance, but worse in practice.

I Can Dijkstra’s find the longest path in a graph?I No. Longest path problem for general graph is NP-hard.

I What if my graph has negative weights?I Bellman-Ford / Shortest Path Faster Algorithm: O (VE ).

I Same as Dijkstra’s, but works with negative weights/cycles.

I Floyd-Warshall: O(V 3

).

I Finds the shortest path between every pair of vertices.

I These have much worse running time than O ((V + E ) logV ).