Skip to content

Commit df110ca

Browse files
authored
GH-117: Topological Sorting in C++ (#302)
1 parent 1aef861 commit df110ca

File tree

6 files changed

+275
-4
lines changed

6 files changed

+275
-4
lines changed

concepts/cpp/graph/notebook.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
"codemirror_mode": "text/x-c++src",
125125
"file_extension": ".cpp",
126126
"mimetype": "text/x-c++src",
127-
"name": "c++",
127+
"name": "C++17",
128128
"version": "17"
129129
},
130130
"orig_nbformat": 4
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Topological Sorting
2+
3+
Topological sorting is a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG.
4+
5+
## Topological Sorting in C++
6+
7+
Example directed acyclic graph:
8+
9+
```
10+
5 0
11+
^ / \
12+
| v v
13+
4 <- 2 <- 3
14+
^ |
15+
| v
16+
1 -> 6 -> 7
17+
```
18+
19+
```cpp
20+
int n = 8;
21+
vector<vector<int>> adj(n);
22+
23+
adj[0] = {2, 3};
24+
adj[1] = {4, 6};
25+
adj[2] = {4};
26+
adj[3] = {2, 7};
27+
adj[4] = {5};
28+
adj[5] = {};
29+
adj[6] = {7};
30+
adj[7] = {};
31+
```
32+
33+
Use DFS to find the topological sort:
34+
35+
```cpp
36+
void dfs(int i, vector<vector<int>> adj, vector<int> &visited, vector<int> &orders){
37+
visited[i] = true;
38+
for(auto j: adj[i]){
39+
if(!visited[j]){
40+
dfs(j, adj, visited, orders);
41+
}
42+
}
43+
orders.push_back(i);
44+
}
45+
46+
vector<int> topological_sort(int n, vector<vector<int>> adj) {
47+
vector<int> visited(n, false);
48+
vector<int> orders;
49+
50+
for(int i=0; i<n; i++){
51+
if(!visited[i]){
52+
dfs(i, adj, visited, orders);
53+
}
54+
}
55+
reverse(orders.begin(), orders.end());
56+
return orders;
57+
}
58+
```
59+
60+
```cpp
61+
vector<int> orders = topological_sort(n, adj);
62+
for (auto i : orders) {
63+
cout << i << " ";
64+
}
65+
```
66+
67+
Output
68+
69+
```
70+
1 6 0 3 7 2 4 5
71+
```
72+
73+
## References
74+
75+
* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)
76+
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
77+
* https://cp-algorithms.com/graph/topological-sort.html
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
void dfs(int i, vector<vector<int>> adj, vector<int> &visited,
8+
vector<int> &orders) {
9+
visited[i] = true;
10+
for (auto j : adj[i]) {
11+
if (!visited[j]) {
12+
dfs(j, adj, visited, orders);
13+
}
14+
}
15+
orders.push_back(i);
16+
}
17+
18+
vector<int> topological_sort(int n, vector<vector<int>> adj) {
19+
vector<int> visited(n, false);
20+
vector<int> orders;
21+
22+
for (int i = 0; i < n; i++) {
23+
if (!visited[i]) {
24+
dfs(i, adj, visited, orders);
25+
}
26+
}
27+
reverse(orders.begin(), orders.end());
28+
return orders;
29+
}
30+
31+
int main() {
32+
int n = 8;
33+
vector<vector<int>> adj(n);
34+
35+
adj[0] = {2, 3};
36+
adj[1] = {4, 6};
37+
adj[2] = {4};
38+
adj[3] = {2, 7};
39+
adj[4] = {5};
40+
adj[5] = {};
41+
adj[6] = {7};
42+
adj[7] = {};
43+
44+
vector<int> orders = topological_sort(n, adj);
45+
for (auto i : orders) {
46+
cout << i << " ";
47+
}
48+
cout << endl;
49+
return 0;
50+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {},
7+
"source": [
8+
"# Topological Sorting\n",
9+
"\n",
10+
"Topological sorting is a linear ordering of vertices in a directed acyclic graph (DAG) such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG."
11+
]
12+
},
13+
{
14+
"attachments": {},
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Topological Sorting in C++"
19+
]
20+
},
21+
{
22+
"attachments": {},
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"Example directed acyclic graph:\n",
27+
"\n",
28+
"```\n",
29+
" 5 0\n",
30+
" ^ / \\\n",
31+
" | v v\n",
32+
" 4 <- 2 <- 3\n",
33+
" ^ |\n",
34+
" | v\n",
35+
" 1 -> 6 -> 7\n",
36+
"```"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {
42+
"vscode": {
43+
"languageId": "cpp"
44+
}
45+
},
46+
"source": [
47+
"```cpp\n",
48+
"int n = 8;\n",
49+
"vector<vector<int>> adj(n);\n",
50+
"\n",
51+
"adj[0] = {2, 3};\n",
52+
"adj[1] = {4, 6};\n",
53+
"adj[2] = {4};\n",
54+
"adj[3] = {2, 7};\n",
55+
"adj[4] = {5};\n",
56+
"adj[5] = {};\n",
57+
"adj[6] = {7};\n",
58+
"adj[7] = {};\n",
59+
"```"
60+
]
61+
},
62+
{
63+
"attachments": {},
64+
"cell_type": "markdown",
65+
"metadata": {
66+
"vscode": {
67+
"languageId": "cpp"
68+
}
69+
},
70+
"source": [
71+
"Use DFS to find the topological sort:\n",
72+
"\n",
73+
"```cpp\n",
74+
"void dfs(int i, vector<vector<int>> adj, vector<int> &visited, vector<int> &orders){\n",
75+
" visited[i] = true;\n",
76+
" for(auto j: adj[i]){\n",
77+
" if(!visited[j]){\n",
78+
" dfs(j, adj, visited, orders);\n",
79+
" }\n",
80+
" }\n",
81+
" orders.push_back(i);\n",
82+
"}\n",
83+
"\n",
84+
"vector<int> topological_sort(int n, vector<vector<int>> adj) {\n",
85+
" vector<int> visited(n, false);\n",
86+
" vector<int> orders;\n",
87+
"\n",
88+
" for(int i=0; i<n; i++){\n",
89+
" if(!visited[i]){\n",
90+
" dfs(i, adj, visited, orders);\n",
91+
" }\n",
92+
" }\n",
93+
" reverse(orders.begin(), orders.end());\n",
94+
" return orders;\n",
95+
"}\n",
96+
"```\n",
97+
"\n",
98+
"```cpp\n",
99+
"vector<int> orders = topological_sort(n, adj);\n",
100+
"for (auto i : orders) {\n",
101+
" cout << i << \" \";\n",
102+
"}\n",
103+
"```\n",
104+
"\n",
105+
"Output\n",
106+
"\n",
107+
"```\n",
108+
"1 6 0 3 7 2 4 5\n",
109+
"```"
110+
]
111+
},
112+
{
113+
"attachments": {},
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"## References\n",
118+
"\n",
119+
"* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)\n",
120+
"* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)\n",
121+
"* https://cp-algorithms.com/graph/topological-sort.html"
122+
]
123+
}
124+
],
125+
"metadata": {
126+
"kernelspec": {
127+
"display_name": "C++17",
128+
"language": "C++17",
129+
"name": "xcpp17"
130+
},
131+
"language_info": {
132+
"codemirror_mode": "text/x-c++src",
133+
"file_extension": ".cpp",
134+
"mimetype": "text/x-c++src",
135+
"name": "c++",
136+
"version": "17"
137+
},
138+
"orig_nbformat": 4
139+
},
140+
"nbformat": 4,
141+
"nbformat_minor": 2
142+
}

concepts/general/topological-sorting/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Topological Sorting
22

33
*See implementation in*
4-
C++,
4+
[C++](/concepts/cpp/topological-sorting/README.md),
55
Java,
66
Python,
77
typescript
@@ -37,4 +37,5 @@ Topological Soring using DFS
3737
## References
3838

3939
* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)
40-
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
40+
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
41+
* https://cp-algorithms.com/graph/topological-sort.html

readme/cpp/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ An algorithm is a finite sequence of rigorous instructions, typically used to so
354354
<code>A</code> Prim’s Algorithm
355355
</li>
356356
<li>
357-
<code>A</code> Topological Sorting
357+
<code>A</code>
358+
<a href="/concepts/cpp/topological-sorting/README.md">Topological Sorting</a>
358359
</li>
359360
<li>
360361
<code>A</code> Articulation Points

0 commit comments

Comments
 (0)