0% found this document useful (0 votes)
31 views13 pages

Floyd Warshall, Find Value of DS

The document describes the Floyd-Warshall algorithm for finding shortest paths in a weighted, directed graph. It defines adjacency matrices for representing graphs and explains that the algorithm works by iteratively updating the shortest path between all pairs of vertices through intermediate vertices. The algorithm runs in O(V3) time, which is more efficient than running Dijkstra's algorithm from each vertex, and it can handle graphs with negative edge weights, unlike Dijkstra's. An example graph is provided and the steps of running Floyd-Warshall on it are shown.

Uploaded by

prachi
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)
31 views13 pages

Floyd Warshall, Find Value of DS

The document describes the Floyd-Warshall algorithm for finding shortest paths in a weighted, directed graph. It defines adjacency matrices for representing graphs and explains that the algorithm works by iteratively updating the shortest path between all pairs of vertices through intermediate vertices. The algorithm runs in O(V3) time, which is more efficient than running Dijkstra's algorithm from each vertex, and it can handle graphs with negative edge weights, unlike Dijkstra's. An example graph is provided and the steps of running Floyd-Warshall on it are shown.

Uploaded by

prachi
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/ 13

SALES PITCH

SUBTITLE GEOS HERE


FLOYD-WARSHALL
ALGORITHM
FLOYD-WARSHALL ALGORITHM
A weighted, directed graph is a collection vertices connected
by weighted edges (where the weight is some real number).
◦ One of the most common examples of a graph in the real world is
a road map.
◦ Each location is a vertex and each road connecting locations is an edge.
◦ We can think of the distance traveled on a road from one location to another as
the weight of that edge.

ABC PQR XYZ 1.5


ABC PQR
ABC 0 1.7 3.5 1.7

3.5 4 2.5
PQR 1.5 0 ∞
XYZ
XYZ 4 2.5 0
STORING A WEIGHTED,
DIRECTED GRAPH
Adjacency Matrix:
◦ Let D be an edge-weighted graph in adjacency-matrix form
• D(i,j) is the weight of edge (i, j), or ¥ if there is no such
edge.
• Update matrix D, with the shortest path through
immediate vertices.

0 1 2 3
1
6 3
0 0 6 5 ∞ 0 4 3
D=
5
1 ∞ 0 4 3 2
2

2 ∞ ∞ 0 2
3 ∞ ∞ ∞ 0
FLOYD-WARSHALL ALGORITHM

Given a weighted graph, we want to know the shortest path from one vertex
in the graph to another.
◦ The Floyd-Warshall algorithm determines the shortest path between all pairs of
vertices in a graph.

◦ What is the difference between Floyd-Warshall and Dijkstra’s??


FLOYD-WARSHALL ALGORITHM
If V is the number of vertices, Dijkstra’s runs in O(V2)
◦ We could just call Dijkstra |V| times, passing a different source
vertex each time.
◦ O(V  V2) = O(V3)
◦ (Which is the same runtime as the Floyd-Warshall Algorithm)

BUT, Dijkstra’s doesn’t work with negative-weight edges.


FLOYD WARSHALL ALGORITHM
Let’s go over the premise of how Floyd-Warshall algorithm
works…
◦ Let the vertices in a graph be numbered from 1 … n.
◦ Consider the subset {1,2,…, k} of these n vertices.

◦ Imagine finding the shortest path from vertex i to vertex j that


uses vertices in the set {1,2,…,k} only.

◦ There are two situations:


1) k is an intermediate vertex on the shortest path.
2) k is not an intermediate vertex on the shortest path.

i j
FLOYD WARSHALL ALGORITHM
Consider the following directed weighted
graph-

Using Floyd Warshall Algorithm, find the shortest path distance between every
pair of vertices.
FLOYD WARSHALL ALGORITHM
Solution-
 
Step-01:
 
•Remove all the self loops and parallel edges (keeping the lowest
weight edge) from the graph.
•In the given graph, there are neither self edges nor parallel edges.

Step-02:
 
• Write the initial distance matrix.
• It represents the distance between every pair of vertices in the form of
given weights.
• For diagonal elements (representing self-loops), distance value = 0.
• For vertices having a direct edge between them, distance value =
weight of that edge.
• For vertices having no direct edge between them, distance value = ∞.
FLOYD WARSHALL ALGORITHM
Initial distance matrix for the given graph is,
Step-03:
Using Floyd Warshall Algorithm, write the following
4 matrices-
D0(2,3) = 1
Check via intermediate vertex 1 D0(2,1) + D0(1,3) = ∞+ ∞= ∞
D0(2,1) + D0(1,3) > D0(2,3) So No Change.

D0(2,4) = ∞
Check via intermediate vertex 1 D0(2,1) + D0(1,4) = ∞+ 1= ∞
D0(2,4) = D0(2,1) + D0(1,4) So No Change.

D0(3,2) = ∞
Check via intermediate vertex 1 D0(3,1) + D0(1,2) = 4+ 8= 12
D0(3,1) + D0(1,2) < D0(3,2) So Change D0(3,2) to 12

D0(3,4) = ∞
Check via intermediate vertex 1 D0(3,1) + D0(1,4) = 4+1= 5
D0(3,1) + D0(1,4) < D0(3,4) So Change D0(3,4) to 5

D0(4,2) = 2
D0(4,3) = 9
Check via intermediate vertex 1 D0(4,1) + D0(1,3) = ∞
+∞= ∞
D0(4,1) + D0(1,3) > D0(4,2) So No Change
1 2 3 4

1
0 8 ∞ 1

∞ 0 1 ∞
= 2

3 4 12 0 5

4 ∞ 2 9 0

You might also like