graph-algorithms/top_order/top_order.cpp

55 lines
1.3 KiB
C++
Raw Permalink Normal View History

2024-07-24 15:57:30 +02:00
// Algorithm outputting a topological order of a directed graph
// Authors: Georǵi Kocharyan
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include "../digraph.h"
int main() {
int size = 10;
Digraph G(size);
G.add_edge(3,4);
G.add_edge(6,7);
G.add_edge(7,5);
2024-07-24 15:57:30 +02:00
// keeps track of vertices with zero indegree, these can be put at the beginning
std::stack<int> zero_indegree;
2024-07-24 15:57:30 +02:00
std::vector<int> indegs = G.indegrees();
int amount = 0;
for(int i = 0; i < size; i++)
{
if (indegs[i] == 0)
{
zero_indegree.push(i);
amount++;
}
}
2024-07-24 15:57:30 +02:00
// update indegs, zero_indegree after adding a vertex to the top. order
while(!zero_indegree.empty())
{
int node_id = zero_indegree.top();
zero_indegree.pop();
std::cout << node_id << ' ';
for(auto i: G.adjList(node_id))
{
2024-07-24 15:57:30 +02:00
if (indegs[i] = 1) // this ensures each vertex added to stack only once
{
zero_indegree.push(i);
amount++;
}
indegs[i]--;
}
}
if (!(amount == size))
{
std::cout << '\n' << "The graph contains cycles and thus has no topological order." << std::endl;
}
return 0;
}