1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| package metier;
/*
* Copyright (c) 2007 by L. Paul Chew.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Straightforward undirected graph implementation.
* Nodes are generic type N.
*
* @author Paul Chew
*
* Created November, December 2007. For use in Delaunay/Voronoi code.
*
*/
public class Graph<N> {
private Map<N, Set<N>> theNeighbors = // Node -> adjacent nodes
new HashMap<N, Set<N>>();
private Set<N> theNodeSet = // Set view of all nodes
Collections.unmodifiableSet(theNeighbors.keySet());
/**
* Add a node. If node is already in graph then no change.
* @param node the node to add
*/
public void add (N node) {
if (theNeighbors.containsKey(node)) return;
theNeighbors.put(node, new ArraySet<N>());
}
/**
* Add a link. If the link is already in graph then no change.
* @param nodeA one end of the link
* @param nodeB the other end of the link
* @throws NullPointerException if either endpoint is not in graph
*/
public void add (N nodeA, N nodeB) throws NullPointerException {
theNeighbors.get(nodeA).add(nodeB);
theNeighbors.get(nodeB).add(nodeA);
}
/**
* Remove node and any links that use node. If node not in graph, nothing
* happens.
* @param node the node to remove.
*/
public void remove (N node) {
if (!theNeighbors.containsKey(node)) return;
for (N neighbor: theNeighbors.get(node))
theNeighbors.get(neighbor).remove(node); // Remove "to" links
theNeighbors.get(node).clear(); // Remove "from" links
theNeighbors.remove(node); // Remove the node
}
/**
* Remove the specified link. If link not in graph, nothing happens.
* @param nodeA one end of the link
* @param nodeB the other end of the link
* @throws NullPointerException if either endpoint is not in graph
*/
public void remove (N nodeA, N nodeB) throws NullPointerException {
theNeighbors.get(nodeA).remove(nodeB);
theNeighbors.get(nodeB).remove(nodeA);
}
/**
* Report all the neighbors of node.
* @param node the node
* @return the neighbors of node
* @throws NullPointerException if node does not appear in graph
*/
public Set<N> neighbors (N node) throws NullPointerException {
return Collections.unmodifiableSet(theNeighbors.get(node));
}
/**
* Returns an unmodifiable Set view of the nodes contained in this graph.
* The set is backed by the graph, so changes to the graph are reflected in
* the set.
* @return a Set view of the graph's node set
*/
public Set<N> nodeSet () {
return theNodeSet;
}
} |
Partager