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