Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.graph; |
| 18 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
| 20 | |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.Set; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.HashSet; |
| 27 | import java.util.stream.Collectors; |
| 28 | |
| 29 | /** |
| 30 | * Suurballe shortest-path graph search algorithm capable of finding both |
| 31 | * a shortest path, as well as a backup shortest path, between a source and a destination |
| 32 | * such that the sum of the path lengths is minimized. |
| 33 | */ |
| 34 | public class SuurballeGraphSearch<V extends Vertex, E extends Edge<V>> extends DijkstraGraphSearch<V, E> { |
| 35 | |
| 36 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 37 | protected Result<V, E> internalSearch(Graph<V, E> graph, V src, V dst, |
| 38 | EdgeWeigher<V, E> weigher, int maxPaths) { |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 39 | // FIXME: This method needs to be refactored as it is difficult to follow and debug. |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 40 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 41 | // FIXME: There is a defect here triggered by 3+ edges between the same vertices, |
| 42 | // which makes an attempt to produce looping paths. Protection against |
| 43 | // this was added to AbstractGraphPathSearch, but the root issue remains here. |
| 44 | |
| 45 | // FIXME: There is a defect here where not all paths are truly disjoint. |
| 46 | // This class needs to filter its own results to make sure that the |
| 47 | // paths are indeed disjoint. Temporary fix for this is provided, but |
| 48 | // the issue needs to be addressed through refactoring. |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 49 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 50 | EdgeWeigher weightf = weigher; |
| 51 | DefaultResult firstDijkstraS = (DefaultResult) super.internalSearch( |
| 52 | graph, src, dst, weigher, ALL_PATHS); |
| 53 | DefaultResult firstDijkstra = (DefaultResult) super.internalSearch( |
| 54 | graph, src, null, weigher, ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 55 | |
| 56 | //choose an arbitrary shortest path to run Suurballe on |
| 57 | Path<V, E> shortPath = null; |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 58 | if (firstDijkstraS.paths().isEmpty()) { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 59 | return firstDijkstraS; |
| 60 | } |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 61 | |
| 62 | DisjointPathResult result = new DisjointPathResult(firstDijkstra, src, dst, maxPaths); |
| 63 | |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 64 | for (Path p: firstDijkstraS.paths()) { |
| 65 | shortPath = p; |
| 66 | //transforms the graph so tree edges have 0 weight |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 67 | EdgeWeigher<V, E> modified = new EdgeWeigher<V, E>() { |
| 68 | @Override |
| 69 | public Weight weight(E edge) { |
| 70 | return edge instanceof ReverseEdge ? |
| 71 | weightf.getInitialWeight() : |
Thomas Vachuska | 3702f9b | 2018-03-19 15:21:50 -0700 | [diff] [blame] | 72 | (weightf.weight(edge).isNegative() ? |
| 73 | new ScalarWeight(-1.0) : |
| 74 | weightf.weight(edge).merge(firstDijkstra.cost(edge.src())) |
| 75 | .subtract(firstDijkstra.cost(edge.dst()))); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Weight getInitialWeight() { |
| 80 | return weightf.getInitialWeight(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public Weight getNonViableWeight() { |
| 85 | return weightf.getNonViableWeight(); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | EdgeWeigher<V, E> modified2 = new EdgeWeigher<V, E>() { |
| 90 | @Override |
| 91 | public Weight weight(E edge) { |
| 92 | return weightf.weight(edge).merge(firstDijkstra.cost(edge.src())) |
| 93 | .subtract(firstDijkstra.cost(edge.dst())); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Weight getInitialWeight() { |
| 98 | return weightf.getInitialWeight(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Weight getNonViableWeight() { |
| 103 | return weightf.getNonViableWeight(); |
| 104 | } |
| 105 | }; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 106 | |
| 107 | //create a residual graph g' by removing all src vertices and reversing 0 length path edges |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 108 | MutableGraph<V, E> gt = mutableCopy(graph); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 109 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 110 | Map<E, E> revToEdge = new HashMap<>(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 111 | graph.getEdgesTo(src).forEach(gt::removeEdge); |
| 112 | for (E edge: shortPath.edges()) { |
| 113 | gt.removeEdge(edge); |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 114 | Edge<V> reverse = new ReverseEdge<V>(edge); |
| 115 | revToEdge.put((E) reverse, edge); |
| 116 | gt.addEdge((E) reverse); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | //rerun dijkstra on the temporary graph to get a second path |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 120 | Result<V, E> secondDijkstra = new DijkstraGraphSearch<V, E>() |
| 121 | .search(gt, src, dst, modified, ALL_PATHS); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 122 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 123 | Path<V, E> residualShortPath = null; |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 124 | if (secondDijkstra.paths().isEmpty()) { |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 125 | result.dpps.add(new DisjointPathPair<>(shortPath, null)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | |
| 129 | for (Path p2: secondDijkstra.paths()) { |
| 130 | residualShortPath = p2; |
| 131 | |
| 132 | MutableGraph<V, E> roundTrip = mutableCopy(graph); |
| 133 | |
| 134 | List<E> tmp = roundTrip.getEdges().stream().collect(Collectors.toList()); |
| 135 | |
| 136 | tmp.forEach(roundTrip::removeEdge); |
| 137 | |
| 138 | shortPath.edges().forEach(roundTrip::addEdge); |
| 139 | |
| 140 | if (residualShortPath != null) { |
| 141 | for (Edge<V> edge: residualShortPath.edges()) { |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 142 | if (edge instanceof ReverseEdge) { |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 143 | roundTrip.removeEdge(revToEdge.get(edge)); |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 144 | } else { |
| 145 | roundTrip.addEdge((E) edge); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | //Actually build the final result |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 150 | DefaultResult lastSearch = (DefaultResult) |
| 151 | super.internalSearch(roundTrip, src, dst, weigher, ALL_PATHS); |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 152 | Path<V, E> primary = lastSearch.paths().iterator().next(); |
| 153 | primary.edges().forEach(roundTrip::removeEdge); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 154 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 155 | Set<Path<V, E>> backups = super.internalSearch(roundTrip, src, dst, |
| 156 | weigher, ALL_PATHS).paths(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 157 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 158 | // Find first backup path that does not share any nodes with the primary |
| 159 | for (Path<V, E> backup : backups) { |
| 160 | if (isDisjoint(primary, backup)) { |
| 161 | result.dpps.add(new DisjointPathPair<>(primary, backup)); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | } |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 165 | } |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | for (int i = result.dpps.size() - 1; i > 0; i--) { |
| 169 | if (result.dpps.get(i).size() <= 1) { |
| 170 | result.dpps.remove(i); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 171 | } |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | result.buildPaths(); |
| 175 | return result; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 178 | private boolean isDisjoint(Path<V, E> a, Path<V, E> b) { |
| 179 | return Sets.intersection(vertices(a), vertices(b)).isEmpty(); |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 182 | private Set<V> vertices(Path<V, E> p) { |
| 183 | Set<V> set = new HashSet<>(); |
| 184 | p.edges().forEach(e -> set.add(e.src())); |
| 185 | set.remove(p.src()); |
| 186 | return set; |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 187 | } |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 188 | |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Creates a mutable copy of an immutable graph. |
| 191 | * |
| 192 | * @param graph immutable graph |
| 193 | * @return mutable copy |
| 194 | */ |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 195 | private MutableGraph<V, E> mutableCopy(Graph<V, E> graph) { |
| 196 | return new MutableAdjacencyListsGraph<>(graph.getVertexes(), graph.getEdges()); |
| 197 | } |
| 198 | |
| 199 | private static final class ReverseEdge<V extends Vertex> extends AbstractEdge<V> { |
| 200 | private ReverseEdge(Edge<V> edge) { |
| 201 | super(edge.dst(), edge.src()); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String toString() { |
| 206 | return "ReversedEdge " + "src=" + src() + " dst=" + dst(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Auxiliary result for disjoint path search |
| 211 | private final class DisjointPathResult implements AbstractGraphPathSearch.Result<V, E> { |
| 212 | |
| 213 | private final Result<V, E> searchResult; |
| 214 | private final V src, dst; |
| 215 | private final int maxPaths; |
| 216 | private final List<DisjointPathPair<V, E>> dpps = new ArrayList<>(); |
| 217 | private final Set<Path<V, E>> disjointPaths = new HashSet<>(); |
| 218 | |
| 219 | private DisjointPathResult(Result<V, E> searchResult, V src, V dst, int maxPaths) { |
| 220 | this.searchResult = searchResult; |
| 221 | this.src = src; |
| 222 | this.dst = dst; |
| 223 | this.maxPaths = maxPaths; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public V src() { |
| 228 | return src; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public V dst() { |
| 233 | return dst; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Set<Path<V, E>> paths() { |
| 238 | return disjointPaths; |
| 239 | } |
| 240 | |
| 241 | private void buildPaths() { |
| 242 | int paths = 0; |
| 243 | for (DisjointPathPair<V, E> path: dpps) { |
| 244 | disjointPaths.add(path); |
| 245 | paths++; |
| 246 | if (paths == maxPaths) { |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Map<V, Set<E>> parents() { |
| 254 | return searchResult.parents(); |
| 255 | } |
| 256 | |
| 257 | @Override |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 258 | public Map<V, Weight> costs() { |
Thomas Vachuska | b1995cb | 2016-08-23 09:36:08 -0700 | [diff] [blame] | 259 | return searchResult.costs(); |
| 260 | } |
Nikhil Cheerla | f7c2e1a | 2015-07-09 12:06:37 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |