souffle  2.0.2-371-g6315b36
Public Types | Public Member Functions | Data Fields | Protected Member Functions | Private Types | Private Attributes
souffle::ram::analysis::MaxMatching Class Reference

#include <Index.h>

Inheritance diagram for souffle::ram::analysis::MaxMatching:
Inheritance graph
Collaboration diagram for souffle::ram::analysis::MaxMatching:
Collaboration graph

Public Types

using Distance = int
 
using Matchings = std::unordered_map< Node, Node >
 Matching represent a solution of the matching, i.e., which node in the bi-partite graph maps to another node. More...
 
using Node = uint32_t
 
using Nodes = std::unordered_set< Node >
 

Public Member Functions

void addEdge (Node u, Node v)
 @Brief add an edge to the bi-partite graph More...
 
int getNumMatchings () const
 @Brief get number of matches in the solution More...
 
const Matchingssolve ()
 @Brief solve the maximum matching problem More...
 

Data Fields

const Distance InfiniteDistance = -1
 
const Node NullVertex = 0
 

Protected Member Functions

bool bfSearch ()
 @Brief perform a breadth first search in the graph More...
 
bool dfSearch (Node u)
 @Brief perform a depth first search in the graph More...
 
Distance getDistance (Node v)
 @Brief get distance of a node More...
 
Node getMatch (Node v)
 @Brief get match for a search signature More...
 

Private Types

using DistanceMap = std::map< Node, Distance >
 distance function of nodes More...
 
using Edges = std::unordered_set< Node >
 Edges in the bi-partite graph. More...
 
using Graph = std::unordered_map< Node, Edges >
 Bi-partite graph of instance. More...
 

Private Attributes

DistanceMap distance
 
Graph graph
 
Matchings match
 

Detailed Description

@Brief Computes a maximum matching with Hopcroft-Karp algorithm

This class is a helper class for RamIndex.

This implements a standard maximum matching algorithm for a bi-partite graph also known as a marriage problem. Given a set of edges in a bi-partite graph select a subset of edges that each node in the bi-partite graph has at most one adjacent edge associated with.

The nodes of the bi-partite graph represent index-signatures stemming from RAM operations and RAM existence checks for a relation. A relation between two nodes represent whether an index operation subsumes another operation.

Source: http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm#Pseudocode

Definition at line 112 of file Index.h.

Member Typedef Documentation

◆ Distance

Definition at line 118 of file Index.h.

◆ DistanceMap

distance function of nodes

Definition at line 188 of file Index.h.

◆ Edges

using souffle::ram::analysis::MaxMatching::Edges = std::unordered_set<Node>
private

Edges in the bi-partite graph.

Definition at line 180 of file Index.h.

◆ Graph

using souffle::ram::analysis::MaxMatching::Graph = std::unordered_map<Node, Edges>
private

Bi-partite graph of instance.

Definition at line 184 of file Index.h.

◆ Matchings

Matching represent a solution of the matching, i.e., which node in the bi-partite graph maps to another node.

If no map exist for a node, there is no adjacent edge exists for that node.

Definition at line 124 of file Index.h.

◆ Node

Definition at line 114 of file Index.h.

◆ Nodes

using souffle::ram::analysis::MaxMatching::Nodes = std::unordered_set<Node>

Definition at line 116 of file Index.h.

Member Function Documentation

◆ addEdge()

void souffle::ram::analysis::MaxMatching::addEdge ( Node  u,
Node  v 
)

@Brief add an edge to the bi-partite graph

Parameters
usearch signature
vsubsuming search signature

Definition at line 170 of file Index.cpp.

170  {
171  graph[u].insert(v);
172  }
173 }
174 
176  auto it = match.find(v);
177  if (it == match.end()) {
178  return NullVertex;
179  }

◆ bfSearch()

bool souffle::ram::analysis::MaxMatching::bfSearch ( )
protected

@Brief perform a breadth first search in the graph

Definition at line 197 of file Index.cpp.

