Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public DepthFirstSearch(Graph g, int rootVertex)
  2. {
  3. checked = new boolean[g.verticesInGraph()];
  4. exploreFromVertex(rootVertex, g);
  5. }
  6.  
  7.  
  8. //Go through the graph from vertex vertex until all connected vertices are explored
  9. private void exploreFromVertex(int vertex, Graph myGraph)
  10. {
  11. checked[vertex] = true; //Mark the vertex as checked or explored
  12.  
  13. for(int x : myGraph.getVerticesConnectedTo(vertex))
  14. {
  15. if(!checked(x))
  16. {
  17. //Call this method recursively if node is not checked
  18. exploreFromVertex(x, myGraph);
  19. }
  20. }
  21. }
  22.  
  23. public boolean checked(int vertex)
  24. {
  25. return checked[vertex];
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement