Documente Academic
Documente Profesional
Documente Cultură
Shortest paths
BFS
Shortest paths
3
3
A
1
E
4
Shortest paths
3
3
A
1
E
4
Shortest paths
3
3
A
1
E
4
Shortest paths
Shortest paths
Shortest paths
100
A
4
50
A
200
Shortest paths
B
100
50
A
200
C
C
Shortest paths
Shortest paths
Shortest paths
Dijkstras algorithm
Dijkstras algorithm
Dijkstras algorithm
prev keeps track of
the shortest path
Dijkstras algorithm
Dijkstras algorithm
Dijkstras algorithm
3
1
A
1
E
4
3
1
1
A
1
C
4
Heap
A
B
C
D
E
1
1
A
1
C
4
Heap
B
C
D
E
1
1
A
1
C
4
Heap
B
C
D
E
1
1
A
1
C
4
Heap
C
B
D
E
1
1
A
1
C
4
Heap
C
B
D
E
1
1
A
1
C
4
Heap
C
B
D
E
3
3
1
1
A
1
C
4
1
3
Heap
C
B
D
E
3
3
1
1
A
1
C
4
1
3
Heap
B 3
D
E
3
3
1
1
A
1
C
4
Heap
B 3
D
E
3
3
1
1
A
1
C
4
Heap
B 3
D
E
3
3
1
1
A
1
C
4
Heap
B 2
D
E
2
3
1
1
A
1
C
4
Heap
B 2
D
E
2
3
1
1
A
1
C
4
Heap
B 2
E 5
D
2
3
1
1
A
1
E 5
C
4
Heap
B 2
E 5
D
2
3
1
1
A
1
E 5
C
4
Frontier?
Heap
B 2
E 5
D
2
3
1
1
A
1
E 5
C
4
Heap
E 3
D 5
2
5
3
1
1
A
1
E 3
C
4
Heap
D 5
2
5
3
1
1
A
1
E 3
C
4
Heap
5
3
1
1
A
1
E 3
C
4
Heap
1
1
A
1
E 3
Invariant:
Running time?
Running time?
1 call to MakeHeap
Running time?
|V| iterations
Running time?
|V| calls
Running time?
O(|E|) calls
Running time?
|V| ExtractMin
|E| DecreaseKey
Array
O(|V|)
O(|V|2)
O(|E|)
Bin heap
O(|V|)
Total
O(|V|2)
Running time?
|V| ExtractMin
|E| DecreaseKey
Array
O(|V|)
O(|V|2)
O(|E|)
Bin heap
O(|V|)
Total
O(|V|2)
Is this an improvement?
Running time?
|V| ExtractMin
|E| DecreaseKey
Array
O(|V|)
O(|V|2)
O(|E|)
Bin heap
O(|V|)
Total
O(|V|2)
Fib heap
O(|V|)
O(|E|)
D
5
10
-10
D
5
10
-10
D
5
10
start of at
only update the value if we find a shorter distance
An update procedure
p1
p2
p3
pk
p1
p2
p3
pk
s
correct
p1
p2
p3
pk
p1
correct
correct
p2
p3
pk
p1
correct
correct
p2
p3
pk
i times
p1
p2
p3
pk
i times
p1
correct
correct
p2
p3
pk
i times
p1
p2
correct
correct
correct
p3
pk
i times
p1
correct
correct
p2
p3
correct correct
pk
i times
p1
correct
correct
p2
p3
correct correct
pk
|V| - 1 edges/vertices
p1
correct
correct
p2
p3
correct correct
pk
Bellman-Ford algorithm
Bellman-Ford algorithm
Initialize all the
distances
Bellman-Ford algorithm
Bellman-Ford algorithm
Bellman-Ford algorithm
Negative cycles
What is the shortest path
from a to e?
1
D
5
10
-10
C
3
Bellman-Ford algorithm
Bellman-Ford algorithm
S
10
A
1
-4
A:
2
1
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
S
10
A
1
-4
A:
2
1
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
S
10
A
1
-4
A:
2
1
1
-2
C
3
-1
D
-1
B:
Bellman-Ford algorithm
S
10
A
1
-4
B
2
A:
B:
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
S
10
A
1
-4
B
2
A:
B:
1
-2
C
3
-1
D
-1
D:
Bellman-Ford algorithm
S
10
A
1
-4
B
2
A:
B:
D:
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
10
Iteration: 0
1
-4
B
2
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
0
10
10
Iteration: 1
1
-4
B
2
1
-2
C
3
-1
D
-1
Bellman-Ford algorithm
0
10
10
Iteration: 2
1
-4
B
2
1
1
9
-2
C
3
-1
D
-1
12
Bellman-Ford algorithm
0
10
Iteration: 3
1
10
8
-4
2
1
1
9
-2
C
3
-1
D
-1
Bellman-Ford algorithm
0
10
Iteration: 4
1
6
8
-4
B
2
1
1
9
-2
C
3
-1
D
-1
11
Bellman-Ford algorithm
0
10
Iteration: 5
1
5
8
-4
2
1
1
9
-2
C
3
-1
D
-1
14
Bellman-Ford algorithm
0
10
Iteration: 6
1
5
8
-4
B
2
1
1
9
-2
C
3
-1
D
-1
10
Bellman-Ford algorithm
0
10
Iteration: 7
1
5
8
-4
2
1
1
9
-2
C
3
-1
D
-1
Correctness of Bellman-Ford
Loop invariant:
Correctness of Bellman-Ford
Runtime of Bellman-Ford
O(|V| |E|)
Runtime of Bellman-Ford
Simple approach
Floyd-Warshall (|V|3)
Johnsons algorithm O(|V|2 log |V| + |V| |E|)
weight (T ) we
eE '
MST example
1
A
3
C
4
E
2
D
4
F
6
C
2
MSTs
C
2
MSTs
C
2
Applications?
Connectivity
Cuts
A
3
C
4
E
2
D
4
F
6
V-S
e
V-S
e
Algorithm ideas?
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
G
B
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
E
2
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
Kruskals algorithm
Add smallest edge that connects
two sets not already connected
1
A
3
C
4
E
2
MST
B
D
4
F
6
Correctness of Kruskals
MakeSet
Linked lists
|V|
FindSet
|E| calls
O(|V| |E|)
Union
|V| calls
|V|
Total
Linked lists +
heuristics
|V|
|V|
Prims algorithm
Prims algorithm
Prims algorithm
Prims algorithm
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
A
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Prims
1
A
3
C
4
E
2
D
4
F
6
MST
1
Correctness of Prims?
(|V|)
(|V|)
|V| ExtractMin
|E| DecreaseKey
Array
O(|V|)
O(|V|2)
O(|E|)
Bin heap
O(|V|)
Total
O(|V|2)
Fib heap
O(|V|)
O(|E|)