Find Maximum Shortest Distance in Each Component of a Graph


Introduction

In C language, finding the most extreme briefest separate in each component of a chart may be a vital assignment. The chart is spoken to utilizing a contiguousness list or lattice. By utilizing Breadth-First Search (BFS) or Depth-First Look (DFS), we will compute the most limited separations from each hub to all other hubs inside a component. To get the most extreme most brief separate in each component, we emphasize through the components and keep up a running most extreme. At last, we yield the comes about for each component. This productive calculation permits us to analyze complex systems, optimizing steering and asset assignment in different applications.

Approach 1: Breadth-First Search (BFS)

Algorithm

  • Step 1 − Creation of BFS() function comprises multiple arguments to perform the BFS.

  • Step 2 − Initialize a 2D array to store nodes for BFS traversal.

  • Step 3 − Repeat through all nodes within the graph.

  • Step 4 − If a node isn't gone to, perform BFS beginning from that node.

  • Step 5 − Amid BFS, compute the briefest separate from the starting node to all other node within the component.

  • Step 6 − Keep track of the greatest remove experienced amid BFS.

  • Step 7 − Finally, invoke the findMaxShortestDistance() function to print the result.

Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_NODES 100

// Function to perform BFS
void BFS(int graph[MAX_NODES][MAX_NODES], int start, int num_nodes, int* visited, int* max_distance) {
   int queue[MAX_NODES];
   int front = 0, rear = 0;
   int distance[MAX_NODES] = {0};
    
   queue[rear++] = start;
   visited[start] = 1;

   while (front < rear) {
      int node = queue[front++];
      for (int i = 0; i < num_nodes; i++) {
         if (graph[node][i] && !visited[i]) {
            queue[rear++] = i;
            visited[i] = 1;
            distance[i] = distance[node] + 1;
            if (distance[i] > *max_distance)
               *max_distance = distance[i];
         }
      }
   }
}

// Function to find maximum shortest distance in each component
void findMaxShortestDistance(int graph[MAX_NODES][MAX_NODES], int num_nodes) {
   int visited[MAX_NODES] = {0};
   int max_distance = 0;

   for (int i = 0; i < num_nodes; i++) {
      if (!visited[i]) {
         BFS(graph, i, num_nodes, visited, &max_distance);
         printf("Maximum shortest distance in component starting from node %d: %d\n", i, max_distance);
         max_distance = 0;
      }
   }
}

// Sample graph representation
int main() {
   int graph[MAX_NODES][MAX_NODES] = {
      {0, 1, 1, 0, 0},
      {1, 0, 0, 1, 0},
      {1, 0, 0, 0, 1},
      {0, 1, 0, 0, 0},
      {0, 0, 1, 0, 0}
   };
   int num_nodes = 5;

   findMaxShortestDistance(graph, num_nodes);

   return 0;
}

Output

Maximum shortest distance in component starting from node 0:2

Approach 2:  Depth-First Search (DFS)

Various steps are listed below

  • Step 1 − Include the required header files.

  • Step 2 − Define the DFS() function containing multiple arguments. On the off chance that a center isn't gone by, perform DFS starting from that center. During DFS, compute the foremost brief expel from the beginning center to all other center points inside the component.

  • Step 3 − Keep track of the most prominent evacuate experienced amid DFS.

  • Step 4 − Abdicate the foremost extraordinary isolated for each component.

Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_NODES 100

// Function to perform DFS
void DFS(int graph[MAX_NODES][MAX_NODES], int start, int num_nodes, int* visited, int* max_distance) {
   visited[start] = 1;

   for (int i = 0; i < num_nodes; i++) {
      if (graph[start][i] && !visited[i]) {
         int distance = *max_distance + 1;
         if (distance > *max_distance)
            *max_distance = distance;
            DFS(graph, i, num_nodes, visited, max_distance);
      }
   }
}

