- Timestamp:
- Sep 29, 2010, 9:13:19 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/weka/clusterers/forMetisMQI/GraphAlgorithms.java
r17 r18 8 8 import weka.clusterers.forMetisMQI.graph.Bisection; 9 9 import weka.clusterers.forMetisMQI.graph.Node; 10 import weka.clusterers.forMetisMQI.graph.Subgraph; 10 11 import weka.clusterers.forMetisMQI.graph.UndirectedGraph; 11 12 import weka.clusterers.forMetisMQI.util.CoarserGraphElement; … … 14 15 public class GraphAlgorithms { 15 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 16 37 17 38 /** … … 22 43 */ 23 44 static public Bisection KL(UndirectedGraph g) { 45 int remainingNumberOfSwap = 50; 24 46 Bisection partition = new Bisection(g); 25 47 Bisection result = partition; 26 int bestEdgeCut = Integer.MAX_VALUE;48 int bestEdgeCut = partition.edgeCut(); 27 49 Node u = partition.getCandidate(); 28 while (u != null ) {50 while (u != null && remainingNumberOfSwap > 0) { 29 51 partition.swap(u); 30 52 if (partition.edgeCut() <= bestEdgeCut) { 31 53 bestEdgeCut = partition.edgeCut(); 32 result = partition.copy(); 33 } 54 result = partition.clone(); 55 remainingNumberOfSwap = 50; 56 } else 57 remainingNumberOfSwap--; 34 58 u = partition.getCandidate(); 35 59 } … … 57 81 */ 58 82 static public Set<Set<Node>> metisMqi(UndirectedGraph g, int numberOfCluster, int sizeFinerGraph) { 83 System.out.println("VERTEX: " + g.getVertexCount()); 84 System.out.println("EDGE: " + 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 } 59 93 Set<Set<Node>> clusters = new HashSet<Set<Node>>(); 60 94 UndirectedGraph gclone = g.clone(); … … 62 96 for (int i = 0; i < numberOfCluster; i++) { 63 97 Bisection partition = metis(g,sizeFinerGraph); 64 Set<Node> cluster = MQI.mqi(partition); 98 System.out.println("Partizione iniziale (Metis)"); 99 // System.out.println("Edge-cut: " + partition.edgeCut() / 2); 100 System.out.println("Conductance: " + 101 ((double)partition.edgeCut() / 2) / Math.min(partition.getSubgraph().totalDegree(),partition.getComplement().totalDegree())); 102 Set<Node> cluster = MQI.mqi(partition,true); 103 104 UndirectedGraph clusterGraph = new Subgraph(gclone,cluster).createInducedSubgraph(); 105 106 // System.out.println(cluster); 107 Bisection mqiBisection = new Bisection(new Subgraph(g,cluster)); 108 System.out.println("Partizione raffinata (MQI)"); 109 // System.out.println("Edge-cut: " + mqiBisection.edgeCut() / 2); 110 double newConductance = ((double)mqiBisection.edgeCut() / 2) / Math.min(mqiBisection.getSubgraph().totalDegree(),mqiBisection.getComplement().totalDegree()); 111 System.out.println("Conductance: " + newConductance); 112 113 114 if(newConductance < 1) { 115 System.out.println("CLUSTER "+ i + ": V=" + clusterGraph.getVertexCount() + ", E=" + clusterGraph.getEdgeCount()+"."); 116 clusters.add(cluster); 117 } 118 65 119 Iterator<Node> clustersNode = cluster.iterator(); 66 120 while(clustersNode.hasNext()){ 67 121 g.removeVertex(clustersNode.next()); 68 122 } 69 70 71 if(cluster.size()>10) {72 clusters.add(cluster);73 }74 System.out.println("CLUSTER "+ i + ": " + cluster);75 123 } 76 124 Util.viewClusters(gclone, clusters);
Note: See TracChangeset
for help on using the changeset viewer.