You know who knows who among n people that you would like to have come to a party. Assume "knows" is symmetric: If I know you, you know me. You make further requirements that you want each person to have at least 5 new people to meet at the party, and also, so nobody feels too isolated, each person should already know at least 5 people at the party. Your original list may not satisfy these extra two conditions, so you may need to eliminate some people from the invitation list (or maybe you cannot have a party at all with these restrictions). Find a largest possible subset of the n people that you could invite and satisfy the the other two requirements. For the basic problem, find an O(n^3) algorithm and explain its order and its logic.
I ask not for the answer, but for guidance on where to start.
I am assuming following data structure for the representation of the information:
Details of everyone (including host) can be stored in "Person individuals[n]".
The host of party being at first position.
A subroutine that i might need for algo:
The proposed algorithm:
Let me know if you face any difficulty in understanding this algo.
Sounds like a good place to apply a graph algorithm.
Form a graph of people,
G
. Forn
people there will ben
nodes in the graph. Link nodesi
andj
if personi
knows personj
.Let the first iteration of
G
be calledG_0
. ObtainG_1
by making a pass throughG
and eliminate any person who knows too many or too few people. (That is, eliminate personi
if the number of links toi
is< 5
or> n-5
.)Repeat the process, obtaining
G_2
,G_3
up to a maximum ofn
(or so) iterations, obtainingG_n
. The people remaining in this graph are the people you should invite.Each of the
n
passes requires a check ofn
people againstn
other people, so the algorithm isO(n^3)
.MATLAB code to accomplish this (you didn't ask for it, but I thought it was interesting and wrote it anyway):
Sample output (10 people, 40 connections, must know at least 3 people, must not know at least 3 people)