Homework Assignment 4

Homework Assignment 4
COMP 211 Introduction to Algorithms
Instructor: Perouz Taslakian
Due Date: April 27th , 2015
1. [40 pts] Suppose you are given a weighted undirected graph G and its minimum spanning
tree T . Design an efficient algorithm that finds a second minimum spanning tree T 0 . That
is, your algorithm should find a spanning tree T 0 of G such that T 0 6= T .
What is the running time of your algorithm? (You may lose points if your algorithm is
not optimal.)
Note that you may assume that T and T 0 differ by exactly one edge. But which edge?
This is what your algorithm has to figure out (it may be useful useful to look at a few
examples).
2. [40 pts] In a certain data structure, the ith operation costs i units of time if i is an exact
power of 2, and 1 unit of time otherwise. Determine the amortized cost of each operation
using a method of your choice.
3. [20 pts] A stack is a data structure that allows for two operations:
• push(a) : puts item a on top of the stack, and
• pop() : returns the item on top of the stack and removes it from the stack.
A queue is a data structure that allows for two operations:
• enqueue(a) : puts item a at the end of a queue, and
• dequeue() : returns the item at the front of the queue and removes it from the queue.
Explain (in pseudocode or in English) how to implement a queue using two stacks S1 and
S2 , and O(1) additional memory, such that each enqueue and dequeue operation takes
constant amortized time.
That is, you need to explain how to implement the operations enqueue and dequeue using
the stacks S1 and S2 . The only access you have to the stacks is through the standard
operations push and pop.
Briefly justify the amortized running time of your implementation.
1