java - Reading an adjacency list from a file into a multimap -
so have adjacency list: columbus node chicago node .5 distance etc.
columbus,chicago,0.5 columbus,miami,2.0 chicago,newyork,1.5 chicago,boston,2.0 chicago,stlouis,0.5 chicago,denver,2.5 chicago,seattle,3.0 boston,newyork,0.5 stlouis,atlanta,1 stlouis,dallas,1.0 atlanta,dallas,1.0 atlanta,miami,1.0 dallas,miami,2.0 dallas,losangeles,2.5 losangeles,sanfrancisco,1.0 losangeles,lasvegas,0.5 sanfrancisco,lasvegas,1.0 sanfrancisco,seattle,2.0 sanfrancisco,denver,2.0 denver,lasvegas,1.0 denver,seattle,2.0
and have below method reads in above list txt file. adds list multi map adj_list missing part:
"note each path between nodes listed once, each path needs added twice adjacency list. example, file lists path between columbus , chicago on first line. needs added adjacency list columbus path destination of chicago , needs added adjacency list chicago destination of columbus"
public static map<string, list<path>> readpathsfromfile(scanner infile) { map<string, list<path>> adj_list = new treemap<string, list<path>>(); arraylist<path> list1 = new arraylist<path>(); while (infile.hasnext()){ // to- add parts both ways. string input = infile.nextline(); string[] token = input.split(","); if(!adj_list.containskey(token[0])){ list1 = new arraylist<path>(); path path2 = new path(token[1],double.parsedouble(token[2])); list1.add(path2); adj_list.put(token[0], list1); }else{ path path = new path(token[1],double.parsedouble(token[2])); list1.add(path); } } return adj_list; }
so, question first above method way go start , if how can modify method make add nodes list in both directions instead of order of list?
edit: clearer on want.
for example have: sanfrancisco: (lasvegas:1.0), (seattle:2.0), (denver:2.0)
i have part needs is:
sanfrancisco: (losangeles:1.0), (lasvegas:1.0), (seattle:2.0), (denver:2.0)
i.e. losangeles has connecting node san francisco not explicitly listed san francisco in adj list. thank you!
you need careful arraylist<path> list1
. if input
columbus,chicago,0.5 chicago,newyork,1.5 columbus,miami,2.0
you map of
columbus (chicago,0.5) chicago (newyork,1.5),(miami,2.0)
because when reading 3rd line, list1
array chicago array (as created during 2nd line). token[0]
known (yes, there columbus entry in map because of line 1), jump straight adding (miame,2.0)
distance list1
-- chicago list.
the following code solves both above problem , problem stated getting rid of list outside while loop , instead fetching correct list map every single time.
public static map<string, list<path>> readpathsfromfile(scanner infile) { map<string, list<path>> adj_list = new treemap<string, list<path>>(); // not need list here // arraylist<path> list1 = new arraylist<path>(); while (infile.hasnext()){ // to- add parts both ways. string input = infile.nextline(); string[] token = input.split(","); // 1.) take care of 0 -> 1 // try , fetch list treemap saved under 0 list<path> token0list = adj_list.get(token[0]); // if there no list saved, have null value if(token0list == null){ // since there no list saved, // create new (empty) 1 , save in tree token0list = new arraylist<path>(); adj_list.put(token[0], token0list ); } // @ point of time, can sure token0list // exists , saved correctly in tree. // need add 0 -> 1 path list // , done part of saving. path path = new path(token[1],double.parsedouble(token[2])); token0list .add(path); // finish 0 -> 1 // 2.) take care of 1 -> 0 // same procedure 0 -> 1, swapping // token[0] , token[1] list<path> token1list = adj_list.get(token[1]); if(token1list == null){ token1list = new arraylist<path>(); adj_list.put(token[1], token1list ); } path path2 = new path(token[0],double.parsedouble(token[2])); token1list .add(path2); // finish 1 -> 0 } return adj_list; }
Comments
Post a Comment