Skip to content

Commit

Permalink
update TopologicalSortV2.java, backup code
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 9, 2024
1 parent 5fe4ff9 commit a4ccd0a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
21 changes: 20 additions & 1 deletion leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public static List<Integer> topologicalSort(int numNodes, List<List<Integer>> ed
// Step 2: Initialize a queue with nodes that have in-degree 0
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < numNodes; i++) {
/**
* NOTE !!!
*
* we add ALL nodes with degree = 0 to queue at init step
*/
if (inDegree[i] == 0) {
queue.offer(i);
}
Expand All @@ -34,11 +39,25 @@ public static List<Integer> topologicalSort(int numNodes, List<List<Integer>> ed

// Step 3: Process the nodes in topological order
while (!queue.isEmpty()) {
/**
* NOTE !!!
*
* ONLY "degree = 0" nodes CAN be added to queue
*
* -> so we can add whatever node from queue to final result (topologicalOrder)
*/
int current = queue.poll();
topologicalOrder.add(current);

for (int neighbor : graph.get(current)) {
inDegree[neighbor]--;
inDegree[neighbor] -= 1;
/**
* NOTE !!!
*
* if a node "degree = 0" means this node can be ACCESSED now,
*
* -> so we need to add it to the queue (for adding to topologicalOrder in the following while loop iteration)
*/
if (inDegree[neighbor] == 0) {
queue.offer(neighbor);
}
Expand Down
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2723,9 +2723,61 @@ public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences)
// LC 207
// https://leetcode.com/problems/course-schedule/
public boolean canFinish(int numCourses, int[][] prerequisites) {
if (prerequisites.length==0){
return true;
}
// if (prerequisites.length==1){
// return true;
// }

return false;
}

private int[] topoSort(int numCourses, int[][] prerequisites){
int[] tSorting = new int[]{numCourses};

// init : 1) preList 2) degree
Map<Integer, List<Integer>> preList = new HashMap<>();
//Map<Integer, Integer> degree = new HashMap<>();
List<Integer> degree = new ArrayList<>();

for (int[] x : prerequisites){
int pre = x[1];
int cur = x[0]; // ???
if (!preList.containsKey(pre)){
preList.put(pre, new ArrayList<>());
}else{
List<Integer> curItems = preList.get(pre);
curItems.add(cur);
preList.put(pre, curItems);
}
}

for (int[] x : prerequisites){
int pre = x[1];
int cur = x[0]; // ???
degree.set(cur, degree.get(cur)+1);
}

Queue<Integer> queue = new LinkedList();
queue.add(0);
int idx = 0;
tSorting[idx] = 0;

while(!queue.isEmpty()){
Integer curNode = queue.poll();
for (Integer subNode : preList.get(curNode)){
if (degree.get(subNode).equals(0)){
idx += 1;
tSorting[idx] = subNode;
degree.set(subNode, degree.get(subNode)-1);
}
}
}

return tSorting;
}


}

Expand Down

0 comments on commit a4ccd0a

Please sign in to comment.