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

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...

#include "ADS/Contract.h"
#include "ADS/complexity/Problems.h"
#include "ADS/graph/ListGraph.h"
#include "ADS/graph/MaxFlow.h"
#include "ADS/graph/Visit.h"
#include "ADS/queue/ArrayQueue.h"
#include <algorithm>
#include <cstdlib>
#include <optional>
#include <set>
#include <utility>
#include <vector>
#include "ADS/complexity/impl/EasyCases.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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:

  • twoSat(), SATISFIABILITY with at most two literals per clause (2-SATISFIABILITY), by the graph of the implications \( \neg a \Rightarrow b \) and \( \neg b \Rightarrow a \) of each clause \( a \vee b \): the formula is satisfiable if and only if no variable is in the strongly connected component of its negation, and a variable is true if its component comes after that of its negation in topological order, \( O( n + m ) \);
  • twoColoring(), the 2-COLORABILITY, by a visit in breadth of each component, \( O( n^2 ) \) on the adjacency matrix;
  • bipartiteMatching(), the 2-DIMENSIONAL MATCHING, by a maximum flow from a source to the left nodes, along the edges, to a sink;
  • intervalGraph(), the graph whose nodes are intervals \( [ a , b ) \) and whose edges join those that intersect, and on it intervalClique(), the largest number of intervals through a point, by a sweep in \( O( n \log n ) \); the independent set and the coloring of intervals are in Intervals.h. The other easy halves are linearTiling() in Domino.h (tested in chapter 18), eulerianCycle() in EulerianCycle.h, fractionalKnapsack() in FractionalKnapsack.h, kruskal() in MinimumSpanningTree.h and shortestPaths() in ShortestPaths.h.

The contracts [see Contract.h]: the inputs are instances of the polynomial problems (clauses of at most two literals, unoriented graphs, intervals with \( a \le b \)) and each solution returned is checked as a postcondition. The operations are defined in EasyCases.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, Conclusione, https://algoritmicamente.it
B. Aspvall, M. F. Plass, R. E. Tarjan, A linear-time algorithm for testing the truth of certain quantified boolean formulas, Information Processing Letters 8(3), 121-123, 1979, doi:10.1016/0020-0190(79)90002-4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa