卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章19437本站已运行343

在双向加权图中,通过删除任意K条边,找到给定节点之间的最短距离

在双向加权图中,通过删除任意K条边,找到给定节点之间的最短距离

简介

这个 C 程序通过移除任意 K 条边来计算双向加权图中两个给定节点之间的最短距离。它使用了修改过的 Dijkstra 算法,将移除 K 条边视为限制条件。该程序使用了一个优先队列来高效地选择节点,并根据移除的要求动态调整边的权重。通过遍历图并找到最短路径,它给出了给定节点之间的最小距离,并考虑了移除 K 条边的影响。

方法一:修改后的Dijkstra算法

算法

步骤 1:创建一个结构来存储节点及其与源节点的分离距离

步骤2:将所有中心的分离度初始化为无限大,但源中心的分离度设为0。

第3步:将源节点与其单独的节点一起放入需求行中。

步骤4:重新执行以下步骤,直到需要的行被清除:

a. 从需要行中删除具有最小移除的节点

b.对于出队节点的每个相邻节点,通过包括边权重来计算未使用的删除,并检查它是否小于当前删除。

c. 如果未使用的移除较少,则升级分离并将中心入队到需求队列中。

d.跟踪每个集线器的疏散边缘的数量。

步骤5:在考虑移除K条边之后,返回源节点和目标节点之间最限制的路径。

Example

的中文翻译为:

示例

'
#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: %dn", distance);
   }
   
   return 0;
}

输出

'
shortest distance: 8

方法二:弗洛伊德-沃尔什算法

算法

步骤 1:用图中边的权重初始化一个二维网络 dist[][]。

步骤 2:初始化一个二维格子 evacuated[][],用于跟踪每对节点之间被驱逐的边的数量。

步骤 3:应用弗洛伊德-沃尔什计算方法,计算每个中继站匹配之间的最短路径,考虑撤离 K 条边。

步骤4:在考虑排除K条边之后,返回源节点和目标节点之间最短的距离。

Example

的中文翻译为:

示例

'
#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: %dn", distance);
   }
   
   return 0;
}

输出

'
Shortest distance: 8

结论

我们研究了两种方法,通过考虑 K 条边的疏散来找到双向加权图中给定中心之间最短的移除。这些方法,具体来说是改变迪杰斯特拉计算、弗洛伊德-沃歇尔计算,为理解该问题提供了多种方法。通过利用C语言中的这些计算,我们将在满足K条边疏散的同时精确计算最小移除量。方法的选择取决于图表度量、复杂性以及当前问题的特定先决条件等组成部分。

卓越飞翔博客
上一篇: 通过PHP Hyperf打造高性能微服务应用:实用技巧与最佳实践
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