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 | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import java.util.HashSet; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | /** |
| 22 | * Implementation of the BFS algorithm. |
| 23 | */ |
| 24 | public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> |
| 25 | extends AbstractGraphPathSearch<V, E> { |
| 26 | |
| 27 | @Override |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 28 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 29 | EdgeWeight<V, E> weight, int maxPaths) { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 30 | checkArguments(graph, src, dst); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 31 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 32 | // Prepare the graph result. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 33 | DefaultResult result = new DefaultResult(src, dst, maxPaths); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 34 | |
| 35 | // Setup the starting frontier with the source as the sole vertex. |
| 36 | Set<V> frontier = new HashSet<>(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 37 | result.updateVertex(src, null, 0.0, true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 38 | frontier.add(src); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 39 | |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 40 | boolean reachedEnd = false; |
| 41 | while (!reachedEnd && !frontier.isEmpty()) { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 42 | // Prepare the next frontier. |
| 43 | Set<V> next = new HashSet<>(); |
| 44 | |
| 45 | // Visit all vertexes in the current frontier. |
| 46 | for (V vertex : frontier) { |
| 47 | double cost = result.cost(vertex); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 48 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 49 | // Visit all egress edges of the current frontier vertex. |
| 50 | for (E edge : graph.getEdgesFrom(vertex)) { |
| 51 | V nextVertex = edge.dst(); |
| 52 | if (!result.hasCost(nextVertex)) { |
| 53 | // If this vertex has not been visited yet, update it. |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 54 | double newCost = cost + (weight == null ? 1.0 : weight.weight(edge)); |
| 55 | result.updateVertex(nextVertex, edge, newCost, true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 56 | // If we have reached our intended destination, bail. |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 57 | if (nextVertex.equals(dst)) { |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 58 | reachedEnd = true; |
| 59 | break; |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 60 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 61 | next.add(nextVertex); |
| 62 | } |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 63 | |
| 64 | if (reachedEnd) { |
| 65 | break; |
| 66 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 69 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 70 | // Promote the next frontier. |
| 71 | frontier = next; |
| 72 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 73 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 74 | // Finally, but the paths on the search result and return. |
| 75 | result.buildPaths(); |
| 76 | return result; |
| 77 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 78 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 79 | } |