当前位置:文档之家› 算法导论课程作业答案

算法导论课程作业答案

算法导论课程作业答案
算法导论课程作业答案

Introduction to Algorithms

Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10

Diagnostic Test Solutions

Problem1

Consider the following pseudocode:

R OUTINE(n)

1if n=1

2then return1

3else return n+R OUTINE(n?1)

(a)Give a one-sentence description of what R OUTINE(n)does.(Remember,don’t guess.) Solution:The routine gives the sum from1to n.

(b)Give a precondition for the routine to work correctly.

Solution:The value n must be greater than0;otherwise,the routine loops forever.

(c)Give a one-sentence description of a faster implementation of the same routine. Solution:Return the value n(n+1)/2.

Problem2

Give a short(1–2-sentence)description of each of the following data structures:

(a)FIFO queue

Solution:A dynamic set where the element removed is always the one that has been in the set for the longest time.

(b)Priority queue

Solution:A dynamic set where each element has an associated priority value.The element removed is the element with the highest(or lowest)priority.

(c)Hash table

Solution:A dynamic set where the location of an element is computed using a function of the element’s key.

Problem3

UsingΘ-notation,describe the worst-case running time of the best algorithm that you know for each of the following:

(a)Finding an element in a sorted array.

Solution:Θ(log n)

(b)Finding an element in a sorted linked-list.

Solution:Θ(n)

(c)Inserting an element in a sorted array,once the position is found.

Solution:Θ(n)

(d)Inserting an element in a sorted linked-list,once the position is found.

Solution:Θ(1)

Problem4

Describe an algorithm that locates the?rst occurrence of the largest element in a?nite list of integers,where the integers are not necessarily distinct.What is the worst-case running time of your algorithm?

Solution:Idea is as follows:go through list,keeping track of the largest element found so far and its index.Update whenever necessary.Running time isΘ(n).

Problem5

How does the height h of a balanced binary search tree relate to the number of nodes n in the tree? Solution:h=O(lg n)

Problem 6

Does an undirected graph with 5vertices,each of degree 3,exist?If so,draw such a graph.If not,explain why no such graph exists.

Solution:No such graph exists by the Handshaking Lemma.Every edge adds 2to the sum of the degrees.Consequently,the sum of the degrees must be even.

Problem 7

It is known that if a solution to Problem A exists,then a solution to Problem B exists also.

(a)Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable.If his proof turns out to be valid,can we conclude that Problem B is also unsolvable?Answer yes or no (or don’t know).

Solution:No

(b)Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable.If the proof turns out to be valid,can we conclude that problem A is unsolvable as well?Answer yes or no (or don’t know).

Solution:Yes

Problem 8

Consider the following statement:

If 5points are placed anywhere on or inside a unit square,then there must exist two that are no more than √2/2units apart.

Here are two attempts to prove this statement.

Proof (a):Place 4of the points on the vertices of the square;that way they are maximally sepa-

rated from one another.The 5th point must then lie within √2/2units of one of the other points,since the furthest from the corners it can be is the center,which is exactly √2/2units from each of the four corners.

Proof (b):Partition the square into 4squares,each with a side of 1/2unit.If any two points are

on or inside one of these smaller squares,the distance between these two points will be at most √2/2units.Since there are 5points and only 4squares,at least two points must fall on or inside one of the smaller squares,giving a set of points that are no more than √2/2apart.Which of the proofs are correct:(a),(b),both,or neither (or don’t know)?

Solution:(b)only

Problem9

Give an inductive proof of the following statement:

For every natural number n>3,we have n!>2n.

Solution:Base case:True for n=4.

Inductive step:Assume n!>2n.Then,multiplying both sides by(n+1),we get(n+1)n!> (n+1)2n>2?2n=2n+1.

Problem10

We want to line up6out of10children.Which of the following expresses the number of possible line-ups?(Circle the right answer.)

(a)10!/6!

(b)10!/4!

(c) 106

(d) 104 ·6!

(e)None of the above

(f)Don’t know

Solution:(b),(d)are both correct

Problem11

A deck of52cards is shuf?ed thoroughly.What is the probability that the4aces are all next to each other?(Circle the right answer.)

(a)4!49!/52!

(b)1/52!

(c)4!/52!

(d)4!48!/52!

