<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
(	O
sometimes	O
more	O
accurately	O
called	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
–	I-Algorithm
Karzanov	I-Algorithm
algorithm	I-Algorithm
)	O
is	O
an	O
algorithm	O
that	O
takes	O
a	O
bipartite	O
graph	B-Application
as	O
input	O
and	O
produces	O
a	O
maximum-cardinality	O
matching	O
as	O
output	O
—	O
a	O
set	O
of	O
as	O
many	O
edges	O
as	O
possible	O
with	O
the	O
property	O
that	O
no	O
two	O
edges	O
share	O
an	O
endpoint	O
.	O
</s>
<s>
It	O
runs	O
in	O
time	O
in	O
the	O
worst	B-General_Concept
case	I-General_Concept
,	O
where	O
is	O
set	O
of	O
edges	O
in	O
the	O
graph	B-Application
,	O
is	O
set	O
of	O
vertices	O
of	O
the	O
graph	B-Application
,	O
and	O
it	O
is	O
assumed	O
that	O
.	O
</s>
<s>
As	O
in	O
previous	O
methods	O
for	O
matching	O
such	O
as	O
the	O
Hungarian	B-Algorithm
algorithm	I-Algorithm
and	O
the	O
work	O
of	O
,	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
repeatedly	O
increases	O
the	O
size	O
of	O
a	O
partial	O
matching	O
by	O
finding	O
augmenting	O
paths	O
.	O
</s>
<s>
These	O
paths	O
are	O
sequences	O
of	O
edges	O
of	O
the	O
graph	B-Application
,	O
which	O
alternate	O
between	O
edges	O
in	O
the	O
matching	O
and	O
edges	O
out	O
of	O
the	O
partial	O
matching	O
,	O
and	O
where	O
the	O
initial	O
and	O
final	O
edge	O
are	O
not	O
in	O
the	O
partial	O
matching	O
.	O
</s>
<s>
Simpler	O
algorithms	O
for	O
bipartite	O
matching	O
,	O
such	O
as	O
the	O
Ford	O
–	O
Fulkerson	O
algorithm‚	O
find	O
one	O
augmenting	O
path	O
per	O
iteration	O
:	O
the	O
Hopcroft-Karp	B-Algorithm
algorithm	I-Algorithm
instead	O
finds	O
a	O
maximal	O
set	O
of	O
shortest	O
augmenting	O
paths	O
,	O
so	O
as	O
to	O
ensure	O
that	O
only	O
iterations	O
are	O
needed	O
instead	O
of	O
iterations	O
.	O
</s>
<s>
The	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
can	O
be	O
seen	O
as	O
a	O
special	O
case	O
of	O
Dinic	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
for	O
the	O
maximum-flow	B-Algorithm
problem	I-Algorithm
.	O
</s>
<s>
An	O
augmenting	O
path	O
in	O
a	O
matching	O
problem	O
is	O
closely	O
related	O
to	O
the	O
augmenting	O
paths	O
arising	O
in	O
maximum	B-Algorithm
flow	I-Algorithm
problems	I-Algorithm
,	O
paths	O
along	O
which	O
one	O
may	O
increase	O
the	O
amount	O
of	O
flow	O
between	O
the	O
terminals	O
of	O
the	O
flow	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
transform	O
the	O
bipartite	O
matching	O
problem	O
into	O
a	O
maximum	B-Algorithm
flow	I-Algorithm
instance	O
,	O
such	O
that	O
the	O
alternating	O
paths	O
of	O
the	O
matching	O
problem	O
become	O
augmenting	O
paths	O
of	O
the	O
flow	O
problem	O
.	O
</s>
<s>
A	O
generalization	O
of	O
the	O
technique	O
used	O
in	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
to	O
find	O
maximum	B-Algorithm
flow	I-Algorithm
in	O
an	O
arbitrary	O
network	O
is	O
known	O
as	O
Dinic	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
may	O
be	O
expressed	O
in	O
the	O
following	O
pseudocode	B-Language
.	O
</s>
<s>
A	O
breadth-first	B-Algorithm
search	I-Algorithm
partitions	O
the	O
vertices	O
of	O
the	O
graph	B-Application
into	O
layers	O
.	O
</s>
<s>
This	O
set	O
may	O
be	O
computed	O
by	O
depth-first	B-Algorithm
search	I-Algorithm
(	O
DFS	O
)	O
from	O
to	O
the	O
free	O
vertices	O
in	O
,	O
using	O
the	O
breadth	O
first	O
layering	O
to	O
guide	O
the	O
search	O
:	O
the	O
DFS	O
is	O
only	O
allowed	O
to	O
follow	O
edges	O
that	O
lead	O
to	O
an	O
unused	O
vertex	O
in	O
the	O
previous	O
layer	O
,	O
and	O
paths	O
in	O
the	O
DFS	O
tree	O
must	O
alternate	O
between	O
matched	O
and	O
unmatched	O
edges	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
work	O
in	O
the	O
other	O
direction	O
,	O
from	O
free	O
vertices	O
in	O
to	O
those	O
in	O
,	O
which	O
is	O
the	O
variant	O
used	O
in	O
the	O
pseudocode	B-Language
.	O
</s>
<s>
The	O
algorithm	O
terminates	O
when	O
no	O
more	O
augmenting	O
paths	O
are	O
found	O
in	O
the	O
breadth	B-Algorithm
first	I-Algorithm
search	I-Algorithm
part	O
of	O
one	O
of	O
the	O
phases	O
.	O
</s>
<s>
Each	O
phase	O
consists	O
of	O
a	O
single	O
breadth	B-Algorithm
first	I-Algorithm
search	I-Algorithm
and	O
a	O
single	O
depth-first	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
Therefore	O
,	O
the	O
first	O
phases	O
,	O
in	O
a	O
graph	B-Application
with	O
vertices	O
and	O
edges	O
,	O
take	O
time	O
.	O
</s>
<s>
Since	O
the	O
algorithm	O
performs	O
a	O
total	O
of	O
at	O
most	O
phases	O
,	O
it	O
takes	O
a	O
total	O
time	O
of	O
in	O
the	O
worst	B-General_Concept
case	I-General_Concept
.	O
</s>
<s>
In	O
many	O
instances	O
,	O
however	O
,	O
the	O
time	O
taken	O
by	O
the	O
algorithm	O
may	O
be	O
even	O
faster	O
than	O
this	O
worst	B-General_Concept
case	I-General_Concept
analysis	O
indicates	O
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
the	O
average	B-General_Concept
case	I-General_Concept
for	O
sparse	O
bipartite	O
random	O
graphs	O
,	O
(	O
improving	O
a	O
previous	O
result	O
of	O
)	O
showed	O
that	O
with	O
high	O
probability	O
all	O
non-optimal	O
matchings	O
have	O
augmenting	O
paths	O
of	O
logarithmic	O
length	O
.	O
</s>
<s>
As	O
a	O
consequence	O
,	O
for	O
these	O
graphs	O
,	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
takes	O
phases	O
and	O
total	O
time	O
.	O
</s>
<s>
For	O
sparse	O
graphs	O
,	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
continues	O
to	O
have	O
the	O
best	O
known	O
worst-case	B-General_Concept
performance	I-General_Concept
,	O
but	O
for	O
dense	O
graphs	O
(	O
)	O
a	O
more	O
recent	O
algorithm	O
by	O
achieves	O
a	O
slightly	O
better	O
time	O
bound	O
,	O
.	O
</s>
<s>
Their	O
algorithm	O
is	O
based	O
on	O
using	O
a	O
push-relabel	B-Algorithm
maximum	I-Algorithm
flow	I-Algorithm
algorithm	I-Algorithm
and	O
then	O
,	O
when	O
the	O
matching	O
created	O
by	O
this	O
algorithm	O
becomes	O
close	O
to	O
optimum	O
,	O
switching	O
to	O
the	O
Hopcroft	O
–	O
Karp	O
method	O
.	O
</s>
<s>
Their	O
results	O
in	O
general	O
tend	O
to	O
show	O
that	O
the	O
Hopcroft	O
–	O
Karp	O
method	O
is	O
not	O
as	O
good	O
in	O
practice	O
as	O
it	O
is	O
in	O
theory	O
:	O
it	O
is	O
outperformed	O
both	O
by	O
simpler	O
breadth-first	B-Algorithm
and	O
depth-first	B-Algorithm
strategies	O
for	O
finding	O
augmenting	O
paths	O
,	O
and	O
by	O
push-relabel	O
techniques	O
.	O
</s>
<s>
Building	O
on	O
the	O
work	O
of	O
several	O
slower	O
predecessors	O
,	O
showed	O
how	O
to	O
implement	O
a	O
phase	O
in	O
linear	O
time	O
,	O
resulting	O
in	O
a	O
non-bipartite	O
matching	O
algorithm	O
with	O
the	O
same	O
time	O
bound	O
as	O
the	O
Hopcroft	B-Algorithm
–	I-Algorithm
Karp	I-Algorithm
algorithm	I-Algorithm
for	O
bipartite	O
graphs	O
.	O
</s>
<s>
Let	O
the	O
vertices	O
of	O
our	O
graph	B-Application
be	O
partitioned	O
in	O
U	O
and	O
V	O
,	O
and	O
consider	O
a	O
partial	O
matching	O
,	O
as	O
indicated	O
by	O
the	O
Pair_U	O
and	O
Pair_V	O
tables	O
that	O
contain	O
the	O
one	O
vertex	O
to	O
which	O
each	O
vertex	O
of	O
U	O
and	O
of	O
V	O
is	O
matched	O
,	O
or	O
NIL	O
for	O
unmatched	O
vertices	O
.	O
</s>
<s>
The	O
key	O
idea	O
is	O
to	O
add	O
two	O
dummy	O
vertices	O
on	O
each	O
side	O
of	O
the	O
graph	B-Application
:	O
uDummy	O
connected	O
to	O
all	O
unmatched	O
vertices	O
in	O
U	O
and	O
vDummy	O
connected	O
to	O
all	O
unmatched	O
vertices	O
in	O
V	O
.	O
Now	O
,	O
if	O
we	O
run	O
a	O
breadth-first	B-Algorithm
search	I-Algorithm
(	O
BFS	O
)	O
from	O
uDummy	O
to	O
vDummy	O
then	O
we	O
can	O
get	O
the	O
paths	O
of	O
minimal	O
length	O
that	O
connect	O
currently	O
unmatched	O
vertices	O
in	O
U	O
to	O
currently	O
unmatched	O
vertices	O
in	O
V	O
.	O
Note	O
that	O
,	O
as	O
the	O
graph	B-Application
is	O
bipartite	O
,	O
these	O
paths	O
always	O
alternate	O
between	O
vertices	O
in	O
U	O
and	O
vertices	O
in	O
V	O
,	O
and	O
we	O
require	O
in	O
our	O
BFS	O
that	O
when	O
going	O
from	O
V	O
to	O
U	O
,	O
we	O
always	O
select	O
a	O
matched	O
edge	O
.	O
</s>
<s>
If	O
BFS	O
returns	O
true	O
,	O
then	O
we	O
can	O
go	O
ahead	O
and	O
update	O
the	O
pairing	O
for	O
vertices	O
on	O
the	O
minimal-length	O
paths	O
found	O
from	O
U	O
to	O
V	O
:	O
we	O
do	O
so	O
using	O
a	O
depth-first	B-Algorithm
search	I-Algorithm
(	O
DFS	O
)	O
.	O
</s>
<s>
As	O
for	O
vDummy	O
,	O
it	O
is	O
denoted	O
as	O
NIL	O
in	O
the	O
pseudocode	B-Language
above	O
.	O
</s>
