Skip to content

Commit

Permalink
feat: stoer-wagner algo
Browse files Browse the repository at this point in the history
  • Loading branch information
AE-Hertz authored Oct 29, 2024
1 parent 85836a4 commit d47a633
Showing 1 changed file with 83 additions and 0 deletions.
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;
}

0 comments on commit d47a633

Please sign in to comment.