Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Basis for various graph path search algorithm implementations. |
| 29 | * |
| 30 | * @param <V> vertex type |
| 31 | * @param <E> edge type |
| 32 | */ |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | public abstract class AbstractGraphPathSearch<V extends Vertex, E extends Edge<V>> |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 34 | implements GraphPathSearch<V, E> { |
| 35 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Default path search result that uses the DefaultPath to convey paths |
| 38 | * in a graph. |
| 39 | */ |
| 40 | protected class DefaultResult implements Result<V, E> { |
| 41 | |
| 42 | private final V src; |
| 43 | private final V dst; |
| 44 | protected final Set<Path<V, E>> paths = new HashSet<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 45 | protected final Map<V, Weight> costs = new HashMap<>(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 46 | protected final Map<V, Set<E>> parents = new HashMap<>(); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 47 | protected final int maxPaths; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 50 | * Creates the result of a single-path search. |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 51 | * |
| 52 | * @param src path source |
| 53 | * @param dst optional path destination |
| 54 | */ |
| 55 | public DefaultResult(V src, V dst) { |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 56 | this(src, dst, 1); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates the result of path search. |
| 61 | * |
| 62 | * @param src path source |
| 63 | * @param dst optional path destination |
| 64 | * @param maxPaths optional limit of number of paths; |
| 65 | * {@link GraphPathSearch#ALL_PATHS} if no limit |
| 66 | */ |
| 67 | public DefaultResult(V src, V dst, int maxPaths) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | checkNotNull(src, "Source cannot be null"); |
| 69 | this.src = src; |
| 70 | this.dst = dst; |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 71 | this.maxPaths = maxPaths; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public V src() { |
| 76 | return src; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public V dst() { |
| 81 | return dst; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Set<Path<V, E>> paths() { |
| 86 | return paths; |
| 87 | } |
| 88 | |
| 89 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 90 | public Map<V, Weight> costs() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 91 | return costs; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Map<V, Set<E>> parents() { |
| 96 | return parents; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Indicates whether or not the given vertex has a cost yet. |
| 101 | * |
| 102 | * @param v vertex to test |
| 103 | * @return true if the vertex has cost already |
| 104 | */ |
| 105 | boolean hasCost(V v) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 106 | return costs.containsKey(v); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the current cost to reach the specified vertex. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 111 | * If the vertex has not been accessed yet, it has no cost |
| 112 | * associated and null will be returned. |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 113 | * |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 114 | * @param v vertex to reach |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 115 | * @return weight cost to reach the vertex if already accessed; |
| 116 | * null otherwise |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 117 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 118 | Weight cost(V v) { |
| 119 | return costs.get(v); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Updates the cost of the vertex using its existing cost plus the |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 124 | * cost to traverse the specified edge. If the search is in single |
| 125 | * path mode, only one path will be accrued. |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 126 | * |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 127 | * @param vertex vertex to update |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 128 | * @param edge edge through which vertex is reached |
| 129 | * @param cost current cost to reach the vertex from the source |
| 130 | * @param replace true to indicate that any accrued edges are to be |
| 131 | * cleared; false to indicate that the edge should be |
| 132 | * added to the previously accrued edges as they yield |
| 133 | * the same cost |
| 134 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 135 | void updateVertex(V vertex, E edge, Weight cost, boolean replace) { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 136 | costs.put(vertex, cost); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 137 | if (edge != null) { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 138 | Set<E> edges = parents.get(vertex); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 139 | if (edges == null) { |
| 140 | edges = new HashSet<>(); |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 141 | parents.put(vertex, edges); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 142 | } |
| 143 | if (replace) { |
| 144 | edges.clear(); |
| 145 | } |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 146 | if (maxPaths == ALL_PATHS || edges.size() < maxPaths) { |
| 147 | edges.add(edge); |
| 148 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes the set of parent edges for the specified vertex. |
| 154 | * |
| 155 | * @param v vertex |
| 156 | */ |
| 157 | void removeVertex(V v) { |
| 158 | parents.remove(v); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * If possible, relax the specified edge using the supplied base cost |
| 163 | * and edge-weight function. |
| 164 | * |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 165 | * @param edge edge to be relaxed |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 166 | * @param cost base cost to reach the edge destination vertex |
| 167 | * @param ew optional edge weight function |
| 168 | * @param forbidNegatives if true negative values will forbid the link |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 169 | * @return true if the edge was relaxed; false otherwise |
| 170 | */ |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 171 | boolean relaxEdge(E edge, Weight cost, EdgeWeigher<V, E> ew, |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 172 | boolean... forbidNegatives) { |
Thomas Vachuska | 7b652ad | 2014-10-30 14:10:51 -0700 | [diff] [blame] | 173 | V v = edge.dst(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 174 | |
| 175 | Weight hopCost = ew.weight(edge); |
| 176 | if ((!hopCost.isViable()) || |
| 177 | (hopCost.isNegative() && forbidNegatives.length == 1 && forbidNegatives[0])) { |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 178 | return false; |
| 179 | } |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 180 | Weight newCost = cost.merge(hopCost); |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 181 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 182 | int compareResult = -1; |
| 183 | if (hasCost(v)) { |
| 184 | Weight oldCost = cost(v); |
| 185 | compareResult = newCost.compareTo(oldCost); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 186 | } |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 187 | |
| 188 | if (compareResult <= 0) { |
| 189 | updateVertex(v, edge, newCost, compareResult < 0); |
| 190 | } |
| 191 | return compareResult < 0; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Builds a set of paths for the specified src/dst vertex pair. |
| 196 | */ |
| 197 | protected void buildPaths() { |
| 198 | Set<V> destinations = new HashSet<>(); |
| 199 | if (dst == null) { |
| 200 | destinations.addAll(costs.keySet()); |
| 201 | } else { |
| 202 | destinations.add(dst); |
| 203 | } |
| 204 | |
| 205 | // Build all paths between the source and all requested destinations. |
| 206 | for (V v : destinations) { |
| 207 | // Ignore the source, if it is among the destinations. |
| 208 | if (!v.equals(src)) { |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 209 | buildAllPaths(this, src, v, maxPaths); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Builds a set of all paths between the source and destination using the |
| 218 | * graph search result by applying breadth-first search through the parent |
| 219 | * edges and vertex costs. |
| 220 | * |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 221 | * @param result graph search result |
| 222 | * @param src source vertex |
| 223 | * @param dst destination vertex |
| 224 | * @param maxPaths limit on the number of paths built; |
| 225 | * {@link GraphPathSearch#ALL_PATHS} if no limit |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 226 | */ |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 227 | private void buildAllPaths(DefaultResult result, V src, V dst, int maxPaths) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 228 | DefaultMutablePath<V, E> basePath = new DefaultMutablePath<>(); |
| 229 | basePath.setCost(result.cost(dst)); |
| 230 | |
| 231 | Set<DefaultMutablePath<V, E>> pendingPaths = new HashSet<>(); |
| 232 | pendingPaths.add(basePath); |
| 233 | |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 234 | while (!pendingPaths.isEmpty() && |
| 235 | (maxPaths == ALL_PATHS || result.paths.size() < maxPaths)) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 236 | Set<DefaultMutablePath<V, E>> frontier = new HashSet<>(); |
| 237 | |
| 238 | for (DefaultMutablePath<V, E> path : pendingPaths) { |
| 239 | // For each pending path, locate its first vertex since we |
| 240 | // will be moving backwards from it. |
| 241 | V firstVertex = firstVertex(path, dst); |
| 242 | |
| 243 | // If the first vertex is our expected source, we have reached |
| 244 | // the beginning, so add the this path to the result paths. |
| 245 | if (firstVertex.equals(src)) { |
| 246 | path.setCost(result.cost(dst)); |
| 247 | result.paths.add(new DefaultPath<>(path.edges(), path.cost())); |
| 248 | |
| 249 | } else { |
| 250 | // If we have not reached the beginning, i.e. the source, |
| 251 | // fetch the set of edges leading to the first vertex of |
| 252 | // this pending path; if there are none, abandon processing |
| 253 | // this path for good. |
| 254 | Set<E> firstVertexParents = result.parents.get(firstVertex); |
| 255 | if (firstVertexParents == null || firstVertexParents.isEmpty()) { |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // Now iterate over all the edges and for each of them |
| 260 | // cloning the current path and then insert that edge to |
| 261 | // the path and then add that path to the pending ones. |
| 262 | // When processing the last edge, modify the current |
| 263 | // pending path rather than cloning a new one. |
| 264 | Iterator<E> edges = firstVertexParents.iterator(); |
| 265 | while (edges.hasNext()) { |
| 266 | E edge = edges.next(); |
| 267 | boolean isLast = !edges.hasNext(); |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 268 | // Exclude any looping paths |
| 269 | if (!isInPath(edge, path)) { |
| 270 | DefaultMutablePath<V, E> pendingPath = isLast ? path : new DefaultMutablePath<>(path); |
| 271 | pendingPath.insertEdge(edge); |
| 272 | frontier.add(pendingPath); |
| 273 | } |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // All pending paths have been scanned so promote the next frontier |
| 279 | pendingPaths = frontier; |
| 280 | } |
| 281 | } |
| 282 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Indicates whether or not the specified edge source is already visited |
| 285 | * in the specified path. |
| 286 | * |
| 287 | * @param edge edge to test |
| 288 | * @param path path to test |
| 289 | * @return true if the edge.src() is a vertex in the path already |
| 290 | */ |
| 291 | private boolean isInPath(E edge, DefaultMutablePath<V, E> path) { |
| 292 | return path.edges().stream().anyMatch(e -> edge.src().equals(e.dst())); |
| 293 | } |
| 294 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 295 | // Returns the first vertex of the specified path. This is either the source |
| 296 | // of the first edge or, if there are no edges yet, the given destination. |
| 297 | private V firstVertex(Path<V, E> path, V dst) { |
| 298 | return path.edges().isEmpty() ? dst : path.edges().get(0).src(); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Checks the specified path search arguments for validity. |
| 303 | * |
| 304 | * @param graph graph; must not be null |
| 305 | * @param src source vertex; must not be null and belong to graph |
| 306 | * @param dst optional target vertex; must belong to graph |
| 307 | */ |
| 308 | protected void checkArguments(Graph<V, E> graph, V src, V dst) { |
| 309 | checkNotNull(graph, "Graph cannot be null"); |
| 310 | checkNotNull(src, "Source cannot be null"); |
| 311 | Set<V> vertices = graph.getVertexes(); |
| 312 | checkArgument(vertices.contains(src), "Source not in the graph"); |
| 313 | checkArgument(dst == null || vertices.contains(dst), |
| 314 | "Destination not in graph"); |
| 315 | } |
| 316 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 317 | @Override |
| 318 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, |
| 319 | EdgeWeigher<V, E> weigher, int maxPaths) { |
| 320 | checkArguments(graph, src, dst); |
| 321 | |
| 322 | return internalSearch(graph, src, dst, |
| 323 | weigher != null ? weigher : new DefaultEdgeWeigher<>(), |
| 324 | maxPaths); |
| 325 | } |
| 326 | |
| 327 | protected abstract Result<V, E> internalSearch(Graph<V, E> graph, V src, V dst, |
| 328 | EdgeWeigher<V, E> weigher, int maxPaths); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 329 | } |