GraphLib
Bearbeitung der Aufgabe Mini Graph Library für OOP WiSe 2023/24
Loading...
Searching...
No Matches
GraphLib::Internal::WeightedGraph< D, I, E > Class Template Reference

A graph class representing weighted graphs. More...

#include <abstract_weighted_graph.h>

Inheritance diagram for GraphLib::Internal::WeightedGraph< D, I, E >:
GraphLib::Internal::Graph< D, I, E >

Public Member Functions

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.
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.
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.
Public Member Functions inherited from GraphLib::Internal::Graph< D, I, E >
virtual ~Graph ()=default
 Virtual destructor for safe polymorphic use.
virtual bool is_directed () const =0
 Check if the graph is directed.
constexpr bool operator== (const Graph &other) const
 Equality comparison between graphs.
bool is_full ()
 Checks if the graph has reached the maximum number of nodes.
bool node_exists (const id_type id) const
 Check if a node with the specified ID exists.
id_type node_count () const
 Get the number of nodes in the graph.
size_t edge_count () const
 Get the number of edges in the graph.
std::unordered_set< id_typeget_nodes () const
 Retrieve the set of all node IDs in the graph.
const data_typeget_node_data (const id_type id) const
 Access the data associated with a node by its ID.
const std::set< id_typeget_neighbors (const id_type id) const
 Get neighbors (adjacent node IDs) of a given node.
bool has_edge (const id_type from_id, const id_type to_id) const
 Check if an edge exists between two nodes.
edge_type get_edge (const id_type from_id, const id_type to_id) const
 Retrieve the edge information between two nodes.
std::set< edge_typeget_edges (const id_type from_id) const
 Get all edges originating from a given node.
void print_data_map () const
 Prints the contents of the data_map_ to standard output.
void print_adjacency_list () const
 Outputs the graph as an adjacency list to the given stream.
void print_with_data (std::ostream &os) const
 Outputs the graph as an adjacency list to the given stream but with its data.
void print_adjacency_matrix () const
 Prints the adjacency matrix of the graph.
id_type add_node (const data_type data)
 Add a new node with the given data.
id_type add_node ()
 Add a new node with default data.
void add_node_with_id (const id_type id, const data_type data)
 Insert a new node with a specified ID and associated data.
void add_node_with_id (const id_type id)
 Insert a new node with a specified ID.
void remove_node (const id_type id)
 Remove a node by ID.
void set_node_data (const id_type id, const data_type data)
 Update the data of a node.
virtual void remove_edge (const id_type from_id, const id_type to_id)=0
 Remove an edge from one node to another.

Additional Inherited Members

Public Types inherited from GraphLib::Internal::Graph< D, I, E >
using data_type = D
 Alias for the data type of nodes.
using id_type = I
 Alias for the node ID type.
using edge_type = E
 Alias for the edge type.
Protected Member Functions inherited from GraphLib::Internal::Graph< D, I, E >
virtual void add_edge_impl (const id_type from_id, const edge_type edge)
 Helper to add an edge including reverse edge if undirected.
Protected Attributes inherited from GraphLib::Internal::Graph< D, I, E >
AdjacencyList< id_type, edge_typeadj_list_ {}
 Stores edges as adjacency list.
std::unordered_map< id_type, data_typedata_map_ {}
 Maps node IDs to node data.
id_type highest_id_ {}
 IDs start at 1 to avoid off-by-one errors when giving node count.

Detailed Description

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
class GraphLib::Internal::WeightedGraph< D, I, E >

A graph class representing weighted graphs.

This class inherits from the generic Graph base class and specializes the behavior to weighted graphs by:

  • Overriding is_weighted() to return true.
  • Providing overloaded add_edge methods with and without explicit weight.
  • Providing get_edge_weight to access the weight of a specific edge.
Template Parameters
DData type stored in nodes.
IType of node IDs.
EType of weighted edges.

Definition at line 40 of file abstract_weighted_graph.h.

Member Function Documentation

◆ add_edge() [1/2]

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
void GraphLib::Internal::WeightedGraph< D, I, E >::add_edge ( const I from_id,
const I to_id )
inlineoverridevirtual

Adds a weighted edge from one node to another with default weight.

Uses the default constructed weight of E::weight_type.

Parameters
from_idThe ID of the origin node.
to_idThe ID of the target node.

Implements GraphLib::Internal::Graph< D, I, E >.

Definition at line 59 of file abstract_weighted_graph.h.

◆ add_edge() [2/2]

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
void GraphLib::Internal::WeightedGraph< D, I, E >::add_edge ( const I from_id,
const I to_id,
const typename E::weight_type weight )
inline

Adds a weighted edge from one node to another with an explicit weight.

Parameters
from_idThe ID of the origin node.
to_idThe ID of the target node.
weightThe weight associated with the edge.

Definition at line 71 of file abstract_weighted_graph.h.

◆ get_edge_weight()

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
E::weight_type GraphLib::Internal::WeightedGraph< D, I, E >::get_edge_weight ( const I from_id,
const I to_id ) const
inline

Retrieves the weight of the edge from from_id to to_id.

Parameters
from_idThe ID of the origin node.
to_idThe ID of the target node.
Returns
The weight associated with the edge.

Definition at line 86 of file abstract_weighted_graph.h.

◆ get_total_weight()

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
E::weight_type GraphLib::Internal::WeightedGraph< D, I, E >::get_total_weight ( ) const
inline

Calculates the total weight of all edges in the graph.

For undirected graphs, each edge is counted only once. For directed graphs, each directed edge is counted.

Returns
The sum of all edge weights in the graph.

Definition at line 100 of file abstract_weighted_graph.h.

◆ is_weighted()

template<valid_data_type D, valid_id_type I, valid_weighted_edge_type< I > E>
bool GraphLib::Internal::WeightedGraph< D, I, E >::is_weighted ( ) const
inlinenodiscardoverridevirtual

Indicates that this graph is weighted.

Returns
true, since edges contain weights.

Implements GraphLib::Internal::Graph< D, I, E >.

Definition at line 46 of file abstract_weighted_graph.h.


The documentation for this class was generated from the following file: