Implementation of Kernighan–Lin algorithm to divide the graph into k partitions.
The edges are stored in a map with edge(u, v) and weight(w) as key value pair. Initially N and K are the total nodes and expected partitions respectively.
- N nodes are divided into 2 partitions of sizes N/K and N - N/K.
- D value is calculated for each node.
- Further steps of KL algorithm are performed, which tells the nodes that need to be swapped in-order to get min edges across those 2 partitions.
- In this step the swaps take place (if any).
- If max value of g found is less than or equal to 0 ie. no swap has taken place, then go to step 2.
- The nodes of partition with size N/K are locked. They will not be considered further. Only their effect will be considered.
- Change N to N - (N/K) and K to K - 1. If K is 1 it means we have obtained all partitions and break. Otherwise, go to step 1.
- Store the u and v of each edge in from.txt and to.txt, number of nodes in vertices.txt. If the graph is weighted then store the weights else store 1 corrosponding to each edge in weight.txt.
- Change the path of all .txt files in buildEdgeSet method in Graph.cpp
Input Graph | Output Graph |
---|---|