在图形的每个组件中查找最大最短距离
给定由N个节点和正权重组成的加权图的邻接矩阵graph[][] ,图中每个连通分量的任务是在每对节点之间的所有可能最短距离中找到最大值。
例子:
Input:
Output:
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.
Similarly,
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.
Input:
Output:
7
Explanation: Since, there is only one component with 2 nodes having an edge between them of distance 7. Therefore, the answer will be 7.
方法:这个给定的问题可以通过使用 DFS 在图中找到连接的组件来解决 并将组件存储在列表列表中。 Floyd Warshall 的算法可用于在基于动态规划的每个连接组件中找到所有对的最短路径。在得到图中所有可能对的最短距离后,找出图中每个分量的最大最短距离。请按照以下步骤解决问题:
- 定义一个函数maxInThisComponent(vector
component, vector 并执行以下步骤:> graph) - 将变量maxDistance初始化为INT_MIN并将n初始化为组件的大小。
- 使用变量i遍历范围[0, n)并执行以下任务:
- 使用变量j迭代范围[i+1, n)并将maxDistance的值更新为maxDistance或graph[component[i]][component[j]]的最大值。
- 返回maxDistance的值作为答案。
- 初始化一个大小为N的向量,并将值初始化为false 。
- 初始化向量,比如components[][]和temp[]以存储图形的每个组件。
- 使用深度优先搜索(DFS)查找所有组件并将它们存储在向量components[][]中。
- 现在,调用函数floydWarshall(graph, V)来实现 Floyd Warshall 算法,以找到图的所有组件对之间的最短距离。
- 初始化一个向量result[]来存储结果。
- 将变量numOfComp初始化为向量components[][] 的大小。
- 使用变量i遍历范围[0, numOfComp)并调用函数maxInThisComponent(components[i], graph)并将其返回的值存储在向量result[]中。
- 执行上述步骤后,打印向量result[]的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Below dfs function will be used to
// get the connected components of a
// graph and stores all the connected
// nodes in the vector component
void dfs(int src, vector& visited,
vector >& graph,
vector& component, int N)
{
// Mark this vertex as visited
visited[src] = true;
// Put this node in component vector
component.push_back(src);
// For all other vertices in graph
for (int dest = 0; dest < N; dest++) {
// 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 (!visited[dest])
dfs(dest, visited, graph,
component, N);
}
}
}
// Below is the Floyd Warshall Algorithm
// which is based on Dynamic Programming
void floydWarshall(
vector >& graph, int N)
{
// For every vertex of graph find
// the shortest distance with
// other vertices
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// Taking care of integer
// overflow
if (graph[i][k] != INT_MAX
&& 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
int maxInThisComponent(vector& component,
vector >& graph)
{
int maxDistance = INT_MIN;
int n = component.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
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
return (maxDistance == INT_MIN
? 0
: 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
vector maximumShortesDistances(
vector >& graph, int N)
{
// Find the connected components
vector visited(N, false);
vector > components;
// For storing the nodes in a
// particular component
vector temp;
// Now for each unvisited node run
// the dfs to get the connected
// component having this unvisited node
for (int i = 0; i < N; i++) {
if (!visited[i]) {
// First of all clear the temp
temp.clear();
dfs(i, visited, graph, temp, N);
components.push_back(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
vector result;
int numOfComp = components.size();
int maxDistance;
for (int i = 0; i < numOfComp; i++) {
maxDistance
= maxInThisComponent(components[i], graph);
result.push_back(maxDistance);
}
return result;
}
// Driver Code
int main()
{
int N = 8;
const int inf = INT_MAX;
// Adjacency Matrix for the first
// graph in the examples
vector > 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
vector result1
= maximumShortesDistances(graph1, N);
// Printing the maximum shortest path
// distances for each components
for (int mx1 : result1)
cout << mx1 << ' ';
return 0;
}
Javascript
11 8 0
时间复杂度: O(N 3 ),其中 N 是图中的顶点数。
辅助空间: O(N)