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

The polynomial halves of the pairs of table 19.1 of the conclusion, problems close to NP-hard ones but solvable in polynomial time, and the special cases that the conclusion names, the interval graphs. More...

Files

file  ADS/complexity/EasyCases.h
 The polynomial halves of the pairs of table 19.1 of the conclusion, problems close to NP-hard ones but solvable in polynomial time, and the special cases that the conclusion names, the interval graphs:

Functions

int ADS::literalNode (int l)
 the node of the literal \( \pm( v + 1 ) \) in the graph of the implications, \( 2 v \) for \( v \) and \( 2 v + 1 \) for its negation
std::optional< std::vector< bool > > ADS::twoSat (const Cnf &F, int n)
 an assignment of the \( n \) variables that satisfies \( F \), each clause of one or two literals, or nothing
std::optional< std::vector< int > > ADS::twoColoring (const Adjacency &G)
 the side, 0 or 1, of each node of \( G \), the ends of each edge apart, or nothing if \( G \) has an odd cycle; the diagonal of \( G \) is ignored, as by coloring() [see Problems.h]
std::vector< std::pair< int, int > > ADS::bipartiteMatching (int p, int q, const std::vector< std::pair< int, int > > &E)
 the pairs \( ( i , j ) \) of a largest matching of the bipartite graph with \( p \) left and \( q \) right nodes and the edges \( ( i , j ) \) of \( E \), by a maximum flow
Adjacency ADS::intervalGraph (const std::vector< std::pair< long, long > > &I)
 the graph of the intervals \( [ a , b ) \), an edge if two share a point: an empty interval \( [ a , a ) \) meets none, as in intervalClique()
int ADS::intervalClique (const std::vector< std::pair< long, long > > &I)
 the largest clique of the graph of the intervals \( [ a , b ) \): the most intervals through one point, the right ends before the left ones

Detailed description

The polynomial halves of the pairs of table 19.1 of the conclusion, problems close to NP-hard ones but solvable in polynomial time, and the special cases that the conclusion names, the interval graphs.

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

Function documentation

◆ bipartiteMatching()

std::vector< std::pair< int, int > > ADS::bipartiteMatching ( int p,
int q,
const std::vector< std::pair< int, int > > & E )
inline

the pairs \( ( i , j ) \) of a largest matching of the bipartite graph with \( p \) left and \( q \) right nodes and the edges \( ( i , j ) \) of \( E \), by a maximum flow

In pseudocode (the conclusion), the reduction of section 15.2:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{bipartiteMatching}( p , q , E )\quad \triangleright\ \text{a maximum flow of unit capacities} \cr 2 & \quad N \gets \text{a network on } 0 , \ldots , p + q + 1 \text{, with the source } s = p + q \text{ and the sink } t = p + q + 1 \cr 3 & \quad \text{add } ( s , i ) \text{, } ( i , p + j ) \text{ and } ( p + j , t ) \text{ of capacity } 1 \text{ for each } i < p \text{, } ( i , j ) \in E \text{, } j < q \cr 4 & \quad N.\mathrm{maxFlow}( s , t ) \cr 5 & \quad \textbf{return}\ \text{the pairs } ( i , j ) \in E \text{ whose edge carries flow} \end{array}} \]

Precondition
\( p , q \ge 0 \) and the edges join \( [ 0 , p ) \) to \( [ 0 , q ) \)
Postcondition
no two pairs returned share an end

◆ intervalClique()

int ADS::intervalClique ( const std::vector< std::pair< long, long > > & I)
inline

the largest clique of the graph of the intervals \( [ a , b ) \): the most intervals through one point, the right ends before the left ones

In pseudocode (the conclusion), a sweep of the ends: a set of intervals that meet pairwise has a common point (the property of Helly on the line), and so a clique is a set of intervals through one point:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intervalClique}( I )\quad \triangleright \ \text{the most intervals over one point} \cr 2 & \quad P \gets \text{the ends of } I \text{, a left end as } ( a , +1 ) \text{, a right end as } ( b , -1 ) \cr 3 & \quad \text{sort } P \text{ by abscissa, a right end first on a tie}\quad \triangleright \ [ a , b ) \text{ is open on the right} \cr 4 & \quad \textbf{return}\ \text{the largest prefix sum of the signs of } P \text{, or } 0 \end{array}} \]

