|
| 1 | +packagepackage1; |
| 2 | + |
| 3 | +importjava.util.*;; |
| 4 | + |
| 5 | + |
| 6 | +publicclassSetValueInDependencyGraph { |
| 7 | +publicstaticvoidmain(String...strings){ |
| 8 | + |
| 9 | +Map<String,List<String>>dependenceMap =newHashMap(); |
| 10 | + |
| 11 | +List<String>list1 =newArrayList(); |
| 12 | +list1.add("B"); |
| 13 | +list1.add("C"); |
| 14 | + |
| 15 | +List<String>list2 =newArrayList(); |
| 16 | +list2.add("A"); |
| 17 | +list2.add("C"); |
| 18 | +list2.add("D"); |
| 19 | + |
| 20 | +List<String>list3 =newArrayList(); |
| 21 | +list3.add("E"); |
| 22 | + |
| 23 | +List<String>list4 =newArrayList(); |
| 24 | +list4.add("A"); |
| 25 | + |
| 26 | +dependenceMap.put("A",list1); |
| 27 | +dependenceMap.put("B",list2); |
| 28 | +dependenceMap.put("C",list3); |
| 29 | +dependenceMap.put("E",list4); |
| 30 | + |
| 31 | +SetValueInDependencyGraph.Method.setValue_v2("A",1,dependenceMap); |
| 32 | + |
| 33 | +} |
| 34 | + |
| 35 | +staticclassMethod{ |
| 36 | +publicstaticvoidsetValue_v2(StringcolumnName,intvalue,Map<String,List<String>>depedenceMap){ |
| 37 | +Map<String,Integer>result =newHashMap(); |
| 38 | +result.put(columnName,value); |
| 39 | + |
| 40 | +Queue<String>queue =newLinkedList(); |
| 41 | +queue.offer(columnName); |
| 42 | + |
| 43 | +while(!queue.isEmpty()){ |
| 44 | +intsize =queue.size(); |
| 45 | +for(inti =0;i <size;i++){ |
| 46 | +StringcurrCol =queue.poll(); |
| 47 | +intthisValue =result.get(currCol); |
| 48 | +if(!depedenceMap.containsKey(currCol))continue; |
| 49 | +for(Stringdep :depedenceMap.get(currCol)){ |
| 50 | +if(!result.containsKey(dep)){ |
| 51 | +result.put(dep,thisValue+1); |
| 52 | +queue.offer(dep); |
| 53 | +} |
| 54 | +} |
| 55 | +} |
| 56 | +} |
| 57 | + |
| 58 | +for(Stringkey :result.keySet()){ |
| 59 | +System.out.println(key +"\t" +result.get(key)); |
| 60 | +} |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +publicvoidsetValue_v1_stopped_by_interviewer(StringcolumnName,intvalue,Map<String,List<String>>depedenceMap){ |
| 65 | +Map<String,Integer>result =newHashMap(); |
| 66 | +for(StringcolName :depedenceMap.keySet()){ |
| 67 | +if(!result.containsKey(colName)){ |
| 68 | +result.put(colName,value); |
| 69 | +} |
| 70 | +for(Stringdep :depedenceMap.get(colName)){ |
| 71 | +if(!result.containsKey(dep)){ |
| 72 | +result.put(dep,result.get(colName)+1); |
| 73 | +} |
| 74 | +} |
| 75 | +} |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +} |
| 80 | +} |