1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.graph;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.LinkedHashMap;
17 import java.util.LinkedHashSet;
18 import java.util.Set;
19
20 import org.apache.commons.collections15.Factory;
21
22 import edu.uci.ics.jung.graph.util.EdgeType;
23 import edu.uci.ics.jung.graph.util.Pair;
24
25
26
27
28
29
30 @SuppressWarnings("serial")
31 public class OrderedSparseMultigraph<V,E>
32 extends SparseMultigraph<V,E>
33 implements MultiGraph<V,E> {
34
35
36
37
38
39
40 public static <V,E> Factory<Graph<V,E>> getFactory() {
41 return new Factory<Graph<V,E>> () {
42 public Graph<V,E> create() {
43 return new OrderedSparseMultigraph<V,E>();
44 }
45 };
46 }
47
48
49
50
51 public OrderedSparseMultigraph()
52 {
53 vertices = new LinkedHashMap<V, Pair<Set<E>>>();
54 edges = new LinkedHashMap<E, Pair<V>>();
55 directedEdges = new LinkedHashSet<E>();
56 }
57
58 @Override
59 public boolean addVertex(V vertex) {
60 if(vertex == null) {
61 throw new IllegalArgumentException("vertex may not be null");
62 }
63 if (!containsVertex(vertex)) {
64 vertices.put(vertex, new Pair<Set<E>>(new LinkedHashSet<E>(), new LinkedHashSet<E>()));
65 return true;
66 } else {
67 return false;
68 }
69 }
70
71
72 @Override
73 public Collection<V> getPredecessors(V vertex)
74 {
75 if (!containsVertex(vertex))
76 return null;
77
78 Set<V> preds = new LinkedHashSet<V>();
79 for (E edge : getIncoming_internal(vertex)) {
80 if(getEdgeType(edge) == EdgeType.DIRECTED) {
81 preds.add(this.getSource(edge));
82 } else {
83 preds.add(getOpposite(vertex, edge));
84 }
85 }
86 return Collections.unmodifiableCollection(preds);
87 }
88
89 @Override
90 public Collection<V> getSuccessors(V vertex)
91 {
92 if (!containsVertex(vertex))
93 return null;
94
95 Set<V> succs = new LinkedHashSet<V>();
96 for (E edge : getOutgoing_internal(vertex)) {
97 if(getEdgeType(edge) == EdgeType.DIRECTED) {
98 succs.add(this.getDest(edge));
99 } else {
100 succs.add(getOpposite(vertex, edge));
101 }
102 }
103 return Collections.unmodifiableCollection(succs);
104 }
105
106 @Override
107 public Collection<V> getNeighbors(V vertex)
108 {
109 if (!containsVertex(vertex))
110 return null;
111
112 Collection<V> out = new LinkedHashSet<V>();
113 out.addAll(this.getPredecessors(vertex));
114 out.addAll(this.getSuccessors(vertex));
115 return out;
116 }
117
118 @Override
119 public Collection<E> getIncidentEdges(V vertex)
120 {
121 if (!containsVertex(vertex))
122 return null;
123
124 Collection<E> out = new LinkedHashSet<E>();
125 out.addAll(this.getInEdges(vertex));
126 out.addAll(this.getOutEdges(vertex));
127 return out;
128 }
129 }