Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.Scanner;
- class Neighbor {
- public int vertexNum;
- public Neighbor next;
- public Neighbor(int vnum, Neighbor nbr) {
- this.vertexNum = vnum;
- next = nbr;
- }
- }
- class Vertex {
- String name;
- Neighbor adjList;
- Vertex(String name, Neighbor neighbors) {
- this.name = name;
- this.adjList = neighbors;
- }
- }
- class Graph {
- Vertex[] adjLists;
- boolean done = false;
- public Graph(String file) throws FileNotFoundException {
- Scanner sc = new Scanner(new File(file));
- String graphType = sc.next();
- boolean undirected=true;
- if (graphType.equals("directed")) {
- undirected=false;
- }
- adjLists = new Vertex[sc.nextInt()];
- // read vertices
- for (int v=0; v < adjLists.length; v++) {
- adjLists[v] = new Vertex(sc.next(), null);
- }
- // read edges
- while (sc.hasNext()) {
- // read vertex names and translate to vertex numbers
- int v1 = indexForName(sc.next());
- int v2 = indexForName(sc.next());
- // add v2 to front of v1's adjacency list and
- // add v1 to front of v2's adjacency list
- adjLists[v1].adjList = new Neighbor(v2, adjLists[v1].adjList);
- if (undirected) {
- adjLists[v2].adjList = new Neighbor(v1, adjLists[v2].adjList);
- }
- }
- }
- int indexForName(String name) {
- for (int v=0; v < adjLists.length; v++) {
- if (adjLists[v].name.equals(name)) {
- return v;
- }
- }
- return -1;
- }
- // recursive dfs
- private void dfs(int v, boolean[] visited, String dest, int limit, int height, String path) {
- visited[v] = true;
- if(v==indexForName(dest)){
- System.out.println("Destination Found!");
- done = true;
- }
- if(height>limit||done == true)
- return;
- //System.out.println("visiting " + adjLists[v].name+"\theight = "+height);
- for (Neighbor nbr=adjLists[v].adjList; nbr != null && done!=true; nbr=nbr.next) {
- if (!visited[nbr.vertexNum]) {
- System.out.println("\n"+path+"-->"+adjLists[nbr.vertexNum].name+" (h="+height+")");
- dfs(nbr.vertexNum, visited, dest, limit, height+1,path+"-->"+adjLists[nbr.vertexNum].name);
- }
- }
- }
- public void dfs(String src, String dest, int limit) {
- boolean[] visited = new boolean[adjLists.length];
- System.out.println("\nStarting at " + src);
- dfs(indexForName(src), visited, dest, limit, 1, src);
- if(!visited[indexForName(dest)])
- System.out.println("Destination Not Found. Try to increase the limit.");
- }
- public static void main(String[] args)
- throws IOException {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter graph input file name: ");
- String file = sc.nextLine();
- System.out.print("Enter the source, destination and the limit: ");
- String src = sc.next(); String dest = sc.next(); int limit = sc.nextInt();
- Graph graph = new Graph(file);
- //graph.print();
- graph.dfs(src, dest, limit);
- }
- }
- /*ip.txt*/
- undirected
- 14
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14
- 1 2
- 1 6
- 2 3
- 3 5
- 3 7
- 4 5
- 5 6
- 7 8
- 7 9
- 8 10
- 8 11
- 9 11
- 9 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement