1 | package weka.clusterers.forMetisMQI; |
---|
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.core.Attribute; |
---|
9 | import weka.core.Instance; |
---|
10 | import weka.core.Instances; |
---|
11 | import edu.uci.ics.jung.graph.UndirectedSparseGraph; |
---|
12 | |
---|
13 | public class UndirectedGraph extends UndirectedSparseGraph<Node, Edge> { |
---|
14 | |
---|
15 | private static final long serialVersionUID = 1L; |
---|
16 | |
---|
17 | public List<Node> vtxsPermutation() { |
---|
18 | Random r = Random.instance(); |
---|
19 | List<Node> perm = new ArrayList<Node>(); |
---|
20 | Iterator<Node> vtxsIterator = getVertices().iterator(); |
---|
21 | while(vtxsIterator.hasNext()) { |
---|
22 | Node node = vtxsIterator.next(); |
---|
23 | perm.add(node); |
---|
24 | } |
---|
25 | for (int i = 0; i < perm.size(); i++) { |
---|
26 | int k = r.nextInt(perm.size()); |
---|
27 | Node swap = perm.get(k); |
---|
28 | perm.set(k, perm.get(i)); |
---|
29 | perm.set(i, swap); |
---|
30 | } |
---|
31 | return perm; |
---|
32 | } |
---|
33 | |
---|
34 | public boolean containsEdge(Node v1, Node v2) { |
---|
35 | return (findEdge(v1, v2) != null); |
---|
36 | } |
---|
37 | |
---|
38 | public Node findVertex(Node v1) { |
---|
39 | Iterator<Node> graphIterator = getVertices().iterator(); |
---|
40 | while(graphIterator.hasNext()) { |
---|
41 | Node v2 = graphIterator.next(); |
---|
42 | if(v1.equals(v2)) return v2; |
---|
43 | } |
---|
44 | return null; |
---|
45 | } |
---|
46 | |
---|
47 | public Collection<Node> getNeighborsPermutation(Node n) { |
---|
48 | Random r = Random.instance(); |
---|
49 | ArrayList<Node> perm = new ArrayList<Node>(); |
---|
50 | Iterator<Node> vtxsIterator = getNeighbors(n).iterator(); |
---|
51 | while(vtxsIterator.hasNext()) { |
---|
52 | Node node = vtxsIterator.next(); |
---|
53 | perm.add(node); |
---|
54 | } |
---|
55 | for (int i = 0; i < perm.size(); i++) { |
---|
56 | int k = r.nextInt(perm.size()); |
---|
57 | Node swap = perm.get(k); |
---|
58 | perm.set(k, perm.get(i)); |
---|
59 | perm.set(i, swap); |
---|
60 | } |
---|
61 | return perm; |
---|
62 | } |
---|
63 | |
---|
64 | public void loadFromInstance(Instances data) { |
---|
65 | Iterator<Instance> dataIterator = data.iterator(); |
---|
66 | Attribute from = data.attribute("from"); |
---|
67 | Attribute to = data.attribute("to"); |
---|
68 | if (from == null || to == null) |
---|
69 | throw new RuntimeException( |
---|
70 | "Unsupported data: check the list of attributes (from and to are needed)."); |
---|
71 | int edgeId = 0; |
---|
72 | while (dataIterator.hasNext()) { |
---|
73 | Instance edge = dataIterator.next(); |
---|
74 | Node node1 = new Node(Integer.toString(((int) Math.round(edge.value(from))))); |
---|
75 | Node node2 = new Node(Integer.toString(((int) Math.round(edge.value(to))))); |
---|
76 | addVertex(node1); |
---|
77 | addVertex(node2); |
---|
78 | addEdge(new Edge(Integer.toString(edgeId),1,1),node1,node2); |
---|
79 | edgeId++; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | @Override |
---|
84 | public UndirectedGraph clone() { |
---|
85 | UndirectedGraph g = new UndirectedGraph(); |
---|
86 | Iterator<Node> nodesIterator = getVertices().iterator(); |
---|
87 | while(nodesIterator.hasNext()) { |
---|
88 | g.addVertex(nodesIterator.next().clone()); |
---|
89 | } |
---|
90 | |
---|
91 | Iterator<Edge> edgesIterator = getEdges().iterator(); |
---|
92 | while(edgesIterator.hasNext()) { |
---|
93 | Edge e = edgesIterator.next(); |
---|
94 | g.addEdge(e.clone(), getEndpoints(e)); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | return g; |
---|
99 | } |
---|
100 | |
---|
101 | } |
---|