199  {
200  distance[it.first] = InfiniteDistance;
201  }
202  }
203 
205  while (!bfQueue.empty()) {
206  u = bfQueue.front();
207  bfQueue.pop();
208  assert(u != NullVertex);
209  const Edges& children = graph[u];
210  for (auto it : children) {
211  Node mv = getMatch(it);
212  if (getDistance(mv) == InfiniteDistance) {
213  distance[mv] = getDistance(u) + 1;
214  if (mv != NullVertex) {
215  bfQueue.push(mv);
216  }
217  }
218  }
219  }
221 }
222 
223 bool MaxMatching::dfSearch(Node u) {
224  if (u != 0) {
225  Edges& children = graph[u];
226  for (auto v : children) {
227  if (getDistance(getMatch(v)) == getDistance(u) + 1) {

◆ dfSearch()

bool souffle::ram::analysis::MaxMatching::dfSearch ( Node  u)
protected

@Brief perform a depth first search in the graph

Parameters
usearch signature

Definition at line 229 of file Index.cpp.

242  {
243  while (bfSearch()) {
244  std::vector<Node> keys(graph.size());
245  for (auto& it : graph) {
246  keys.push_back(it.first);

Referenced by solve().

◆ getDistance()

MaxMatching::Distance souffle::ram::analysis::MaxMatching::getDistance ( Node  v)
protected

@Brief get distance of a node

Definition at line 189 of file Index.cpp.

191  {
192  Node u;
193  std::queue<Node> bfQueue;
194  // Build layers
195  for (auto& it : graph) {

◆ getMatch()

MaxMatching::Node souffle::ram::analysis::MaxMatching::getMatch ( Node  v)
protected

@Brief get match for a search signature

Parameters
vsearch signature

Definition at line 181 of file Index.cpp.

183  {
184  auto it = distance.find(v);
185  if (it == distance.end()) {
186  return InfiniteDistance;
187  }

Referenced by solve().

◆ getNumMatchings()

int souffle::ram::analysis::MaxMatching::getNumMatchings ( ) const
inline

@Brief get number of matches in the solution

Returns
number of matches

Definition at line 142 of file Index.h.

142  {
143  return match.size() / 2;
144  }

References match.

◆ solve()

const MaxMatching::Matchings & souffle::ram::analysis::MaxMatching::solve ( )

@Brief solve the maximum matching problem

Returns
returns the matching

Definition at line 248 of file Index.cpp.

248  : keys) {
249  if (getMatch(node) == NullVertex) {
250  dfSearch(node);
251  }
252  }
253  }
254  return match;
255 }
256 
258  // map the keys in the key set to lexicographical order
259  if (searches.empty()) {
260  return;
261  }

References dfSearch(), getMatch(), and NullVertex.

Here is the call graph for this function:

Field Documentation

◆ distance

DistanceMap souffle::ram::analysis::MaxMatching::distance
private

Definition at line 192 of file Index.h.

◆ graph

Graph souffle::ram::analysis::MaxMatching::graph
private

Definition at line 191 of file Index.h.

◆ InfiniteDistance

const Distance souffle::ram::analysis::MaxMatching::InfiniteDistance = -1

Definition at line 130 of file Index.h.

◆ match

Matchings souffle::ram::analysis::MaxMatching::match
private

Definition at line 190 of file Index.h.

Referenced by getNumMatchings().

◆ NullVertex

const Node souffle::ram::analysis::MaxMatching::NullVertex = 0

Definition at line 127 of file Index.h.

Referenced by solve().


The documentation for this class was generated from the following files:
souffle::ram::analysis::MaxMatching::getDistance
Distance getDistance(Node v)
@Brief get distance of a node
Definition: Index.cpp:189
souffle::ram::analysis::MaxMatching::NullVertex
const Node NullVertex
Definition: Index.h:127
souffle::ram::analysis::MaxMatching::getMatch
Node getMatch(Node v)
@Brief get match for a search signature
Definition: Index.cpp:181
souffle::ram::analysis::MaxMatching::InfiniteDistance
const Distance InfiniteDistance
Definition: Index.h:130
souffle::ram::analysis::MaxMatching::bfSearch
bool bfSearch()
@Brief perform a breadth first search in the graph
Definition: Index.cpp:197
souffle::ram::analysis::MaxMatching::graph
Graph graph
Definition: Index.h:191
souffle::ram::analysis::MinIndexSelection::solve
void solve()
@Brief map the keys in the key set to lexicographical order
Definition: Index.cpp:263
souffle::ram::analysis::MaxMatching::distance
DistanceMap distance
Definition: Index.h:192
souffle::ram::analysis::MaxMatching::Node
uint32_t Node
Definition: Index.h:114
souffle::ram::analysis::MaxMatching::match
Matchings match
Definition: Index.h:190
souffle::ram::analysis::MaxMatching::dfSearch
bool dfSearch(Node u)
@Brief perform a depth first search in the graph
Definition: Index.cpp:229
souffle::ram::analysis::MaxMatching::Edges
std::unordered_set< Node > Edges
Edges in the bi-partite graph.
Definition: Index.h:180