Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- /**
- *
- * @author Oleg
- */
- class DFS {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- }
- private static int length;
- public static int findWay(Graph g, Graph.Vertex start, Graph.Vertex end) {
- length = 0;
- List<Graph.Vertex> way = new ArrayList<Graph.Vertex>();
- way.add(start);
- nextStep(g,start,end,way);
- return --length;
- }
- private static void nextStep(Graph g, Graph.Vertex start, Graph.Vertex end, List<Graph.Vertex> way) {
- Set<Graph.Vertex> neighbors = g.getNeighbors(start);
- for(Graph.Vertex neighbor : neighbors) {
- if(way.contains(neighbor))
- continue;
- way.add(neighbor);
- if(neighbor.equals(end) && (length == 0 || length > way.size()))
- length = way.size();
- nextStep(g,neighbor,end,way);
- way.remove(way.size() - 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement