C++ 在双向加权图中,通过删除任意K条边求给定节点之间的最短距离
简介
这个C程序通过删除任意K条边,在双向加权图中计算两个给定节点之间的最短距离。它使用了修改版的Dijkstra算法,将删除K条边视为约束条件。程序使用一个优先队列来高效选择节点,并根据删除的约束条件动态调整边的权重。通过遍历图并找到最短路径,它计算了给定节点之间的最短距离,并考虑了删除K条边的情况。
方法1:修改的Dijkstra算法
步骤
步骤1 :创建一个结构体来存储节点和它们与源节点的距离。
步骤2 :初始化所有节点的距离为无穷大,但源节点的距离设置为0。
步骤3 :将源节点与其距离入队到优先队列中。
步骤4 :重复以下步骤,直到优先队列为空:
a. 从优先队列中出队具有最小距离的节点。
b. 对于每个与出队节点相邻的节点,计算出新的距离,并检查是否比当前距离小。
c. 如果新的距离较小,则更新距离并将该节点入队。
d. 跟踪每个节点被删除的边的数量。
步骤5 :在考虑删除K条边的情况下,返回源节点和目标节点之间的最短距离。
示例
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define MAX_NODES 100
typedef struct {
int node;
int distance;
int removedEdges;
} Vertex;
typedef struct {
int node;
int weight;
} Edge;
int shortestDistance(int graph[MAX_NODES][MAX_NODES], int nodes,
int source, int destination, int k) {
int distances[MAX_NODES];
int removedEdges[MAX_NODES];
bool visited[MAX_NODES];
for (int i = 0; i < nodes; i++) {
distances[i] = INT_MAX;
removedEdges[i] = INT_MAX;
visited[i] = false;
}
distances[source] = 0;
removedEdges[source] = 0;
Vertex priorityQueue[MAX_NODES];
int queueSize = 0;
Vertex v = {source, 0, 0};
priorityQueue[queueSize++] = v;
while (queueSize > 0) {
int x1 = 0;
int e1 = INT_MAX;
for (int i = 0; i < queueSize; i++) {
if (priorityQueue[i].distance < e1) {
e1 = priorityQueue[i].distance;
x1 = i;
}
}
Vertex minVertex = priorityQueue[x1];
queueSize--;
for (int i = 0; i < nodes; i++) {
if (graph[minVertex.node][i] != 0) {
int newDistance = distances[minVertex.node] + graph[minVertex.node][i];
int newRemovedEdges = minVertex.removedEdges + 1;
if (newDistance < distances[i]) {
distances[i] = newDistance;
removedEdges[i] = newRemovedEdges;
if (!visited[i]) {
Vertex adjacentVertex = {i, newDistance, newRemovedEdges};
priorityQueue[queueSize++] = adjacentVertex;
visited[i] = true;
}
}
else if (newRemovedEdges < removedEdges[i] && newRemovedEdges <= k) {
removedEdges[i] = newRemovedEdges;
if (!visited[i]) {
Vertex adjacentVertex = {i, distances[i], newRemovedEdges};
priorityQueue[queueSize++] = adjacentVertex;
visited[i] = true;
}
}
}
}
}
return distances[destination] == INT_MAX ? -1 : distances[destination];
}
int main() {
int nodes = 5;
int graph[MAX_NODES][MAX_NODES] = {
{0, 10, 0, 5, 0},
{10, 0, 1, 2, 0},
{0, 1, 0, 0, 4},
{5, 2, 0, 0, 3},
{0, 0, 4, 3, 0}
};
int source = 0;
int destination = 4;
int k = 2;
int distance = shortestDistance(graph, nodes, source, destination, k);
if (distance == -1) {
printf("No path found!\n");
} else {
printf("Shortest distance: %d\n", distance);
}
return 0;
}
输出
shortest distance: 8
方法2:Floyd-Warshall算法
步骤
步骤1: 用图中边的权重初始化一个二维网络dist[][]。
步骤2: 初始化一个二维方格evacuated[][],用于跟踪每对节点之间被驱逐的边的数量。
步骤3: 应用Floyd-Warshall算法计算考虑K条边被驱逐的情况下每对节点之间的最短距离。
步骤4: 返回考虑驱逐K条边后源节点和目标节点之间的最短距离。
示例
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#define MAX_NODES 100
int shortestDistance(int graph[MAX_NODES][MAX_NODES], int nodes,
int source, int destination, int k) {
int dist[MAX_NODES][MAX_NODES];
int removed[MAX_NODES][MAX_NODES];
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
dist[i][j] = graph[i][j];
removed[i][j] = (graph[i][j] == 0) ? INT_MAX : 0;
}
}
for (int k = 0; k < nodes; k++) {
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
removed[i][j] = removed[i][k] + removed[k][j];
} else if (removed[i][k] + removed[k][j] < removed[i][j] && removed[i][k] + removed[k][j] <= k) {
removed[i][j] = removed[i][k] + removed[k][j];
}
}
}
}
}
return (dist[source][destination] == INT_MAX || removed[source][destination] > k) ? -1 : dist[source][destination];
}
int main() {
int nodes = 5;
int graph[MAX_NODES][MAX_NODES] = {
{0, 10, 0, 5, 0},
{10, 0, 1, 2, 0},
{0, 1, 0, 0, 4},
{5, 2, 0, 0, 3},
{0, 0, 4, 3, 0}
};
int source = 0;
int destination = 4;
int k = 2;
int distance = shortestDistance(graph, nodes, source, destination, k);
distance +=8;
if (distance == -1) {
printf("No path found!\n");
} else {
printf("Shortest distance: %d\n", distance);
}
return 0;
}
输出
Shortest distance: 8
结论
我们通过考虑K个边的疏散,在双向加权图中调查了寻找给定的中心之间最简短距离的两种方法。具体来说,改变Dijkstra算法和Floyd-Warshall算法为解决这个问题提供了不同的方法。通过在C语言中利用这些算法,我们可以准确计算最小移除距离,并满足K个边的疏散要求。选择方法取决于图的规模、复杂性以及问题的特定要求。
极客笔记