FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/src/main/java/com/thealgorithms/graph/HopcroftKarp.java at master · bytecar/Java · GitHub
bytecar
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
graph
/
HopcroftKarp.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
103 lines (89 loc) · 2.69 KB
Breadcrumbs
Java
/
src
/
main
/
java
/
com
/
thealgorithms
/
graph
/
HopcroftKarp.java
Copy path
File metadata and controls
103 lines (89 loc) · 2.69 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package
com
.
thealgorithms
.
graph
;
import
java
.
util
.
ArrayDeque
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
List
;
import
java
.
util
.
Queue
;
/**
* Hopcroft–Karp algorithm for maximum bipartite matching.
*
* Left part: vertices [0,nLeft-1], Right part: [0,nRight-1].
* Adjacency list: for each left vertex u, list right vertices v it connects to.
*
* Time complexity: O(E * sqrt(V)).
*
* @see <a href="https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm">
* Wikipedia: Hopcroft–Karp algorithm</a>
* @author ptzecher
*/
public
class
HopcroftKarp
{
private
final
int
nLeft
;
private
final
List
<
List
<
Integer
>>
adj
;
private
final
int
[]
pairU
;
private
final
int
[]
pairV
;
private
final
int
[]
dist
;
public
HopcroftKarp
(
int
nLeft
,
int
nRight
,
List
<
List
<
Integer
>>
adj
) {
this
.
nLeft
=
nLeft
;
this
.
adj
=
adj
;
this
.
pairU
=
new
int
[
nLeft
];
this
.
pairV
=
new
int
[
nRight
];
this
.
dist
=
new
int
[
nLeft
];
Arrays
.
fill
(
pairU
, -
1
);
Arrays
.
fill
(
pairV
, -
1
);
}
/** Returns the size of the maximum matching. */
public
int
maxMatching
() {
int
matching
=
0
;
while
(
bfs
()) {
for
(
int
u
=
0
;
u
<
nLeft
;
u
++) {
if
(
pairU
[
u
] == -
1
&&
dfs
(
u
)) {
matching
++;
}
}
}
return
matching
;
}
// BFS to build layers
private
boolean
bfs
() {
Queue
<
Integer
>
queue
=
new
ArrayDeque
<>();
Arrays
.
fill
(
dist
, -
1
);
for
(
int
u
=
0
;
u
<
nLeft
;
u
++) {
if
(
pairU
[
u
] == -
1
) {
dist
[
u
] =
0
;
queue
.
add
(
u
);
}
}
boolean
foundAugPath
=
false
;
while
(!
queue
.
isEmpty
()) {
int
u
=
queue
.
poll
();
for
(
int
v
:
adj
.
get
(
u
)) {
int
matchedLeft
=
pairV
[
v
];
if
(
matchedLeft
== -
1
) {
foundAugPath
=
true
;
}
else
if
(
dist
[
matchedLeft
] == -
1
) {
dist
[
matchedLeft
] =
dist
[
u
] +
1
;
queue
.
add
(
matchedLeft
);
}
}
}
return
foundAugPath
;
}
// DFS to find augmenting paths within the BFS layering
private
boolean
dfs
(
int
u
) {
for
(
int
v
:
adj
.
get
(
u
)) {
int
matchedLeft
=
pairV
[
v
];
if
(
matchedLeft
== -
1
|| (
dist
[
matchedLeft
] ==
dist
[
u
] +
1
&&
dfs
(
matchedLeft
))) {
pairU
[
u
] =
v
;
pairV
[
v
] =
u
;
return
true
;
}
}
dist
[
u
] = -
1
;
return
false
;
}
public
int
[]
getLeftMatches
() {
return
pairU
.
clone
();
}
public
int
[]
getRightMatches
() {
return
pairV
.
clone
();
}
}
Back
|
FazBrowse Home
|
New Git URL