Precondition
each interval \( [ a , b ) \) has \( a \le b \)
Postcondition
a number in \( [ 0 , n ] \)

◆ intervalGraph()

Adjacency ADS::intervalGraph ( const std::vector< std::pair< long, long > > & I)
inline

the graph of the intervals \( [ a , b ) \), an edge if two share a point: an empty interval \( [ a , a ) \) meets none, as in intervalClique()

In pseudocode (the conclusion), \( O( n^2 ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intervalGraph}( I ) \cr 2 & \quad \textbf{return}\ \text{the graph with } G[ u ][ v ] \text{ iff } u \ne v \text{ and } \max\{ a_u , a_v \} < \min\{ b_u , b_v \}\quad \triangleright\ \text{they meet} \end{array}} \]

Precondition
each interval \( [ a , b ) \) has \( a \le b \)
Postcondition
an unoriented graph, one node per interval

◆ literalNode()

int ADS::literalNode ( int l)
inline

the node of the literal \( \pm( v + 1 ) \) in the graph of the implications, \( 2 v \) for \( v \) and \( 2 v + 1 \) for its negation

Precondition
\( l \ne 0 \)

◆ twoColoring()

std::optional< std::vector< int > > ADS::twoColoring ( const Adjacency & G)
inline

the side, 0 or 1, of each node of \( G \), the ends of each edge apart, or nothing if \( G \) has an odd cycle; the diagonal of \( G \) is ignored, as by coloring() [see Problems.h]

In pseudocode (the conclusion), a visit in breadth from each node not reached, which gives each node the side opposite to that of its parent:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{twoColoring}( G )\quad \triangleright \ \text{a visit in breadth of each component} \cr 2 & \quad c[ v ] \gets \textbf{nil} \text{ for every node } v \cr 3 & \quad \textbf{foreach}\ r \text{ with } c[ r ] = \textbf{nil} \ \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 \ne u \text{ with } G[ u ][ v ] \ \textbf{do}\quad \triangleright \ \text{the other color, or an odd cycle} \cr 8 & \qquad\qquad \textbf{if}\ c[ v ] = \textbf{nil}\ \textbf{then}\ c[ v ] \gets 1 - c[ u ];\ \mathrm{enqueue}( Q , v )\ \textbf{else if}\ c[ v ] = c[ u ]\ \textbf{then return nil} \cr 9 & \quad \textbf{return}\ c \end{array}} \]

Precondition
\( G \) is unoriented, i.e., square and symmetric
Postcondition
a coloring returned is proper with two colors

◆ twoSat()

std::optional< std::vector< bool > > ADS::twoSat ( const Cnf & F,
int n )
inline

an assignment of the \( n \) variables that satisfies \( F \), each clause of one or two literals, or nothing

In pseudocode (the algorithm of Aspvall, Plass and Tarjan, beyond the book), a clause of one literal \( a \) taken as \( a \vee a \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{twoSat}( F , n ) \cr 2 & \quad G \gets \text{a graph of the } 2 n \text{ literals and no edge} \cr 3 & \quad \textbf{foreach}\ a \vee b \in F\ \textbf{do}\ \text{insert } ( \neg a , b ) \text{ and } ( \neg b , a ) \text{ in } G \cr 4 & \quad C \gets \mathrm{scc}( G );\ r \gets \text{the rank of each component}\ \text{in the order of } \mathrm{topsort}( G ) \cr 5 & \quad \textbf{for}\ v \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 6 & \qquad \textbf{if}\ C[ v ] = C[ \neg v ]\ \textbf{then return nil} \cr 7 & \qquad x[ v ] \gets r[ C[ v ] ] > r[ C[ \neg v ] ] \cr 8 & \quad \textbf{return}\ x \end{array}} \]

Precondition
\( n \ge 0 \), each clause has one or two literals of \( [ 0 , n ) \)
Postcondition
an assignment returned passes satCertificate()