java - How to increment a number for 3 different paths while using recursion? -


i have program prints reachable paths of graph. contains 2 classes graphpath1 , search . program given below:

class graphpath1:

import java.util.arraylist; import java.util.hashmap; import java.util.linkedhashset; import java.util.linkedlist; import java.util.list; import java.util.map;   public class graphpath1 {     list<string> src=new arraylist<string>();  // source node     list<string> dest=new arraylist<string>(); // destination node   private map<string, linkedhashset<string>> map = new hashmap();  public void addedge(string node1, string node2){     linkedhashset<string> adjacent = map.get(node1);     if(adjacent==null) {         adjacent = new linkedhashset();         map.put(node1, adjacent);         src.add(node1);     }     adjacent.add(node2);     dest.add(node2); }    public linkedlist<string> adjacentnodes(string last) {     linkedhashset<string> adjacent = map.get(last);     if(adjacent==null) {         return new linkedlist();     }     return new linkedlist<string>(adjacent); }  } 

class search:

import java.util.arraylist; import dfs.graphpath1; import java.util.linkedlist; import java.util.list;  import dfs.loansystem;   public class search { private static final string start = "1"; private static final string end = "7"; public static void main(string[] args) {       // graph directional     graphpath1 graph = new graphpath1();      graph.addedge("1", "2");     graph.addedge("1", "3");     graph.addedge("2", "5");     graph.addedge("3", "4");     graph.addedge("4", "5");     graph.addedge("4", "6");     graph.addedge("5", "7");     graph.addedge("6", "7");     //graph.addedge("7", "1");      /*     list<string> s = graph.src;     list<string> d = graph.dest;     system.out.print(s);     system.out.print(d);*/      linkedlist<string> visited = new linkedlist();     visited.add(start);     new search().dfs(graph, visited);  }   private void dfs(graphpath1 graph, linkedlist<string> visited) { linkedlist<string> nodes = graph.adjacentnodes(visited.getlast()); // examine adjacent nodes (string node : nodes) {     if (visited.contains(node)) {         continue;     }     if (node.equals(end)) {         visited.add(node);         printpath(visited);         visited.removelast();         break;     } }    // in dfs, recursion needs come after visiting adjacent nodes (string node : nodes) {     if (visited.contains(node) || node.equals(end)) {         continue;     }      visited.addlast(node);     dfs(graph, visited);     visited.removelast(); }  } /* public list<edge> getedgelist (linkedlist<string> visited){     list<edge> edges = new arraylist<edge>();     for(int i=0;i<=visited.size();i++)         edges.add(new edge(visited.get(i), visited.get(i+1)));      return edges; } */  private void printpath(linkedlist<string> visited) {      arraylist<string> sequence = new arraylist<string>();     {     (string node : visited) {         sequence.add(node);      }  }     arraylist<string> sequences = new arraylist<string>();     sequences.addall(sequence);     system.out.println(sequences);  } } 

the output of program :

1,2,5,7 1,3,4,5,7 1,3,4,6,7 

now need print 3 different messages these 3 paths. example:

this path 1: 1,2,5,7 path 2: 1,3,4,5,7 path 3: 1,3,4,6,7  

but don't know how this. can give me idea how can increment number used in message (i.e. path 1:) 3 different paths?

this not hard do. need counter variable keep track of path printing. in case can set counter 0 before call dfs() function. before each print increment , print line saying path is. after call printpath(). that:

private int pathcount = 0;  // more of code ...  private void dfs(graphpath1 graph, linkedlist<string> visited) {     linkedlist<string> nodes = graph.adjacentnodes(visited.getlast());     // examine adjacent nodes     (string node : nodes) {         if (visited.contains(node)) {             continue;         }         if (node.equals(end)) {             visited.add(node);             pathnumber++;             system.out.println("this path " + pathnumber + ":");             printpath(visited);             visited.removelast();             break;         }     }      // rest of algorithm ... } 

one more thing: if make dfs static function (private static void dfs(...)), can call directly main function without having create instance of search class , new search().dfs(graph, visited); can turned dfs(graph, visited);. use instance variable keep track of path count, 1 instance of search class per search want.

edit: reworked code snippet use instance variable instead of local 1 in function, not work function recursive. andreas pointing out.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -