雙向加權圖中給定節點之間的最短距離(移除任意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:初始化一個二維陣列 removed[][],以跟蹤每個節點組合之間移除的邊數。
步驟 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 條邊的條件。方法的選擇取決於諸如圖的大小、複雜度以及手頭問題的特定要求等因素。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP