| 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.Pair; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
@SuppressWarnings("serial") |
| 30 | |
public class UndirectedOrderedSparseMultigraph<V,E> |
| 31 | |
extends UndirectedSparseMultigraph<V,E> |
| 32 | |
implements UndirectedGraph<V,E> { |
| 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 | 6 | public UndirectedGraph<V,E> create() { |
| 42 | 4 | return new UndirectedOrderedSparseMultigraph<V,E>(); |
| 43 | |
} |
| 44 | |
}; |
| 45 | |
} |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | 4 | public UndirectedOrderedSparseMultigraph() { |
| 51 | 4 | vertices = new LinkedHashMap<V, Set<E>>(); |
| 52 | 4 | edges = new LinkedHashMap<E, Pair<V>>(); |
| 53 | 4 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
public boolean addVertex(V vertex) { |
| 57 | 10 | if(vertex == null) { |
| 58 | 2 | throw new IllegalArgumentException("vertex may not be null"); |
| 59 | |
} |
| 60 | 8 | if (!containsVertex(vertex)) |
| 61 | |
{ |
| 62 | 6 | vertices.put(vertex, new LinkedHashSet<E>()); |
| 63 | 6 | return true; |
| 64 | |
} else { |
| 65 | 2 | return false; |
| 66 | |
} |
| 67 | |
} |
| 68 | |
|
| 69 | |
@Override |
| 70 | |
public Collection<V> getNeighbors(V vertex) { |
| 71 | 0 | if (!containsVertex(vertex)) |
| 72 | 0 | return null; |
| 73 | |
|
| 74 | 0 | Set<V> neighbors = new LinkedHashSet<V>(); |
| 75 | 0 | for (E edge : getIncident_internal(vertex)) |
| 76 | |
{ |
| 77 | 0 | Pair<V> endpoints = this.getEndpoints(edge); |
| 78 | 0 | V e_a = endpoints.getFirst(); |
| 79 | 0 | V e_b = endpoints.getSecond(); |
| 80 | 0 | if (vertex.equals(e_a)) |
| 81 | 0 | neighbors.add(e_b); |
| 82 | |
else |
| 83 | 0 | neighbors.add(e_a); |
| 84 | 0 | } |
| 85 | |
|
| 86 | 0 | return Collections.unmodifiableCollection(neighbors); |
| 87 | |
} |
| 88 | |
} |