-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
Miscellaneous Algorithms/Stoer-Wagner Algorithm/Stoer-Wagner-Algo.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Stoer-Wagner Algorithm | ||
|
||
#include <stdio.h> | ||
#include <limits.h> | ||
#include <stdbool.h> | ||
|
||
#define MAX_NODES 100 // Define maximum nodes for the graph | ||
|
||
// Function to find the minimum cut in the graph | ||
int minCut(int graph[MAX_NODES][MAX_NODES], int n) { | ||
int minCutValue = INT_MAX; // Initialize the minimum cut value | ||
int vertices[MAX_NODES]; // Stores the vertices in the graph | ||
|
||
// Initialize vertices list | ||
for (int i = 0; i < n; i++) { | ||
vertices[i] = i; | ||
} | ||
|
||
// Iterate over phases | ||
for (int phase = n; phase > 1; phase--) { | ||
int maxWeights[MAX_NODES]; | ||
bool added[MAX_NODES] = {false}; | ||
|
||
// Start with the first vertex | ||
added[vertices[0]] = true; | ||
int lastAdded = vertices[0]; | ||
|
||
// Add vertices one by one | ||
for (int i = 1; i < phase; i++) { | ||
int maxWeight = -1, maxIndex = -1; | ||
|
||
// Find the vertex with the maximum weight edge | ||
for (int j = 0; j < phase; j++) { | ||
if (!added[vertices[j]]) { | ||
maxWeights[vertices[j]] += graph[lastAdded][vertices[j]]; | ||
|
||
if (maxWeights[vertices[j]] > maxWeight) { | ||
maxWeight = maxWeights[vertices[j]]; | ||
maxIndex = j; | ||
} | ||
} | ||
} | ||
|
||
added[vertices[maxIndex]] = true; | ||
lastAdded = vertices[maxIndex]; | ||
} | ||
|
||
// Calculate the cut value for this phase | ||
int s = vertices[lastAdded]; | ||
int t = vertices[lastAdded - 1]; | ||
minCutValue = (minCutValue < maxWeights[s]) ? minCutValue : maxWeights[s]; | ||
|
||
// Merge vertices s and t | ||
for (int i = 0; i < phase; i++) { | ||
graph[vertices[i]][t] += graph[vertices[i]][s]; | ||
graph[t][vertices[i]] = graph[vertices[i]][t]; | ||
} | ||
|
||
vertices[lastAdded - 1] = t; | ||
} | ||
|
||
return minCutValue; | ||
} | ||
|
||
// Driver code to test the algorithm | ||
int main() { | ||
int n; | ||
printf("Enter number of vertices: "); | ||
scanf("%d", &n); | ||
|
||
int graph[MAX_NODES][MAX_NODES]; | ||
printf("Enter the adjacency matrix (weights of edges):\n"); | ||
for (int i = 0; i < n; i++) { | ||
for (int j = 0; j < n; j++) { | ||
scanf("%d", &graph[i][j]); | ||
} | ||
} | ||
|
||
int min_cut = minCut(graph, n); | ||
printf("The minimum cut of the graph is: %d\n", min_cut); | ||
|
||
return 0; | ||
} |