Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Placment Of students

Moderate
0/80
Average time to solve is 15m
profile
Contributed by
73 upvotes
Asked in companies
SamsungMindtreeNagaaro

Problem statement

The coordinator of the placement cell has received many applications of students applying in different companies. There are M students and N companies who are offering jobs. Each student is interested in a particular number of companies for a job. Each job opening can only accept one student and a student can only have 1 job. As a placement coordinator, you want to place a maximum number of students.

Your task is to find the maximum number of students that can be placed in one of their desired jobs

The data about the set of favourable jobs are given in the form of an M * N binary matrix named ‘mat’, i.e for M students we have M rows each having N integers. Now for example if the first candidate is interested in job1 the value of mat[i][j] will be 1 otherwise it will be 0.

Note:
It is possible that a single candidate is interested in multiple jobs but he can take up only one of the job out of his favourable jobs, also there is no priority in jobs, i.e all favourable jobs are equally favourable to the candidate
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.

The first line of each test case contains 2 space-separated integers ‘M’ and ‘N’ which denote the number of applicants and number of jobs respectively.

The next ‘M’ lines have ‘N’ space-separated binary numbers which denote the favourable jobs of each applicant.
Output Format:
For each test case, return a single integer denoting the maximum number of applicants that got their favourable jobs

The output for each test case is in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= M * N <= 3000

Time Limit: 1sec
Sample Input 1:
2
3 2
1 0
0 1
0 0
4 4
0 0 1 1
1 1 1 1
0 0 0 1
1 0 0 0
Sample Output 1:
2
4
Explanation of Sample Input 1:
Test Case 1: 

We see that we have 3 candidates and 2 jobs. 
Candidate 1 wants jobs 1 only
Candidate 2 wants job 2 only
Candidate 3 does not like any job

So, with this given arrangement, we can have that candidate 1 gets a job1 and candidate 2 gets job 2. So we placed 2 candidates. Hence we return 2.

Test Case 2:

We have 4 candidates and 4 jobs
Candidate 1 wants jobs 3 or jobs 4.
Candidate 2 wants any of the 4 jobs (pretty desperate)
Candidate 3 wants only job 4 
Candidate 4 wants only job 1

So with this arrangement, we can have candidate 1 with job 3, candidate 2 with job 2 candidates 3 with job 4, and candidate 4 with job 1.
Hence we placed all the candidates, hence we return 4.
Sample Input 2:
2 
3 4
1 1 1 1
1 1 1 1
1 1 1 1
6 6
0 1 1 0 0 0
1 0 0 1 0 0
0 0 1 0 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
Sample Output 2:
3
5
Hint

Try to match people with the jobs they want using a DFS on a bipartite graph.

Approaches (1)
DFS APPROACH
  • The key idea is to think of the problem as a bipartite graph where one side has nodes of applicants and the other side has nodes of jobs and the edges in the graph denote the relation between the jobs and candidate. (There is a directed edge between the candidate and job if the candidate wants that job.
  • Lets consider the following example:

 

0 1 1 0 0 0

1 0 0 1 0 0

0 0 1 0 0 0

0 0 1 1 0 0

0 0 0 0 0 0

0 0 0 0 0 1

 

  • The arrows in grey signify the ideal matches.
  • But how can this be achieved?
    • We can achieve this by simply doing a depth-first search from each applicant node and see if any of his preferred jobs are available or not. We can keep track of assigned jobs using an ‘assign’ array and Keep the count of the number of people who have their jobs assigned.

 

We can take the following approach.

 

  • Make an array ‘assign’ which keeps track of assignment of jobs i.e what job is assigned to what person. Initially, it is -1 for all ‘N’ elements.
  • Make a variable ‘jobCount’ to keep track of the number of applicants placed.
  • Make all the jobs unvisited initially.
  • Then we loop through all the applicants and for each applicant we check all favorable jobs for that particular applicant and if we find a job which is either not assigned or if assigned to some other applicant, the other applicant has other jobs available, we assign the current job to that applicant.
  • We do this using a DFS like function that goes through the row of the current applicant and tries to assign one of his favorite jobs using the above-mentioned algorithm.
  • If we are able to assign a job to the applicant we increment the value of ‘jobCount’.
  • Finally, after traversing all nodes, we return the value of ‘jobCount’.
Time Complexity

O(N * M), where ‘N’ is the number of applicants and ‘M’ is the number of jobs available.

 

For each applicant, we do a DFS which will take the order of ‘M’ time and we do this for all ‘N’ applicants.

Space Complexity

O(N), where ‘N’ is the number of applicants

 

We need an array of size ‘N’ to keep track of jobs assigned

Code Solution
(100% EXP penalty)
Placment Of students
All tags
Sort by
Search icon

Interview problems

This has TLE but explained

#include <bits/stdc++.h> 

 

// Make a function to check job available for that student or not

bool isJob(int id, vector<vector<int>>& mat, vector<int>& jobAlloted, vector<bool> check, int n){

    // Iterate over each job for this student

    for(int i=0; i<n; i++){

        // Check student job eligibility and already no job

        if(mat[id][i]==1 && !check[i]){

            check[i] = true;    // allot job 

            // if that job not alloted to anyone or if alloted, other job for that student available or not

            if(jobAlloted[i] == -1 || isJob(jobAlloted[i], mat, jobAlloted, check, n)){

                jobAlloted[i]=id;

                return true;

            }

        }

    }

    return false;

}

int maxMatch(vector<vector<int> > &mat) {

    int m = mat.size(), n = mat[0].size();

    // Number of job is fixed, to store which job corr. to which student

    vector<int> jobAlloted(n,-1);  

    int jobCount = 0;

    // Iterate over each student to allot a job

    for(int i=0; i<m; i++){

        vector<bool> check(n, false);

        if(isJob(i, mat, jobAlloted, check, n)){

            jobCount++;

        }

    }

    return jobCount;

}

6 views
0 replies
0 upvotes

Interview problems

Maximum bipartite graph matching based problem

Approach:

  1. For each student, use a depth-first search (DFS) to find an augmenting path in the bipartite graph.
  2. Keep track of the matching of students to jobs using an array.
  3. Iterate through each student and attempt to find an augmenting path using DFS.
  4. If an augmenting path is found, increment the count of placed students.
  5. The maximum number of placed students will be the result.

Implementation:

  1. Define a DFS function to find an augmenting path in the bipartite graph.
  2. Initialize an array to keep track of the matching of students to jobs.
  3. Iterate through each student, call DFS to find an augmenting path, and update the matching array.
#include <vector>

using namespace std;

bool dfs(int i, vector<vector<int>> &mat, vector<int> &match, vector<bool> &check, int n) {
    for (int j = 0; j < n; j++) {
        if (mat[i][j] == 1 && !check[j]) {
            check[j] = true;
            if (match[j] == -1 || dfs(match[j], mat, match, check, n)) {
                match[j] = i;
                return true;
            }
        }
    }
    return false;
}

int maxMatch(vector<vector<int>> &mat) {
    // Number of students and jobs
    int m = mat.size();
    int n = mat[0].size();

    // Matching array
    vector<int> match(n, -1);

    // Count of placed students
    int count = 0;

    // Iterate through each student
    for (int i = 0; i < m; i++) {
        // Visited array for DFS
        vector<bool> check(n, false);

        // If an augmenting path is found, increment count
        if (dfs(i, mat, match, check, n)) {
            count++;
        }
    }

    return count;
}
161 views
0 replies
1 upvote

Interview problems

CPP DFS

#include <bits/stdc++.h> ;
bool dfs(int i,vector<vector<int> > &mat,vector<int>&vis,vector<int>&dp){
    for(int j=0;j<mat[0].size();j++){
        if(mat[i][j] and dp[j]==0){
            dp[j]=1;
            if(vis[j]==-1 or dfs(vis[j],mat,vis,dp)){
                vis[j]=i;
                return true;
            }
        }
    }
    return false;
}
int maxMatch(vector<vector<int> > &mat) 
{
    int n=mat.size(),m=mat[0].size(),cnt=0;
    vector<int>vis(m,-1);
    for(int i=0;i<n;i++){
        vector<int>dp(m,0);
        if(dfs(i,mat,vis,dp)){
            cnt++;
        }
    }
    return cnt;
    
}
335 views
0 replies
1 upvote

Interview problems

python solution

def asg(mat, u, n, vis, ch):
    for i in range(n):
        if mat[u][i] and not vis[i]:
            vis[i] = True
            if ch[i] < 0 or asg(mat, ch[i], n, vis, ch):
                ch[i] = u
                return True
    return False

def solve(n, m, mat):
    res = 0
    ch = [-1] * n

    for i in range(m):
        vis = [False] * n
        if asg(mat, i, n, vis, ch):
            res += 1

    return res

def maxMatch(mat):
    n = len(mat[0])
    m = len(mat)
    return solve(n, m, mat)

python

155 views
0 replies
0 upvotes

Interview problems

C++ code 88.09% success

#include<bits/stdc++.h>

bool asg(vector<vector<int>> &mat,int u,int &n,vector<int> &vis,vector<int> &ch)

{

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

   {

       if(mat[u][i] && !vis[i])

       {

           vis[i]=true;

           if(ch[i]<0 || asg(mat,ch[i],n,vis,ch))

           {

               ch[i]=u;

               return true;

           }

           

       }

   }

   return false;

}

int solve(int n,int m,vector<vector<int>> &mat)

{

   int res=0;

   vector<int> ch(n,-1);

   

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

   {

       vector<int> vis(n,0);

       if(asg(mat,i,n,vis,ch))

           res++;

   }

   return res;

}

int maxMatch(vector<vector<int> > &mat)  

{

   int n=mat[0].size(),m=mat.size();

   return solve(n,m,mat);

}

100daysofcode

426 views
1 reply
1 upvote

can we solve this using dp?

I tried using the dp but not getting the correct output.

 

96 views
0 replies
0 upvotes

Interview problems

Discussion thread on Interview Problem | Placment Of students

Hey everyone, creating this thread to discuss the interview problem - Placment Of students.

 

Practise this interview question on Coding Ninjas Studio (hyperlinked with the following link): Placment Of students

 

656 views
11 replies
0 upvotes
Full screen
Console