realsdx

bp-gfg

Sep 2nd, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. # Python program to find
  2. # maximal Bipartite matching.
  3.  
  4. class GFG:
  5. def __init__(self,graph):
  6.  
  7. # residual graph
  8. self.graph = graph
  9. self.ppl = len(graph)
  10. self.jobs = len(graph[0])
  11.  
  12. # A DFS based recursive function
  13. # that returns true if a matching
  14. # for vertex u is possible
  15. def bpm(self, u, matchR, seen):
  16.  
  17. # Try every job one by one
  18. for v in range(self.jobs):
  19.  
  20. # If applicant u is interested
  21. # in job v and v is not seen
  22. if self.graph[u][v] and seen[v] == False:
  23.  
  24. # Mark v as visited
  25. seen[v] = True
  26.  
  27. '''If job 'v' is not assigned to
  28. an applicant OR previously assigned
  29. applicant for job v (which is matchR[v])
  30. has an alternate job available.
  31. Since v is marked as visited in the
  32. above line, matchR[v] in the following
  33. recursive call will not get job 'v' again'''
  34. if matchR[v] == -1 or self.bpm(matchR[v],
  35. matchR, seen):
  36. matchR[v] = u
  37. return True
  38. return False
  39.  
  40. # Returns maximum number of matching
  41. def maxBPM(self):
  42. '''An array to keep track of the
  43. applicants assigned to jobs.
  44. The value of matchR[i] is the
  45. applicant number assigned to job i,
  46. the value -1 indicates nobody is assigned.'''
  47. matchR = [-1] * self.jobs
  48.  
  49. # Count of jobs assigned to applicants
  50. result = 0
  51. for i in range(self.ppl):
  52.  
  53. # Mark all jobs as not seen for next applicant.
  54. seen = [False] * self.jobs
  55.  
  56. # Find if the applicant 'u' can get a job
  57. if self.bpm(i, matchR, seen):
  58. result += 1
  59. return result
  60.  
  61.  
  62. bpGraph =[[0, 1, 1, 0, 0, 0],
  63. [1, 0, 0, 1, 0, 0],
  64. [0, 0, 1, 0, 0, 0],
  65. [0, 0, 1, 1, 0, 0],
  66. [0, 0, 0, 0, 0, 0],
  67. [0, 0, 0, 0, 0, 1]]
  68.  
  69. g = GFG(bpGraph)
  70.  
  71. print ("Maximum number of applicants that can get job is %d " % g.maxBPM())
Add Comment
Please, Sign In to add comment