HW3solutions

HW3solutions - Homework #3 Solutions 1. You are given a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Homework #3 Solutions 1. You are given a weighted directed acyclic graph G = ( V,E ). You want to find the longest path in G . (a) (3) Split the problem up into a list of ordered decisions. What is the first decision your algorithm must make? Solution: At each point, we must decide what is the next node we visit. Our first decision is figuring out what node to start at. (b) (10) Define OPT ( x ) to be the length of the longest path in G that starts at node x . Give the recursive procedure to solve this problem (dont forget the base case!) Solution: If x has no outgoing edges, then OPT ( x ) = 0. Otherwise, OPT ( x ) = max ( x,y ) E [ c xy + OPT ( y )]. If you consider the case where there can be negative edge weights, then OPT ( x ) = max (0 ,max ( x,y ) E [ c xy + OPT ( y )]) (c) (3) What order do you need to fill the array in to transform this into an iterative procedure? Solution: Fill the array in reverse topological order (start with a node with no outgoing edges, end with a node with no incoming edges). (d) (3) Specify where the answer is stored in your array. Solution: Answer = max x OPT ( x ), where x is any node with no incoming edges. If you consider the case where there can be negative edge weights, you have to consider x to be any node. (e) (3) Analyze the running time of your iterative procedure. Solution: A first glance tells us that there are n nodes, and each takes m time to fill. But we can improve this by noting we look at each edge once, so the actual runtime is ( n + m ). (f) (3) Explain how to reconstruct the actual path, instead of just stating the length of the path. Solution: Starting at the node where our answer is stored ( max x OPT ( x )), determine which edge was the next visited by checking all edges ( x,y ) E , and seeing if OPT ( x ) = c xy + OPT ( y ). Once we find the y we visit next, repeat until we find OPT ( z ) = 0. This is the longest path. 2. (25) Given a ribbon strip of length k , a tailor will cut it once, in any place you choose, for the price of k dollars. Suppose you are given a strip of length L , marked to be cut in n different locations labeled 1 , 2 ,...,n . For simplicity, let indices 0 and n + 1 denote the left and right endpoints of the original ribbon strip of length...
View Full Document

Page1 / 4

HW3solutions - Homework #3 Solutions 1. You are given a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online