Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
graph::HKGraph Class Reference

Represents Bipartite graph for Hopcroft Karp implementation. More...

Collaboration diagram for graph::HKGraph:
[legend]

Public Member Functions

 HKGraph ()
 Default Constructor for initialization.
 
 HKGraph (int m, int n)
 Constructor for initialization. More...
 
void addEdge (int u, int v)
 function to add edge from u to v More...
 
bool bfs ()
 This function checks for the possibility of augmented path availability. More...
 
bool dfs (int u)
 This functions checks whether an augmenting path is available exists beginning with free vertex u. More...
 
int hopcroftKarpAlgorithm ()
 This function counts the number of augmenting paths between left and right sides of the Bipartite graph. More...
 

Private Attributes

int m {}
 m is the number of vertices on left side of Bipartite Graph
 
int n {}
 n is the number of vertices on right side of Bipartite Graph
 
const int NIL {0}
 
const int INF {INT_MAX}
 
std::vector< std::list< int > > adj
 adj[u] stores adjacents of left side and 0 is used for dummy vertex
 
std::vector< int > pair_u
 value of vertex 'u' ranges from 1 to m
 
std::vector< int > pair_v
 value of vertex 'v' ranges from 1 to n
 
std::vector< int > dist
 dist represents the distance between vertex 'u' and vertex 'v'
 

Detailed Description

Represents Bipartite graph for Hopcroft Karp implementation.

Constructor & Destructor Documentation

◆ HKGraph()

graph::HKGraph::HKGraph ( int  m,
int  n 
)

Constructor for initialization.

Parameters
mis the number of vertices on left side of Bipartite Graph
nis the number of vertices on right side of Bipartite Graph
231  {
232  this->m = m;
233  this->n = n;
235 }

Member Function Documentation

◆ addEdge()

void graph::HKGraph::addEdge ( int  u,
int  v 
)

function to add edge from u to v

Parameters
uis the position of first vertex
vis the position of second vertex
243 {
244  adj[u].push_back(v); // Add v to u’s list.
245 }
Here is the call graph for this function:

◆ bfs()

bool graph::HKGraph::bfs ( )

This function checks for the possibility of augmented path availability.

Returns
true if there is an augmenting path available
false if there is no augmenting path available
134 {
135  std::queue<int> q; // an integer queue for bfs
136 
137  // First layer of vertices (set distance as 0)
138  for (int u = 1; u <= m; u++)
139  {
140  // If this is a free vertex, add it to queue
141  if (pair_u[u] == NIL){
142 
143  dist[u] = 0; // u is not matched so distance is 0
144  q.push(u);
145  }
146 
147  else{
148  dist[u] = INF; // set distance as infinite so that this vertex is considered next time for availibility
149  }
150  }
151 
152 
153  dist[NIL] = INF; // Initialize distance to NIL as infinite
154 
155  // q is going to contain vertices of left side only.
156  while (!q.empty())
157  {
158  int u = q.front(); // dequeue a vertex
159  q.pop();
160 
161  // If this node is not NIL and can provide a shorter path to NIL then
162  if (dist[u] < dist[NIL])
163  {
164  // Get all the adjacent vertices of the dequeued vertex u
166  for (it = adj[u].begin(); it != adj[u].end(); ++it)
167  {
168  int v = *it;
169 
170  // If pair of v is not considered so far i.e. (v, pair_v[v]) is not yet explored edge.
171  if (dist[pair_v[v]] == INF)
172  {
173  dist[pair_v[v]] = dist[u] + 1;
174  q.push(pair_v[v]); // Consider the pair and push it to queue
175  }
176  }
177  }
178  }
179 
180 
181 
182  return (dist[NIL] != INF); // If we could come back to NIL using alternating path of distinct vertices then there is an augmenting path available
183 }
Here is the call graph for this function:

◆ dfs()

bool graph::HKGraph::dfs ( int  u)

This functions checks whether an augmenting path is available exists beginning with free vertex u.

Parameters
urepresents position of vertex
Returns
true if there is an augmenting path beginning with free vertex u
false if there is no augmenting path beginning with free vertex u
192 {
193  if (u != NIL)
194  {
196  for (it = adj[u].begin(); it != adj[u].end(); ++it)
197  {
198 
199  int v = *it; // Adjacent vertex of u
200 
201  // Follow the distances set by BFS search
202  if (dist[pair_v[v]] == dist[u] + 1)
203  {
204  // If dfs for pair of v also return true then new matching possible, store the matching
205  if (dfs(pair_v[v]) == true)
206  {
207  pair_v[v] = u;
208  pair_u[u] = v;
209  return true;
210  }
211  }
212  }
213 
214 
215  dist[u] = INF; // If there is no augmenting path beginning with u then set distance to infinite.
216  return false;
217  }
218  return true;
219 }
Here is the call graph for this function:

◆ hopcroftKarpAlgorithm()

int graph::HKGraph::hopcroftKarpAlgorithm ( )

This function counts the number of augmenting paths between left and right sides of the Bipartite graph.

Returns
size of maximum matching
96 {
97 
98  // pair_u[u] stores pair of u in matching on left side of Bipartite Graph.
99  // If u doesn't have any pair, then pair_u[u] is NIL
100  pair_u = std::vector<int>(m + 1,NIL);
101 
102  // pair_v[v] stores pair of v in matching on right side of Biparite Graph.
103  // If v doesn't have any pair, then pair_u[v] is NIL
104  pair_v = std::vector<int>(n + 1,NIL);
105 
106  dist = std::vector<int>(m + 1); // dist[u] stores distance of left side vertices
107 
108  int result = 0; // Initialize result
109 
110  // Keep updating the result while there is an augmenting path possible.
111  while (bfs())
112  {
113  // Find a free vertex to check for a matching
114  for (int u = 1; u <= m; u++){
115 
116  // If current vertex is free and there is
117  // an augmenting path from current vertex
118  // then increment the result
119  if (pair_u[u] == NIL && dfs(u)){
120  result++;
121  }
122  }
123  }
124  return result;
125 }
Here is the call graph for this function:

The documentation for this class was generated from the following file:
std::list
STL class.
graph::HKGraph::m
int m
m is the number of vertices on left side of Bipartite Graph
Definition: hopcroft_karp.cpp:68
graph::HKGraph::n
int n
n is the number of vertices on right side of Bipartite Graph
Definition: hopcroft_karp.cpp:69
std::vector
STL class.
std::queue
STL class.
std::vector::push_back
T push_back(T... args)
graph::HKGraph::bfs
bool bfs()
This function checks for the possibility of augmented path availability.
Definition: hopcroft_karp.cpp:133
std::begin
T begin(T... args)
graph::HKGraph::adj
std::vector< std::list< int > > adj
adj[u] stores adjacents of left side and 0 is used for dummy vertex
Definition: hopcroft_karp.cpp:73
graph::HKGraph::dist
std::vector< int > dist
dist represents the distance between vertex 'u' and vertex 'v'
Definition: hopcroft_karp.cpp:77
std::vector::end
T end(T... args)
graph::HKGraph::pair_v
std::vector< int > pair_v
value of vertex 'v' ranges from 1 to n
Definition: hopcroft_karp.cpp:76
graph::HKGraph::pair_u
std::vector< int > pair_u
value of vertex 'u' ranges from 1 to m
Definition: hopcroft_karp.cpp:75
graph::HKGraph::dfs
bool dfs(int u)
This functions checks whether an augmenting path is available exists beginning with free vertex u.
Definition: hopcroft_karp.cpp:191