Guest User

Untitled

a guest
Jan 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. public void WidthFirstSearch(MatrixGraph g, int from, int to)
  2. {
  3. boolean[] visited = new boolean[g.numVertices()];
  4. LinkedList<Vertex> queue = new LinkedList<Vertex>();
  5.  
  6. visited[from] = true;
  7. Vertex first = new Vertex(from, null);
  8. queue.addLast(first);
  9.  
  10. while(!queue.isEmpty())
  11. {
  12.  
  13. Vertex a = queue.removeFirst();
  14.  
  15. VertexIterator vi = g.adjacentVertices(Vertex.getValue());
  16. while(vi.hasNext())
  17. {
  18. int i = vi.next();
  19. if(!visited[i])
  20. {
  21. queue.addLast(new Vertex(i, a));
  22. visited[i] = true;
  23. }
  24. }
  25. }
  26.  
  27.  
  28. }
  29. public void action(int a)
  30. {
  31. System.out.print(a + " ");
  32. }
  33.  
  34. private class Vertex
  35. {
  36. int value;
  37. Vertex parent;
  38. private Vertex(int v, Vertex p)
  39. {
  40. value = v;
  41. parent = p;
  42. }
  43. public int getValue()
  44. {
  45. return value;
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment