2024-07-14 16:44:12 +02:00
|
|
|
#include "digraph.h"
|
|
|
|
|
|
|
|
Node::Node(std::list<int> neighbours_) : neighbours(std::move(neighbours_))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::add_edge(int v)
|
|
|
|
{
|
|
|
|
neighbours.push_back(v);
|
|
|
|
}
|
|
|
|
|
2024-07-24 15:54:43 +02:00
|
|
|
int Node::outdeg() const
|
|
|
|
{
|
|
|
|
return neighbours.size();
|
|
|
|
}
|
|
|
|
|
2024-07-14 16:44:12 +02:00
|
|
|
Digraph::Digraph(size_t num_nodes) : nodes(num_nodes)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Digraph::add_edge(int from, int to)
|
|
|
|
{
|
|
|
|
nodes[from].add_edge(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<int> Digraph::adjList(int node_id) const
|
|
|
|
{
|
|
|
|
return (nodes[node_id]).neighbours;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Digraph::isEdge(int from, int to) const
|
|
|
|
{
|
|
|
|
return (std::find(adjList(from).begin(), adjList(from).end(), to) != adjList(from).end());
|
|
|
|
}
|
|
|
|
|
2024-07-24 15:54:43 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-07-14 16:44:12 +02:00
|
|
|
size_t Digraph::num_nodes() const
|
|
|
|
{
|
|
|
|
return nodes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Digraph Digraph::transpose() const
|
|
|
|
{
|
|
|
|
Digraph G(num_nodes());
|
|
|
|
for (int i = 0; i < num_nodes(); i++)
|
|
|
|
{
|
|
|
|
for (auto const& j: ((nodes[i]).neighbours))
|
|
|
|
{
|
|
|
|
G.add_edge(j,i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return G;
|
|
|
|
}
|