(e)None of the above

(f)Don’t know

Solution:(a)

Problem12

The weather forecaster says that the probability of rain on Saturday is25%and that the probability of rain on Sunday is25%.Consider the following statement:

The probability of rain during the weekend is50%.

Which of the following best describes the validity of this statement?

(a)If the two events(rain on Sat/rain on Sun)are independent,then we can add up the two

probabilities,and the statement is true.Without independence,we can’t tell.

(b)True,whether the two events are independent or not.

(c)If the events are independent,the statement is false,because the the probability of no rain

during the weekend is9/16.If they are not independent,we can’t tell.

(d)False,no matter what.

(e)None of the above.

(f)Don’t know.

Solution:(c)

Problem13

A player throws darts at a target.On each trial,independently of the other trials,he hits the bull’s-eye with probability1/4.How many times should he throw so that his probability is75%of hitting the bull’s-eye at least once?

(a)3

(b)4

(c)5

(d)75%can’t be achieved.

(e)Don’t know.

Solution:(c),assuming that we want the probability to be≥0.75,not necessarily exactly0.75.

Problem14

Let X be an indicator random variable.Which of the following statements are true?(Circle all that apply.)

(a)Pr{X=0}=Pr{X=1}=1/2

(b)Pr{X=1}=E[X]

(c)E[X]=E[X2]

(d)E[X]=(E[X])2

Solution:(b)and(c)only

算法导论 第三版 第24章 答案 英

Chapter24 Michelle Bodnar,Andrew Lohr April12,2016 Exercise24.1-1 If we change our source to z and use the same ordering of edges to decide what to relax,the d values after successive iterations of relaxation are: s t x y z ∞∞∞∞0 2∞7∞0 25790 25690 24690 Theπvalues are: s t x y z NIL NIL NIL NIL NIL z NIL z NIL NIL z x z s NIL z x y s NIL z x y s NIL Now,if we change the weight of edge(z,x)to4and rerun with s as the source,we have that the d values after successive iterations of relaxation are: s t x y z 0∞∞∞∞ 06∞7∞ 06472 02472 0247?2 Theπvalues are: s t x y z NIL NIL NIL NIL NIL NIL s NIL s NIL NIL s y s t NIL x y s t NIL x y s t 1

Note that these values are exactly the same as in the worked example.The di?erence that changing this edge will cause is that there is now a negative weight cycle,which will be detected when it considers the edge(z,x)in the for loop on line5.Since x.d=4>?2+4=z.d+w(z,x),it will return false on line7. Exercise24.1-2 Suppose there is a path from s to v.Then there must be a shortest such path of lengthδ(s,v).It must have?nite length since it contains at most|V|?1 edges and each edge has?nite length.By Lemma24.2,v.d=δ(s,v)<∞upon termination.On the other hand,suppose v.d<∞when BELLMAN-FORD ter-minates.Recall that v.d is monotonically decreasing throughout the algorithm, and RELAX will update v.d only if u.d+w(u,v)

算法导论 第三版 第21章 答案 英

Chapter21 Michelle Bodnar,Andrew Lohr April12,2016 Exercise21.1-1 EdgeP rocessed initial{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k} (d,i){a}{b}{c}{d,i}{e}{f}{g}{h}{j}{k} (f,k){a}{b}{c}{d,i}{e}{f,k}{g}{h}{j} (g,i){a}{b}{c}{d,i,g}{e}{f,k}{h}{j} (b,g){a}{b,d,i,g}{c}{e}{f,k}{h}{j} (a,h){a,h}{b,d,i,g}{c}{e}{f,k}{j} (i,j){a,h}{b,d,i,g,j}{c}{e}{f,k} (d,k){a,h}{b,d,i,g,j,f,k}{c}{e} (b,j){a,h}{b,d,i,g,j,f,k}{c}{e} (d,f){a,h}{b,d,i,g,j,f,k}{c}{e} (g,j){a,h}{b,d,i,g,j,f,k}{c}{e} (a,e){a,h,e}{b,d,i,g,j,f,k}{c} So,the connected that we are left with are{a,h,e},{b,d,i,g,j,f,k}, and{c}. Exercise21.1-2 First suppose that two vertices are in the same connected component.Then there exists a path of edges connecting them.If two vertices are connected by a single edge,then they are put into the same set when that edge is processed. At some point during the algorithm every edge of the path will be processed,so all vertices on the path will be in the same set,including the endpoints.Now suppose two vertices u and v wind up in the same set.Since every vertex starts o?in its own set,some sequence of edges in G must have resulted in eventually combining the sets containing u and v.From among these,there must be a path of edges from u to v,implying that u and v are in the same connected component. Exercise21.1-3 Find set is called twice on line4,this is run once per edge in the graph,so, we have that?nd set is run2|E|times.Since we start with|V|sets,at the end 1

