0% found this document useful (0 votes)
230 views38 pages

Dijkstra's, Kruskals and Floyd-Warshall Algorithms

The document discusses three algorithms for solving shortest path problems: Dijkstra's algorithm, Kruskal's algorithm, and Floyd-Warshall algorithm. Dijkstra's algorithm finds the shortest path from a source node to all other nodes in a graph with non-negative edge weights. Kruskal's algorithm finds the minimum spanning tree of a connected, undirected graph by adding edges in order of weight. The Floyd-Warshall algorithm can find shortest paths in a directed graph and is applicable to graphs with negative edge weights.

Uploaded by

Rajan Jaiprakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
230 views38 pages

Dijkstra's, Kruskals and Floyd-Warshall Algorithms

The document discusses three algorithms for solving shortest path problems: Dijkstra's algorithm, Kruskal's algorithm, and Floyd-Warshall algorithm. Dijkstra's algorithm finds the shortest path from a source node to all other nodes in a graph with non-negative edge weights. Kruskal's algorithm finds the minimum spanning tree of a connected, undirected graph by adding edges in order of weight. The Floyd-Warshall algorithm can find shortest paths in a directed graph and is applicable to graphs with negative edge weights.

Uploaded by

Rajan Jaiprakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Dijkstras, Kruskals and Floyd-

Warshall Algorithms
Single-Source Shortest Path Problem
Single-Source Shortest Path Problem - The
problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.

Works on both directed and undirected graphs.
However, all edges must have nonnegative weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source vertex vV,
such that all edge weights are nonnegative

Output: Lengths of shortest paths (or the shortest
paths themselves) from a given source vertex vV to
all other vertices


Dijkstra's algorithm - Pseudocode
dist[s] 0 (distance to source vertex is zero)
for all v V{s}
do dist[v] (set all other distances to infinity)
S (S, the set of visited vertices is initially empty)
QV (Q, the queue initially contains all
vertices)
while Q (while the queue is not empty)
do u mindistance(Q,dist) (select the element of Q with the min. distance)
SS{u} (add u to list of visited vertices)
for all v neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist

Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Kruskal Algorithm
Minimum Spanning Tree
Disjoint Sets

4
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
Complete Graph
1
4
2
5
2
5
4
3
4
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
A A B D
B B
B
C D
J C
C
E
F
D
D H
J E G
F F G I
G G I J
H J J I
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
J I
1
A D
4
B C
4
A B
Sort Edges
(in reality they are placed in a priority
queue - not sorted - but sorting them
makes the algorithm easier to visualize)
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
J I
1
A D
4
B C
4
A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
J I
1
A D
4
B C
4
A B
Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4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
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
J I
1
A D
4
B C
4
A B
Cycle
Dont Add Edge
2
5
2
5
4
3
4
4
10
1
6
3
3
2
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
B
B
D
J
C
C
E
F
D
D H
J
E G
F
F
G
I
G
G
I
J
H J
J I
1
A D
4
B C
4
A B
Add Edge
4
1
2
2
1
3
3
2
4
A
B C
D
E
F
G
H
I
J
4
1
2
3
2
1
3
5
3
4
2
5 6
4
4
10
A
B C
D
E
F
G
H
I
J
Minimum Spanning Tree Complete Graph
FloydWarshall algorithm

Thank You

You might also like