Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
util::ListGraph< NodeIndexType, ArcIndexType > Class Template Reference

#include <graph.h>

Inheritance diagram for util::ListGraph< NodeIndexType, ArcIndexType >:
util::BaseGraph< int32_t, int32_t, false >

Classes

struct  OutgoingArcIteratorTag
 Do not use directly. More...

Public Types

using OutgoingArcIterator
using OutgoingHeadIterator = ArcHeadIterator<ListGraph, OutgoingArcIterator>
Public Types inherited from util::BaseGraph< int32_t, int32_t, false >
typedef int32_t NodeIndex
typedef int32_t ArcIndex

Public Member Functions

 ListGraph ()
 ListGraph (NodeIndexType num_nodes, ArcIndexType arc_capacity)
void AddNode (NodeIndexType node)
ArcIndexType AddArc (NodeIndexType tail, NodeIndexType head)
void Build ()
void Build (std::vector< ArcIndexType > *permutation)
NodeIndexType Tail (ArcIndexType arc) const
 Returns the tail/head of a valid arc.
NodeIndexType Head (ArcIndexType arc) const
ArcIndexType OutDegree (NodeIndexType node) const
BeginEndWrapper< OutgoingArcIteratorOutgoingArcs (NodeIndexType node) const
BeginEndWrapper< OutgoingArcIteratorOutgoingArcsStartingFrom (NodeIndexType node, ArcIndexType from) const
BeginEndWrapper< OutgoingHeadIteratoroperator[] (NodeIndexType node) const
void ReserveNodes (NodeIndexType bound) override
void ReserveArcs (ArcIndexType bound) override
bool IsArcValid (ArcIndexType arc) const
Public Member Functions inherited from util::BaseGraph< int32_t, int32_t, false >
 BaseGraph ()
BaseGraphoperator= (const BaseGraph &)=default
virtual ~BaseGraph ()=default
int32_t num_nodes () const
int32_t size () const
int32_t num_arcs () const
 Returns the number of valid arcs in the graph.
IntegerRange< NodeIndexAllNodes () const
 BaseGraph implementation -------------------------------------------------—.
IntegerRange< ArcIndexAllForwardArcs () const
bool IsNodeValid (int32_t node) const
 Returns true if the given node is a valid node of the graph.
bool IsArcValid (int32_t arc) const
int32_t node_capacity () const
 Capacity reserved for future nodes, always >= num_nodes_.
int32_t arc_capacity () const
 Capacity reserved for future arcs, always >= num_arcs_.
virtual void ReserveNodes (int32_t bound)
virtual void ReserveArcs (int32_t bound)
void Reserve (int32_t node_capacity, int32_t arc_capacity)
void FreezeCapacities ()

Additional Inherited Members

Static Public Attributes inherited from util::BaseGraph< int32_t, int32_t, false >
static constexpr bool kHasNegativeReverseArcs
static constexpr int32_t kNilNode
static constexpr int32_t kNilArc
Protected Member Functions inherited from util::BaseGraph< int32_t, int32_t, false >
void ComputeCumulativeSum (internal::Vector< int32_t, int32_t > *v)
 Functions commented when defined because they are implementation details.
void BuildStartAndForwardHead (internal::SVector< int32_t, int32_t > *head, internal::Vector< int32_t, int32_t > *start, std::vector< int32_t > *permutation)
Protected Attributes inherited from util::BaseGraph< int32_t, int32_t, false >
int32_t num_nodes_
int32_t node_capacity_
int32_t num_arcs_
int32_t arc_capacity_
bool const_capacities_

Detailed Description

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
class util::ListGraph< NodeIndexType, ArcIndexType >

Basic graph implementation without reverse arc. This class also serves as a documentation for the generic graph interface (minus the part related to reverse arcs).

This implementation uses a linked list and compared to StaticGraph:

  • Is a bit faster to construct (if the arcs are not ordered by tail).
  • Does not require calling Build().
  • Has slower outgoing arc iteration.
  • Uses more memory: ArcIndexType * node_capacity()
  • Has an efficient Tail() but need an extra NodeIndexType/arc memory for it.
  • Never changes the initial arc index returned by AddArc().

All graphs should be -compatible, but we haven't tested that.

Definition at line 638 of file graph.h.

Member Typedef Documentation

◆ OutgoingArcIterator

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
using util::ListGraph< NodeIndexType, ArcIndexType >::OutgoingArcIterator

◆ OutgoingHeadIterator

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
using util::ListGraph< NodeIndexType, ArcIndexType >::OutgoingHeadIterator = ArcHeadIterator<ListGraph, OutgoingArcIterator>

Definition at line 693 of file graph.h.

Constructor & Destructor Documentation

◆ ListGraph() [1/2]

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
util::ListGraph< NodeIndexType, ArcIndexType >::ListGraph ( )
inline

Definition at line 648 of file graph.h.

◆ ListGraph() [2/2]

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
util::ListGraph< NodeIndexType, ArcIndexType >::ListGraph ( NodeIndexType num_nodes,
ArcIndexType arc_capacity )
inline