算法导论第二章答案

第二章算法入门 由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。 给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。 插入排序算法伪代码 INSERTION-SORT(A) 1 for j ← 2 to length[A] 2 do key ←A[j] 3 Insert A[j] into the sorted sequence A[1..j-1] 4 i ←j-1 5 while i > 0 and A[i] > key 6 do A[i+1]←A[i] 7 i ←i ? 1 8 A[i+1]←key C#对揑入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { T key; int i; for (int j = 1; j < Input.Length; j++) { key = Input[j]; i = j - 1; for (; i >= 0 && Input[i].CompareTo(key)>0;i-- ) Input[i + 1] = Input[i]; Input[i+1]=key; } } 揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j] 这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1. 如果按照大部分计算机编程语言的思路,修改为: INSERTION-SORT(A) 1 for j ← 1 to length[A] 2 do key ←A[j] 3 i ←j-1

算法导论 第三版 第六章 答案 英

Chapter6 Michelle Bodnar,Andrew Lohr December31,2016 Exercise6.1-1 At least2h and at most2h+1?1.Can be seen because a complete binary tree of depth h?1hasΣh?1 i=02i=2h?1elements,and the number of elements in a heap of depth h is between the number for a complete binary tree of depth h?1exclusive and the number in a complete binary tree of depth h inclusive. Exercise6.1-2 Write n=2m?1+k where m is as large as possible.Then the heap consists of a complete binary tree of height m?1,along with k additional leaves along the bottom.The height of the root is the length of the longest simple path to one of these k leaves,which must have length m.It is clear from the way we de?ned m that m= lg n . Exercise6.1-3 If there largest element in the subtee were somewhere other than the root, it has a parent that is in the subtree.So,it is larger than it’s parent,so,the heap property is violated at the parent of the maximum element in the subtree Exercise6.1-4 The smallest element must be a a leaf node.Suppose that node x contains the smallest element and x is not a leaf.Let y denote a child node of x.By the max-heap property,the value of x is greater than or equal to the value of y.Since the elements of the heap are distinct,the inequality is strict.This contradicts the assumption that x contains the smallest element in the heap. Exercise6.1-5 Yes,it is.The index of a child is always greater than the index of the parent, so the heap property is satis?ed at each vertex. 1

大连理工大学软件学院算法导论第一次大作业源码

\documentclass{ctexart} \usepackage{amsmath} \usepackage{amssymb} \usepackage{fancyhdr} \begin{document} \pagestyle{fancy} \title{算法分析与设计第一次作业} \author{XXXX XXX\XXXXXX} \date{2013/9/11} \maketitle \noindent 3.1-2\ 证明: 证明$(n+a)^b=\Theta(n^b)$等价于证明存在$c_{1},c_{2},n_{0}>0$使得对于任意的 $n>n_{0}$,都有$0\leq c_{1}n^b \leq (n+a)^b \leq c_{2}n^b$成立。 $\because$ $n+a\leq n+|a|$,$\therefore$当$n\geq |a|$时,$n+a\leq 2n$。 又$\because$ $n+a\geq n-|a|$,$\therefore$当$|a|\leq \frac{1}{2}$时,$n+a \geq \frac{1}{2}n$。综上,当$n\geq 2|a|$时,$0\leq \frac{1}{2}n \leq (n+a) \leq 2n$。 $\therefore$ 对于$b>0$,有$0\leq (\frac{1}{2}n)^b \leq (n+a)^b \leq (2n)^b$ $\therefore$ 存在$c_{1} = (\frac{1}{2}n)^b$,$c_{2} = (2n)^b$,$n_{0}=2|a|$, 使得$0\leq c_{1}n^b \leq (n+a)^b \leq c_{2}n^b$成立。\ \ \ $\therefore$原命题得证。 \\ \noindent 3.1-3\ 解释:设运行时间为$F(n)$,则$F(n)\geq 0(n^2)$, $\therefore$ 若$F_{1}(n) = 0(n^2)$,则$F(n)\geq F_{1}(n)$, 又$\because$ $\forall$ n,$T(n)=0$时,$T(n)=0(n^2)$,且运行时间都大于0, $\therefore$对于所有的运行时间$F(n)$都有$F(n)\geq 0(n^2)$, $\therefore$这句话是没有意义的。 \\ \noindent

