Restructured folders, added topological order alg
This commit is contained in:
parent
56a5b594e4
commit
a2bc786543
5 changed files with 93 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(Kosaraju)
|
||||
add_executable(Kosaraju
|
||||
digraph.cpp
|
||||
digraph.h
|
||||
../digraph.cpp
|
||||
../digraph.h
|
||||
main.cpp)
|
|
@ -10,6 +10,11 @@ void Node::add_edge(int v)
|
|||
neighbours.push_back(v);
|
||||
}
|
||||
|
||||
int Node::outdeg() const
|
||||
{
|
||||
return neighbours.size();
|
||||
}
|
||||
|
||||
Digraph::Digraph(size_t num_nodes) : nodes(num_nodes)
|
||||
{
|
||||
|
||||
|
@ -30,6 +35,24 @@ bool Digraph::isEdge(int from, int to) const
|
|||
return (std::find(adjList(from).begin(), adjList(from).end(), to) != adjList(from).end());
|
||||
}
|
||||
|
||||
int Digraph::outdeg(int node_id) const
|
||||
{
|
||||
return nodes[node_id].outdeg();
|
||||
}
|
||||
|
||||
std::vector<int> Digraph::indegrees() const
|
||||
{
|
||||
std::vector<int> indegs (num_nodes(),0);
|
||||
for(int i = 0; i < num_nodes(); i++)
|
||||
{
|
||||
for (auto const& j: ((nodes[i]).neighbours))
|
||||
{
|
||||
indegs[j]++;
|
||||
}
|
||||
}
|
||||
return indegs;
|
||||
}
|
||||
|
||||
size_t Digraph::num_nodes() const
|
||||
{
|
||||
return nodes.size();
|
|
@ -12,6 +12,7 @@ struct Node {
|
|||
Node() = default;
|
||||
Node(std::list<int> neighbours);
|
||||
void add_edge(int v);
|
||||
int outdeg() const;
|
||||
};
|
||||
|
||||
class Digraph {
|
||||
|
@ -22,6 +23,8 @@ public:
|
|||
size_t num_nodes() const;
|
||||
Digraph transpose() const;
|
||||
bool isEdge(int from, int to) const;
|
||||
int outdeg(int v) const;
|
||||
std::vector<int> indegrees() const;
|
||||
|
||||
private:
|
||||
std::vector<Node> nodes;
|
6
top_order/CMakeLists.txt
Normal file
6
top_order/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
project(top_order)
|
||||
add_executable(top_order
|
||||
../digraph.cpp
|
||||
../digraph.h
|
||||
top_order.cpp)
|
58
top_order/top_order.cpp
Normal file
58
top_order/top_order.cpp
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Kosaraju’s algorithm to find strongly connected components of a directed graph
|
||||
// Authors: Georǵi Kocharyan, Maximilian Keßler
|
||||
|
||||
#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(4,3);
|
||||
// G.add_edge(5,6);
|
||||
G.add_edge(6,7);
|
||||
G.add_edge(7,5);
|
||||
// G.add_edge(7,3);
|
||||
// G.add_edge(0,3);
|
||||
// G.add_edge(3,0);
|
||||
|
||||
std::stack<int> zero_indegree;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
while(!zero_indegree.empty())
|
||||
{
|
||||
int node_id = zero_indegree.top();
|
||||
zero_indegree.pop();
|
||||
std::cout << node_id << ' ';
|
||||
for(auto i: G.adjList(node_id))
|
||||
{
|
||||
if (indegs[i] = 1)
|
||||
{
|
||||
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;
|
||||
}
|
Loading…
Reference in a new issue