• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
October 11, 2024 |330 Views

SDE Sheet - Floyd Warshall

  Share   Like
Description
Discussion

This video is part of the Graph section under GFG SDE Sheet.

The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed graph. The graph is represented as an adjacency matrix of size n*n. Matrix[i][j] denotes the weight of the edge from i to j. If Matrix[i][j]=-1, it means there is no edge from i to j.
Note : Modify the distances for every pair in-place.

Examples 1 :

Input: matrix = [[0, 25],[-1, 0]] 
Output: [[0, 25],[-1, 0]] 
Explanation: The shortest distance between every pair is already given(if it exists).

Examples 2 :

Input: matrix = [[0, 1, 43],[1, 0, 6],[-1, -1, 0]] 
Output: [[0, 1, 7],[1, 0, 6],[-1, -1, 0]] 
Explanation: We can reach 2 from 0 as 0->1->2 and the cost will be 1+6=7 which is less than 43.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-
Problem: https://www.geeksforgeeks.org/problems/implementing-floyd-warshall2042/1
SDE Sheet Link: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/
Article Link: https://www.geeksforgeeks.org/floyd-warshall-algorithm-dp-16/