Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void WidthFirstSearch(MatrixGraph g, int from, int to)
- {
- boolean[] visited = new boolean[g.numVertices()];
- LinkedList<Vertex> queue = new LinkedList<Vertex>();
- visited[from] = true;
- Vertex first = new Vertex(from, null);
- queue.addLast(first);
- while(!queue.isEmpty())
- {
- Vertex a = queue.removeFirst();
- VertexIterator vi = g.adjacentVertices(Vertex.getValue());
- while(vi.hasNext())
- {
- int i = vi.next();
- if(!visited[i])
- {
- queue.addLast(new Vertex(i, a));
- visited[i] = true;
- }
- }
- }
- }
- public void action(int a)
- {
- System.out.print(a + " ");
- }
- private class Vertex
- {
- int value;
- Vertex parent;
- private Vertex(int v, Vertex p)
- {
- value = v;
- parent = p;
- }
- public int getValue()
- {
- return value;
- }
- }
- }
Add Comment
Please, Sign In to add comment