Advertisement
Shailrshah

Depth Limited Search

Oct 16th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6. class Neighbor {
  7.     public int vertexNum;
  8.     public Neighbor next;
  9.     public Neighbor(int vnum, Neighbor nbr) {
  10.             this.vertexNum = vnum;
  11.             next = nbr;
  12.     }
  13. }
  14.  
  15. class Vertex {
  16.     String name;
  17.     Neighbor adjList;
  18.     Vertex(String name, Neighbor neighbors) {
  19.             this.name = name;
  20.             this.adjList = neighbors;
  21.     }
  22. }
  23.  
  24.  
  25. class Graph {
  26.  
  27.     Vertex[] adjLists;
  28.     boolean done = false;
  29.      
  30.     public Graph(String file) throws FileNotFoundException {
  31.          
  32.         Scanner sc = new Scanner(new File(file));
  33.          
  34.         String graphType = sc.next();
  35.         boolean undirected=true;
  36.         if (graphType.equals("directed")) {
  37.             undirected=false;
  38.         }
  39.          
  40.         adjLists = new Vertex[sc.nextInt()];
  41.  
  42.         // read vertices
  43.         for (int v=0; v < adjLists.length; v++) {
  44.             adjLists[v] = new Vertex(sc.next(), null);
  45.         }
  46.  
  47.         // read edges
  48.         while (sc.hasNext()) {
  49.              
  50.             // read vertex names and translate to vertex numbers
  51.             int v1 = indexForName(sc.next());
  52.             int v2 = indexForName(sc.next());
  53.              
  54.             // add v2 to front of v1's adjacency list and
  55.             // add v1 to front of v2's adjacency list
  56.             adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);
  57.             if (undirected) {
  58.                 adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);
  59.             }
  60.         }
  61.     }
  62.      
  63.     int indexForName(String name) {
  64.         for (int v=0; v < adjLists.length; v++) {
  65.             if (adjLists[v].name.equals(name)) {
  66.                 return v;
  67.             }
  68.         }
  69.         return -1;
  70.     }  
  71.      
  72.     // recursive dfs
  73.     private void dfs(int v, boolean[] visited, String dest, int limit, int height, String path) {
  74.         visited[v] = true;
  75.         if(v==indexForName(dest)){
  76.             System.out.println("Destination Found!");
  77.             done = true;
  78.         }
  79.         if(height>limit||done == true)
  80.             return;
  81.         //System.out.println("visiting " + adjLists[v].name+"\theight = "+height);
  82.         for (Neighbor nbr=adjLists[v].adjList; nbr != null && done!=true; nbr=nbr.next) {
  83.             if (!visited[nbr.vertexNum]) {
  84.                 System.out.println("\n"+path+"-->"+adjLists[nbr.vertexNum].name+" (h="+height+")");
  85.                 dfs(nbr.vertexNum, visited, dest, limit, height+1,path+"-->"+adjLists[nbr.vertexNum].name);
  86.             }
  87.         }
  88.     }
  89.      
  90.     public void dfs(String src, String dest, int limit) {
  91.         boolean[] visited = new boolean[adjLists.length];
  92.         System.out.println("\nStarting at " + src);
  93.         dfs(indexForName(src), visited, dest, limit, 1, src);
  94.         if(!visited[indexForName(dest)])
  95.             System.out.println("Destination Not Found. Try to increase the limit.");
  96.     }
  97.      
  98.     public static void main(String[] args)
  99.     throws IOException {
  100.         // TODO Auto-generated method stub
  101.         Scanner sc = new Scanner(System.in);
  102.         System.out.print("Enter graph input file name: ");
  103.         String file = sc.nextLine();
  104.         System.out.print("Enter the source, destination and the limit: ");
  105.         String src = sc.next(); String dest = sc.next(); int limit = sc.nextInt();
  106.         Graph graph = new Graph(file);
  107.         //graph.print();
  108.         graph.dfs(src, dest, limit);
  109.     }
  110. }
  111.  
  112. /*ip.txt*/
  113. undirected
  114. 14
  115. 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  116.  
  117. 1 2
  118. 1 6
  119. 2 3
  120. 3 5
  121. 3 7
  122. 4 5
  123. 5 6
  124. 7 8
  125. 7 9
  126. 8 10
  127. 8 11
  128. 9 11
  129. 9 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement