GraphLib
Bearbeitung der Aufgabe Mini Graph Library für OOP WiSe 2023/24
Loading...
Searching...
No Matches
abstract_weighted_graph.h
Go to the documentation of this file.
1#ifndef GRAPHLIB_INTERNAL_ABSTRACT_WEIGHTED_GRAPH_H
2#define GRAPHLIB_INTERNAL_ABSTRACT_WEIGHTED_GRAPH_H
3
6#include <ostream>
7
22
23namespace GraphLib::Internal {
24
39template <valid_data_type D, valid_id_type I, valid_weighted_edge_type<I> E>
40class WeightedGraph : public virtual Graph<D, I, E> {
41public:
46 [[nodiscard]] bool is_weighted() const override
47 {
48 return true;
49 }
50
59 void add_edge(const I from_id, const I to_id) override
60 {
61 add_edge(from_id, to_id, typename E::weight_type {});
62 }
63
71 void add_edge(const I from_id, const I to_id, const typename E::weight_type weight)
72 {
73 auto edge = E {};
74 edge.node_id = to_id;
75 edge.weight = weight;
76 this->add_edge_impl(from_id, edge);
77 }
78
86 typename E::weight_type get_edge_weight(const I from_id, const I to_id) const
87 {
88 auto edge = this->get_edge(from_id, to_id);
89 return edge.weight;
90 }
91
100 typename E::weight_type get_total_weight() const
101 {
102 typename E::weight_type total_weight {};
103
104 for (const auto& from_id : this->get_nodes()) {
105 for (const auto& edge : this->get_edges(from_id)) {
106 if (this->is_directed() || from_id < edge.node_id) {
107 total_weight += edge.weight;
108 }
109 }
110 }
111
112 return total_weight;
113 }
114};
115
116} // namespace GraphLib::Internal
117
118#endif
Defines the generic abstract Graph class template supporting directed/undirected and weighted/unweigh...
Abstract base class for a generic graph structure.
virtual void add_edge_impl(const id_type from_id, const edge_type edge)
Helper to add an edge including reverse edge if undirected.
virtual bool is_directed() const =0
Check if the graph is directed.
edge_type get_edge(const id_type from_id, const id_type to_id) const
Retrieve the edge information between two nodes.
std::unordered_set< id_type > get_nodes() const
Retrieve the set of all node IDs in the graph.
std::set< edge_type > get_edges(const id_type from_id) const
Get all edges originating from a given node.
A graph class representing weighted graphs.
E::weight_type get_edge_weight(const I from_id, const I to_id) const
Retrieves the weight of the edge from from_id to to_id.
E::weight_type get_total_weight() const
Calculates the total weight of all edges in the graph.
void add_edge(const I from_id, const I to_id, const typename E::weight_type weight)
Adds a weighted edge from one node to another with an explicit weight.
bool is_weighted() const override
Indicates that this graph is weighted.
void add_edge(const I from_id, const I to_id) override
Adds a weighted edge from one node to another with default weight.
Defines C++20 concepts used for type constraints in the GraphLib library.