CS6212
Youssef

May 25, 2011

 

Homework 2

Due Date: June 6, 2011

Problem 1: (20 points)

a)     Show step by step how the quicksort algorithm sorts the following array: 12, 7, 5, 9, 3, 18, 22, 13, 1, 7.

b)     Show step by step how the mergesort algorithm sorts the same arrary.

c)     Show step by step how the insertion sort algorithm sorts the same arrary.

d)     Show step by step how the selection sort algorithm sorts the same arrary.


Problem 2: (20 points)

a)     Write a divide-and-conquer recursive algorithm that takes as input a real array a[1..n], and gives as output an array A[1..n] where A[i]=a[1]*a[2]*...*a[i].

b)      Give a non-recursive algorithm that performs the same task. Analyze the time complexities of both algorithms.


Problem 3: (25 points)

a)      Give a divide-and-conquer recursive algorithm to compute xn for any real number x and any positive integer n. Analyze the time complexity of your algorithm.

b)     Let a[0..n-1] be a real array where n is a power of 2. Give a divide-and-conquer recursive algorithm to compute the polynomial P(x)=a[0]+a[1]x+a[2]x2+...+a[n-1]xn-1for any x.

c)      Give a non-recursive algorithm that performs the same task as in (b). Analyze the time complexites of both algorithms.


Problem 4: (15 points)

Consider this instance of the knapsack problem:
Weights: 15, 63, 27, 15, 9, 45, 30, 63
Prices: 24, 48, 18, 12, 42, 30, 18, 45
Capacity: M=56.

Apply the greedy method algorithm to find the solution (that is, how much of every item we are going to get). Show the process of deriving the solution step by step.


Problem 5: (20 points)

Let G=(V,E) be the following weighted graph: V={1,2,3,4,5,6,7} and E={[(1,2) 7],[(1,4) 4],[(2,4) 4],[(1,3) 22], [(3,5) 5],[(4,5) 10],[(2,6) 4],[(4,6) 5],[(5,7) 3],[(6,7) 5]}, where [(i,j) a] means that (i,j) is an edge of weight a.
a)     Find a minimum spanning tree of G, showing which edge is being considered each step.
b)     Using the greedy single source shortest path algorithm, find the distances between node 1 and all the other nodes. Show the values of the DIST array at every step.