CS 161 - Algorithms Homework 2, 25 Points
Due: Friday, October 12, 2012, 5:00pm
Please submit your solutions to the TA, Michael Bannister, as he is requesting.
-
5 points.
Problem C-2.2 from Goodrich-Tamassia.
-
5 points.
Problem C-2.19 from Goodrich-Tamassia.
-
5 points.
Train track switching networks are often used as models for stacks and queues.
For example, a queue can be modeled as a straight section of train track with
a station in the middle---cars come in one end and go out the other,
implementing the FIFO protocol. Similarly, a stack can be modeled using the
switching network shown here,
where cars come in on the
input track, are ``pushed'' into the station (representing the stack),
and exit onto the output track after they are ``popped.''
Suppose now that you would like to use a train switching network to
model a double-ended queue, or "deque," which is
a list where elements can be
inserted or deleted at either the front or the rear of the list.
How would you draw such a train switching network?
-
5 points.
In the children's game ``hot potato,''
a group of n children sit in a circle passing an object, called the
`potato,' around the circle (say in a clockwise direction).
The children continue passing the potato until a leader rings a bell,
at which point the child holding the potato must leave the game, and
the other children close up the circle.
This process is then continued until there is only one child
remaining, who is declared the winner.
Using a list, describe an efficient method for implementing
this game.
Suppose the leader always rings the bell immediately after the potato has been
passed k times.
(Determining the last child remaining in this variation of hot potato
is known as the Josephus problem.)
What is the running time of your method, in terms of n and k,
assuming the list is implemented with a doubly linked list?
What if the list is implemented with an array?
-
5 points.
Suppose you work for a company, iPuritan.com, that has strict rules for when
two employees, x and y, may date one another, requiring approval from
their lowest-level common supervisor.
The employees at iPuritan.com are organized in a tree, T,
such that each node in T corresponds
to an employee and each employee, z, is considered a supervisor for all of the
employees in the subtree of T rooted at z (including z itself).
The lowest-level common supervisor for x and y
is the employee lowest in the organizational chart, T, that is a supervisor
for both x and y.
Thus, to find a lowest-level common supervisor for
the two employees, x and y,
you need to find
the lowest common ancestor (LCA) between the two nodes for x
and y, which is the lowest node in T that has both x and y as
descendants (where we allow a node to be a descendant of itself).
Given the nodes corresponding to the two
employees x and y, describe an efficient algorithm for
finding the supervisor who may approve whether or not x
and y may date each other, that is, the LCA of x and y in T.
What is the running time of your method?