## Python program for the above approach:
INT_MAX = 9223372036854775807
INT_MIN = -9223372036854775808
## Below dfs function will be used to
## get the connected components of a
## graph and stores all the connected
## nodes in the vector component
def dfs(src, visited, graph, component, N):
## Mark this vertex as visited
visited[src] = True
## Put this node in component vector
component.append(src);
## For all other vertices in graph
for dest in range(0, N):
## If there is an edge between
## src and dest i.e., the value
## of graph[u][v]!=INT_MAX
if (graph[src][dest] != INT_MAX):
## If we haven't visited dest
## then recursively apply
## dfs on dest
if not visited[dest]:
dfs(dest, visited, graph, component, N);
## Below is the Floyd Warshall Algorithm
## which is based on Dynamic Programming
def floydWarshall(graph, N):
## For every vertex of graph find
## the shortest distance with
## other vertices
for k in range(0, N):
for i in range(0, N):
for j in range(0, N):
## Taking care of integer
## overflow
if (graph[i][k] != INT_MAX and graph[k][j] != INT_MAX):
## Update distance between
## vertex i and j if choosing
## k as an intermediate vertex
## make a shorter distance
if (graph[i][k] + graph[k][j] < graph[i][j]):
graph[i][j] = graph[i][k] + graph[k][j];
## Function to find the maximum shortest
## path distance in a component by checking
## the shortest distances between all
## possible pairs of nodes
def maxInThisComponent(component, graph):
maxDistance = INT_MIN;
n = len(component);
for i in range(0, n):
for j in range(i+1, n):
maxDistance = max(maxDistance, graph[component[i]][component[j]])
## If the maxDistance is still INT_MIN
## then return 0 because this component
## has a single element
if maxDistance == INT_MIN:
return 0
return maxDistance
## Below function uses above two method
## to get the maximum shortest distances
## in each component of the graph the
## function returns a vector, where each
## element denotes maximum shortest path
## distance for a component
def maximumShortesDistances(graph, N):
## Find the connected components
visited = []
for i in range(0, N):
visited.append(False)
components = []
## Now for each unvisited node run
## the dfs to get the connected
## component having this unvisited node
for i in range(0, N):
if not visited[i]:
## For storing the nodes in a
## particular component
temp = []
dfs(i, visited, graph, temp, N)
components.append(temp)
## Now for all-pair find the shortest
## path distances using Floyd Warshall
floydWarshall(graph, N)
## Now for each component find the
## maximum shortest distance and
## store it in result
result = []
numOfComp = len(components)
maxDistance = 0
for i in range(0, numOfComp):
maxDistance = maxInThisComponent(components[i], graph)
result.append(maxDistance)
return result
## Driver code
if __name__=='__main__':
N = 8
inf = INT_MAX;
## Adjacency Matrix for the first
## graph in the examples
graph1 = [
[ 0, inf, 9, inf, inf, inf, 3, inf ],
[ inf, 0, inf, 10, 1, 8, inf, inf ],
[ 9, inf, 0, inf, inf, inf, 11, inf ],
[ inf, 10, inf, 0, 5, 13, inf, inf ],
[ inf, 1, inf, 5, 0, 3, inf, inf ],
[ 8, inf, inf, 13, 3, 0, inf, inf ],
[ 3, inf, 11, inf, inf, inf, 0, inf ],
[ inf, inf, inf, inf, inf, inf, inf, 0 ],
];
## Find the maximum shortest distances
result1 = maximumShortesDistances(graph1, N);
## Printing the maximum shortest path
## distances for each components
for mx1 in result1:
print(mx1, end = " ")
print("")
# This code is contributed by subhamgoyal2014.