-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1426 from AE-Hertz/branch5
[NEW ALGORITHM] : Stoer-Wagner Algorithm in C #1423
- Loading branch information
Showing
2 changed files
with
105 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; | ||
} |
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,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. | ||
|
||
 | ||
|
||
### 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. |