Question

We have a group of people and we know which pairs of people are friends. We...

We have a group of people and we know which pairs of people are friends. We want to give some of the people money in such a way that for every pair of friends exactly one gets money. Explain how this is a graph problem, and give pseudocode for an algorithm to determine if it possible. (language=c++)

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

Let's say that we have n people numbered from 1 to n. And there are m friends pair in the form of (a, b) are the persons.

Then, if we select a person x then we can not select any of his friend.

Now suppose we have graph of n vertices each representing one of the person and m edges connecting each pair of friends. Then, we have to divide the vertice into two sets, one of the set will contain those who will get money and the other will contain those who won't. So basically, we want our graph to not contain any edge between the people lying in the same set.

To do this, we can do a depth first search, and calculate an attribute st[x] for each x, which will store the set person x belong to.

And we want to have two sets. So, we will set st[x] to be either 1 or 2. We will start dfs from person number 1 and assign st[1]=1.

Then for each of the friends of person 1, we will assign st[x]=2. And then for friends of friends we will assign st[y]=1. And similarly continue doing dfs.

And once we visited a node which has already been assigned st[x] and is consistent with the calculation done till now, then such distribution is possible and else it is not.

//CPP CODE

#include <bits/stdc++.h>

using namespace std;

vector <int> v[100001];//adjacency list for the graph

int st[100001];//attribute of each of the person

bool dfs(int s, int assign)

{

    if(st[s]!=0)

    {

        /*If an already visited node is visited again, and is the value which is going to be assigned to

        it now is not same then the desired distribution is not possible */

        if(st[s]!=assign)return false;

        else return true;

    }

    st[s]=assign;

    bool res=true;

    for(auto it:v[s])

    {

        res&=dfs(it, (assign==1)?2:1);//if the parent is assigned 1, child will be assigned 2 and vice versa

    }

    return res;

}

int main()

{

    int n, m;

    

    cin>>n;//Enter number of persons

    cin>>m;//Enter number of friends pair

    int a, b;

    memset(st, 0, sizeof(st));

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

    {

        cin>>a>>b;// Enter a and b the friends pairs

        v[b].push_back(a);

        v[a].push_back(b);

    }

    bool ans=dfs(1, 1);//stores the boolean answer

    if(ans)cout<<"POSSIBLE"<<endl;

    else cout<<"NOT POSSIBLE"<<endl;

    return 0;

}

//CPP Code Ends here

/*

Happy Coding :)

*/

Add a comment
Know the answer?
Add Answer to:
We have a group of people and we know which pairs of people are friends. We...
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
  • Suppose you are organizing a party for a large group of your friends. Your friends are...

    Suppose you are organizing a party for a large group of your friends. Your friends are pretty opinionated, though, and you don’t want to invite two friends if they don’t like each other. So you have asked each of your friends to give you an “enemies” list, which identifies all the other people among your friends that they dislike and for whom they know the feeling is mutual. Your goal is to invite the largest set of friends possible such...

  • Suppose we have 2n people, some of which are related to some of the others. We might want to spli...

    Suppose we have 2n people, some of which are related to some of the others. We might want to split them into groups of two, so that the two people in a group are related (if this is possible). Expressing this as a graph problem, suppose we have an undirected graph G = hV;Ei. A pairing is a set P E of edges such that for all (u; v); (x; y) 2 P, the nodes u; v; x; y are...

  • Suppose we have 2n people, some of which are related to some of the others. We might want to spli...

    Suppose we have 2n people, some of which are related to some of the others. We might want to split them into groups of two, so that the two people in a group are related (if this is possible) Expressing this as a graph problem, suppose we have an undirected graph G-(WB). A pairing is a set P C E of edges such that for all (u,v),(x,y) є P, the nodes u,v,z, y are all different. In other words, no...

  • A group of four people is said to be “interesting", if there are at most five...

    A group of four people is said to be “interesting", if there are at most five pairs who are friends. Assume that each pair of people are friends, independent of every other pair, with probability 1/2 . Let S be the number of pairs that are friends in this group. Question : What is the probability that a randomly chosen group of four people is “interesting"? Question : if 128 different people observe randomly chosen groups of four people, how...

  • Some of your friends have gotten into the burgeoning field of time-series data mining, in which...

    Some of your friends have gotten into the burgeoning field of time-series data mining, in which one looks for patterns in sequences of events that occur over time. Purchases at stock exchanges—what’s being bought— are one source of data with a natural ordering in time. Given a long sequence S of such events, your friends want an efficient way to detect certain “patterns” in them—for example, they may want to know if the four events buy Yahoo, buy eBay, buy...

  • In this problem, your goal is to identify who among a group of people has a...

    In this problem, your goal is to identify who among a group of people has a certain disease. You collect a blood sample from each of the people in the group, and label them 1 through n. Suppose that you know in advance that exactly one person is infected with the disease, and you must identify who that person is by performing blood tests. In a single blood test, you can specify any subset of the samples, combine a drop...

  • Problem 2. Is it possible for each person in a group of 9 people to be...

    Problem 2. Is it possible for each person in a group of 9 people to be friends with exactly five of the nine? Explain your answer.

  • Problem 6. (Weighted Graph Reduction) Your friend has written an algorithm which solves the all pairs shortest path pr...

    Problem 6. (Weighted Graph Reduction) Your friend has written an algorithm which solves the all pairs shortest path problem for unweighted undirected graphs. The cost of a path in this setting is the number of edges in the path. The algorithm UNWEIGHTEDAPSP takes the following input and output: UNWEİGHTEDA PSP Input: An unweighted undirected graph G Output: The costs of the shortest paths between each pair of vertices fu, v) For example, consider the following graph G. The output of...

  • Pouring water. We have three containers whose sizes are 10 pints, 7 pints, and 4 pints,...

    Pouring water. We have three containers whose sizes are 10 pints, 7 pints, and 4 pints, respectively. The 7-pint and 4-pint containers start out full of water, but the 10-pint container is initially empty. We are allowed one type of operation: pouring the contents of one container into another, stopping only when the source container is empty or the destination container is full. We want to know if there is a sequence of pourings that leaves exactly 2 pints in...

  • 2. Suppose you have 15 identical dollar bills to give to your friends Adam, Beth and...

    2. Suppose you have 15 identical dollar bills to give to your friends Adam, Beth and Charlie. You give all 15 dollar bills away by randomly selecting a person for each dollar. Each person is equally likely to recieve a dollar for each dollar (a) What is the probability that Adam gets exactly 4 dollars? (b) If you know that each person recieved at least one dollar, what is the probability that Adam gets exactly 4 dollars? (c) What is...

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