Skip to content

Commit 5792823

Browse files
committed
add TopologicalSortV2, update cheatsheet, progress
1 parent 00b80cc commit 5792823

File tree

6 files changed

+90
-15
lines changed

6 files changed

+90
-15
lines changed

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241109: 210(todo),444
1+
20241109: 207,210(todo),444
22
20241108: 1188,130,855(again)
33
20241104: 207
44
20241103: 900

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
2025-01-03 -> ['210(todo),444']
1+
2025-01-03 -> ['207,210(todo),444']
22
2025-01-02 -> ['1188,130,855(again)']
33
2024-12-28 -> ['900']
44
2024-12-27 -> ['26,27', '802,1197,26']
55
2024-12-21 -> ['855,846']
66
2024-12-20 -> ['932']
77
2024-12-18 -> ['951,792']
88
2024-12-14 -> ['163,1048']
9-
2024-12-13 -> ['210(todo),444', '298,729']
9+
2024-12-13 -> ['207,210(todo),444', '298,729']
1010
2024-12-12 -> ['1188,130,855(again)', '1146']
1111
2024-12-08 -> ['737']
1212
2024-12-07 -> ['900', '686,734,737']
1313
2024-12-06 -> ['26,27', '802,1197,26', '353']
1414
2024-12-05 -> ['528,334']
1515
2024-12-03 -> ['1145']
16-
2024-11-30 -> ['210(todo),444', '855,846', '1145,1219']
16+
2024-11-30 -> ['207,210(todo),444', '855,846', '1145,1219']
1717
2024-11-29 -> ['1188,130,855(again)', '932']
1818
2024-11-27 -> ['951,792', '524,221,889']
1919
2024-11-26 -> ['743,889']
2020
2024-11-25 -> ['837']
2121
2024-11-24 -> ['900']
2222
2024-11-23 -> ['26,27', '802,1197,26', '163,1048', '981']
23-
2024-11-22 -> ['210(todo),444', '298,729', '1087']
23+
2024-11-22 -> ['207,210(todo),444', '298,729', '1087']
2424
2024-11-21 -> ['1188,130,855(again)', '1146']
2525
2024-11-20 -> ['939']
2626
2024-11-18 -> ['430']
27-
2024-11-17 -> ['210(todo),444', '855,846', '737', '363']
27+
2024-11-17 -> ['207,210(todo),444', '855,846', '737', '363']
2828
2024-11-16 -> ['1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
2929
2024-11-15 -> ['26,27', '802,1197,26', '353', '947']
30-
2024-11-14 -> ['210(todo),444', '951,792', '528,334']
30+
2024-11-14 -> ['207,210(todo),444', '951,792', '528,334']
3131
2024-11-13 -> ['1188,130,855(again)']
32-
2024-11-12 -> ['210(todo),444', '1145', '753']
33-
2024-11-11 -> ['210(todo),444', '1188,130,855(again)', '900', '727']
34-
2024-11-10 -> ['210(todo),444', '1188,130,855(again)', '26,27', '802,1197,26', '163,1048']
32+
2024-11-12 -> ['207,210(todo),444', '1145', '753']
33+
2024-11-11 -> ['207,210(todo),444', '1188,130,855(again)', '900', '727']
34+
2024-11-10 -> ['207,210(todo),444', '1188,130,855(again)', '26,27', '802,1197,26', '163,1048']
3535
2024-11-09 -> ['1188,130,855(again)', '855,846', '298,729', '1145,1219']
3636
2024-11-08 -> ['900', '932', '1146']
3737
2024-11-07 -> ['26,27', '802,1197,26']

doc/cheatsheet/topology_sorting.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# Topology sorting
22

3-
## 0) Concept
4-
- Topological Sort is a algorithm can find "ordering" on an "order dependency" graph
3+
## 0) Concept
4+
5+
- Topological Sort is a algorithm can find `ordering` based on `order dependency` graph
6+
57
- Concept
68
- [techbridge : topological-sort](https://blog.techbridge.cc/2020/05/10/leetcode-topological-sort/)
79
- [do topological-sort via DFS](https://alrightchiu.github.io/SecondRound/graph-li-yong-dfsxun-zhao-dagde-topological-sorttuo-pu-pai-xu.html)
810
- Code
9-
- [algorithm/python/topological_sort.py](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/topological_sort.py)
11+
- [topological_sort.py](https://github.com/yennanliu/CS_basics/blob/master/algorithm/python/topological_sort.py)
12+
- [TopologicalSort.java](https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/TopologicalSort.java)
1013

1114
### 0-1) Types
1215
- Courses

leetcode_java/src/main/java/AlgorithmJava/TopologicalSort.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public class TopologicalSort {
3737
*/
3838
public int[] topologicalSort(int numCourses, int[][] prerequisites) {
3939

40+
/** NOTE !!!
41+
*
42+
* we create 2 things for topological sort
43+
*
44+
* 1) List<List<Integer>> graph : for tracking nodes and their "prerequisite" node
45+
*
46+
* 2) int[] inDegree = new int[numCourses] : for tracking "degree" of a node,
47+
* when degree==0, the node CAN be ACCESSED
48+
*
49+
*/
50+
4051
// Step 1: Build the graph
4152
List<List<Integer>> graph = new ArrayList<>();
4253
for (int i = 0; i < numCourses; i++) {
@@ -60,8 +71,8 @@ public int[] topologicalSort(int numCourses, int[][] prerequisites) {
6071
*
6172
* int[][] prerequisites = { {1, 0}, {2, 0}, {3, 1}, {3, 2} };
6273
*
63-
* -> 0 is a prerequisite for 1 and 2.
64-
* -> 1 and 2 are prerequisites for 3.
74+
* -> 0 is the prerequisite for 1 and 2.
75+
* -> 1 and 2 are the prerequisites for 3.
6576
*
6677
*/
6778
// int dest = prereq[0];: dest represents the course that depends on src.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package AlgorithmJava;
2+
3+
import java.util.*;
4+
5+
public class TopologicalSortV2 {
6+
7+
// V2
8+
// Function to perform Topological Sort (GPT)
9+
public static List<Integer> topologicalSort(int numNodes, List<List<Integer>> edges) {
10+
// Step 1: Build the graph and calculate in-degrees
11+
Map<Integer, List<Integer>> graph = new HashMap<>();
12+
int[] inDegree = new int[numNodes];
13+
14+
for (int i = 0; i < numNodes; i++) {
15+
graph.put(i, new ArrayList<>());
16+
}
17+
18+
for (List<Integer> edge : edges) {
19+
int from = edge.get(0);
20+
int to = edge.get(1);
21+
graph.get(from).add(to);
22+
inDegree[to]++;
23+
}
24+
25+
// Step 2: Initialize a queue with nodes that have in-degree 0
26+
Queue<Integer> queue = new LinkedList<>();
27+
for (int i = 0; i < numNodes; i++) {
28+
if (inDegree[i] == 0) {
29+
queue.offer(i);
30+
}
31+
}
32+
33+
List<Integer> topologicalOrder = new ArrayList<>();
34+
35+
// Step 3: Process the nodes in topological order
36+
while (!queue.isEmpty()) {
37+
int current = queue.poll();
38+
topologicalOrder.add(current);
39+
40+
for (int neighbor : graph.get(current)) {
41+
inDegree[neighbor]--;
42+
if (inDegree[neighbor] == 0) {
43+
queue.offer(neighbor);
44+
}
45+
}
46+
}
47+
48+
// If topologicalOrder does not contain all nodes, there was a cycle in the graph
49+
if (topologicalOrder.size() != numNodes) {
50+
throw new IllegalArgumentException("The graph has a cycle, so topological sort is not possible.");
51+
}
52+
53+
return topologicalOrder;
54+
}
55+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,12 @@ public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences)
27202720
return false;
27212721
}
27222722

2723+
// LC 207
2724+
// https://leetcode.com/problems/course-schedule/
2725+
public boolean canFinish(int numCourses, int[][] prerequisites) {
2726+
return false;
2727+
}
2728+
27232729

27242730
}
27252731

0 commit comments

Comments
 (0)