| 1 |
/* |
| 2 |
* Created on Apr 2, 2006 |
| 3 |
* |
| 4 |
* Copyright (c) 2006, the JUNG Project and the Regents of the University |
| 5 |
* of California |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* This software is open-source under the BSD license; see either |
| 9 |
* "license.txt" or |
| 10 |
* http://jung.sourceforge.net/license.txt for a description. |
| 11 |
*/ |
| 12 |
package edu.uci.ics.jung.graph; |
| 13 |
|
| 14 |
import java.io.Serializable; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.Collection; |
| 17 |
import java.util.Collections; |
| 18 |
|
| 19 |
import edu.uci.ics.jung.graph.util.EdgeType; |
| 20 |
import edu.uci.ics.jung.graph.util.Pair; |
| 21 |
|
| 22 |
/** |
| 23 |
* Abstract implementation of the <code>Graph</code> interface. |
| 24 |
* Designed to simplify implementation of new graph classes. |
| 25 |
* |
| 26 |
* @author Joshua O'Madadhain |
| 27 |
*/ |
| 28 |
@SuppressWarnings("serial") |
| 29 |
public abstract class AbstractGraph<V, E> implements Graph<V,E>, Serializable |
| 30 |
{ |
| 31 |
public boolean addEdge(E edge, Collection<? extends V> vertices) |
| 32 |
{ |
| 33 |
return addEdge(edge, vertices, this.getDefaultEdgeType()); |
| 34 |
} |
| 35 |
|
| 36 |
@SuppressWarnings("unchecked") |
| 37 |
public boolean addEdge(E edge, Collection<? extends V> vertices, EdgeType edgeType) { |
| 38 |
if (vertices == null) |
| 39 |
throw new IllegalArgumentException("'vertices' parameter must not be null"); |
| 40 |
if (vertices.size() == 2) |
| 41 |
return addEdge(edge, |
| 42 |
vertices instanceof Pair ? (Pair<V>)vertices : new Pair<V>(vertices), |
| 43 |
edgeType); |
| 44 |
else if (vertices.size() == 1) |
| 45 |
{ |
| 46 |
V vertex = vertices.iterator().next(); |
| 47 |
return addEdge(edge, new Pair<V>(vertex, vertex), edgeType); |
| 48 |
} |
| 49 |
else |
| 50 |
throw new IllegalArgumentException("Graph objects connect 1 or 2 vertices; vertices arg has " + vertices.size()); |
| 51 |
} |
| 52 |
|
| 53 |
public boolean addEdge(E e, V v1, V v2) |
| 54 |
{ |
| 55 |
return addEdge(e, v1, v2, this.getDefaultEdgeType()); |
| 56 |
} |
| 57 |
|
| 58 |
public boolean addEdge(E e, V v1, V v2, EdgeType edge_type) |
| 59 |
{ |
| 60 |
return addEdge(e, new Pair<V>(v1, v2), edge_type); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Adds {@code edge} to this graph with the specified {@code endpoints}, |
| 65 |
* with the default edge type. |
| 66 |
* @return {@code} true iff the graph was modified as a result of this call |
| 67 |
*/ |
| 68 |
public boolean addEdge(E edge, Pair<? extends V> endpoints) |
| 69 |
{ |
| 70 |
return addEdge(edge, endpoints, this.getDefaultEdgeType()); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Adds {@code edge} to this graph with the specified {@code endpoints} |
| 75 |
* and {@code EdgeType}. |
| 76 |
* @return {@code} true iff the graph was modified as a result of this call |
| 77 |
*/ |
| 78 |
public abstract boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType); |
| 79 |
|
| 80 |
protected Pair<V> getValidatedEndpoints(E edge, Pair<? extends V> endpoints) |
| 81 |
{ |
| 82 |
if (edge == null) |
| 83 |
throw new IllegalArgumentException("input edge may not be null"); |
| 84 |
|
| 85 |
if (endpoints == null) |
| 86 |
throw new IllegalArgumentException("endpoints may not be null"); |
| 87 |
|
| 88 |
Pair<V> new_endpoints = new Pair<V>(endpoints.getFirst(), endpoints.getSecond()); |
| 89 |
if (containsEdge(edge)) |
| 90 |
{ |
| 91 |
Pair<V> existing_endpoints = getEndpoints(edge); |
| 92 |
if (!existing_endpoints.equals(new_endpoints)) { |
| 93 |
throw new IllegalArgumentException("edge " + edge + |
| 94 |
" already exists in this graph with endpoints " + existing_endpoints + |
| 95 |
" and cannot be added with endpoints " + endpoints); |
| 96 |
} else { |
| 97 |
return null; |
| 98 |
} |
| 99 |
} |
| 100 |
return new_endpoints; |
| 101 |
} |
| 102 |
|
| 103 |
public int inDegree(V vertex) |
| 104 |
{ |
| 105 |
return this.getInEdges(vertex).size(); |
| 106 |
} |
| 107 |
|
| 108 |
public int outDegree(V vertex) |
| 109 |
{ |
| 110 |
return this.getOutEdges(vertex).size(); |
| 111 |
} |
| 112 |
|
| 113 |
public boolean isPredecessor(V v1, V v2) |
| 114 |
{ |
| 115 |
return this.getPredecessors(v1).contains(v2); |
| 116 |
} |
| 117 |
|
| 118 |
public boolean isSuccessor(V v1, V v2) |
| 119 |
{ |
| 120 |
return this.getSuccessors(v1).contains(v2); |
| 121 |
} |
| 122 |
|
| 123 |
public int getPredecessorCount(V vertex) |
| 124 |
{ |
| 125 |
return this.getPredecessors(vertex).size(); |
| 126 |
} |
| 127 |
|
| 128 |
public int getSuccessorCount(V vertex) |
| 129 |
{ |
| 130 |
return this.getSuccessors(vertex).size(); |
| 131 |
} |
| 132 |
|
| 133 |
public boolean isNeighbor(V v1, V v2) |
| 134 |
{ |
| 135 |
if (!containsVertex(v1) || !containsVertex(v2)) |
| 136 |
throw new IllegalArgumentException("At least one of these not in this graph: " + v1 + ", " + v2); |
| 137 |
return this.getNeighbors(v1).contains(v2); |
| 138 |
} |
| 139 |
|
| 140 |
public boolean isIncident(V vertex, E edge) |
| 141 |
{ |
| 142 |
if (!containsVertex(vertex) || !containsEdge(edge)) |
| 143 |
throw new IllegalArgumentException("At least one of these not in this graph: " + vertex + ", " + edge); |
| 144 |
return this.getIncidentEdges(vertex).contains(edge); |
| 145 |
} |
| 146 |
|
| 147 |
public int getNeighborCount(V vertex) |
| 148 |
{ |
| 149 |
if (!containsVertex(vertex)) |
| 150 |
throw new IllegalArgumentException(vertex + " is not a vertex in this graph"); |
| 151 |
return this.getNeighbors(vertex).size(); |
| 152 |
} |
| 153 |
|
| 154 |
public int degree(V vertex) |
| 155 |
{ |
| 156 |
if (!containsVertex(vertex)) |
| 157 |
throw new IllegalArgumentException(vertex + " is not a vertex in this graph"); |
| 158 |
return this.getIncidentEdges(vertex).size(); |
| 159 |
} |
| 160 |
|
| 161 |
public int getIncidentCount(E edge) |
| 162 |
{ |
| 163 |
Pair<V> incident = this.getEndpoints(edge); |
| 164 |
if (incident == null) |
| 165 |
return 0; |
| 166 |
if (incident.getFirst() == incident.getSecond()) |
| 167 |
return 1; |
| 168 |
else |
| 169 |
return 2; |
| 170 |
} |
| 171 |
|
| 172 |
public V getOpposite(V vertex, E edge) |
| 173 |
{ |
| 174 |
Pair<V> incident = this.getEndpoints(edge); |
| 175 |
V first = incident.getFirst(); |
| 176 |
V second = incident.getSecond(); |
| 177 |
if (vertex.equals(first)) |
| 178 |
return second; |
| 179 |
else if (vertex.equals(second)) |
| 180 |
return first; |
| 181 |
else |
| 182 |
throw new IllegalArgumentException(vertex + " is not incident to " + edge + " in this graph"); |
| 183 |
} |
| 184 |
|
| 185 |
public E findEdge(V v1, V v2) |
| 186 |
{ |
| 187 |
for (E e : getOutEdges(v1)) |
| 188 |
{ |
| 189 |
if (getOpposite(v1, e).equals(v2)) |
| 190 |
return e; |
| 191 |
} |
| 192 |
return null; |
| 193 |
} |
| 194 |
|
| 195 |
public Collection<E> findEdgeSet(V v1, V v2) |
| 196 |
{ |
| 197 |
if (!getVertices().contains(v1)) |
| 198 |
throw new IllegalArgumentException(v1 + " is not an element of this graph"); |
| 199 |
|
| 200 |
if (!getVertices().contains(v2)) |
| 201 |
throw new IllegalArgumentException(v2 + " is not an element of this graph"); |
| 202 |
|
| 203 |
Collection<E> edges = new ArrayList<E>(); |
| 204 |
for (E e : getOutEdges(v1)) |
| 205 |
{ |
| 206 |
if (getOpposite(v1, e).equals(v2)) |
| 207 |
edges.add(e); |
| 208 |
} |
| 209 |
return Collections.unmodifiableCollection(edges); |
| 210 |
} |
| 211 |
|
| 212 |
public Collection<V> getIncidentVertices(E edge) |
| 213 |
{ |
| 214 |
Pair<V> endpoints = this.getEndpoints(edge); |
| 215 |
Collection<V> incident = new ArrayList<V>(); |
| 216 |
incident.add(endpoints.getFirst()); |
| 217 |
incident.add(endpoints.getSecond()); |
| 218 |
|
| 219 |
return Collections.unmodifiableCollection(incident); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public String toString() { |
| 224 |
StringBuffer sb = new StringBuffer("Vertices:"); |
| 225 |
for(V v : getVertices()) { |
| 226 |
sb.append(v+","); |
| 227 |
} |
| 228 |
sb.setLength(sb.length()-1); |
| 229 |
sb.append("\nEdges:"); |
| 230 |
for(E e : getEdges()) { |
| 231 |
Pair<V> ep = getEndpoints(e); |
| 232 |
sb.append(e+"["+ep.getFirst()+","+ep.getSecond()+"] "); |
| 233 |
} |
| 234 |
return sb.toString(); |
| 235 |
} |
| 236 |
|
| 237 |
} |