Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 19 | package org.onlab.graph; |
| 20 | |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | /** |
| 25 | * Implementation of the BFS algorithm. |
| 26 | */ |
| 27 | public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> |
| 28 | extends AbstractGraphPathSearch<V, E> { |
| 29 | |
| 30 | @Override |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 31 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, |
| 32 | EdgeWeight<V, E> weight) { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | checkArguments(graph, src, dst); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 34 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 35 | // Prepare the graph result. |
| 36 | DefaultResult result = new DefaultResult(src, dst); |
| 37 | |
| 38 | // Setup the starting frontier with the source as the sole vertex. |
| 39 | Set<V> frontier = new HashSet<>(); |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 40 | result.updateVertex(src, null, 0.0, true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 41 | frontier.add(src); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 42 | |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 43 | boolean reachedEnd = false; |
| 44 | while (!reachedEnd && !frontier.isEmpty()) { |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 45 | // Prepare the next frontier. |
| 46 | Set<V> next = new HashSet<>(); |
| 47 | |
| 48 | // Visit all vertexes in the current frontier. |
| 49 | for (V vertex : frontier) { |
| 50 | double cost = result.cost(vertex); |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 51 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 52 | // Visit all egress edges of the current frontier vertex. |
| 53 | for (E edge : graph.getEdgesFrom(vertex)) { |
| 54 | V nextVertex = edge.dst(); |
| 55 | if (!result.hasCost(nextVertex)) { |
| 56 | // If this vertex has not been visited yet, update it. |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 57 | double newCost = cost + (weight == null ? 1.0 : weight.weight(edge)); |
| 58 | result.updateVertex(nextVertex, edge, newCost, true); |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 59 | // If we have reached our intended destination, bail. |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 60 | if (nextVertex.equals(dst)) { |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 61 | reachedEnd = true; |
| 62 | break; |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 63 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 64 | next.add(nextVertex); |
| 65 | } |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 66 | |
| 67 | if (reachedEnd) { |
| 68 | break; |
| 69 | } |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 70 | } |
| 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 | // Promote the next frontier. |
| 74 | frontier = next; |
| 75 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 76 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 77 | // Finally, but the paths on the search result and return. |
| 78 | result.buildPaths(); |
| 79 | return result; |
| 80 | } |
tom | c53fa0d | 2014-08-29 11:57:11 -0700 | [diff] [blame] | 81 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 82 | } |