java - Finding a single path from any node on a graph to a specific node -
some more background on question.
line 1 a, goes b. line 2 goes c. line 1 b, goes a. line 2 b goes c. line 1 c, goes b. line 2 c goes a.
suppose lost @ 1 of stations a, b, or c. independent of are, if take line 2, , line 1, end @ station b. having path takes same place called meeting path. interested in finding meeting path consists of fixed set of instructions like, 'take line 1, line 2,' etc. possible might visit station multiple times. possible such path might not exist.
i thinking of using depth first search on graph find each node. because can repeat nodes not sure when should stop search , there no path.
is there better way finding paths in cyclical directed graph dfs?
once have given path test current implementation.
public static int calculate(int[][] n, list<integer> list){ boolean flag = false; int list_counter = 0; mapeven.clear(); mapodd.clear(); for(int = 0; i<n.length;i++){ int item = list.get(0); mapodd.put(i, 1); } for(int x = 0; x < list.size() && !flag;x++){ for(int = 0; < n.length ; i++){ int station = path(n,i,list.get(x)); if (station == -1){ continue; } int old_station_amount; if(x%2==1){ old_station_amount = mapeven.getordefault(i,0); mapodd.put(station, old_station_amount+mapodd.getordefault(station,0)); }else{ old_station_amount = mapodd.getordefault(i,0); mapeven.put(station, old_station_amount+mapeven.getordefault(station,0)); } } if(x%2==1){ mapeven.clear(); }else{ mapodd.clear(); } if(mapeven.values().contains((integer) n.length) ||mapodd.values().contains((integer) n.length)){ return -1; } } if(mapeven.values().contains((integer) n.length) ||mapodd.values().contains((integer) n.length)){ return -1; }else{ return -3; } }
Comments
Post a Comment