// Function to find maximum shortest distance in each component
void findMaxShortestDistance(int graph[MAX_NODES][MAX_NODES], int num_nodes) {
   int visited[MAX_NODES] = {0};
   int max_distance = 0;

   for (int i = 0; i < num_nodes; i++) {
      if (!visited[i]) {
         DFS(graph, i, num_nodes, visited, &max_distance);
         printf("Maximum shortest distance in component starting from node %d: %d\n", i, max_distance);
         max_distance = 0;
      }
   }
}

// Sample graph representation
int main() {
   int graph[MAX_NODES][MAX_NODES] = {
      {0, 1, 0, 1, 0},
      {1, 0, 1, 0, 0},
      {0, 1, 0, 0, 1},
      {1, 0, 0, 0, 1},
      {0, 0, 1, 1, 0}
   };
   int num_nodes = 5;

   findMaxShortestDistance(graph, num_nodes);

   return 0;
}

Output

Maximum shortest distance in component starting from node 0:4

Approach 3: Floyd-Warshall Algorithm

Various steps are depicted below

  • Step 1 − Initialize a remove framework with starting separations between nodes.

  • Step 2 − Perform the Floyd-Warshall calculation to compute all-pairs most brief separations.

  • Step 3 − Repeat through the components and discover the greatest most limited separate in each component.

  • Step 4 − Yield the most extreme remove for each component.

Example

#include <stdio.h>
#include <stdbool.h>

#define MAX_NODES 100
#define INF 999999

// Function to find maximum shortest distance in each component
void findMaxShortestDistance(int gr[MAX_NODES][MAX_NODES], int num_nodes) {
   int dt[MAX_NODES][MAX_NODES];

   // Initialize distance matrix
   for (int i = 0; i < num_nodes; i++) {
      for (int j = 0; j < num_nodes; j++) {
         dt[i][j] = gr[i][j];
      }
   }
   
   // Floyd-Warshall algorithm
   for (int k = 0; k < num_nodes; k++) {
      for (int i = 0; i < num_nodes; i++) {
         for (int j = 0; j < num_nodes; j++) {
            if (dt[i][k] + dt[k][j] < dt[i][j]) {
               dt[i][j] = dt[i][k] + dt[k][j];
            }
         }
      }
   }

   // Find maximum distance in each component
   int max_distance;
   for (int i = 0; i < num_nodes; i++) {
      max_distance = 0;
      for (int j = 0; j < num_nodes; j++) {
         if (dt[i][j] > max_distance && dt[i][j] != INF) {
            max_distance = dt[i][j];
         }
      }
      printf("Maximum shortest distance in component starting from node %d: %d\n", i, max_distance);
   }
}

// Sample graph representation
int main() {
   int graph[MAX_NODES][MAX_NODES] = {
      {0, 1, 1, INF, INF},
      {1, 0, INF, 1, INF},
      {1, INF, 0, INF, 1},
      {INF, 1, INF, 0, INF},
      {INF, INF, 1, INF, 0}
   };
   int num_nodes = 5;

   findMaxShortestDistance(graph, num_nodes);

   return 0;
}

Output

Maximum shortest distance in component starting from node 0: 2
Maximum shortest distance in component starting from node 1: 3
Maximum shortest distance in component starting from node 2: 3
Maximum shortest distance in component starting from node 3: 4
Maximum shortest distance in component starting from node 4: 4

Conclusion

In conclusion, finding the most extreme most limited separate in each component of a chart may be a noteworthy errand in chart examination. We investigated three approaches in C dialect: Breadth-First Look (BFS), Depth-First Look (DFS), and the Floyd-Warshall calculation. These approaches empower us to effectively compute the most limited separations from a given hub to all other hubs inside a component. By keeping up a running most extreme, we will decide the most extreme briefest separate for each component. These procedures have commonsense applications in different spaces, such as organize examination, steering optimization, and asset allotment, permitting for superior understanding and utilization of complex chart structures.

Updated on: 25-Aug-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements