Skip to content

Commit

Permalink
Merge pull request #1426 from AE-Hertz/branch5
Browse files Browse the repository at this point in the history
 [NEW ALGORITHM] : Stoer-Wagner Algorithm in C #1423
  • Loading branch information
pankaj-bind authored Oct 29, 2024
2 parents bd6ceff + d47a633 commit ab98f52
Show file tree
Hide file tree
Showing 2 changed files with 105 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;
}
22 changes: 22 additions & 0 deletions Miscellaneous Algorithms/Stoer-Wagner Algorithm/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

## Key Concepts of the Stoer-Wagner Algorithm
- **Minimum Cut**: This is a way to split the nodes of a graph into two groups while minimizing the sum of the weights of edges that connect nodes in different groups.
- **Global Minimum Cut**: Unlike the typical s-t cut that finds the minimum cut between two specific nodes (s and t), the Stoer-Wagner algorithm finds a minimum cut across the entire graph without any specific starting or ending node.

### How It Works
The algorithm follows a **greedy** approach and uses **maximum adjacency searches**:
1. It repeatedly contracts nodes to form smaller graphs, updating the weights accordingly.
2. At each step, it keeps track of the **most recently added pair of nodes**, which gives the minimum cut of that particular iteration.
3. This process is repeated for all nodes, maintaining the minimum cut found in each step.
4. After considering all possible cuts, the algorithm returns the one with the minimum weight.

![Adaptation-of-the-Stoer-Wagner-algorithm](https://github.com/user-attachments/assets/d9a91d04-45ad-455a-8e2c-949c4a9fd4bc)

### Importance and Applications in C
1. **Efficiency**: Stoer-Wagner is one of the most efficient algorithms for finding the global minimum cut, with a time complexity of \(O(V^3)\) (where \(V\) is the number of vertices). In languages like C, its implementation benefits from the language's low-level operations, making it fast for large graphs.

2. **Network Reliability**: Used in evaluating network robustness, where minimizing cuts helps identify bottlenecks or vulnerable points in network structures.

3. **Clustering and Partitioning**: Applications in image segmentation, data clustering, and distributed computing benefit from efficiently partitioning a system into smaller, interconnected subsets.

4. **Practical Use in C**: Since C is close to the hardware, implementing the Stoer-Wagner algorithm in C can handle memory management and performance optimizations effectively.

0 comments on commit ab98f52

Please sign in to comment.