Question

In: Computer Science

Your job is to arrange n rambunctious children in a straight line, facing front. You are...

Your job is to arrange n rambunctious children in a straight line, facing front. You are given a list of m

statements of the form “i hates j". If i hates j, then you do not want to put i somewhere behind j because

then i is capable of throwing something at j.

Give an algorithm that orders the line (or says it's not possible) in O(m + n) time.

Solutions

Expert Solution

Answer:-----------
Create a graph G in which each child is a vertex and every statement of the form “i hates j” results in a directed edge from i to j. Topologically sort the vertices. Use a modified topological sort that returns “false” or some other marker if the graph has a cycle (if the graph has a cycle, it’s not possible to line the children up safely). If the topological sort succeeds, line the children up according to the topological sort result.

Topological-Sort-Children(G)

  1. select v ∈ G.V with no incoming edges
  2. if v = nil and |G.V| ≠ 0
  3. then return false
  4. output v
  5. G.V = G.V - v
  6. Topological-Sort-Children(G)

The order of the output vertices (children) is the reverse order to put them in line; you can reverse the order in O(n) time (or you could have made edges from j to i instead of from i to j) Creating the graph takesO(n+m) time; running topological sort takes O(V+E) =O(m+n) time.


Related Solutions

Three perfectly logical men are told to stand in a straight line, one in front of...
Three perfectly logical men are told to stand in a straight line, one in front of the other. A hat is put on each of their heads. Each of these hats was selected from a group of five hats: two identical black hats and three identical white hats. None of the men can see the hat on his own head, and they can only see the person's hat in front of him. In how many distributions of the hats can...
Paper Select a job (a customer-facing job would be good) and use that job for your...
Paper Select a job (a customer-facing job would be good) and use that job for your analysis. Use the template and complete the job description (BELOW) Job Title and company of the interviewee Purpose Job functions (with weights) and two most important tasks for each function List the most important KSAOs List the demands, specifications, and rewards TEMPLATE: Job Description Job Title Company Purpose Job Functions                                                                                                                                % Job Requirements Demands Specifications Rewards Helpful Hints Task Statement (A task statement is a...
Find a job posting that interests you and is in line with your education and experience....
Find a job posting that interests you and is in line with your education and experience. This could be from a company website, a job posting site, in the newspaper, etc. Copy/paste or scan the job posting into a word document.
Imagine that you are on the front line (also known as triage) in a hospital and...
Imagine that you are on the front line (also known as triage) in a hospital and are receiving patients presenting with possible Coronavirus symptoms. What would you be looking for in terms of symptoms for these patients? How would you differentiate them from people who may be coming in for different reasons? Your role is to investigate what symptoms these patients arrive with that makes you suspicious for this disease. Also provide a reason why this virus has a special...
You start a job on a Fabrication line for silicon based devices. To validate that your...
You start a job on a Fabrication line for silicon based devices. To validate that your fab process is working, you inspect for defective wafers. Suppose that in a tray containing 20 wafers pulled off the line, four are defective based on your line statistics. Suppose that you pull two wafers from this tray for analysis. Find the probability of the following: a) Neither wafer is defective. b) At least one of the wafers is defective. c) Neither is defective,...
In 2020, you purchased a piece of equipment for $125000. You will use the Straight Line...
In 2020, you purchased a piece of equipment for $125000. You will use the Straight Line Depreciation method and will use a depreciable salvage value of $6,000. The equipment has a depreciable and useful life of five years. The first-year benefit is $20,000 and increases by $5,000 each year thereafter. You sell the equipment at the end of year 5 for a market value of $15000.   You are in the 32% income tax bracket. Ordinary gains/losses are taxed at 18%...
A straight line is fitted to some data using least squares. Summary statistics are below. n=10,...
A straight line is fitted to some data using least squares. Summary statistics are below. n=10, $\bar{x}=$5, $\bar{y}=$12, SSxx=142, SSxy=123, SSyy=155 The least squares intercept and slope are 7.65 and 0.87, respectively, and the ANOVA table is below. Source DF SS MS Regression 1 106.54 106.54 Residual 8 48.46 6.06 Total 9 155 Compute a 95% confidence interval for the mean response when x=8.What is the critical value from the table? 2.3060 [1 pt(s)] You are correct. Your receipt no....
Congratulations on your new job in the Major League Lacrosse (MLL) front office. The league currently...
Congratulations on your new job in the Major League Lacrosse (MLL) front office. The league currently has 8 teams in the US and Canada, located mainly on the East Coast. This budding league is busy trying to find a team west of the Mississippi to help the Denver Outlaws find a rival. They could move a current team west or they could add an additional team. (d.) Some of the teams have talked about disbanding the league and just operating...
Congratulations on your new job in the Major League Lacrosse (MLL) front office. The league currently...
Congratulations on your new job in the Major League Lacrosse (MLL) front office. The league currently has 8 teams in the US and Canada, located mainly on the East Coast. This budding league is busy trying to find a team west of the Mississippi to help the Denver Outlaws find a rival. They could move a current team west or they could add an additional team. a) Some owners have recommended eliminating the playoffs and just crowning the regular season...
True or false justify your answer TFC straight line means you could increase q without increasing...
True or false justify your answer TFC straight line means you could increase q without increasing that type of cost There is no logic behind the idea that: MC must intersect the AC at its minimum Economies of scale enable the firm to expand production while reducing the LRAC regardless of the of Q level No economic bases for economies of scale, it is just a wishful thinking Two perfect competition assumptions are necessary to have a meaningful market structure...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT