Page 1

1. New Seats The setup for this problem was that a group of students sat in classroom where the seats were arranged in a straight line perpendicular to the chalkboard at the front of the room. The algorithm described needs to identify the student who has the worst seat, where the student who has the worst seat is defined as the seat with the most taller students ahead of him. Inputs into the algorithm are the ordering of students in their seats and the height of each student. It is assumed that no two students are the exact same height. My algorithm uses the efficient insertion and splay properties of splay trees to solve this problem efficiently. First, build an empty splay tree with the tree size of each node stored as augmented information for each node. Additionally, declare two variables which (1) store the highest number of taller people seen so far (max_worst) and (2) store the index of the student which corresponds to this maximum (max_index). Both of these variable should be initialized as 0. Then, pass over each seat from the closest seat to the board to the furthest seat from the board. For each student, insert his height into the splay tree, splay the inserted node to the root of the tree, and update the augmented information appropriately. Then, compare max_worst to the size information augmented on the right child of the root node. If the right subtree has a size larger than max_worst, set max_worst equal to the size of the right subtree and set max_index to the index of the current seat being analyzed. After the nth iteration of the algorithm, all seats have been analyzed and max_index contains the index of the worst seat. This algorithm works because at any given time, the splay tree contains only the ith element and all students ahead of i, where i is the current iterated student. Therefore, the size of the right subtree of the splay tree when i is splayed to root is always the number of students ahead of student i who are taller than him. Once every student has been passed, max_worst contains the largest right-subtree size seen, which is the same as the largest number of taller-students seen, and max_index contains the index of the student that correlates to this max_worst. The asymptotic runtime of this algorithm depends only on the time needed to insert all values into the augmented splay tree. Since we don't have to worry about the ordering of taller students ahead of any particular student and only the number of taller students ahead, it is fine to throw away ordering information by splaying an element to the root. Standard splay trees are guaranteed to have a worst-case insertion runtime of . This algorithm performs n insertions, and the runtime complexity of each insertion is , where i is the size of the splay tree at every iteration of the algorithm and ranges from incrementing by one. The time complexity can therefore be written as the summation from 0 to n of log(i). Since , this summation is equal to , which is . can be shown to be . Thus, the runtime of n insertions into a standard splay tree with an initial size of zero is . That runtime works for n insertions into a standard, non-augmented splay tree, but what about a splay tree augmented with subtree size information? For each splay operation, the augmented information does not have to be recalculated. Rotating a splay tree only requires that the size information of the root and pivot nodes be swapped, which is a constant-time operation. This means that splay (and insert) operations still take time using my augmented splay tree.

Therefore, since the runtime of my algorithm depends only on the runtime of n insertions, and n insertions takes , my algorithm has a worst-case runtime of .

Algorithms assignment  
Read more
Read more
Similar to
Popular now
Just for you