Finding the minimum-cut value of graph using Kruskal's algorithm

500 Views Asked by At

I'm a beginner and I am trying to find the minimum cut of a graph using Kruskal's algorithm in Java.

I have gotten to where I can read the input and create vertexCount^2 number of MST's with random weights for the edges. All I have left to do is figure out from my MST how many cuts are required to separate S and V-S. This will allow me to choose the minimum out of the vertexCount^2 number of choices.

I think I understand correctly that I am supposed to ignore the last edge of the MST to get S and V-S. But I'm lost on how to figure out how many edges are connecting S and V-S.

So my question is: 1) Is vertexCount^2 random MST's enough to be confident that it will contain the minimum-cut? 2) How can I find how many edges are connecting S and V-S?

PS. This is a snippet form my code:

            // create weighted edge graph from input.txt
            int vertexCount, edgeCount;
            Edge edgeTemp;
            vertexCount = s.nextInt();
            edgeCount = s.nextInt();
            EdgeWeightedGraph G = new EdgeWeightedGraph(vertexCount, edgeCount);
            for (int j = 0; j < edgeCount; j++) {
                edgeTemp = new Edge(s.nextInt(), s.nextInt(), new Random().nextInt(edgeCount));
                G.addEdge(edgeTemp);
            }

            // create kruskal's mst from graph G
            for (int j = 0; j < vertexCount*vertexCount; j++) {
                KruskalMST mst = new KruskalMST(G);
                for (Edge e : mst.edges()) {
                    System.out.println(e);
                }
                System.out.println(NEWLINE);
                if (j != vertexCount*vertexCount - 2)
                    G.randomizeWeight(edgeCount);
            }  

PSS. In case this is relevant, I looked at the code from http://algs4.cs.princeton.edu/43mst/ when writing my code.

1

There are 1 best solutions below

0
On

When getting the MST from the graph, I used Kruskal's algorithm. That means I must have used union and find methods.

Each vertex is its own parent in the beginning. When getting the union of distinct components from the graph, I assign the combining components (including singletons) to have a single parent. So by the time I'm left with S and V-S, all the vertices of each component will have the same parent!

Therefore, I go back to my EdgeWeightedGraph and iterate all the edges in the graph (not the MST!). When I find an edge whose two vertices have different parents, that means that the edge connects component S and V-S. I count++ every time I see an edge like this.

This gives me the total number of cuts needed in the graph!