You and your friends decided to hold a “Secret Santa” gift exchange, where each person buys a gift for someone else. To see how this whole thing works, let’s consider the following example. Suppose there are 7 people A, B, C, D, E, F, and G. We denote x → y to mean “x gives a gift to y.” If the gift exchange starts with person A, then they give a gift to E. Then E gives a gift to B. And it is entirely possible that B gives a gift to A; in such a case we have completed a “cycle.” In case a cycle occurs, the gift exchange resumes with another person that hasn’t given their gift yet. If the gift exchange resumes with person D, then they give a gift to G. Then G gives a gift to F. Then F gives a gift to C. Then finally C gives a gift to D, which completes another cycle. Since all of the people have given their gifts, the giftexchange is done, otherwise the gift exchange resumes again with another person. All in all, there are two cycles that occurred during the gift exchange: A → E → B → A and D → G → F → C → D. You want to know how many such cycles will occur during the gift exchange, given n people and m exchanges. Describe an efficient algorithm that, given n people and m exchanges, determines the number of cycles that will occur during the gift exchange. Due to the way Secret Santa works, you may assume that nobody will pick themselves as the recipient, each person gives a gift to exactly one other person, and each person receives a gift from exactly one other person.
You and your friends decided to hold a “Secret Santa” gift exchange, where each person buys a gift for someone else. To see how this whole thing works, let’s consider the following example. Suppose there are 7 people A, B, C, D, E, F, and G. We denote x → y to mean “x gives a gift to y.” If the gift exchange starts with person A, then they give a gift to E. Then E gives a gift to B. And it is entirely possible that B gives a gift to A; in such a case we have completed a “cycle.” In case a cycle occurs, the gift exchange resumes with another person that hasn’t given their gift yet. If the gift exchange resumes with person D, then they give a gift to G. Then G gives a gift to F. Then F gives a gift to C. Then finally C gives a gift to D, which completes another cycle. Since all of the people have given their gifts, the giftexchange is done, otherwise the gift exchange resumes again with another person. All in all, there are two cycles that occurred during the gift exchange: A → E → B → A and D → G → F → C → D. You want to know how many such cycles will occur during the gift exchange, given n people and m exchanges.
Describe an efficient
exactly one other person.
Step by step
Solved in 3 steps with 1 images