Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. package dfs;
  2.  
  3. import java.util.Set;
  4. /**
  5. *
  6. * @author Oleg
  7. */
  8. public class DFS {
  9.  
  10. /**
  11. * @param args the command line arguments
  12. */
  13. public static void main(String[] args) {
  14.  
  15. }
  16.  
  17. public static int findWay(Graph g, Vertex start, Vertex end)
  18. {
  19. Set<Vertex> old = new Set<Vertex>();
  20. int length = nextStep(start, old, 0);
  21. return length;
  22. }
  23.  
  24. public static int nextStep(Vertex v, Set<Vertex> old, int length) {
  25. int maxLength = length + 1;
  26. for (Vertex neighbor : v.getNeighbors()) {
  27. if (neighbor.equals(end)) {
  28. return maxLength + 1;
  29. }
  30. if (!old.contains(neighbor)) {
  31. old.add(neighbor);
  32. int l = nextStep(neighbor);
  33. if (l > maxLength) {
  34. maxLength = l;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement