1 25 45 package org.jgrapht; 46 47 import java.util.*; 48 49 import org.jgrapht.graph.*; 50 51 52 58 public abstract class Graphs 59 { 60 61 63 77 public static <V, E> E addEdge( 78 Graph<V, E> g, 79 V sourceVertex, 80 V targetVertex, 81 double weight) 82 { 83 EdgeFactory<V, E> ef = g.getEdgeFactory(); 84 E e = ef.createEdge(sourceVertex, targetVertex); 85 86 89 assert (g instanceof WeightedGraph) : g.getClass(); 90 ((WeightedGraph<V, E>) g).setEdgeWeight(e, weight); 91 92 return g.addEdge(sourceVertex, targetVertex, e) ? e : null; 93 } 94 95 107 public static <V, E> E addEdgeWithVertices( 108 Graph<V, E> g, 109 V sourceVertex, 110 V targetVertex) 111 { 112 g.addVertex(sourceVertex); 113 g.addVertex(targetVertex); 114 115 return g.addEdge(sourceVertex, targetVertex); 116 } 117 118 130 public static <V, E> boolean addEdgeWithVertices( 131 Graph<V, E> targetGraph, 132 Graph<V, E> sourceGraph, 133 E edge) 134 { 135 V sourceVertex = sourceGraph.getEdgeSource(edge); 136 V targetVertex = sourceGraph.getEdgeTarget(edge); 137 138 targetGraph.addVertex(sourceVertex); 139 targetGraph.addVertex(targetVertex); 140 141 return targetGraph.addEdge(sourceVertex, targetVertex, edge); 142 } 143 144 158 public static <V, E> E addEdgeWithVertices( 159 Graph<V, E> g, 160 V sourceVertex, 161 V targetVertex, 162 double weight) 163 { 164 g.addVertex(sourceVertex); 165 g.addVertex(targetVertex); 166 167 return addEdge(g, sourceVertex, targetVertex, weight); 168 } 169 170 187 public static <V, E> boolean addGraph( 188 Graph<V, E> destination, 189 Graph<V, E> source) 190 { 191 boolean modified = addAllVertices(destination, source.vertexSet()); 192 modified |= addAllEdges(destination, source, source.edgeSet()); 193 194 return modified; 195 } 196 197 207 public static <V, E> void addGraphReversed( 208 DirectedGraph<V, E> destination, 209 DirectedGraph<V, E> source) 210 { 211 addAllVertices(destination, source.vertexSet()); 212 213 for (E edge : source.edgeSet()) { 214 destination.addEdge( 215 source.getEdgeTarget(edge), 216 source.getEdgeSource(edge)); 217 } 218 } 219 220 233 public static <V, E> boolean addAllEdges( 234 Graph<V, E> destination, 235 Graph<V, E> source, 236 Collection<? extends E> edges) 237 { 238 boolean modified = false; 239 240 for (E e : edges) { 241 V s = source.getEdgeSource(e); 242 V t = source.getEdgeTarget(e); 243 destination.addVertex(s); 244 destination.addVertex(t); 245 modified |= destination.addEdge(s, t, e); 246 } 247 248 return modified; 249 } 250 251 268 public static <V, E> boolean addAllVertices( 269 Graph<V, E> destination, 270 Collection<? extends V> vertices) 271 { 272 boolean modified = false; 273 274 for (V v : vertices) { 275 modified |= destination.addVertex(v); 276 } 277 278 return modified; 279 } 280 281 292 public static <V, E> List<V> neighborListOf(Graph<V, E> g, 293 V vertex) 294 { 295 List<V> neighbors = new ArrayList<V>(); 296 297 for (E e : g.edgesOf(vertex)) { 298 neighbors.add(getOppositeVertex(g, e, vertex)); 299 } 300 301 return neighbors; 302 } 303 304 315 public static <V, E> List<V> predecessorListOf( 316 DirectedGraph<V, E> g, 317 V vertex) 318 { 319 List<V> predecessors = new ArrayList<V>(); 320 Set<? extends E> edges = g.incomingEdgesOf(vertex); 321 322 for (E e : edges) { 323 predecessors.add(getOppositeVertex(g, e, vertex)); 324 } 325 326 return predecessors; 327 } 328 329 340 public static <V, E> List<V> successorListOf( 341 DirectedGraph<V, E> g, 342 V vertex) 343 { 344 List<V> successors = new ArrayList<V>(); 345 Set<? extends E> edges = g.outgoingEdgesOf(vertex); 346 347 for (E e : edges) { 348 successors.add(getOppositeVertex(g, e, vertex)); 349 } 350 351 return successors; 352 } 353 354 369 public static <V, E> UndirectedGraph<V, E> undirectedGraph(Graph<V, E> g) 370 { 371 if (g instanceof DirectedGraph) { 372 return new AsUndirectedGraph<V, E>((DirectedGraph<V, E>) g); 373 } else if (g instanceof UndirectedGraph) { 374 return (UndirectedGraph<V, E>) g; 375 } else { 376 throw new IllegalArgumentException ( 377 "Graph must be either DirectedGraph or UndirectedGraph"); 378 } 379 } 380 381 390 public static <V, E> boolean testIncidence(Graph<V, E> g, E e, V v) 391 { 392 return (g.getEdgeSource(e).equals(v)) 393 || (g.getEdgeTarget(e).equals(v)); 394 } 395 396 405 public static <V, E> V getOppositeVertex(Graph<V, E> g, E e, V v) 406 { 407 V source = g.getEdgeSource(e); 408 V target = g.getEdgeTarget(e); 409 if (v.equals(source)) { 410 return target; 411 } else if (v.equals(target)) { 412 return source; 413 } else { 414 throw new IllegalArgumentException ("no such vertex"); 415 } 416 } 417 } 418 | Popular Tags |