TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Topological Sort Algorithm More...
#include <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <stdexcept>
#include <vector>
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. | |
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.
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.
v | Starting vertex for depth-first search |
visited | Array representing whether each node has been visited |
graph | Adjacency list of the graph |
s | Stack containing the vertices for topological sorting |
Definition at line 79 of file topological_sort.cpp.
int main | ( | void | ) |
Main function.
Definition at line 186 of file topological_sort.cpp.
|
static |
Self-test implementation.
Definition at line 126 of file topological_sort.cpp.
std::vector< int > graph::topological_sort::topologicalSort | ( | const Graph & | g | ) |
Function to get the topological sort of the graph.
g | Graph object |
Definition at line 95 of file topological_sort.cpp.