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