ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Properties.h

Properties of a GRAPH and of its trees, decided or measured by the visits of section 9.5. More...

Files

file  ADS/graph/Properties.h
 Properties of a GRAPH and of its trees, decided or measured by the visits of section 9.5:

Functions

template<Graph G>
bool ADS::isTree (const G &g)
 whether the unoriented graph is a free tree, connected and with \( n - 1 \) edges
template<Graph G>
int ADS::diameter (const G &g)
 the largest distance in edges between two nodes
template<Graph G>
int ADS::treeDiameter (const G &g)
 the edges of a longest path of the free tree
template<class T>
std::pair< int, int > ADS::treeDiameter (const BinaryTree< T > *t)
 the diameter of a binary tree: its height and the edges of its longest path, \( ( -1 , 0 ) \) for the empty tree
template<Graph G>
std::vector< int > ADS::treeVertexCover (const G &g)
 the nodes of a minimum vertex cover of the forest, in increasing order
template<Graph G>
std::int64_t ADS::countTriangles (const G &g)
 the triangles of the unoriented graph
template<Graph G>
std::optional< std::vector< int > > ADS::twoColoring (const G &g)
 a color 0 or 1 for each node, the ends of each edge apart, -1 on the names that are not nodes; nothing if there is an odd cycle
template<class T>
int ADS::universalSink (const std::vector< std::vector< T > > &A)
 the node with an edge from every other one and no edge out, NIL if there is none, from the matrix of adjacency \( A \), whose entries convert to bool (the diagonal is not read)
template<Graph G>
requires requires( const G g , int u , int v ) { { g.hasEdge( u , v ) } -> std::convertible_to< bool >; }
int ADS::universalSink (const G &g)
 the universal sink of the graph \( G \), NIL if there is none, by the elimination of universalSink( A ) above on hasEdge(), at most \( 3 ( n - 1 ) \) calls of it, \( O( n ) \) time on the adjacency matrix of MatrixGraph (exercise 12.10 of Morin's book), where the in-degrees would take \( \Theta( n^2 ) \)
template<Graph G>
std::optional< std::vector< int > > ADS::uniqueTopologicalOrder (const G &g)
 the topological order of the oriented graph if it is the only one, nothing if it has a cycle or more orders

Detailed description

Properties of a GRAPH and of its trees, decided or measured by the visits of section 9.5.

The definitions, the invariants and the costs of the area are in Theory of graphs.

Function documentation

◆ countTriangles()

template<Graph G>
std::int64_t ADS::countTriangles ( const G & g)

the triangles of the unoriented graph

In pseudocode (the ordering of Chiba and Nishizeki): each edge is oriented from the end of smaller degree, ties by name, and a triangle is counted at its first node.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countTriangles}( G ) \cr 2 & \quad \textbf{foreach}\ [ u , v ] \in E\ \textbf{do}\ \text{put } v \text{ in } \mathit{out}[ u ] \text{ if } ( \deg u , u ) < ( \deg v , v ) \text{ , else } u \text{ in } \mathit{out}[ v ] \cr 3 & \quad t \gets 0 \cr 4 & \quad \textbf{foreach}\ u \in V\ \textbf{do} \cr 5 & \qquad \text{mark the nodes of } \mathit{out}[ u ] \cr 6 & \qquad \textbf{foreach}\ v \in \mathit{out}[ u ] , x \in \mathit{out}[ v ]\ \textbf{do}\ \textbf{if}\ x \text{ is marked }\ \textbf{then}\ t \gets t + 1 \cr 7 & \qquad \text{unmark the nodes of } \mathit{out}[ u ] \cr 8 & \quad \textbf{return}\ t \end{array}} \]

Precondition
the graph is unoriented

◆ diameter()

template<Graph G>
int ADS::diameter ( const G & g)

the largest distance in edges between two nodes

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{diameter}( G ) \cr 2 & \quad D \gets 0 \cr 3 & \quad \textbf{foreach}\ u \in V\ \textbf{do}\ D \gets \max( D , \max_v \mathrm{distance}( G , u )[ v ] ) \cr 4 & \quad \textbf{return}\ D \end{array}} \]

Precondition
the graph has a node, and every node reaches all the others

◆ isTree()

template<Graph G>
bool ADS::isTree ( const G & g)

whether the unoriented graph is a free tree, connected and with \( n - 1 \) edges

In pseudocode:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isTree}( G ) \cr 2 & \quad \textbf{return}\ m = n - 1\ \textbf{and}\ \text{a visit from } \text{any node reaches all the } n \text{ nodes} \end{array}} \]

Precondition
the graph is unoriented, with a node

◆ treeDiameter() [1/2]

template<class T>
std::pair< int, int > ADS::treeDiameter ( const BinaryTree< T > * t)

the diameter of a binary tree: its height and the edges of its longest path, \( ( -1 , 0 ) \) for the empty tree

In pseudocode, in a postvisit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{treeDiameter}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return}\ ( -1 , 0 ) \cr 3 & \quad ( h_l , D_l ) \gets \mathrm{treeDiameter}( t.\mathrm{left}() ); \ ( h_r , D_r ) \gets \mathrm{treeDiameter}( t.\mathrm{right}() ) \cr 4 & \quad \textbf{return}\ ( 1 + \max( h_l , h_r ) , \max( D_l , D_r , ( h_l + 1 ) + ( h_r + 1 ) ) ) \end{array}} \]

◆ treeDiameter() [2/2]

template<Graph G>
int ADS::treeDiameter ( const G & g)

the edges of a longest path of the free tree

