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 |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 28 | protected Result<V, E> internalSearch(Graph<V, E> graph, V src, V dst, |
| 29 | EdgeWeigher<V, E> weigher, int maxPaths) { |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 30 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 31 | // Prepare the graph result. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 32 | DefaultResult result = new DefaultResult(src, dst, maxPaths); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | |
| 34 | // Setup the starting frontier with the source as the sole vertex. |
| 35 | Set<V> frontier = new HashSet<>(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 36 | result.updateVertex(src, null, weigher.getInitialWeight(), true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 37 | frontier.add(src); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 38 | |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 39 | boolean reachedEnd = false; |
| 40 | while (!reachedEnd && !frontier.isEmpty()) { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 41 | // Prepare the next frontier. |
| 42 | Set<V> next = new HashSet<>(); |
| 43 | |
| 44 | // Visit all vertexes in the current frontier. |
| 45 | for (V vertex : frontier) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 46 | Weight cost = result.cost(vertex); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 47 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 48 | // Visit all egress edges of the current frontier vertex. |
| 49 | for (E edge : graph.getEdgesFrom(vertex)) { |
| 50 | V nextVertex = edge.dst(); |
| 51 | if (!result.hasCost(nextVertex)) { |
| 52 | // If this vertex has not been visited yet, update it. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 53 | Weight newCost = cost.merge(weigher.weight(edge)); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 54 | result.updateVertex(nextVertex, edge, newCost, true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 55 | // If we have reached our intended destination, bail. |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 56 | if (nextVertex.equals(dst)) { |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 57 | reachedEnd = true; |
| 58 | break; |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 59 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 60 | next.add(nextVertex); |
| 61 | } |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 62 | |
| 63 | if (reachedEnd) { |
| 64 | break; |
| 65 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 68 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 69 | // Promote the next frontier. |
| 70 | frontier = next; |
| 71 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 72 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 73 | // Finally, but the paths on the search result and return. |
| 74 | result.buildPaths(); |
| 75 | return result; |
| 76 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 77 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 78 | } |