From a4ccd0a7e6224c7e58afc0b89391e1f3483998a8 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 9 Nov 2024 19:11:19 +0800 Subject: [PATCH] update TopologicalSortV2.java, backup code --- .../java/AlgorithmJava/TopologicalSortV2.java | 21 +++++++- .../src/main/java/dev/workspace5.java | 52 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java b/leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java index b9b17bc5..85ee7b9e 100644 --- a/leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java +++ b/leetcode_java/src/main/java/AlgorithmJava/TopologicalSortV2.java @@ -25,6 +25,11 @@ public static List topologicalSort(int numNodes, List> ed // Step 2: Initialize a queue with nodes that have in-degree 0 Queue 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); } @@ -34,11 +39,25 @@ public static List topologicalSort(int numNodes, List> 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); } diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 52d4b764..7a649537 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -2723,9 +2723,61 @@ public boolean sequenceReconstruction(int[] nums, List> 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> preList = new HashMap<>(); + //Map degree = new HashMap<>(); + List 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 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 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; + } + }