算法导论 第三版 第七章 答案 英

Chapter7 Michelle Bodnar,Andrew Lohr April12,2016 Exercise7.1-1 13199512874212611 13199512874212611 13199512874212611 91913512874212611 95131912874212611 95131912874212611 95819121374212611 95871213194212611 95874131912212611 95874131912212611 95874219122113611 95874261221131911 95874261121131912 Exercise7.1-2 If all elements in the array have the same value,PARTITION returns r.To make PARTITION return q= (p+r)/2 when all elements have the same value,modify line4of the algorithm to say this:if A[j]≤x and j(mod2)= (p+1)(mod2).This causes the algorithm to treat half of the instances of the same value to count as less than,and the other half to count as greater than. Exercise7.1-3 The for loop makes exactly r?p iterations,each of which takes at most constant time.The part outside the for loop takes at most constant time.Since r?p is the size of the subarray,PARTITION takes at most time proportional to the size of the subarray it is called on. Exercise7.1-4 To modify QUICKSORT to run in non-increasing order we need only modify line4of PARTITION,changing≤to≥. 1

MIT麻省理工学院 算法导论公开课Problem Set 1

Introduction to Algorithms September7,2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D.Demaine and Charles E.Leiserson Handout5

(a) (b) (c) (d)and(note the little-) (e)and Problem1-2.Recurrences Give asymptotic upper and lower bounds for in each of the following recurrences.Assume that is constant for.Make your bounds as tight as possible,and justify your answers. (a) (b) (c) (d)

Figure1:An example of a convex polygon represented by the array.is the vertex with the minimum-coordinate,and are ordered counterclockwise. (b)Give an algorithm to?nd the vertex with the maximum coordinate in time. (c)Give an algorithm to?nd the vertex with the maximum coordinate in time.

算法导论 第三版 第二章 答案 英

Chapter2 Michelle Bodnar,Andrew Lohr April12,2016 Exercise2.1-1 314159264158 314159264158 314159264158 263141594158 263141415958 263141415859 Exercise2.1-2 Algorithm1Non-increasing Insertion-Sort(A) 1:for j=2to A.length do 2:key=A[j] 3://Insert A[j]into the sorted sequence A[1..j?1]. 4:i=j?1 5:while i>0and A[i]

《算法导论2版》课后答案_加强版2

1 算法导论第三次习题课 2008.12.17

2 19.1-1 如果x 是根节点:degree[sibling[x]] > sibling[x] 如果x 不是根节点:degree[sibling[x]] = sibling[x] –119.1-3 略

3 19.2-2 过程略( union 操作) 19.2-3 (1)decrease-key (2)extract-min 过程略 19.2-6 算法思想:找到堆中最小值min ,用min-1代替-∞. [遍历堆中树的根节点]

4 15.1-1 15.1-2 略P195 或P329 15.1-4 f i [j]值只依赖f i [j-1]的值,从而可以从2n 压缩为2个。再加上f*、l*、l i [j]。 Print-station(l, I, j ) //i 为线路,j 为station if j>1 then Print-station(l, l i [j], j-1 ) print “line”I, “station”j;

5 15.2-1 略(见课本) 15.2-2 15.2-4 略 MATRIX-CHAIN-MULTIPLY(A, s, i, j) if j>i x= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j), j) y= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j)+1,j) return MATRIX-MULTIPLY(x, y) else return A i

6 15.3-1 (归纳法)递归调用 枚举15.3-2 没有效果,没有重叠子问题 15.3-4 略 (3)n Ο3/2(4/) n n Θ

算法导论课程设计_最大长方体问题

