Advertisement
Guest User

Untitled

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