DS9B
DS9B
CSO102
SPANNING TREE:
Example:
- A town has a set of houses and a set of roads.
- A road connects 2 and only 2 houses.
- A road connecting houses u and v has a repair cost w(u, v).
A={}
PRIM’S ALGORITHM:
negative cycle
BELLMAN-FORD ALGORITHM:
All-Pairs Shortest Paths- Floyd Warshall Algorithm:
● Initialize the solution matrix same as the input graph matrix as a
first step.
● Then update the solution matrix by considering all vertices as an
intermediate vertex.
● The idea is to pick all vertices one by one and updates all
shortest paths which include the picked vertex as an
intermediate vertex in the shortest path.
● When we pick vertex number k as an intermediate vertex, we
already have considered vertices {0, 1, 2, .. k-1} as intermediate
vertices.
● For every pair (i, j) of the source and destination vertices
respectively, there are two possible cases.
● k is not an intermediate vertex in shortest path from i to j. We
keep the value of dist[i][j] as it is.
● k is an intermediate vertex in shortest path from i to j. We update
the value of dist[i][j] as dist[i][k] + dist[k][j], if dist[i][j] > dist[i][k] +
dist[k][j]
Floyd Warshall Algorithm:
036∞∞∞∞ 0345677
3021∞∞∞ 3021344
620142∞ 4201323
∞1102∞4 5110233
∞∞42021 6332021
∞∞2∞201 7423201
∞∞∞4110 7433110
Input Output
Floyd Warshall Algorithm:
For k = 0 to n – 1
For i = 0 to n – 1
For j = 0 to n – 1
Distance[i, j] = min(Distance[i, j], Distance[i, k] + Distance[k, j])