1 | package weka.clusterers.forMetisMQI.graph; |
---|
2 | |
---|
3 | import java.io.PrintStream; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.Collection; |
---|
6 | import java.util.HashMap; |
---|
7 | import java.util.Iterator; |
---|
8 | import java.util.List; |
---|
9 | import java.util.TreeMap; |
---|
10 | |
---|
11 | import weka.clusterers.forMetisMQI.util.Random; |
---|
12 | import weka.core.Attribute; |
---|
13 | import weka.core.Instance; |
---|
14 | import weka.core.Instances; |
---|
15 | import edu.uci.ics.jung.graph.UndirectedSparseGraph; |
---|
16 | import edu.uci.ics.jung.graph.util.Pair; |
---|
17 | |
---|
18 | public class UndirectedGraph extends UndirectedSparseGraph<Node, Edge> { |
---|
19 | |
---|
20 | private static final long serialVersionUID = 1L; |
---|
21 | |
---|
22 | public List<Node> vtxsPermutation() { |
---|
23 | Random r = Random.instance(); |
---|
24 | List<Node> perm = new ArrayList<Node>(); |
---|
25 | Iterator<Node> vtxsIterator = getVertices().iterator(); |
---|
26 | while(vtxsIterator.hasNext()) { |
---|
27 | Node node = vtxsIterator.next(); |
---|
28 | perm.add(node); |
---|
29 | } |
---|
30 | for (int i = 0; i < perm.size(); i++) { |
---|
31 | int k = r.nextInt(perm.size()); |
---|
32 | Node swap = perm.get(k); |
---|
33 | perm.set(k, perm.get(i)); |
---|
34 | perm.set(i, swap); |
---|
35 | } |
---|
36 | return perm; |
---|
37 | } |
---|
38 | |
---|
39 | public boolean containsEdge(Node v1, Node v2) { |
---|
40 | return (findEdge(v1, v2) != null); |
---|
41 | } |
---|
42 | |
---|
43 | public Node findVertex(Node v1) { |
---|
44 | Iterator<Node> graphIterator = getVertices().iterator(); |
---|
45 | while(graphIterator.hasNext()) { |
---|
46 | Node v2 = graphIterator.next(); |
---|
47 | if(v1.equals(v2)) return v2; |
---|
48 | } |
---|
49 | return null; |
---|
50 | } |
---|
51 | |
---|
52 | public Collection<Node> getNeighborsPermutation(Node n) { |
---|
53 | Random r = Random.instance(); |
---|
54 | ArrayList<Node> perm = new ArrayList<Node>(); |
---|
55 | Iterator<Node> vtxsIterator = getNeighbors(n).iterator(); |
---|
56 | while(vtxsIterator.hasNext()) { |
---|
57 | Node node = vtxsIterator.next(); |
---|
58 | perm.add(node); |
---|
59 | } |
---|
60 | for (int i = 0; i < perm.size(); i++) { |
---|
61 | int k = r.nextInt(perm.size()); |
---|
62 | Node swap = perm.get(k); |
---|
63 | perm.set(k, perm.get(i)); |
---|
64 | perm.set(i, swap); |
---|
65 | } |
---|
66 | return perm; |
---|
67 | } |
---|
68 | |
---|
69 | public void loadFromInstance(Instances data) { |
---|
70 | Iterator<Instance> dataIterator = data.iterator(); |
---|
71 | Attribute from = data.attribute("from"); |
---|
72 | Attribute to = data.attribute("to"); |
---|
73 | if (from == null || to == null) |
---|
74 | throw new RuntimeException( |
---|
75 | "Unsupported data: check the list of attributes (from and to are needed)."); |
---|
76 | int edgeId = 0; |
---|
77 | while (dataIterator.hasNext()) { |
---|
78 | Instance edge = dataIterator.next(); |
---|
79 | Node node1 = new Node(edge.stringValue(from)); |
---|
80 | Node node2 = new Node(edge.stringValue(to)); |
---|
81 | addVertex(node1); |
---|
82 | addVertex(node2); |
---|
83 | addEdge(new Edge(Integer.toString(edgeId),1,1),node1,node2); |
---|
84 | edgeId++; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | @Override |
---|
89 | public UndirectedGraph clone() { |
---|
90 | UndirectedGraph g = new UndirectedGraph(); |
---|
91 | Iterator<Node> nodesIterator = getVertices().iterator(); |
---|
92 | while(nodesIterator.hasNext()) { |
---|
93 | g.addVertex(nodesIterator.next().clone()); |
---|
94 | } |
---|
95 | Iterator<Edge> edgesIterator = getEdges().iterator(); |
---|
96 | while(edgesIterator.hasNext()) { |
---|
97 | Edge e = edgesIterator.next(); |
---|
98 | g.addEdge(e.clone(), getEndpoints(e)); |
---|
99 | } |
---|
100 | return g; |
---|
101 | } |
---|
102 | |
---|
103 | public int getAdjcncyWeight(Node v1){ |
---|
104 | Iterator<Node> nbsIterator = getNeighbors(v1).iterator(); |
---|
105 | int adjcncyWgt = 0; |
---|
106 | while(nbsIterator.hasNext()) { |
---|
107 | Node v2 = nbsIterator.next(); |
---|
108 | Edge edge = findEdge(v1, v2); |
---|
109 | adjcncyWgt += edge.getWeight(); |
---|
110 | } |
---|
111 | return adjcncyWgt; |
---|
112 | } |
---|
113 | |
---|
114 | public void printForMetis(PrintStream output) { |
---|
115 | TreeMap<Integer,Node> map = new TreeMap<Integer,Node>(); |
---|
116 | HashMap<Node,Integer> reverseMap = new HashMap<Node,Integer>(); |
---|
117 | Iterator<Node> nodesIterator = getVertices().iterator(); |
---|
118 | int id = 1; |
---|
119 | while(nodesIterator.hasNext()) { |
---|
120 | Node node = nodesIterator.next(); |
---|
121 | map.put(id, node); |
---|
122 | reverseMap.put(node,id); |
---|
123 | id++; |
---|
124 | } |
---|
125 | output.println(getVertexCount() + " "+ getEdgeCount()); |
---|
126 | Iterator<Integer> mappedIterator = map.keySet().iterator(); |
---|
127 | while(mappedIterator.hasNext()) { |
---|
128 | id = mappedIterator.next(); |
---|
129 | Iterator<Node> neighbors = getNeighbors(map.get(id)).iterator(); |
---|
130 | while(neighbors.hasNext()){ |
---|
131 | output.print(reverseMap.get(neighbors.next())); |
---|
132 | if(neighbors.hasNext()) output.print(" "); |
---|
133 | } |
---|
134 | output.println(); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | public String toString() { |
---|
139 | StringBuffer sb = new StringBuffer("Vertices:"); |
---|
140 | for(Node v : getVertices()) { |
---|
141 | sb.append(v+ " Adjw: "+ getAdjcncyWeight(v) + ","); |
---|
142 | } |
---|
143 | sb.setLength(sb.length()-1); |
---|
144 | sb.append("\nEdges:"); |
---|
145 | for(Edge e : getEdges()) { |
---|
146 | Pair<Node> ep = getEndpoints(e); |
---|
147 | sb.append(e+"["+ep.getFirst()+","+ep.getSecond()+"] "); |
---|
148 | } |
---|
149 | return sb.toString(); |
---|
150 | } |
---|
151 | } |
---|