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