Docsity

Homework Assignment - Fundamental Algorithms, Fall 2009 | CS 473, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 473 Homework 0 (due January 27, 2009) Spring 2009
CS 473: Undergraduate Algorithms, Spring 2009
Homework 0
Due in class at 11:00am, Tuesday, January 27, 2009
•
This homework tests your familiarity with prerequisite material—big-Oh notation, elementary
algorithms and data structures, recurrences, graphs, and most importantly, induction—to help
you identify gaps in your background knowledge.
You are responsible for filling those gaps.
The early chapters of any algorithms textbook should be sufficient review, but you may also want
consult your favorite discrete mathematics and data structures textbooks. If you need help, please
ask in office hours and/or on the course newsgroup.
•
Each student must submit individual solutions for this homework. For all future homeworks,
groups of up to three students may submit a single, common solution.
•
Please carefully read the course policies linked from the course web site. If you have any questions,
please ask during lecture or office hours, or post your question to the course newsgroup. In
particular:
–
Submit five separately stapled solutions, one for each numbered problem, with your name
and NetID clearly printed on each page. Please do not staple everything together.
–
You may use any source at your disposal—paper, electronic, or human—but you
must
write
your solutions in your own words, and you must cite every source that you use.
–Unless explicitly stated otherwise, every homework problem requires a proof.
–
Answering ā€œI don’t knowā€ to any homework or exam problem (except for extra credit
problems) is worth 25% partial credit.
–
Algorithms or proofs containing phrases like ā€œand so onā€ or ā€œrepeat this process for all
n
ā€,
instead of an explicit loop, recursion, or induction, will receive 0 points.
Write the sentence ā€œI understand the course policies." at the top of your solution to problem 1.
1.
Professor George O’Jungle has a 27-node binary tree, in which every node is labeled with a unique
letter of the Roman alphabet or the character
&
. Preorder and postorder traversals of the tree visit
the nodes in the following order:
•Preorder: IQJHLEMVOTSBRGYZKCA&FPNUDWX
•Postorder: HEMLJVQSGYRZBTCPUDNFW&XAKOI
(a) List the nodes in George’s tree in the order visited by an inorder traversal.
(b) Draw George’s tree.
1
pf3
pf4

Partial preview of the text

Download Homework Assignment - Fundamental Algorithms, Fall 2009 | CS 473 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 473: Undergraduate Algorithms, Spring 2009

Homework 0

Due in class at 11:00am, Tuesday, January 27, 2009

  • This homework tests your familiarity with prerequisite material—big-Oh notation, elementary algorithms and data structures, recurrences, graphs, and most importantly, induction—to help you identify gaps in your background knowledge. You are responsible for filling those gaps. The early chapters of any algorithms textbook should be sufficient review, but you may also want consult your favorite discrete mathematics and data structures textbooks. If you need help, please ask in office hours and/or on the course newsgroup.
  • Each student must submit individual solutions for this homework. For all future homeworks, groups of up to three students may submit a single, common solution.
  • Please carefully read the course policies linked from the course web site. If you have any questions, please ask during lecture or office hours, or post your question to the course newsgroup. In particular: - Submit five separately stapled solutions, one for each numbered problem, with your name and NetID clearly printed on each page. Please do not staple everything together. - You may use any source at your disposal—paper, electronic, or human—but you must write your solutions in your own words, and you must cite every source that you use. - Unless explicitly stated otherwise, every homework problem requires a proof. - Answering ā€œI don’t knowā€ to any homework or exam problem (except for extra credit problems) is worth 25% partial credit. - Algorithms or proofs containing phrases like ā€œand so onā€ or ā€œrepeat this process for all n ā€, instead of an explicit loop, recursion, or induction, will receive 0 points.

