Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:37:02+00:00 2026-06-06T05:37:02+00:00

A friend asked me a question today about an assignment problem. I found a

  • 0

A friend asked me a question today about an assignment problem. I found a quite straightforward solution, but I feel that it can be made simpler and faster.
Your help would be appreciated.

The problem:
Assuming that I have N people, I need to assign them into M buildings, each building can house K people. Not all people are willing to live with each other, so i have a matrix of N*N cells and a 1 that marks the people that are willing to live with each other. If a cell contains 1 it means that I and J can live together. Obviously the matrix is symmetrical around the main diagonal.

My solution is as follows (pseudo Code):

int[] Match(int[] people, int[][] pairs, int numBuildings, int buildingsSize)
{
    int[] freePeople = findFreePeople(people);
    if(freePeople) = 0 
    {
        return people;
    }

    foreach(int person in freePeople)
    {
        for(int buildingIndex=0 to numBuildings)
        {
            if( CheckIfPersonFitsInBuilding(...) )
            {
                int[] tempPeople = people.Copy();
                tempPeople[person] = buildingIndex;
                int[] result = Match(tempPeople,pairs,numBuildings,buildingsSize);
                if(result != null)
                {
                    return result;
                }
            }
        }
    }
    return null;
}

I just cover all the possible arrangements using recursion. I feel this could be optimized greatly, and I’m not talking about heuristics but a solution with far lesser complexity.

  1. Is there a formal well known problem that is similar to this?
  2. Is there a better algorithm?

I think that this might be related to the stable marriage problem, though I’m not sure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:37:04+00:00Added an answer on June 6, 2026 at 5:37 am

    This problem is known to be NP-hard by a reduction from the NP-complete problem of decomposing a graph into cliques (the clique cover problem).

    First, some terminology and discussion. A clique in a graph is a set of k different nodes such that each node is connected to each other node in the clique. An independent set in the graph is a set of k different nodes such that no two nodes are connected to one another. If you take any graph G and then introduce edges whenever an edge is missing and remove all edges that previously existed, you get the complement graph of the original graph. This means that the problem of finding a clique in an initial graph is equivalent to finding an independent set in the complement graph.

    The reason this is interesting is that in the problem you’re describing, you are given a graph of people where each edge indicates “these two people can’t be housed together.” Consequently, you can think of the problem you’re describing as trying to find a way to break the graph up into k subgraphs, each of which is an independent set. If you construct the complement of this graph, you get the graph of all people who are okay being placed together. In that case, you want to break the group up into k groups that are all cliques.

    It is known that the following problem is NP-complete:

    Given a graph and a number k, can you break the nodes in the graph apart into k cliques?

    We can reduce this problem to your problem in polynomial time, showing that your problem is NP-hard. The construction is simple – given the initial graph G and number k, first construct the complement of G. This means that if you can break apart the complement into k independent sets, then the original graph G can be broken apart into k cliques (because of the duality between cliques and independent sets). Now, take this new graph and convert it into a set of people, one per node, where two people cannot be placed into the same room as one another if their nodes were connected in the original graph. Now, there is a way to distribute these people into k houses iff the complement of G can be broken down into k independent sets iff G can be broken down into k cliques. Consequently, the known NP-complete problem of clique cover can be reduced to your problem in polynomial time, so your problem is NP-hard. To ensure that any independent set can be fit into a house, just set the maximum capacity of each room to n, the number of nodes in the graph. This allows any independent set to be housed in any room.

    Since your problem is NP-hard, there cannot be a polynomial-time solution to it unless P = NP. Consequently, as best as anyone knows, there is no polynomial time algorithm for it and your backtracking recursion very well might be the optimal solution.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked to my friend about this question, he said that it is used
A friend of mine was asked the following question today at interview for the
A friend of mine asked to create a static website and I found that
My friend asked me this question today. How to test a vending machine and
Yesterday my friend asked me a question about this query: select * from user
This was a question that was asked to my friend in a Google interview
I'm sure this question has been asked before, but I can't seem to find
i asked a question about chess game 2days ago,and a friend suggest me the
This is related to another question that I had asked Is fb:multi-friend-selector deprecated? .
My friend asked this question I am not sure why it is on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.