Find Maximum Shortest Distance in Each Component of a Graph
Given an adjacency matrix graph of a weighted graph consisting of N nodes and positive weights, the task for each connected component of the graph is to find the maximum among all possible shortest distances between every pair of nodes.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
8 0 11
Explanation: There are three components in the graph namely a, b, c. In component (a) the shortest paths are following:
- The shortest distance between 3 and 4 is 5 units.
- The shortest distance between 3 and 1 is 1+5=6 units.
- The shortest distance between 3 and 5 is 5+3=8 units.
- The shortest distance between 1 and 4 is 1 unit.
- The shortest distance between 1 and 5 is 1+3=4 units.
- The shortest distance between 4 and 5 is 3 units.
Out of these shortest distances:
The maximum shortest distance in component (a) is 8 units between node 3 and node 5.
The maximum shortest distance in component (b) is 0 units.
The maximum shortest distance in component (c) is 11 units between nodes 2 and 6.
Explanation: Since, there is only one component with 2 nodes having an edge between them of distance 7. Therefore, the answer will be 7.
Approach: This given problem can be solved by finding the connected components in the graph using DFS and store the components in a list of lists. Floyd Warshall’s Algorithm can be used to find all-pairs shortest paths in each connected component which is based on Dynamic Programming. After getting the shortest distances of all possible pairs in the graph, find the maximum shortest distances for each and every component in the graph. Follow the steps below to solve the problem:
- Define a function maxInThisComponent(vector<int> component, vector<vector<int>> graph) and perform the following steps:
- Initialize the variable maxDistance as INT_MIN and n as the size of the component.
- Iterate over the range [0, n) using the variable i and perform the following tasks:
- Iterate over the range [i+1, n) using the variable j and update the value of maxDistance as the maximum of maxDistance or graph[component[i]][component[j]].
- Return the value of maxDistance as the answer.
- Initialize a vector visited of size N and initialize the values as false.
- Initialize vectors, say components and temp to store each component of the graph.
- Using Depth First Search(DFS) find all the components and store them in the vector components.
- Now, call the function floydWarshall(graph, V) to implement Floyd Warshall algorithm to find the shortest distance between all pairs of a component of a graph.
- Initialize a vector result to store the result.
- Initialize the variable numOfComp as the size of the vector components.
- Iterate over the range [0, numOfComp) using the variable i and call the function maxInThisComponent(components[i], graph) and store the value returned by it in the vector result.
- After performing the above steps, print the values of the vector result as the answer.
Below is the implementation of the above approach:
11 8 0
Time Complexity: O(N3), where N is the number of vertices in the graph.
Auxiliary Space: O(N)