updated weighted_graph

This commit is contained in:
GregorSamsa42 2024-08-01 23:04:06 +02:00
parent 5f71dd5d45
commit 54cef5809d
2 changed files with 10 additions and 1 deletions

View file

@ -19,13 +19,14 @@ int Node::deg() const
WeightedGraph::WeightedGraph(size_t num_nodes) : nodes(num_nodes) WeightedGraph::WeightedGraph(size_t num_nodes) : nodes(num_nodes)
{ {
edges = 0;
} }
void WeightedGraph::add_edge(int from, int to, double weight) void WeightedGraph::add_edge(int from, int to, double weight)
{ {
nodes[from].add_edge(to, weight); nodes[from].add_edge(to, weight);
nodes[to].add_edge(from, weight); nodes[to].add_edge(from, weight);
edges++;
} }
std::list<std::pair<int, double>> WeightedGraph::adjList(int node_id) const std::list<std::pair<int, double>> WeightedGraph::adjList(int node_id) const
@ -43,6 +44,11 @@ size_t WeightedGraph::num_nodes() const
return nodes.size(); return nodes.size();
} }
int WeightedGraph::num_edges() const
{
return edges;
}
std::pair<int,double> WeightedGraph::min_neighbour(int node_id) const std::pair<int,double> WeightedGraph::min_neighbour(int node_id) const
{ {
if (adjList(node_id).empty()) if (adjList(node_id).empty())
@ -82,6 +88,7 @@ WeightedGraph WeightedGraph::remove_parallel() const
for (const auto& [to, weight] : lightestEdges) for (const auto& [to, weight] : lightestEdges)
{ {
G.nodes[i].add_edge(to, weight); G.nodes[i].add_edge(to, weight);
G.edges++;
} }
} }
return G; return G;

View file

@ -20,12 +20,14 @@ public:
void add_edge(int from, int to, double weight); void add_edge(int from, int to, double weight);
std::list<std::pair<int, double>> adjList(int node_id) const; std::list<std::pair<int, double>> adjList(int node_id) const;
size_t num_nodes() const; size_t num_nodes() const;
int num_edges() const;
int deg(int v) const; int deg(int v) const;
std::pair<int,double> min_neighbour(int node_id) const; std::pair<int,double> min_neighbour(int node_id) const;
WeightedGraph remove_parallel() const; WeightedGraph remove_parallel() const;
private: private:
std::vector<Node> nodes; std::vector<Node> nodes;
int edges;
}; };
#endif //C___WEIGHTEDGRAPH_H #endif //C___WEIGHTEDGRAPH_H