shortest path algorithms
DESCRIPTION
Shortest Path Algorithms. Shortest Path Finding. Unweighted Shortest Path All-pairs Shortest-path Single-source Shortest-path Single-source-destination Shortest-path. Single-source Shortest Path. Bellman-Ford algorithm Dijkstra’s algorithm. Dijkstra’s Algorithm. Dijkstra(DG, source) - PowerPoint PPT PresentationTRANSCRIPT
Shortest Path Finding
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Unweighted Shortest PathAll-pairs Shortest-pathSingle-source Shortest-pathSingle-source-destination Shortest-path
Single-source Shortest Path
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Bellman-Ford algorithmDijkstra’s algorithm
Dijkstra’s Algorithm
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Dijkstra(DG, source) initialize(DG, source) verticesFound queue.addAll(DG.vertices) while (queue not empty) v queue.deleteMin() add v to verticesFound relaxEdges(v)
Example
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
a
b
d
c
e
10
1
2
3
5
94
67
2
0null
null
null
null
null
10
1
2
3
5
94
67
2
Initialize a
b c
d e
queue: a(0), b(), c(), d(), e()verticesFound: {}
Example – 1st Iteration
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
0null
null
null
null
null
10
1
2
3
5
94
67
2
1st Iter a
b c
d e
queue: a(0), b(), c(), d(), e()verticesFound: {}
0null
null
null
null
null
10
1
2
3
5
94
67
2
a
b c
d e
queue: b(), c(), d(), e()verticesFound: {a}
10
10a
5
5a
10 5
Example – 2nd Iteration
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
0null
10a
5a
null
null
1
2
39
4
67
2
2nd Iter a
b c
d e
10
5
0null
10a
5a
null
null
10
1
2
3
5
94
67
2
a
b c
d e
queue: d(5), b(10), c(), e()verticesFound: {a}
10
5
queue: b(10), c(), e()verticesFound: {a, d}
3
8d
8
9
14d
14
27d
7
Example – 3rd Iteration
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
0null
8d
5a
14c
7d
1
2
39
4
67
2
3rd Iter a
b c
d e
10
5
queue: e(7), b(8), c(14)verticesFound: {a, d}
39
2
0null
8d
5a
14d
7d
1
2
39
4
67
2
a
b c
d e
10
5
39
2
queue: b(8), c(14)verticesFound: {a, d, e}
76
13e
13
Example – 4th Iteration
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
0null
8d
5a
13e
7d
1
2
39
4
67
2
4th Iter a
b c
d e
10
5
queue: b(8), c(13)verticesFound: {a, d, e}
39
2
queue: c(13)verticesFound: {a, d, e, b}
76
0null
8d
5a
13e
7d
1
2
39
4
67
2
a
b c
d e
10
5
39
27
6
1
9b
9
2
Example – 5th Iteration
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
0null
8d
5a
9b
7d
1
2
39
4
67
2
5th Iter a
b c
d e
10
5
queue: c(9)verticesFound: {a, d, e, b}
39
2
queue: verticesFound: {a, d, e, b, c}
76
1
20
null
8d
5a
9b
7d
1
2
39
4
67
2
a
b c
d e
10
5
39
27
6
1
2 4
Analysis of Dijkstra’s Algorithm
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Loop executed |V| timescall removeMin in queue |V| times
decreaseKey in queue called |E| timesPriority Queue implementation:
Array – O(|V|2)Binary Heap – O(|E|log|V|) Fibonacci Heap – O(|V|log|V| + |E|)
All-pairs Shortest-path
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Run Dijkstra’s algorithm on all verticesRecursive Matrix MultiplicationFloyd-Warshall Algorithm
Recursive Solution
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Considers length of shortest pathAt most |V| - 1Path must contain sub path which is also
shortestThe shortest path from u to v either
goes directly from u to v (path length 1), orgoes through some other vertex w
Divide and Conquer Algorithm
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
APSP-Recursive(G, u, v) if (u = v) return 0 minPathLength for each vertex w in G a APSP-Recursive(G, u, w) if (a+weight(w,v)< minPathLength)
minPath a + weight(w,v) return minPathLength
Analysis of Divide and Conquer
04/21/23//pages.cpsc.ucalgary.ca/~verwaal/335
Run time O(2n)Duplicating workInstead compute from the bottom-upDynamic Programming AlgorithmIn table store shortest path considering
each vertex as intermediate