最大长方体问题 Problem description 一个长,宽,高分别为m,n,p 的长方体被分割成个m*n*p 个小立方体。每个小立方体内有一个整数。试设计一个算法,计算出所给长方体的最大子长方体。子长方体的大小由它所含所有整数之和确定。 Input 输入的第1 行是3 个正整数m,n,p,1≤m,n,p≤50。接下来m*n行每行p个正整数,表示小立方体中的数。 Output 程序运行结束时,输出计算出的最大子长方体的大小。 Sample Input 3 3 3 0 -1 2 1 2 2 1 1 -2 -2 -1 -1 -3 3 -2 -2 -3 1 -2 3 3 0 1 3 2 1 -3 Sample Output 14 #include "stdio.h" #include int MaxSum(int n,int* a){//用动态规划算法计算“最大子段和问题”,对应于一维数组int sum=0,b=0; int i=0; for (i=1;i<=n;i++) { if (b>0) { b+=a[i]; } else b=a[i]; if (b>sum) { sum=b; } } return sum;

} int MaxSum2(int m,int n,int a[][50]){//用动态规划算法计算“最大子矩阵问题”,对应二维数组 int sum=0; int b[50]; memset(b,0,sizeof(b)); int i=0,j=0,k=0; for (i=1;i<=m;i++) { for(k=1;k<=n;k++) b[k]=0; for (j=i;j<=m;j++) { for(k=1;k<=n;k++) b[k]+=a[j][k]; int max=MaxSum(n,b); if(max>sum) sum=max; } } return sum; } int MaxSum3(int m,int n,int p,int a[][50][50]){//用动态规划算法计算“最大子长方体问题”,对应 int sum=0; int b[50][50]; memset(b,0,sizeof(b)); int i,j,k1,k2; for (i=1;i<=m;i++) { memset(b,0,sizeof(b)); for(j=i;j<=m;j++){ for(k1=1;k1<=n;k1++) for(k2=1;k2<=p;k2++) b[k1][k2]+=a[j][k1][k2]; int max=MaxSum2(n,p,b); if(max>sum) sum=max; } } return sum; } int main(){

算法导论习题答案

Chapter2 Getting Start 2.1 Insertion sort 2.1.2 将Insertion-Sort 重写为按非递减顺序排序 2.1.3 计算两个n 位的二进制数组之和 2.2 Analyzing algorithms 当前n-1个元素排好序后,第n 个元素已经是最大的元素了. 最好时间和最坏时间均为2()n Θ 2.3 Designing algorithms 2.3.3 计算递归方程的解 22()2(/2)2,1k if n T n T n n if n for k =?=?+ = >? (1) 当1k =时,2n =,显然有()lg T n n n = (2) 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===?, 则当1k i =+,即12i n +=时, 2.3.4 给出insertion sort 的递归版本的递归式 2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ? 虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但

是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ 2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x 首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x) QuickSort(A,n); i←1; j←n; while A[i]+A[j]≠x and i,()()b b n a n +=Θ 0a >时,()()2b b b b n a n n n +<+= 对于121,2b c c ==,12()b b b c n n a c n <+< 0a <时,()b b n a n +<

算法导论 第三版 第十九章 答案 英

Chapter19 Michelle Bodnar,Andrew Lohr April12,2016 Exercise19.2-1 First,we take the subtrees rooted at24,17,and23and add them to the root list.Then,we set H.min to18.Then,we run consolidate.First this has its degree2set to the subtree rooted at18.Then the degree1is the subtree rooted at38.Then,we get a repeated subtree of degree2when we consider the one rooted at24.So,we make it a subheap by placing the24node under18. Then,we consider the heap rooted at17.This is a repeat for heaps of degree1, so we place the heap rooted https://www.doczj.com/doc/b214475313.html,stly we consider the heap rooted at23,and then we have that all the di?erent heaps have distinct degrees and are done,setting H.min to the smallest,that is,the one rooted at17. The three heaps that we end up with in our root list are: 23 17 38 30 41 and 1

Ch10算法导论 第三版 第十章 答案 英

