blob: db8402d0182dc3dc9c8af13a639772d27473570e [file] [log] [blame]
weibit0d0ef612014-11-03 14:39:25 -08001/*
alshabibab984662014-12-04 18:56:18 -08002 * Copyright 2014 Open Networking Laboratory
weibit0d0ef612014-11-03 14:39:25 -08003 *
alshabibab984662014-12-04 18:56:18 -08004 * 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
weibit0d0ef612014-11-03 14:39:25 -08007 *
alshabibab984662014-12-04 18:56:18 -08008 * 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.
weibit0d0ef612014-11-03 14:39:25 -080015 */
16package org.onlab.graph;
17
18import java.util.ArrayList;
19//import java.util.HashMap;
20import java.util.Iterator;
21import java.util.List;
22//import java.util.Map;
23//import java.util.PriorityQueue;
24import java.util.Set;
25
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080026import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
27
weibit0d0ef612014-11-03 14:39:25 -080028/**
29 * K-shortest-path graph search algorithm capable of finding not just one,
weibit6c415d12014-11-03 19:15:42 -080030 * but K shortest paths with ascending order between the source and destinations.
weibit0d0ef612014-11-03 14:39:25 -080031 */
32
33public 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;
49 mutableGraph = new MutableAdjacencyListsGraph(graph.getVertexes(),
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++) {
weibit6c415d12014-11-03 19:15:42 -0800112 // 3.1.1 convert the graph into mutable.
weibit0d0ef612014-11-03 14:39:25 -0800113 convertGraph();
weibit6c415d12014-11-03 19:15:42 -0800114 // 3.1.2 transform the graph.
weibit0d0ef612014-11-03 14:39:25 -0800115 List<E> rootPath = createSpurNode(lastPath, i);
116 transformGraph(rootPath);
weibit6c415d12014-11-03 19:15:42 -0800117 // 3.1.3 find the deviation node.
weibit0d0ef612014-11-03 14:39:25 -0800118 V devNode;
119 devNode = getDevNode(rootPath);
120 List<E> spurPath;
weibit6c415d12014-11-03 19:15:42 -0800121 // 3.1.4 find the shortest path in the transformed graph.
weibit0d0ef612014-11-03 14:39:25 -0800122 spurPath = searchShortestPath(mutableGraph, devNode, sink);
weibit6c415d12014-11-03 19:15:42 -0800123 // 3.1.5 update the path candidates.
weibit0d0ef612014-11-03 14:39:25 -0800124 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
139 private List<E> searchShortestPath(Graph<V, E> graph, V src, V dst) {
140 // Determine the shortest path from the source to the destination by using the Dijkstra algorithm.
141 DijkstraGraphSearch dijkstraAlg = new DijkstraGraphSearch();
Thomas Vachuskac31d9f12015-01-22 12:33:27 -0800142 Set<Path> paths = dijkstraAlg.search(graph, src, dst, weight, ALL_PATHS).paths();
weibit0d0ef612014-11-03 14:39:25 -0800143 Iterator<Path> itr = paths.iterator();
144 if (!itr.hasNext()) {
145 return null;
146 }
147 // return the first shortest path only.
148 return (List<E>) itr.next().edges();
149 }
150
151 private void convertGraph() {
152 // clear the mutableGraph first
153 if (mutableGraph != null) {
154 ((MutableAdjacencyListsGraph) mutableGraph).clear();
155 }
156
157 // create a immutableGraph
158 Set<E> copyEa = immutableGraph.getEdges();
159 Set<V> copyVa = immutableGraph.getVertexes();
160 for (V vertex : copyVa) {
161 mutableGraph.addVertex(vertex);
162 }
163 for (E edge : copyEa) {
164 mutableGraph.addEdge(edge);
165 }
166 }
167
168 private V getDevNode(List<E> path) {
169 V srcA;
170 V dstB;
171
172 if (path.size() == 0) {
173 return source;
174 }
175
176 E temp1 = path.get(path.size() - 1);
177 srcA = temp1.src();
178 dstB = temp1.dst();
179
180 if (path.size() == 1) {
181 if (srcA.equals(source)) {
182 return dstB;
183 } else {
184 return srcA;
185 }
186 } else {
187 E temp2 = path.get(path.size() - 2);
188 if (srcA.equals(temp2.src()) || srcA.equals(temp2.dst())) {
189 return dstB;
190 } else {
191 return srcA;
192 }
193 }
194 }
195
196 private List<E> createSpurNode(List<E> path, int n) {
197 List<E> root = new ArrayList<E>();
198
199 for (int i = 0; i < n; i++) {
200 root.add(path.get(i));
201 }
202 return root;
203 }
204
205 private void transformGraph(List<E> rootPath) {
206 List<E> prePath;
207 //remove edges
208 for (int i = 0; i < pathResults.size(); i++) {
209 prePath = pathResults.get(i);
210 if (prePath.size() == 1) {
211 mutableGraph.removeEdge(prePath.get(0));
212 } else if (comparePath(rootPath, prePath)) {
213 for (int j = 0; j <= rootPath.size(); j++) {
214 mutableGraph.removeEdge(prePath.get(j));
215 }
216 }
217 }
218 for (int i = 0; i < pathCandidates.size(); i++) {
219 prePath = pathCandidates.get(i);
220 if (prePath.size() == 1) {
221 mutableGraph.removeEdge(prePath.get(0));
222 } else if (comparePath(rootPath, prePath)) {
223 for (int j = 0; j <= rootPath.size(); j++) {
224 mutableGraph.removeEdge(prePath.get(j));
225 }
226 }
227 }
228
229 if (rootPath.size() == 0) {
230 return;
231 }
232
233 //remove nodes
234 List<V> nodes = new ArrayList<V>();
235 nodes.add(source);
236 V pre = source;
237 V srcA;
238 V dstB;
239 for (int i = 0; i < rootPath.size() - 1; i++) {
240 E temp = rootPath.get(i);
241 srcA = temp.src();
242 dstB = temp.dst();
243
244 if (srcA.equals(pre)) {
245 nodes.add(dstB);
246 pre = dstB;
247 } else {
248 nodes.add(srcA);
249 pre = srcA;
250 }
251 }
252 for (int i = 0; i < nodes.size(); i++) {
253 mutableGraph.removeVertex(nodes.get(i));
254 }
255 }
256
257 private boolean comparePath(List<E> path1, List<E> path2) {
258 if (path1.size() > path2.size()) {
259 return false;
260 }
261 if (path1.size() == 0) {
262 return true;
263 }
264 for (int i = 0; i < path1.size(); i++) {
265 if (path1.get(i) != path2.get(i)) {
266 return false;
267 }
268 }
269 return true;
270 }
271
272 private void addPathResult() {
273 List<E> sp;
274 sp = pathCandidates.get(0);
275 for (int i = 1; i < pathCandidates.size(); i++) {
276 if (sp.size() > pathCandidates.get(i).size()) {
277 sp = pathCandidates.get(i);
278 }
279 }
280 pathResults.add(sp);
281 // Log.info(sp.toString());
282 pathCandidates.remove(sp);
283 }
284
285}