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