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