Docsity

Homework 3 - Data Structures and Algorithms - Fall 2007 | EECS 281, Assignments of Algorithms and Programming

Toggle sidebar
bg1
EECS 281 Fall 2007
Homework Assignment 3
Due Tuesday 11/20/07 at 1:00 PM
Instructions: Complete all problems and turn in a hard copy to the EECS 281
lock box in 2420 EECS by the specified due date. Type or write legibly and
include both your name and uniquename on each page of the assignment. There
are 6 questions worth a total of 45 points.
1.) (8 points) Consider the following graph:
a.) (2 points) Create an adjacency matrix for this graph where M[i][j]
denotes the weight of an edge from node i to node j
b.) (3 points) Perform a depth-first traversal on this graph starting from
node a. Show your results as a table shown below. Also what data
structure would be best suited for a DFS and why?
Node traversed Current elements in data structure
c.) (3 points) Now perform a breadth-first traversal on this graph starting
from node a. Show your results in the same table format as in part b.).
What data structure would be best suited for a BFS and why?
pf3
pf4
pf5

Partial preview of the text

Download Homework 3 - Data Structures and Algorithms - Fall 2007 | EECS 281 and more Assignments Algorithms and Programming in PDF only on Docsity!

EECS 281 Fall 2007

Homework Assignment 3

Due Tuesday 11/20/07 at 1:00 PM

Instructions : Complete all problems and turn in a hard copy to the EECS 281 lock box in 2420 EECS by the specified due date. Type or write legibly and include both your name and uniquename on each page of the assignment. There are 6 questions worth a total of 45 points.

1.) (8 points) Consider the following graph:

a.) (2 points) Create an adjacency matrix for this graph where M[i][j] denotes the weight of an edge from node i to node j

b.) (3 points) Perform a depth-first traversal on this graph starting from node a. Show your results as a table shown below. Also what data structure would be best suited for a DFS and why?

Node traversed Current elements in data structure

c.) (3 points) Now perform a breadth-first traversal on this graph starting from node a. Show your results in the same table format as in part b.). What data structure would be best suited for a BFS and why?

2.) (7 points) Referring to the same graph as in Question 1

a.) (5 points) Run a Dijkstra’s algorithm to determine the shortest path from a to g. Complete the table below

Shortest distance from a Step Vertex Nodes traversed a b c d e f g h i 1 - a 0 2 4 6 ∞ ∞ ∞ ∞ ∞ 2 b a,b 3 4 5 6 7 8 9

b.) (2 points) Using an example from part a.) explain how ‘relaxation’ is observed in Dijkstra’s algorithm.

c.) (2 points) Comment on the memory usage of the above code. Is it efficient? How can we make it better?

4.) (8 points) You've been working late at night in the CSE labs, trying to finish PA2. It's due tomorrow afternoon, and you've already used up all of your late days. Needless to say, it's going to be a long night. Since you can't spare the time to leave the labs, you decide to round up a few of your friends, and see if they'll help you out. You have four tasks that you want them to help you with:

i.) Go get some food for you (food) ii.) Retrieve your class notes on the LZW algorithm (notes) iii.) Return a book to the library that's due today, which you forgot about until now (library) iv.) Fill in for you at your part-time job at McDonald's (job)

You've also got four friends: Mike, Sarah, Joe, Bill.

You would like to think that your friends would be altruistic, and understand your situation (i.e. Help you out for free). Unfortunately, conniving as they are, your friends agree to help you, only under the condition that you pay them for their assistance.

Due to their different preferences, they each have a different price for a particular task. These prices are listed in the following matrix:

food notes library job Mike $11 $12 $18 $ Sarah $14 $15 $13 $ Joe $11 $17 $19 $ Bill $17 $14 $20 $

The values in the matrix correspond to how much you would have to pay a particular person to do a certain task. For example, to get Joe to go to the library, it will cost you $19.

a.) (4 points) Use branch-and-bound to find the optimal assignment of friends to tasks (with one task per person). The optimal assignment is the one that costs you the least. An example assignment is: <Mike->food, Sarah-

notes, Joe->library, Bill->job>, having a total cost of $11 + $15 + $19 + $28 = $73. You must show all intermediate steps to receive credit for this problem.

b.) (4 points) Write a pseudo-code function that takes in an NxN array, such as the one you used in part a, and outputs the optimal cost of getting your

friends to help you. You must use branch-and-bound to determine the optimal cost.

5.) (8 points) An undirected graph with an edge between every pair of nodes is sometimes referred to as a complete graph. Consider a complete graph having N nodes, with labeled nodes 1, 2, ..., n, in which the edge joining nodes i and j has the weight c{i, j} = i+j for all i != j (eg. The weight of the edge connecting nodes 4 and 7 will have weight 4+7=11).

Use Prim's algorithm to determine a minimum weight spanning tree for

a. (2 points) The complete graph with N=4 nodes. b. (2 points) The complete graph with N=6 nodes. c. (2 points) The complete graph with N=8 nodes. d. (2 points) The complete graph with N=10 nodes.

You must show all work for each part to receive credit.

6.) (7 points) Topological Sorting. a. (3 points) Give a topological sort (a topological ordering of the nodes) of the graph given in problem 1, earlier in this homework:

b. (2 points) Construct a graph of 10 nodes, having no nodes in common with the graph from part a, which has a unique topological sort. What is the unique topological sort?

c. (2 points) Construct a graph with N nodes (you choose what N is) which has EXACTLY 24 unique topological sorts. What are these topological