Register for our webinar

1 hour

Step 1

Step 2

Congratulations!

You have registered for our webinar

Oops! Something went wrong while submitting the form.

Step 1

Step 2

Confirmed

You are scheduled with Interview Kickstart.

Redirecting...

Oops! Something went wrong while submitting the form.

Head of Career Skills Development & Coaching

*Based on past data of successful IK students

Interview Kickstart has enabled over 3500 engineers to uplevel.

# Introduction to Floyd-Warshall Algorithm
The Floyd-Warshall algorithm is a dynamic programming algorithm used to find all-pairs shortest paths in a weighted graph with positive and negative edge weights. It is used to solve the All-Pairs Shortest Path Problem, which involves finding the shortest paths between all pairs of vertices in a graph. The algorithm was developed by Robert Floyd in 1962, and is sometimes referred to as the Floyd-Warshall-Moore algorithm, after its similar application by Edward Moore in 1957.
The Floyd-Warshall algorithm is a powerful tool for solving problems that involve finding the shortest paths in graphs. It can be used to solve problems such as finding shortest paths between two cities, determining the optimal route for a delivery truck, or finding the most efficient way to connect computers on a network. The algorithm works by using dynamic programming to find the shortest paths between all pairs of vertices. It works by successively computing the shortest paths between every pair of vertices in a graph.
The algorithm starts with a graph and a distance matrix, which contains the distances between each pair of vertices. The algorithm then iterates over each vertex in the graph, updating the distances in the distance matrix. At each iteration, the algorithm finds the shortest path between each pair of vertices using the distances from the previous iteration. The algorithm then updates the distance matrix with the new shortest path information. This process is repeated until the distance matrix contains the shortest paths between all pairs of vertices.
Once the algorithm has finished, the distance matrix contains the shortest paths between all pairs of vertices. The algorithm is useful in many applications such as network routing, where it can be used to find the most efficient route for a packet to take on a network. The algorithm is also used in image processing and computer graphics, where it can be used to find the shortest paths between points in an image.

Attend our webinar on

"How to nail your next tech interview" and learn

Hosted By

Ryan Valles

Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

The 4 areas you must prepare for

How you can accelerate your learnings

The Floyd Warshall Algorithm is an algorithm for finding the shortest paths between all pairs of vertices in a weighted graph. It is a dynamic programming approach that can be used to solve the all-pairs shortest path problem in time O(V^3), where V is the number of vertices in the graph. The algorithm works by considering all possible paths between each pair of vertices and selecting the shortest ones.
In order to understand the algorithm, it is important to understand the concept of a weight matrix. A weight matrix is a two-dimensional array, with each element representing the distance between two vertices in the graph.
Sample Code:
```
# Define a weight matrix to represent the graph
graph = [[0, 3, 8, 9999, -4],
[9999, 0, 9999, 1, 7],
[9999, 4, 0, 9999, 9999],
[2, 9999, -5, 0, 9999],
[9999, 9999, 999999, 6, 0]]
# Initialize the distance matrix, with each element representing the shortest distance between two vertices
dist = [[9999 for x in range(len(graph))] for y in range(len(graph))]
# Start the Floyd Warshall Algorithm
for k in range(len(graph)):
# Pick all vertices as source one by one
for i in range(len(graph)):
# Pick all vertices as destination for the
# above picked source
for j in range(len(graph)):
# If vertex k is on the shortest path from
# i to j, then update the value of dist[i][j]
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
# Print the shortest distances
print(dist)
```