-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11_ KuhnS_Algorithm.java
More file actions
56 lines (53 loc) · 1.49 KB
/
day11_ KuhnS_Algorithm.java
File metadata and controls
56 lines (53 loc) · 1.49 KB
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
class Solution
{
boolean bpm(int bpGraph[][], int u,
boolean seen[], int matchR[], int M, int N)
{
// Try every job one by one
for (int v = 0; v < N; v++)
{
// If applicant u is interested
// in job v and v is not visited
if (bpGraph[u][v]==1 && !seen[v])
{
// Mark v as visited
seen[v] = true;
if (matchR[v] < 0 || bpm(bpGraph, matchR[v],
seen, matchR, M, N))
{
matchR[v] = u;
return true;
}
}
}
return false;
}
// Returns maximum number
// of matching from M to N
int maxBPM(int bpGraph[][], int M, int N)
{
int matchR[] = new int[N];
for(int i = 0; i < N; ++i)
matchR[i] = -1;
// Count of jobs assigned to applicants
int result = 0;
for (int u = 0; u < M; u++)
{
boolean seen[] =new boolean[N] ;
for(int i = 0; i < N; ++i)
seen[i] = false;
// Find if the applicant 'u' can get a job
if (bpm(bpGraph, u, seen, matchR, M, N))
result++;
}
return result;
}
public int maximumMatch(int[][] G)
{
// Code here
int M=G.length;
int N=G[0].length;
int ans = maxBPM(G,M,N);
return ans;
}
}