Question

Can I have a bipartite matching java program best solution

Can I have a bipartite matching java program best solution

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

import java.lang.*;

import java.io.*;

  

class GFG

{

    // P is the number of applicants

    // and Q is the number of jobs

    static final int P = 6;

    static final int Q = 6;

  

    // A DFS recursive function that returns true if a matching for vertex u is possible

    boolean bpm(boolean bpGraph[][], int u, boolean seen[], int matchR[])

    {

        for (int v = 0; v < Q; v++)

        {

            // If applicant u is interested in job v and v is not visited

            if (bpGraph[u][v] && !seen[v])

            {

                  

                // Mark v as visited

                seen[v] = true;

  

                // If job 'v' is not assigned to an applicant or previously assigned applicant for job v (which is matchR[v]) has an //alternate job available. Since v is marked as visited, matchR[v] in the following recursive call will not get the job 'v' again

                if (matchR[v] < 0 || bpm(bpGraph, matchR[v],

                                         seen, matchR))

                {

                    matchR[v] = u;

                    return true;

                }

            }

        }

        return false;

    }

  

    //It will return the maximum number of matching from M to N

    int maxBPM(boolean bpGraph[][])

    {

        // Creating an  array to keep track of the  applicants assigned to jobs. The value of matchR[i] is the applicant number //assigned to job i, the value -1 indicates nobody is assigned .

        int matchR[] = new int[N];

  

        // Initially all jobs are available to be assigned

        for(int i = 0; i < Q; ++i)

            matchR[i] = -1;

  

        // It will store the count of jobs assigned to applicants

        int result = 0;

        for (int u = 0; u < P; u++)

        {

            // Mark all jobs as not seen for next applicant.

            boolean seen[] =new boolean[Q] ;

            for(int i = 0; i < Q; ++i)

                seen[i] = false;

  

            // Find if 'u' can get a job

            if (bpm(bpGraph, u, seen, matchR))

                result++;

        }

        return result;

    }

  

  

    public static void main (String[] args) throws java.lang.Exception

    {

        // Let us create a bpGraph

        boolean bpGraph[][] = new boolean[][]; // assign 6*6 i.e. 36 values to the bpGraph

        GFG m = new GFG();

        System.out.println( "Maximum number of applicants that can get job is "+ m.maxBPM(bpGraph));

    }

}

Add a comment
Know the answer?
Add Answer to:
Can I have a bipartite matching java program best solution
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT