weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 3 | * |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [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 |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 7 | * |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [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. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 15 | */ |
| 16 | package org.onlab.graph; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | //import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | //import java.util.Map; |
| 23 | //import java.util.PriorityQueue; |
| 24 | import java.util.Set; |
| 25 | |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 26 | import static org.onlab.graph.GraphPathSearch.ALL_PATHS; |
| 27 | |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 28 | /** |
| 29 | * K-shortest-path graph search algorithm capable of finding not just one, |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 30 | * but K shortest paths with ascending order between the source and destinations. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | public class KshortestPathSearch<V extends Vertex, E extends Edge<V>> { |
| 34 | |
| 35 | // Define class variables. |
| 36 | private Graph<V, E> immutableGraph; |
| 37 | private MutableGraph<V, E> mutableGraph; |
| 38 | private List<List<E>> pathResults = new ArrayList<List<E>>(); |
| 39 | private List<List<E>> pathCandidates = new ArrayList<List<E>>(); |
| 40 | private V source; |
| 41 | private V sink; |
| 42 | private int numK = 0; |
| 43 | private EdgeWeight<V, E> weight = null; |
| 44 | // private PriorityQueue<List<E>> pathCandidates = new PriorityQueue<List<E>>(); |
| 45 | |
| 46 | // Initialize the graph. |
| 47 | public KshortestPathSearch(Graph<V, E> graph) { |
| 48 | immutableGraph = graph; |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 49 | mutableGraph = new MutableAdjacencyListsGraph<>(graph.getVertexes(), |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 50 | graph.getEdges()); |
| 51 | } |
| 52 | |
| 53 | public List<List<E>> search(V src, |
| 54 | V dst, |
| 55 | EdgeWeight<V, E> wei, |
| 56 | int k) { |
| 57 | |
| 58 | weight = wei; |
| 59 | source = src; |
| 60 | sink = dst; |
| 61 | numK = k; |
| 62 | // pathCandidates = new PriorityQueue<List<E>>(); |
| 63 | |
| 64 | pathResults.clear(); |
| 65 | pathCandidates.clear(); |
| 66 | |
| 67 | // Double check the parameters |
| 68 | checkArguments(immutableGraph, src, dst, numK); |
| 69 | |
| 70 | // DefaultResult result = new DefaultResult(src, dst); |
| 71 | |
| 72 | searchKShortestPaths(); |
| 73 | |
| 74 | return pathResults; |
| 75 | } |
| 76 | |
| 77 | private void checkArguments(Graph<V, E> graph, V src, V dst, int k) { |
| 78 | if (graph == null) { |
| 79 | throw new NullPointerException("graph is null"); |
| 80 | } |
| 81 | if (!graph.getVertexes().contains(src)) { |
| 82 | throw new NullPointerException("source node does not exist"); |
| 83 | } |
| 84 | if (!graph.getVertexes().contains(dst)) { |
| 85 | throw new NullPointerException("target node does not exist"); |
| 86 | } |
| 87 | if (k <= 0) { |
| 88 | throw new NullPointerException("K is negative or 0"); |
| 89 | } |
| 90 | if (weight == null) { |
| 91 | throw new NullPointerException("the cost matrix is null"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private void searchKShortestPaths() { |
| 96 | // Step 1: find the shortest path. |
| 97 | List<E> shortestPath = searchShortestPath(immutableGraph, source, sink); |
| 98 | // no path exists, exit. |
| 99 | if (shortestPath == null) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Step 2: update the results. |
| 104 | pathResults.add(shortestPath); |
| 105 | // pathCandidates.add(shortestPath); |
| 106 | |
| 107 | // Step 3: find the other K-1 paths. |
| 108 | while (/*pathCandidates.size() > 0 &&*/pathResults.size() < numK) { |
| 109 | // 3.1 the spur node ranges from the first node to the last node in the previous k-shortest path. |
| 110 | List<E> lastPath = pathResults.get(pathResults.size() - 1); |
| 111 | for (int i = 0; i < lastPath.size(); i++) { |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 112 | // 3.1.1 convert the graph into mutable. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 113 | convertGraph(); |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 114 | // 3.1.2 transform the graph. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 115 | List<E> rootPath = createSpurNode(lastPath, i); |
| 116 | transformGraph(rootPath); |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 117 | // 3.1.3 find the deviation node. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 118 | V devNode; |
| 119 | devNode = getDevNode(rootPath); |
| 120 | List<E> spurPath; |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 121 | // 3.1.4 find the shortest path in the transformed graph. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 122 | spurPath = searchShortestPath(mutableGraph, devNode, sink); |
weibit | 6c415d1 | 2014-11-03 19:15:42 -0800 | [diff] [blame] | 123 | // 3.1.5 update the path candidates. |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 124 | if (spurPath != null) { |
| 125 | // totalPath = rootPath + spurPath; |
| 126 | rootPath.addAll(spurPath); |
| 127 | pathCandidates.add(rootPath); |
| 128 | } |
| 129 | } |
| 130 | // 3.2 if there is no spur path, exit. |
| 131 | if (pathCandidates.size() == 0) { |
| 132 | break; |
| 133 | } |
| 134 | // 3.3 add the path into the results. |
| 135 | addPathResult(); |
| 136 | } |
| 137 | } |
| 138 | |
Ray Milkey | 8dc8208 | 2015-02-20 16:22:38 -0800 | [diff] [blame] | 139 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 140 | private List<E> searchShortestPath(Graph<V, E> graph, V src, V dst) { |
| 141 | // Determine the shortest path from the source to the destination by using the Dijkstra algorithm. |
| 142 | DijkstraGraphSearch dijkstraAlg = new DijkstraGraphSearch(); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 143 | Set<Path> paths = dijkstraAlg.search(graph, src, dst, weight, ALL_PATHS).paths(); |
weibit | 0d0ef61 | 2014-11-03 14:39:25 -0800 | [diff] [blame] | 144 | Iterator<Path> itr = paths.iterator(); |
| 145 | if (!itr.hasNext()) { |
| 146 | return null; |
| 147 | } |
| 148 | // return the first shortest path only. |
| 149 | return (List<E>) itr.next().edges(); |
| 150 | } |
| 151 | |
| 152 | private void convertGraph() { |
| 153 | // clear the mutableGraph first |
| 154 | if (mutableGraph != null) { |
| 155 | ((MutableAdjacencyListsGraph) mutableGraph).clear(); |
| 156 | } |
| 157 | |
| 158 | // create a immutableGraph |
| 159 | Set<E> copyEa = immutableGraph.getEdges(); |
| 160 | Set<V> copyVa = immutableGraph.getVertexes(); |
| 161 | for (V vertex : copyVa) { |
| 162 | mutableGraph.addVertex(vertex); |
| 163 | } |
| 164 | for (E edge : copyEa) { |
| 165 | mutableGraph.addEdge(edge); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private V getDevNode(List<E> path) { |
| 170 | V srcA; |
| 171 | V dstB; |
| 172 | |
| 173 | if (path.size() == 0) { |
| 174 | return source; |
| 175 | } |
| 176 | |
| 177 | E temp1 = path.get(path.size() - 1); |
| 178 | srcA = temp1.src(); |
| 179 | dstB = temp1.dst(); |
| 180 | |
| 181 | if (path.size() == 1) { |
| 182 | if (srcA.equals(source)) { |
| 183 | return dstB; |
| 184 | } else { |
| 185 | return srcA; |
| 186 | } |
| 187 | } else { |
| 188 | E temp2 = path.get(path.size() - 2); |
| 189 | if (srcA.equals(temp2.src()) || srcA.equals(temp2.dst())) { |
| 190 | return dstB; |
| 191 | } else { |
| 192 | return srcA; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private List<E> createSpurNode(List<E> path, int n) { |
| 198 | List<E> root = new ArrayList<E>(); |
| 199 | |
| 200 | for (int i = 0; i < n; i++) { |
| 201 | root.add(path.get(i)); |
| 202 | } |
| 203 | return root; |
| 204 | } |
| 205 | |
| 206 | private void transformGraph(List<E> rootPath) { |
| 207 | List<E> prePath; |
| 208 | //remove edges |
| 209 | for (int i = 0; i < pathResults.size(); i++) { |
| 210 | prePath = pathResults.get(i); |
| 211 | if (prePath.size() == 1) { |
| 212 | mutableGraph.removeEdge(prePath.get(0)); |
| 213 | } else if (comparePath(rootPath, prePath)) { |
| 214 | for (int j = 0; j <= rootPath.size(); j++) { |
| 215 | mutableGraph.removeEdge(prePath.get(j)); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | for (int i = 0; i < pathCandidates.size(); i++) { |
| 220 | prePath = pathCandidates.get(i); |
| 221 | if (prePath.size() == 1) { |
| 222 | mutableGraph.removeEdge(prePath.get(0)); |
| 223 | } else if (comparePath(rootPath, prePath)) { |
| 224 | for (int j = 0; j <= rootPath.size(); j++) { |
| 225 | mutableGraph.removeEdge(prePath.get(j)); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (rootPath.size() == 0) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | //remove nodes |
| 235 | List<V> nodes = new ArrayList<V>(); |
| 236 | nodes.add(source); |
| 237 | V pre = source; |
| 238 | V srcA; |
| 239 | V dstB; |
| 240 | for (int i = 0; i < rootPath.size() - 1; i++) { |
| 241 | E temp = rootPath.get(i); |
| 242 | srcA = temp.src(); |
| 243 | dstB = temp.dst(); |
| 244 | |
| 245 | if (srcA.equals(pre)) { |
| 246 | nodes.add(dstB); |
| 247 | pre = dstB; |
| 248 | } else { |
| 249 | nodes.add(srcA); |
| 250 | pre = srcA; |
| 251 | } |
| 252 | } |
| 253 | for (int i = 0; i < nodes.size(); i++) { |
| 254 | mutableGraph.removeVertex(nodes.get(i)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | private boolean comparePath(List<E> path1, List<E> path2) { |
| 259 | if (path1.size() > path2.size()) { |
| 260 | return false; |
| 261 | } |
| 262 | if (path1.size() == 0) { |
| 263 | return true; |
| 264 | } |
| 265 | for (int i = 0; i < path1.size(); i++) { |
| 266 | if (path1.get(i) != path2.get(i)) { |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | private void addPathResult() { |
| 274 | List<E> sp; |
| 275 | sp = pathCandidates.get(0); |
| 276 | for (int i = 1; i < pathCandidates.size(); i++) { |
| 277 | if (sp.size() > pathCandidates.get(i).size()) { |
| 278 | sp = pathCandidates.get(i); |
| 279 | } |
| 280 | } |
| 281 | pathResults.add(sp); |
| 282 | // Log.info(sp.toString()); |
| 283 | pathCandidates.remove(sp); |
| 284 | } |
| 285 | |
| 286 | } |