TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
topological_sort.cpp File Reference

Topological Sort Algorithm More...

#include <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <stdexcept>
#include <vector>
Include dependency graph for topological_sort.cpp:

Go to the source code of this file.

Classes

class  graph::topological_sort::Graph
 Class that represents a directed graph and provides methods for manipulating the graph. More...
 

Namespaces

namespace  graph
 Graph Algorithms.
 
namespace  topological_sort
 Topological Sort Algorithm.
 

Functions

void graph::topological_sort::dfs (int v, std::vector< int > &visited, const std::vector< std::vector< int > > &graph, std::stack< int > &s)
 Function to perform Depth First Search on the graph.
 
std::vector< int > graph::topological_sort::topologicalSort (const Graph &g)
 Function to get the topological sort of the graph.
 
static void test ()
 Self-test implementation.
 
int main ()
 Main function.
 

Detailed Description

Topological Sort Algorithm

Topological sorting of a directed graph is a linear ordering or its vertices such that for every directed edge (u,v) from vertex u to vertex v, u comes before v in the oredering.

A topological sort is possible only in a directed acyclic graph (DAG). This file contains code of finding topological sort using Kahn's Algorithm which involves using Depth First Search technique

Definition in file topological_sort.cpp.

Function Documentation

◆ dfs()

void graph::topological_sort::dfs ( int v,
std::vector< int > & visited,
const std::vector< std::vector< int > > & graph,
std::stack< int > & s )

Function to perform Depth First Search on the graph.

Parameters
vStarting vertex for depth-first search
visitedArray representing whether each node has been visited
graphAdjacency list of the graph
sStack containing the vertices for topological sorting

Definition at line 79 of file topological_sort.cpp.

80 {
81 visited[v] = 1;
82 for (int neighbour : graph[v]) {
83 if (!visited[neighbour]) {
84 dfs(neighbour, visited, graph, s);
85 }
86 }
87 s.push(v);
88}
std::vector< size_t > dfs(const std::vector< std::vector< size_t > > &graph, size_t start)
Explores the given vertex, exploring a vertex means traversing over all the vertices which are connec...
Graph Algorithms.

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 186 of file topological_sort.cpp.

186 {
187 test(); // run self test implementations
188 return 0;
189}
static void test()
Self-test implementation.

◆ test()

static void test ( )
static

Self-test implementation.

Returns
void

Definition at line 126 of file topological_sort.cpp.

126 {
127 // Test 1
128 std::cout << "Testing for graph 1\n";
129 int n_1 = 6;
131 graph1.addEdge(4, 0);
132 graph1.addEdge(5, 0);
133 graph1.addEdge(5, 2);
134 graph1.addEdge(2, 3);
135 graph1.addEdge(3, 1);
136 graph1.addEdge(4, 1);
137 std::vector<int> ans_1 = graph::topological_sort::topologicalSort(graph1);
138 std::vector<int> expected_1 = {5, 4, 2, 3, 1, 0};
139 std::cout << "Topological Sorting Order: ";
140 for (int i : ans_1) {
141 std::cout << i << " ";
142 }
143 std::cout << '\n';
144 assert(ans_1 == expected_1);
145 std::cout << "Test Passed\n\n";
146
147 // Test 2
148 std::cout << "Testing for graph 2\n";
149 int n_2 = 5;
151 graph2.addEdge(0, 1);
152 graph2.addEdge(0, 2);
153 graph2.addEdge(1, 2);
154 graph2.addEdge(2, 3);
155 graph2.addEdge(1, 3);
156 graph2.addEdge(2, 4);
157 std::vector<int> ans_2 = graph::topological_sort::topologicalSort(graph2);
158 std::vector<int> expected_2 = {0, 1, 2, 4, 3};
159 std::cout << "Topological Sorting Order: ";
160 for (int i : ans_2) {
161 std::cout << i << " ";
162 }
163 std::cout << '\n';
164 assert(ans_2 == expected_2);
165 std::cout << "Test Passed\n\n";
166
167 // Test 3 - Graph with cycle
168 std::cout << "Testing for graph 3\n";
169 int n_3 = 3;
171 graph3.addEdge(0, 1);
172 graph3.addEdge(1, 2);
173 graph3.addEdge(2, 0);
174 try {
176 } catch (std::invalid_argument& err) {
177 assert(std::string(err.what()) == "cycle detected in graph");
178 }
179 std::cout << "Test Passed\n";
180}
Class that represents a directed graph and provides methods for manipulating the graph.
std::vector< int > topologicalSort(const Graph &g)
Function to get the topological sort of the graph.

◆ topologicalSort()

std::vector< int > graph::topological_sort::topologicalSort ( const Graph & g)

Function to get the topological sort of the graph.

Parameters
gGraph object
Returns
A vector containing the topological order of nodes

Definition at line 95 of file topological_sort.cpp.

95 {
96 int n = g.getNumNodes();
97 const auto& adj = g.getAdjacencyList();
98 std::vector<int> visited(n, 0);
99 std::stack<int> s;
100
101 for (int i = 0; i < n; i++) {
102 if (!visited[i]) {
103 dfs(i, visited, adj, s);
104 }
105 }
106
107 std::vector<int> ans;
108 while (!s.empty()) {
109 int elem = s.top();
110 s.pop();
111 ans.push_back(elem);
112 }
113
114 if (ans.size() < n) { // Cycle detected
115 throw std::invalid_argument("cycle detected in graph");
116 }
117 return ans;
118}
double g(double x)
Another test function.