Reserve space for the graph at construction and do not allow it to grow beyond that, see FreezeCapacities(). This constructor also makes any nodes in [0, num_nodes) valid.

Definition at line 653 of file graph.h.

Member Function Documentation

◆ AddArc()

template<typename NodeIndexType, typename ArcIndexType>
ArcIndexType util::ListGraph< NodeIndexType, ArcIndexType >::AddArc ( NodeIndexType tail,
NodeIndexType head )

Adds an arc to the graph and returns its current index which will always be num_arcs() - 1. It will also automatically call AddNode(tail) and AddNode(head). It will fail in DEBUG mode if the capacities are fixed and this cause the graph to grow beyond them.

Note
Self referencing arcs and duplicate arcs are supported.

Definition at line 1330 of file graph.h.

◆ AddNode()

template<typename NodeIndexType, typename ArcIndexType>
void util::ListGraph< NodeIndexType, ArcIndexType >::AddNode ( NodeIndexType node)

If node is not a valid node, sets num_nodes_ to node + 1 so that the given node becomes valid. It will fail in DEBUG mode if the capacities are fixed and the new node is out of range.

Definition at line 1322 of file graph.h.

◆ Build() [1/2]

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
void util::ListGraph< NodeIndexType, ArcIndexType >::Build ( )
inline

Some graph implementations need to be finalized with Build() before they can be used. After Build() is called, the arc indices (which had been the return values of previous AddArc() calls) may change: the new index of former arc #i will be stored in permutation[i] if #i is smaller than permutation.size() or will be unchanged otherwise. If you don't care about these, just call the simple no-output version Build().

Note
some implementations become immutable after calling Build().

Definition at line 682 of file graph.h.

◆ Build() [2/2]

template<typename NodeIndexType, typename ArcIndexType>
void util::ListGraph< NodeIndexType, ArcIndexType >::Build ( std::vector< ArcIndexType > * permutation)

Definition at line 1360 of file graph.h.

◆ Head()

template<typename NodeIndexType, typename ArcIndexType>
NodeIndexType util::ListGraph< NodeIndexType, ArcIndexType >::Head ( ArcIndexType arc) const

Definition at line 1307 of file graph.h.

◆ IsArcValid()

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
bool util::BaseGraph< NodeIndexType, ArcIndexType, HasNegativeReverseArcs >::IsArcValid ( ArcIndexType arc) const
inline

Returns true if the given arc is a valid arc of the graph.

Note
the arc validity range changes for graph with reverse arcs.

Definition at line 249 of file graph.h.

◆ operator[]()

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
BeginEndWrapper< OutgoingHeadIterator > util::ListGraph< NodeIndexType, ArcIndexType >::operator[] ( NodeIndexType node) const
inline

This loops over the heads of the OutgoingArcs(node). It is just a more convenient way to achieve this. Moreover this interface is used by some graph algorithms.

Definition at line 725 of file graph.h.

◆ OutDegree()

template<typename NodeIndexType, typename ArcIndexType>
ArcIndexType util::ListGraph< NodeIndexType, ArcIndexType >::OutDegree ( NodeIndexType node) const

Graph jargon: the "degree" of a node is its number of arcs. The out-degree is the number of outgoing arcs. The in-degree is the number of incoming arcs, and is only available for some graph implementations, below.

ListGraph<>::OutDegree() works in O(degree).

Definition at line 1314 of file graph.h.

◆ OutgoingArcs()

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
BeginEndWrapper< OutgoingArcIterator > util::ListGraph< NodeIndexType, ArcIndexType >::OutgoingArcs ( NodeIndexType node) const
inline

Allows to iterate over the forward arcs that verify Tail(arc) == node. This is meant to be used as: for (const ArcIndex arc : graph.OutgoingArcs(node)) { ... }

Definition at line 705 of file graph.h.

◆ OutgoingArcsStartingFrom()

template<typename NodeIndexType = int32_t, typename ArcIndexType = int32_t>
BeginEndWrapper< OutgoingArcIterator > util::ListGraph< NodeIndexType, ArcIndexType >::OutgoingArcsStartingFrom ( NodeIndexType node,
ArcIndexType from ) const
inline

Advanced usage. Same as OutgoingArcs(), but allows to restart the iteration from an already known outgoing arc of the given node. If from is kNilArc, an empty range is returned.

Definition at line 714 of file graph.h.

◆ ReserveArcs()

template<typename NodeIndexType, typename ArcIndexType>
void util::ListGraph< NodeIndexType, ArcIndexType >::ReserveArcs ( ArcIndexType bound)
override

Definition at line 1351 of file graph.h.

◆ ReserveNodes()

template<typename NodeIndexType, typename ArcIndexType>
void util::ListGraph< NodeIndexType, ArcIndexType >::ReserveNodes ( NodeIndexType bound)
override

Definition at line 1344 of file graph.h.

◆ Tail()

template<typename NodeIndexType, typename ArcIndexType>
NodeIndexType util::ListGraph< NodeIndexType, ArcIndexType >::Tail ( ArcIndexType arc) const

Returns the tail/head of a valid arc.

ListGraph implementation -------------------------------------------------—.

Definition at line 1300 of file graph.h.


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