|
| 1 | +package dev; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * Offer the global sorting based on the `prev, next relations` info. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * input = [a,b], [b,c], [c,d] |
| 11 | + * |
| 12 | + * -> output: [a,b,c,d] |
| 13 | + * |
| 14 | + * Example 2: |
| 15 | + * |
| 16 | + * input = [1,2], [2,4], [4,5] |
| 17 | + * |
| 18 | + * -> output: [1,2,4,5] |
| 19 | + * |
| 20 | + * |
| 21 | + */ |
| 22 | +public class TopoSortTest1 { |
| 23 | + |
| 24 | + // attr |
| 25 | + List<Integer> degrees; |
| 26 | + List<Integer> sortedRes; |
| 27 | + Map<Integer, List<Integer>> graph; |
| 28 | + Queue<Integer> queue; |
| 29 | + |
| 30 | + //List<List<String>> dependencies; |
| 31 | + |
| 32 | + // constructor |
| 33 | + public TopoSortTest1(){ |
| 34 | + this.degrees = new ArrayList<>(); |
| 35 | + this.sortedRes = new ArrayList<>(); |
| 36 | + this.graph = new HashMap<>(); |
| 37 | + this.queue = new LinkedList<>(); |
| 38 | + //this.dependencies = new ArrayList<>(); |
| 39 | + } |
| 40 | + |
| 41 | + // method |
| 42 | + /** |
| 43 | + * |
| 44 | + * List<List<String>> dependencies |
| 45 | + * |
| 46 | + * {prev: next} |
| 47 | + * |
| 48 | + */ |
| 49 | + public List<Integer> topoSort(List<List<Integer>> dependencies, int size) throws Exception { |
| 50 | + // edge case |
| 51 | + if (dependencies.isEmpty() || size == 0) { |
| 52 | + return this.sortedRes; |
| 53 | + } |
| 54 | + |
| 55 | + // init degree |
| 56 | + //int N = size; //dependencies.size(); // ?? |
| 57 | + for(int i = 0; i < size; i++){ |
| 58 | + this.degrees.add(0); |
| 59 | + } |
| 60 | + |
| 61 | + // build graph |
| 62 | + for(List<Integer> x: dependencies){ |
| 63 | + Integer prev = x.get(0); |
| 64 | + Integer next = x.get(1); |
| 65 | + // update degree |
| 66 | + if(this.graph.containsKey(prev)){ |
| 67 | + this.degrees.add(prev, this.degrees.get(prev)+1); |
| 68 | + } |
| 69 | + List<Integer> curNext = this.graph.getOrDefault(prev, new ArrayList<>()); |
| 70 | + curNext.add(next); |
| 71 | + this.graph.put(prev, curNext); |
| 72 | + } |
| 73 | + |
| 74 | + // add `degree = 0` elements to queue |
| 75 | + for(Integer x: degrees){ |
| 76 | + if(x==0){ |
| 77 | + this.queue.add(x); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // bfs ?? |
| 82 | + while(!this.queue.isEmpty()){ |
| 83 | + Integer curVal = queue.poll(); |
| 84 | + this.sortedRes.add(curVal); // ??? |
| 85 | + // go through `next` nodes |
| 86 | + if(this.graph.containsKey(curVal)){ |
| 87 | + List<Integer> nextList = this.graph.get(curVal); |
| 88 | + for(Integer x: nextList){ |
| 89 | + //this.queue.add(x); |
| 90 | + this.degrees.add(this.degrees.get(x), this.degrees.get(x)-1); |
| 91 | + |
| 92 | + // NOTE !!! if degree = 0, put into queue |
| 93 | + if(this.degrees.get(x) == 0){ |
| 94 | + this.queue.add(x); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + System.out.println(">>> this.sortedRes = " + this.sortedRes); |
| 101 | + |
| 102 | + // validate |
| 103 | + if(this.sortedRes.size() != size){ |
| 104 | + throw new Exception("Not able to do topological sort, sorted size not equals to input size"); |
| 105 | + } |
| 106 | + |
| 107 | + return this.sortedRes; |
| 108 | + } |
| 109 | + |
| 110 | + // testing |
| 111 | + public static void main(String[] args) throws Exception { |
| 112 | + System.out.println(">>> 123"); |
| 113 | + TopoSortTest1 topoSortTest1 = new TopoSortTest1(); |
| 114 | + List<List<Integer>> deps = new ArrayList<>(); |
| 115 | + List<Integer> tmp1 = new ArrayList<>(); |
| 116 | + List<Integer> tmp2 = new ArrayList<>(); |
| 117 | + List<Integer> tmp3 = new ArrayList<>(); |
| 118 | + |
| 119 | + tmp1.add(1); |
| 120 | + tmp1.add(2); |
| 121 | + |
| 122 | + tmp2.add(2); |
| 123 | + tmp2.add(4); |
| 124 | + |
| 125 | + tmp3.add(4); |
| 126 | + tmp3.add(5); |
| 127 | + |
| 128 | + deps.add(tmp1); |
| 129 | + deps.add(tmp2); |
| 130 | + deps.add(tmp3); |
| 131 | + |
| 132 | + // topoSortTest1.topoSort(deps, 4); |
| 133 | + |
| 134 | + System.out.println(topoSortTest1.topoSort(deps, 4)); |
| 135 | + |
| 136 | + } |
| 137 | +} |
0 commit comments