Chapter10 Michelle Bodnar,Andrew Lohr April12,2016 Exercise10.1-1 4 41 413 41 418 41 Exercise10.1-2 We will call the stacks T and R.Initially,set T.top=0and R.top=n+1. Essentially,stack T uses the?rst part of the array and stack R uses the last part of the array.In stack T,the top is the rightmost element of T.In stack R, the top is the leftmost element of R. Algorithm1PUSH(S,x) 1:if S==T then 2:if T.top+1==R.top then 3:error“over?ow” 4:else 5:T.top=T.top+1 6:T[T.top]=x 7:end if 8:end if 9:if S==R then 10:if R.top?1==T.top then 11:error“over?ow” 12:else 13:R.top=R.top?1 14:T[T.top]=x 15:end if 16:end if 1

Algorithm2POP(S) if S==T then if T.top==0then error“under?ow” else T.top=T.top?1. return T[T.top+1] end if end if if S==R then if R.top==n+1then error“under?ow” else R.top=R.top+1. return R[R.top?1] end if end if Exercise10.1-3 4 41 413 13 138 38 Exercise10.1-4 Algorithm3ENQUEUE if Q.head==Q.tail+1,or Q.head==1and Q.tail==Q.length then error“over?ow” end if Q[Q.tail]=x if Q.tail==Q.length then Q.tail=1 else Q.tail=Q.head+1 end if Exercise10.1-5 As in the example code given in the section,we will neglect to check for over?ow and under?ow errors. 2

算法导论 第八章答案

8.2-4 :在O(1)的时间内,回答出输入的整数中有多少个落在区间[a...b]内。给出的算法的预处理时间为O(n+k) 算法思想:利用计数排序,由于在计数排序中有一个存储数值个数的临时存储区C[0...k],利用这个数组即可。 #include using namespace std; //通过改编计数排序而来,因为有些部分需要注释掉 void counting_sort(int*&a, int length, int k, int*&b, int*&c); int main() { const int LEN =100; int*a =newint[LEN]; for(int i =0; i < LEN; i++) a[i] = (i -50)*(i -50) +4; int* b =new int[LEN]; const int k =2504; int* c =new int[k +1]; counting_sort(a, LEN, k, b, c); //这里需要注释掉 //for(int i = 0; i < LEN; i++) //cout<>m>>n) { if(m >n) cout<<"区间输入不对"< k && m >0) cout<<"个数为"< k && m <=0) cout<<"个数为"<= 0; i--) { b[c[a[i]] - 1] = a[i]; c[a[i]]--; }*/ } PS:计数排序的总时间为O(k+n),在实践中,如果当k = O(n)时,我们常常采用计数排序,

算法导论 第三版 第十六章 答案 英

Chapter16 Michelle Bodnar,Andrew Lohr April12,2016 Exercise16.1-1 The given algorithm would just stupidly compute the minimum of the O(n) numbers or return zero depending on the size of S ij.There are a possible number of subproblems that is O(n2)since we are selecting i and j so that 1≤i≤j≤n.So,the runtime would be O(n3). Exercise16.1-2 This becomes exactly the same as the original problem if we imagine time running in reverse,so it produces an optimal solution for essentially the same reasons.It is greedy because we make the best looking choice at each step. Exercise16.1-3 As a counterexample to the optimality of greedily selecting the shortest, suppose our activity times are{(1,9),(8,11),(10,20)}then,picking the shortest ?rst,we have to eliminate the other two,where if we picked the other two instead,we would have two tasks not one. As a counterexample to the optimality of greedily selecting the task that con?icts with the fewest remaining activities,suppose the activity times are {(?1,1),(2,5),(0,3),(0,3),(0,3),(4,7),(6,9),(8,11),(8,11),(8,11),(10,12)}.Then, by this greedy strategy,we would?rst pick(4,7)since it only has a two con- ?icts.However,doing so would mean that we would not be able to pick the only optimal solution of(?1,1),(2,5),(6,9),(10,12). As a counterexample to the optimality of greedily selecting the earliest start times,suppose our activity times are{(1,10),(2,3),(4,5)}.If we pick the ear-liest start time,we will only have a single activity,(1,10),whereas the optimal solution would be to pick the two other activities. Exercise16.1-4 Maintain a set of free(but already used)lecture halls F and currently busy lecture halls B.Sort the classes by start time.For each new start time which you encounter,remove a lecture hall from F,schedule the class in that room, 1

相关主题
文本预览
相关文档 最新文档