CS 421
Problem Set 6

Greedy Algorithms and Basic Graph Algorithms
(25 points)

"I want an Oompa-Loompa!' screamed Veruca."
-- Roald Dahl, Charlie and the Chocolate Factory

Readings

This problem set covers material covered in Chapters 16 and 22 of the textbook.

Notes

Mark the top sheet with your name and the names of any students with whom you collaborated. If you didn't collaborate with anyone, mark your collaborators as "None."

Begin each problem at the top of a sheet of paper, front or back. That means for three problems you should turn in at least two sheets of paper: front and back of the first one, and front of the second. That's at a minimum; there is no maximum. And be sure to staple your pages together. Homework not following these guidelines will be returned and marked down for being late.

Remember, your goal is to communicate. Full credit will be given only to correct solutions which are described clearly. Convoluted and obtuse descriptions will receive low marks. Show ALL of your work.

To complete your homework, you may ONLY consult the following material:

  1. lecture slides posted on the class webpage
  2. course notes you or others took during lecture
  3. the required text (CLRS)
  4. websites that may clarify the concepts covered in the material but do not in any way provide solutions to the problems.

Problems

Problem 1 (5 pts). Making Change

The change-making problem involves giving change for a specific amount n with the least number of coins of demonimations d1 > d2 > ... > dm. In the U.S., the most widely used coin denominations are d1 = 25, d2 = 10, d3 = 5 and d4 = 1.

  1. Write pseudocode for a greedy algorithm that solves the change-making problem. Your algorithm should return the number of each denomination in the solution or the statement "no solution" if no solution exists. Your algorithm should also work with any set of denomiations. What is the time efficiency class of your algorithm?

  2. Give an instance (i.e. a collection of denominations) for which the greedy algorthm will not yield an optimal solution.

Problem 2 (10 pts). Huffman Coding

  1. Determine a variable-length, prefix-free encoding for the characters in the following string using the Huffman Algorithm:

    how_much_wood_would_a_woodchuck_chuck

    Don't forget to count the underscore.

  2. What is the maximal possible length of a codeword in a Huffman encoding of an alphabet with n characters?

Problem 3 (10 pts). Depth-First Search and Breadth-First Search

BFS/DFS Graph
  1. (5 pts) Using depth-first search (DFS), traverse the above graph starting at vertex A. Resolve ties using the alphabetical ordering of the vertices. Give the order in which the vertices are visited for the first time (i.e. are pushed onto the stack), and the order in which they become dead ends (i.e. are popped off the stack).

  2. (5 pts)Using breadth-first search (BFS), traverse the above graph starting at vertex A. Resolve ties using the alphabetical ordering of the vertices. Give the order in which the vertices are removed from the queue (i.e. are dequeued).

Extra Credit (5 pts)

Decision Tree.

Design a strategy that minimizes the expected number of questions asked in the following game:

You have a deck of cards that consists of one ace of spades, two deuces of spades, three threes, and so on up to nine nines, for a total of 45 cards in all. Someone draws a card from the shuffled deck, which you have to identify by asking questions answerable with a 'yes' or a 'no'.