1 | package weka.clusterers.forMetisMQI; |
---|
2 | |
---|
3 | import java.util.HashSet; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.Set; |
---|
6 | import java.util.Stack; |
---|
7 | |
---|
8 | import weka.clusterers.forMetisMQI.graph.Bisection; |
---|
9 | import weka.clusterers.forMetisMQI.graph.Node; |
---|
10 | import weka.clusterers.forMetisMQI.graph.Subgraph; |
---|
11 | import weka.clusterers.forMetisMQI.graph.UndirectedGraph; |
---|
12 | import weka.clusterers.forMetisMQI.util.CoarserGraphElement; |
---|
13 | import weka.clusterers.forMetisMQI.util.GraphsFrame; |
---|
14 | import weka.clusterers.forMetisMQI.util.Util; |
---|
15 | |
---|
16 | public class GraphAlgorithms { |
---|
17 | |
---|
18 | |
---|
19 | static public Bisection KLRefinement(Bisection b) { |
---|
20 | int remainingNumberOfSwap = 50; |
---|
21 | Bisection partition = b; |
---|
22 | Bisection result = partition; |
---|
23 | int bestEdgeCut = partition.edgeCut(); |
---|
24 | Node u = partition.getCandidate(); |
---|
25 | while (u != null && remainingNumberOfSwap > 0) { |
---|
26 | partition.swap(u); |
---|
27 | if (partition.edgeCut() <= bestEdgeCut) { |
---|
28 | bestEdgeCut = partition.edgeCut(); |
---|
29 | result = partition.clone(); |
---|
30 | remainingNumberOfSwap = 50; |
---|
31 | } else |
---|
32 | remainingNumberOfSwap--; |
---|
33 | u = partition.getCandidate(); |
---|
34 | } |
---|
35 | return result; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * Given an undirected graph, performs the Kernighan-Li algorithm to find a bisection and |
---|
41 | * then returns it. |
---|
42 | * @param g |
---|
43 | * @return |
---|
44 | */ |
---|
45 | static public Bisection KL(UndirectedGraph g) { |
---|
46 | int remainingNumberOfSwap = 50; |
---|
47 | Bisection partition = new Bisection(g); |
---|
48 | Bisection result = partition; |
---|
49 | int bestEdgeCut = partition.edgeCut(); |
---|
50 | Node u = partition.getCandidate(); |
---|
51 | while (u != null && remainingNumberOfSwap > 0) { |
---|
52 | partition.swap(u); |
---|
53 | if (partition.edgeCut() <= bestEdgeCut) { |
---|
54 | bestEdgeCut = partition.edgeCut(); |
---|
55 | result = partition.clone(); |
---|
56 | remainingNumberOfSwap = 50; |
---|
57 | } else |
---|
58 | remainingNumberOfSwap--; |
---|
59 | u = partition.getCandidate(); |
---|
60 | } |
---|
61 | return result; |
---|
62 | } |
---|
63 | |
---|
64 | static public Bisection metis(UndirectedGraph g, int sizeFinerGraph) { |
---|
65 | Coarse.setFinerSize(sizeFinerGraph); |
---|
66 | Stack<CoarserGraphElement> stack = Coarse.coarse(g); |
---|
67 | Bisection partition = null; |
---|
68 | if (stack.size() > 0) { |
---|
69 | partition = KL(stack.peek().getContracted()); |
---|
70 | partition = Uncoarse.uncoarse(stack, partition); |
---|
71 | } |
---|
72 | return partition; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Given an UndirectedGraph, runs metis+mqi for <code>numberOfCluster</code> times and |
---|
77 | * returns a set of clusters. With the third parameter you can control the maximum size of the finer |
---|
78 | * graph during the coarsening phase. |
---|
79 | * @param g |
---|
80 | * @param numberOfCluster |
---|
81 | * @param sizeFinerGraph |
---|
82 | */ |
---|
83 | static public Set<Set<Node>> metisMqi(UndirectedGraph g, int numberOfCluster, int sizeFinerGraph, boolean randomBisection) { |
---|
84 | GraphsFrame gf = new GraphsFrame("Prova"); |
---|
85 | System.out.println("Vertex count: " + g.getVertexCount()); |
---|
86 | System.out.println("Edges count: " + g.getEdgeCount()); |
---|
87 | Iterator<Node> iNodes = g.getVertices().iterator(); |
---|
88 | int degreeCounter = 0; |
---|
89 | while(iNodes.hasNext()) { |
---|
90 | Node node = iNodes.next(); |
---|
91 | if(g.degree(node) == 1) { |
---|
92 | degreeCounter++; |
---|
93 | } |
---|
94 | } |
---|
95 | Set<Set<Node>> clusters = new HashSet<Set<Node>>(); |
---|
96 | UndirectedGraph gclone = g.clone(); |
---|
97 | gf.addPanel(Util.panelGraph(gclone)); |
---|
98 | for (int i = 0; i < numberOfCluster; i++) { |
---|
99 | Bisection bisection = null; |
---|
100 | if(!randomBisection) |
---|
101 | bisection = metis(g,sizeFinerGraph); |
---|
102 | else |
---|
103 | bisection = new Bisection(g); |
---|
104 | System.out.print("Partizione iniziale: "); |
---|
105 | System.out.print("V1: " + bisection.getSubgraph().getVertexCount()); |
---|
106 | System.out.print(" V2: " + bisection.getComplement().getVertexCount()); |
---|
107 | System.out.println(" EC: " + bisection.edgeCut() * 0.5); |
---|
108 | System.out.println("Conductance: " + |
---|
109 | ((double)bisection.edgeCut() / 2) / Math.min(bisection.getSubgraph().totalDegree(),bisection.getComplement().totalDegree())); |
---|
110 | Set<Node> cluster = MQI.mqi(bisection,true); |
---|
111 | |
---|
112 | UndirectedGraph clusterGraph = new Subgraph(gclone,cluster).createInducedSubgraph(); |
---|
113 | |
---|
114 | // System.out.println(cluster); |
---|
115 | Bisection mqiBisection = new Bisection(new Subgraph(g,cluster)); |
---|
116 | System.out.println("Partizione raffinata (MQI)"); |
---|
117 | double newConductance = ((double)mqiBisection.edgeCut() / 2) / Math.min(mqiBisection.getSubgraph().totalDegree(),mqiBisection.getComplement().totalDegree()); |
---|
118 | System.out.println("Conductance: " + newConductance); |
---|
119 | |
---|
120 | System.out.println("CLUSTER "+ i + ": V=" + clusterGraph.getVertexCount() + ", E=" + clusterGraph.getEdgeCount()+"."); |
---|
121 | clusters.add(cluster); |
---|
122 | |
---|
123 | System.out.println(); |
---|
124 | Iterator<Node> clustersNode = cluster.iterator(); |
---|
125 | while(clustersNode.hasNext()){ |
---|
126 | g.removeVertex(clustersNode.next()); |
---|
127 | } |
---|
128 | } |
---|
129 | gf.addPanel(Util.panelClusters(gclone, clusters)); |
---|
130 | gf.setVisible(true); |
---|
131 | return clusters; |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|