Shortest distance between given Nodes in a Bidirectional Weighted Graph by Removing any K Edges


Introduction

This C program calculates the most limited separation between two given hubs in a bidirectional weighted chart by evacuating any K edges. It utilizes an altered form of Dijkstra's calculation, considering the expulsion of K edges as a limitation. The program utilizes a need line for effective hub determination, and powerfully alters the edge weights based on the expulsion imperative. By navigating the chart and finding the briefest way, it gives the least remove between the given hubs whereas bookkeeping for the expulsion of K edges.

Approach 1: Modified Dijkstra's Algorithm

Algorithm

Step 1: Create a structure to store nodes and their separations from the source node.

Step 2: Initialize the separations of all hubs as limitlessness but for the source hub, which is set to 0.

Step 3: Enqueue the source node into the need line with its separate.

Step 4: Rehash the taking after steps until the need line is purged:

a. Dequeue the node with the least remove from the need line.

b. For each adjoining node of the dequeued node, calculate the unused remove by including the edge weight and check in case it's less than the current remove.

c. In the event that the unused remove is less, overhaul the separate and enqueue the hub into the need line.

d. Keep track of the number of evacuated edges for each hub.

Step 5: Return the most limited remove between the source and goal nodes after considering the expulsion of K edges.

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: %d\n", distance);
   }
   
   return 0;
}

Output

shortest distance: 8

Approach 2: Floyd-Warshall Algorithm

Algorithm

Step 1: Initialize a 2D network dist[][] with the weights of the edges within the graph.

Step 2: Initialize a 2D lattice evacuated[][] to keep track of the number of expelled edges between each combination of nodes.

Step 3: Apply the Floyd-Warshall calculation to calculate the most limited separation between each match of hubs, considering the evacuation of K edges.

Step 4: Return the briefest separation between the source and goal nodes after considering the expulsion of K edges.

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: %d\n", distance);
   }
   
   return 0;
}

Output

Shortest distance: 8

Conclusion

We have investigated two approaches to finding the briefest removal between given hubs in a bidirectional weighted chart by considering the evacuation of K edges. These approaches, to be specific Altered Dijkstra's Calculation, Floyd-Warshall Calculation, give diverse methodologies for understanding the issue. By leveraging these calculations in C dialect, we will precisely calculate the least remove while pleasing the evacuation of K edges. The choice of approach depends on components such as chart measure, complexity, and particular prerequisites of the issue at hand.

Updated on: 09-Aug-2023

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements