| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
package edu.uci.ics.jung.graph; |
| 13 | |
|
| 14 | |
import java.util.ArrayList; |
| 15 | |
import java.util.Collection; |
| 16 | |
import java.util.Collections; |
| 17 | |
import java.util.HashMap; |
| 18 | |
import java.util.Map; |
| 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 | |
@SuppressWarnings("serial") |
| 30 | |
public class UndirectedSparseGraph<V, E> extends AbstractTypedGraph<V, E> |
| 31 | |
implements UndirectedGraph<V, E> |
| 32 | |
{ |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
public static <V,E> Factory<UndirectedGraph<V,E>> getFactory() { |
| 40 | 2 | return new Factory<UndirectedGraph<V,E>> () { |
| 41 | |
|
| 42 | 6 | public UndirectedGraph<V,E> create() { |
| 43 | 4 | return new UndirectedSparseGraph<V,E>(); |
| 44 | |
} |
| 45 | |
}; |
| 46 | |
} |
| 47 | |
|
| 48 | |
protected Map<V, Map<V,E>> vertices; |
| 49 | |
protected Map<E, Pair<V>> edges; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
public UndirectedSparseGraph() { |
| 55 | 4 | super(EdgeType.UNDIRECTED); |
| 56 | 4 | vertices = new HashMap<V, Map<V,E>>(); |
| 57 | 4 | edges = new HashMap<E, Pair<V>>(); |
| 58 | 4 | } |
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) |
| 62 | |
{ |
| 63 | 8 | this.validateEdgeType(edgeType); |
| 64 | 8 | Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints); |
| 65 | 6 | if (new_endpoints == null) |
| 66 | 4 | return false; |
| 67 | |
|
| 68 | 2 | V v1 = new_endpoints.getFirst(); |
| 69 | 2 | V v2 = new_endpoints.getSecond(); |
| 70 | |
|
| 71 | 2 | if (findEdge(v1, v2) != null) |
| 72 | 0 | return false; |
| 73 | |
|
| 74 | 2 | edges.put(edge, new_endpoints); |
| 75 | |
|
| 76 | 2 | if (!vertices.containsKey(v1)) |
| 77 | 2 | this.addVertex(v1); |
| 78 | |
|
| 79 | 2 | if (!vertices.containsKey(v2)) |
| 80 | 2 | this.addVertex(v2); |
| 81 | |
|
| 82 | |
|
| 83 | 2 | vertices.get(v1).put(v2, edge); |
| 84 | 2 | vertices.get(v2).put(v1, edge); |
| 85 | |
|
| 86 | 2 | return true; |
| 87 | |
} |
| 88 | |
|
| 89 | |
public Collection<E> getInEdges(V vertex) |
| 90 | |
{ |
| 91 | 0 | return this.getIncidentEdges(vertex); |
| 92 | |
} |
| 93 | |
|
| 94 | |
public Collection<E> getOutEdges(V vertex) |
| 95 | |
{ |
| 96 | 0 | return this.getIncidentEdges(vertex); |
| 97 | |
} |
| 98 | |
|
| 99 | |
public Collection<V> getPredecessors(V vertex) |
| 100 | |
{ |
| 101 | 0 | return this.getNeighbors(vertex); |
| 102 | |
} |
| 103 | |
|
| 104 | |
public Collection<V> getSuccessors(V vertex) |
| 105 | |
{ |
| 106 | 0 | return this.getNeighbors(vertex); |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public E findEdge(V v1, V v2) |
| 111 | |
{ |
| 112 | 2 | if (!containsVertex(v1) || !containsVertex(v2)) |
| 113 | 2 | return null; |
| 114 | 0 | return vertices.get(v1).get(v2); |
| 115 | |
} |
| 116 | |
|
| 117 | |
@Override |
| 118 | |
public Collection<E> findEdgeSet(V v1, V v2) |
| 119 | |
{ |
| 120 | 0 | if (!containsVertex(v1) || !containsVertex(v2)) |
| 121 | 0 | return null; |
| 122 | 0 | ArrayList<E> edge_collection = new ArrayList<E>(1); |
| 123 | |
|
| 124 | |
|
| 125 | 0 | E e = findEdge(v1, v2); |
| 126 | 0 | if (e == null) |
| 127 | 0 | return edge_collection; |
| 128 | 0 | edge_collection.add(e); |
| 129 | 0 | return edge_collection; |
| 130 | |
} |
| 131 | |
|
| 132 | |
public Pair<V> getEndpoints(E edge) |
| 133 | |
{ |
| 134 | 6 | return edges.get(edge); |
| 135 | |
} |
| 136 | |
|
| 137 | |
public V getSource(E directed_edge) |
| 138 | |
{ |
| 139 | 0 | return null; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public V getDest(E directed_edge) |
| 143 | |
{ |
| 144 | 0 | return null; |
| 145 | |
} |
| 146 | |
|
| 147 | |
public boolean isSource(V vertex, E edge) |
| 148 | |
{ |
| 149 | 0 | return false; |
| 150 | |
} |
| 151 | |
|
| 152 | |
public boolean isDest(V vertex, E edge) |
| 153 | |
{ |
| 154 | 0 | return false; |
| 155 | |
} |
| 156 | |
|
| 157 | |
public Collection<E> getEdges() |
| 158 | |
{ |
| 159 | 0 | return Collections.unmodifiableCollection(edges.keySet()); |
| 160 | |
} |
| 161 | |
|
| 162 | |
public Collection<V> getVertices() |
| 163 | |
{ |
| 164 | 0 | return Collections.unmodifiableCollection(vertices.keySet()); |
| 165 | |
} |
| 166 | |
|
| 167 | |
public boolean containsVertex(V vertex) |
| 168 | |
{ |
| 169 | 14 | return vertices.containsKey(vertex); |
| 170 | |
} |
| 171 | |
|
| 172 | |
public boolean containsEdge(E edge) |
| 173 | |
{ |
| 174 | 12 | return edges.containsKey(edge); |
| 175 | |
} |
| 176 | |
|
| 177 | |
public int getEdgeCount() |
| 178 | |
{ |
| 179 | 6 | return edges.size(); |
| 180 | |
} |
| 181 | |
|
| 182 | |
public int getVertexCount() |
| 183 | |
{ |
| 184 | 10 | return vertices.size(); |
| 185 | |
} |
| 186 | |
|
| 187 | |
public Collection<V> getNeighbors(V vertex) |
| 188 | |
{ |
| 189 | 0 | if (!containsVertex(vertex)) |
| 190 | 0 | return null; |
| 191 | 0 | return Collections.unmodifiableCollection(vertices.get(vertex).keySet()); |
| 192 | |
} |
| 193 | |
|
| 194 | |
public Collection<E> getIncidentEdges(V vertex) |
| 195 | |
{ |
| 196 | 0 | if (!containsVertex(vertex)) |
| 197 | 0 | return null; |
| 198 | 0 | return Collections.unmodifiableCollection(vertices.get(vertex).values()); |
| 199 | |
} |
| 200 | |
|
| 201 | |
public boolean addVertex(V vertex) |
| 202 | |
{ |
| 203 | 10 | if(vertex == null) { |
| 204 | 2 | throw new IllegalArgumentException("vertex may not be null"); |
| 205 | |
} |
| 206 | 8 | if (!containsVertex(vertex)) { |
| 207 | 6 | vertices.put(vertex, new HashMap<V,E>()); |
| 208 | 6 | return true; |
| 209 | |
} else { |
| 210 | 2 | return false; |
| 211 | |
} |
| 212 | |
} |
| 213 | |
|
| 214 | |
public boolean removeVertex(V vertex) |
| 215 | |
{ |
| 216 | 0 | if (!containsVertex(vertex)) |
| 217 | 0 | return false; |
| 218 | |
|
| 219 | |
|
| 220 | 0 | for (E edge : new ArrayList<E>(vertices.get(vertex).values())) |
| 221 | 0 | removeEdge(edge); |
| 222 | |
|
| 223 | 0 | vertices.remove(vertex); |
| 224 | 0 | return true; |
| 225 | |
} |
| 226 | |
|
| 227 | |
public boolean removeEdge(E edge) |
| 228 | |
{ |
| 229 | 0 | if (!containsEdge(edge)) |
| 230 | 0 | return false; |
| 231 | |
|
| 232 | 0 | Pair<V> endpoints = getEndpoints(edge); |
| 233 | 0 | V v1 = endpoints.getFirst(); |
| 234 | 0 | V v2 = endpoints.getSecond(); |
| 235 | |
|
| 236 | |
|
| 237 | 0 | vertices.get(v1).remove(v2); |
| 238 | 0 | vertices.get(v2).remove(v1); |
| 239 | |
|
| 240 | 0 | edges.remove(edge); |
| 241 | 0 | return true; |
| 242 | |
} |
| 243 | |
} |