In pseudocode (the double sweep):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{treeDiameter}( G ) \cr 2 & \quad a \gets \text{the node farthest from any node, by } \mathrm{distance} \cr 3 & \quad \textbf{return}\ \max_v \mathrm{distance}( G , a )[ v ] \end{array}} \]

Precondition
the graph is a free tree

◆ treeVertexCover()

template<Graph G>
std::vector< int > ADS::treeVertexCover ( const G & g)

the nodes of a minimum vertex cover of the forest, in increasing order

In pseudocode (the greedy on the leaves): a cover has a leaf or its parent, and the parent covers more.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{treeVertexCover}( G ) \cr 2 & \quad Q \gets \text{the nodes in the order of a visit in breadth from } \text{a node of each tree};\ C \gets \emptyset \cr 3 & \quad \textbf{foreach}\ u \in Q \text{ from the last }\ \textbf{do} \cr 4 & \qquad \textbf{if}\ p[ u ] \ne \textbf{nil}\ \textbf{and}\ u \notin C \ \textbf{and}\ p[ u ] \notin C\ \textbf{then}\ C \gets C \cup \{ p[ u ] \} \cr 5 & \quad \textbf{return}\ C \end{array}} \]

Precondition
the graph is unoriented and acyclic
Postcondition
each edge has an end in the cover (audit)

◆ twoColoring()

template<Graph G>
std::optional< std::vector< int > > ADS::twoColoring ( const G & g)

a color 0 or 1 for each node, the ends of each edge apart, -1 on the names that are not nodes; nothing if there is an odd cycle

In pseudocode (the theorem of Koenig):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{twoColoring}( G ) \cr 2 & \quad \textbf{foreach}\ v \in V\ \textbf{do}\ c[ v ] \gets -1 \cr 3 & \quad \textbf{foreach}\ r \in V\ \textbf{with}\ c[ r ] = -1 \ \textbf{do} \cr 4 & \qquad c[ r ] \gets 0;\ Q \gets ( r ) \cr 5 & \qquad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 6 & \qquad\quad u \gets \mathrm{dequeue}( Q ) \cr 7 & \qquad\quad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 8 & \qquad\qquad \textbf{if}\ c[ v ] = -1\ \textbf{then}\ c[ v ] \gets 1 - c[ u ];\ \mathrm{enqueue}( Q , v ) \cr 9 & \qquad\qquad \textbf{else if}\ c[ v ] = c[ u ]\ \textbf{then return nil}\quad \triangleright\ \text{an odd cycle} \cr 10 & \quad \textbf{return}\ c \end{array}} \]

Precondition
the graph is unoriented
Postcondition
the ends of each edge have different colors (audit)

◆ uniqueTopologicalOrder()

template<Graph G>
std::optional< std::vector< int > > ADS::uniqueTopologicalOrder ( const G & g)

the topological order of the oriented graph if it is the only one, nothing if it has a cycle or more orders

In pseudocode (the algorithm of Kahn), the order unique if and only if there is never more than one source:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{uniqueTopologicalOrder}( G ) \cr 2 & \quad \textbf{foreach}\ ( u , v ) \in E\ \textbf{do}\ \mathit{in}[ v ] \gets \mathit{in}[ v ] + 1 \cr 3 & \quad S \gets \text{the nodes with } \mathit{in} = 0;\ L \gets () \cr 4 & \quad \textbf{while}\ |S| = 1\ \textbf{do} \cr 5 & \qquad u \gets \mathrm{pop}( S );\ \text{append } u \text{ to } L \cr 6 & \qquad \textbf{foreach}\ v \in G.\mathrm{adj}( u )\ \textbf{do} \cr 7 & \qquad\quad \mathit{in}[ v ] \gets \mathit{in}[ v ] - 1 \cr 8 & \qquad\quad \textbf{if}\ \mathit{in}[ v ] = 0\ \textbf{then} \ \mathrm{push}( S , v ) \cr 9 & \quad \textbf{if}\ |L| < n\ \textbf{then return nil}\quad \triangleright\ \text{two sources, or a cycle} \cr 10 & \quad \textbf{return}\ L \end{array}} \]

Precondition
the graph is oriented
Postcondition
an order given is topological (audit)

◆ universalSink() [1/2]

template<Graph G>
requires requires( const G g , int u , int v ) { { g.hasEdge( u , v ) } -> std::convertible_to< bool >; }
int ADS::universalSink ( const G & g)

the universal sink of the graph \( G \), NIL if there is none, by the elimination of universalSink( A ) above on hasEdge(), at most \( 3 ( n - 1 ) \) calls of it, \( O( n ) \) time on the adjacency matrix of MatrixGraph (exercise 12.10 of Morin's book), where the in-degrees would take \( \Theta( n^2 ) \)

Precondition
the graph is oriented

◆ universalSink() [2/2]

template<class T>
int ADS::universalSink ( const std::vector< std::vector< T > > & A)

the node with an edge from every other one and no edge out, NIL if there is none, from the matrix of adjacency \( A \), whose entries convert to bool (the diagonal is not read)

In pseudocode (the elimination of King and Smith-Thomas): the entry ( c , j ) rules out c if it is an edge and j otherwise.

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{universalSink}( A ) \cr 2 & \quad c \gets 0 \cr 3 & \quad \textbf{for}\ j \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \ \textbf{if}\ A[ c ][ j ]\ \textbf{then}\ c \gets j \cr 4 & \quad \textbf{foreach}\ j \ne c\ \textbf{do} \cr 5 & \qquad \textbf{if}\ A[ c ][ j ]\ \textbf{or not}\ A[ j ][ c ] \ \textbf{then return nil} \cr 6 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( A \) is square