| 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 | 2 | return new Factory<Graph<V,E>> () { |
| 42 | 6 | public Graph<V,E> create() { |
| 43 | 4 | return new OrderedSparseMultigraph<V,E>(); |
| 44 | |
} |
| 45 | |
}; |
| 46 | |
} |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public OrderedSparseMultigraph() |
| 52 | 104 | { |
| 53 | 104 | vertices = new LinkedHashMap<V, Pair<Set<E>>>(); |
| 54 | 104 | edges = new LinkedHashMap<E, Pair<V>>(); |
| 55 | 104 | directedEdges = new LinkedHashSet<E>(); |
| 56 | 104 | } |
| 57 | |
|
| 58 | |
@Override |
| 59 | |
public boolean addVertex(V vertex) { |
| 60 | 300 | if(vertex == null) { |
| 61 | 2 | throw new IllegalArgumentException("vertex may not be null"); |
| 62 | |
} |
| 63 | 298 | if (!containsVertex(vertex)) { |
| 64 | 296 | vertices.put(vertex, new Pair<Set<E>>(new LinkedHashSet<E>(), new LinkedHashSet<E>())); |
| 65 | 296 | return true; |
| 66 | |
} else { |
| 67 | 2 | return false; |
| 68 | |
} |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
@Override |
| 73 | |
public Collection<V> getPredecessors(V vertex) |
| 74 | |
{ |
| 75 | 0 | if (!containsVertex(vertex)) |
| 76 | 0 | return null; |
| 77 | |
|
| 78 | 0 | Set<V> preds = new LinkedHashSet<V>(); |
| 79 | 0 | for (E edge : getIncoming_internal(vertex)) { |
| 80 | 0 | if(getEdgeType(edge) == EdgeType.DIRECTED) { |
| 81 | 0 | preds.add(this.getSource(edge)); |
| 82 | |
} else { |
| 83 | 0 | preds.add(getOpposite(vertex, edge)); |
| 84 | |
} |
| 85 | |
} |
| 86 | 0 | return Collections.unmodifiableCollection(preds); |
| 87 | |
} |
| 88 | |
|
| 89 | |
@Override |
| 90 | |
public Collection<V> getSuccessors(V vertex) |
| 91 | |
{ |
| 92 | 0 | if (!containsVertex(vertex)) |
| 93 | 0 | return null; |
| 94 | |
|
| 95 | 0 | Set<V> succs = new LinkedHashSet<V>(); |
| 96 | 0 | for (E edge : getOutgoing_internal(vertex)) { |
| 97 | 0 | if(getEdgeType(edge) == EdgeType.DIRECTED) { |
| 98 | 0 | succs.add(this.getDest(edge)); |
| 99 | |
} else { |
| 100 | 0 | succs.add(getOpposite(vertex, edge)); |
| 101 | |
} |
| 102 | |
} |
| 103 | 0 | return Collections.unmodifiableCollection(succs); |
| 104 | |
} |
| 105 | |
|
| 106 | |
@Override |
| 107 | |
public Collection<V> getNeighbors(V vertex) |
| 108 | |
{ |
| 109 | 0 | if (!containsVertex(vertex)) |
| 110 | 0 | return null; |
| 111 | |
|
| 112 | 0 | Collection<V> out = new LinkedHashSet<V>(); |
| 113 | 0 | out.addAll(this.getPredecessors(vertex)); |
| 114 | 0 | out.addAll(this.getSuccessors(vertex)); |
| 115 | 0 | return out; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public Collection<E> getIncidentEdges(V vertex) |
| 120 | |
{ |
| 121 | 44 | if (!containsVertex(vertex)) |
| 122 | 0 | return null; |
| 123 | |
|
| 124 | 44 | Collection<E> out = new LinkedHashSet<E>(); |
| 125 | 44 | out.addAll(this.getInEdges(vertex)); |
| 126 | 44 | out.addAll(this.getOutEdges(vertex)); |
| 127 | 44 | return out; |
| 128 | |
} |
| 129 | |
} |