Skip to content

GH-117: Topological Sorting in C++ #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion concepts/cpp/graph/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"name": "C++17",
"version": "17"
},
"orig_nbformat": 4
Expand Down
77 changes: 77 additions & 0 deletions concepts/cpp/topological-sorting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Topological Sorting

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.

## Topological Sorting in C++

Example directed acyclic graph:

```
5 0
^ / \
| v v
4 <- 2 <- 3
^ |
| v
1 -> 6 -> 7
```

```cpp
int n = 8;
vector<vector<int>> adj(n);

adj[0] = {2, 3};
adj[1] = {4, 6};
adj[2] = {4};
adj[3] = {2, 7};
adj[4] = {5};
adj[5] = {};
adj[6] = {7};
adj[7] = {};
```

Use DFS to find the topological sort:

```cpp
void dfs(int i, vector<vector<int>> adj, vector<int> &visited, vector<int> &orders){
visited[i] = true;
for(auto j: adj[i]){
if(!visited[j]){
dfs(j, adj, visited, orders);
}
}
orders.push_back(i);
}

vector<int> topological_sort(int n, vector<vector<int>> adj) {
vector<int> visited(n, false);
vector<int> orders;

for(int i=0; i<n; i++){
if(!visited[i]){
dfs(i, adj, visited, orders);
}
}
reverse(orders.begin(), orders.end());
return orders;
}
```

```cpp
vector<int> orders = topological_sort(n, adj);
for (auto i : orders) {
cout << i << " ";
}
```

Output

```
1 6 0 3 7 2 4 5
```

## References

* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
* https://cp-algorithms.com/graph/topological-sort.html
50 changes: 50 additions & 0 deletions concepts/cpp/topological-sorting/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

void dfs(int i, vector<vector<int>> adj, vector<int> &visited,
vector<int> &orders) {
visited[i] = true;
for (auto j : adj[i]) {
if (!visited[j]) {
dfs(j, adj, visited, orders);
}
}
orders.push_back(i);
}

vector<int> topological_sort(int n, vector<vector<int>> adj) {
vector<int> visited(n, false);
vector<int> orders;

for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(i, adj, visited, orders);
}
}
reverse(orders.begin(), orders.end());
return orders;
}

int main() {
int n = 8;
vector<vector<int>> adj(n);

adj[0] = {2, 3};
adj[1] = {4, 6};
adj[2] = {4};
adj[3] = {2, 7};
adj[4] = {5};
adj[5] = {};
adj[6] = {7};
adj[7] = {};

vector<int> orders = topological_sort(n, adj);
for (auto i : orders) {
cout << i << " ";
}
cout << endl;
return 0;
}
142 changes: 142 additions & 0 deletions concepts/cpp/topological-sorting/notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Topological Sorting\n",
"\n",
"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."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Topological Sorting in C++"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Example directed acyclic graph:\n",
"\n",
"```\n",
" 5 0\n",
" ^ / \\\n",
" | v v\n",
" 4 <- 2 <- 3\n",
" ^ |\n",
" | v\n",
" 1 -> 6 -> 7\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"source": [
"```cpp\n",
"int n = 8;\n",
"vector<vector<int>> adj(n);\n",
"\n",
"adj[0] = {2, 3};\n",
"adj[1] = {4, 6};\n",
"adj[2] = {4};\n",
"adj[3] = {2, 7};\n",
"adj[4] = {5};\n",
"adj[5] = {};\n",
"adj[6] = {7};\n",
"adj[7] = {};\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"vscode": {
"languageId": "cpp"
}
},
"source": [
"Use DFS to find the topological sort:\n",
"\n",
"```cpp\n",
"void dfs(int i, vector<vector<int>> adj, vector<int> &visited, vector<int> &orders){\n",
" visited[i] = true;\n",
" for(auto j: adj[i]){\n",
" if(!visited[j]){\n",
" dfs(j, adj, visited, orders);\n",
" }\n",
" }\n",
" orders.push_back(i);\n",
"}\n",
"\n",
"vector<int> topological_sort(int n, vector<vector<int>> adj) {\n",
" vector<int> visited(n, false);\n",
" vector<int> orders;\n",
"\n",
" for(int i=0; i<n; i++){\n",
" if(!visited[i]){\n",
" dfs(i, adj, visited, orders);\n",
" }\n",
" }\n",
" reverse(orders.begin(), orders.end());\n",
" return orders;\n",
"}\n",
"```\n",
"\n",
"```cpp\n",
"vector<int> orders = topological_sort(n, adj);\n",
"for (auto i : orders) {\n",
" cout << i << \" \";\n",
"}\n",
"```\n",
"\n",
"Output\n",
"\n",
"```\n",
"1 6 0 3 7 2 4 5\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)\n",
"* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)\n",
"* https://cp-algorithms.com/graph/topological-sort.html"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
5 changes: 3 additions & 2 deletions concepts/general/topological-sorting/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Topological Sorting

*See implementation in*
C++,
[C++](/concepts/cpp/topological-sorting/README.md),
Java,
Python,
typescript
Expand Down Expand Up @@ -37,4 +37,5 @@ Topological Soring using DFS
## References

* [Topological Sort Algorithm | Graph Theory, WilliamFiset](https://www.youtube.com/watch?v=eL-KzMXSXXI&t=308s&ab_channel=WilliamFiset)
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
* [Topological Sort | GeeksforGeeks](https://www.geeksforgeeks.org/topological-sorting/)
* https://cp-algorithms.com/graph/topological-sort.html
3 changes: 2 additions & 1 deletion readme/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ An algorithm is a finite sequence of rigorous instructions, typically used to so
<code>A</code> Prim’s Algorithm
</li>
<li>
<code>A</code> Topological Sorting
<code>A</code>
<a href="/concepts/cpp/topological-sorting/README.md">Topological Sorting</a>
</li>
<li>
<code>A</code> Articulation Points
Expand Down