Write the sentence ā€œI understand the course policies." at the top of your solution to problem 1.

  1. Professor George O’Jungle has a 27-node binary tree, in which every node is labeled with a unique letter of the Roman alphabet or the character &. Preorder and postorder traversals of the tree visit the nodes in the following order: - Preorder: I Q J H L E M V O T S B R G Y Z K C A & F P N U D W X - Postorder: H E M L J V Q S G Y R Z B T C P U D N F W & X A K O I

(a) List the nodes in George’s tree in the order visited by an inorder traversal. (b) Draw George’s tree.

  1. (a) [5 pts] Solve the following recurrences. State tight asymptotic bounds for each function in the form Θ( f ( n )) for some recognizable function f ( n ). Assume reasonable but nontrivial base cases. If your solution requires a particular base case, say so. Do not submit proofs —just a list of five functions—but you should do them anyway, just for practice.

A ( n ) = 10 A ( n/ 5 ) + n

B ( n ) = 2 B

n + 3 4

  • 5 n^6 /^7 āˆ’ 8

r n log n

ƶ log^10 n

ù āˆ’ 11

C ( n ) = 3 C ( n/ 2 ) + C ( n/ 3 ) + 5 C ( n/ 6 ) + n^2 D ( n ) = max 0 <k<n

( D ( k ) + D ( n āˆ’ k ) + n )

E ( n ) =

E ( n āˆ’ 1 ) E ( n āˆ’ 3 ) E ( n āˆ’ 2 )

[Hint: Write out the first 20 terms.]

(b) [5 pts] Sort the following functions from asymptotically smallest to asymptotically largest, indicating ties if there are any. Do not submit proofs —just a sorted list of 16 functions—but you should do them anyway, just for practice. Write f ( n )  g ( n ) to indicate that f ( n ) = o ( g ( n )), and write f ( n ) ≔ g ( n ) to mean f ( n ) = Θ( g ( n )). We use the notation lg n = log 2 n.

n lg n

p n 3 n p lg n lg p n 3

p n p 3 n

3 lg^ n^ lg( 3 n ) 3 lg^

p n 3

p lg n p 3 lg^ n^ lg( 3

p n ) lg

p 3 n^

p lg( 3 n )

  1. Suppose you are given a pointer to the head of singly linked list. Normally, each node in the list has a pointer to the next element, and the last node’s pointer is NULL. Unfortunately, your list might have been corrupted (by a bug in somebody else’s code, of course), so that some node’s pointer leads back to an earlier node in the list.

Top: A standard singly-linked list. Bottom: A corrupted singly linked list.

Describe an algorithm^1 that determines whether the linked list is corrupted or not. Your algorithm must not modify the list. For full credit, your algorithm should run in O ( n ) time, where n is the number of nodes in the list, and use O ( 1 ) extra space (not counting the list itself).

(^1) Since you understand the course policies, you know what this phrase means. Right?

? 6. [Extra credit] You may be familiar with the story behind the famous Tower of HanoĆÆ puzzle:

At the great temple of Benares, there is a brass plate on which three vertical diamond shafts are fixed. On the shafts are mounted n golden disks of decreasing size. At the time of creation, the god Brahma placed all of the disks on one pin, in order of size with the largest at the bottom. The Hindu priests unceasingly transfer the disks from peg to peg, one at a time, never placing a larger disk on a smaller one. When all of the disks have been transferred to the last pin, the universe will end.

Recently the temple at Benares was relocated to southern California, where the monks are considerably more laid back about their job. At the ā€œTowers of Hollywoodā€, the golden disks have been replaced with painted plywood, and the diamond shafts have been replaced with Plexiglas. More importantly, the restriction on the order of the disks has been relaxed. While the disks are being moved, the bottom disk on any pin must be the largest disk on that pin, but disks further up in the stack can be in any order. However, after all the disks have been moved, they must be in sorted order again.

The Towers of Hollywood. The sixth move leaves the disks out of order.

Describe an algorithm that moves a stack of n disks from one pin to the another using the smallest possible number of moves. Exactly how many moves does your algorithm perform? [Hint: The Hollywood monks can bring about the end of the universe considerably faster than their Benaresian counterparts.]