算法导论答案建模仿真试题

合集下载

《算法导论》习题答案12、13、14章

《算法导论》习题答案12、13、14章

第9章 中位数和顺序统计学
9.3-2
大于x的数至少有3n/10-6, n≥140时,易证3n/10-6 ≥n/4 小于x的数同理。
9.3-4
通过比较得到第i小元素,每次保留比较信息。 在比较过程中比这个元素小的元素构成的集合即为i – 1个 小数集合,而比较过程中比这个元素大的元素则构成了n – i 个大元素集合。不需要增加比较次数。
Preprocessing(A,k) for i←0 to k do C[i]←0 for j←1 to length[A] do C[A[j]] ←C[A[j]]+1 for i←1 to k do C[i] ←C[i]+C[i-1] Query(C,k,a,b) if b<a or b<1 or a>k return 0 if a<1 then a=1 if b>k then b=k if a≠1 then return C[b]-C[a-1] else return C[b]
0 +1
k +1
k +1
( k +1) +1
第6章 堆排序
6.4-3 不论递增还是递减,时间均为O(nlgn) 6.4-4 最坏情况下,n-1次调用MAX-HEAPIFY,运 行时间为O(nlgn)
第6章 堆排序
6.5-3
HEAP-MINIMUM(A) if heap-size[A]<1 then error”heap underflow” else return A[1] HEAP-EXTRACT-MIN(A) if heap-size[A]<1 then error”heap underflow” min<-A[1] A[1]<-A[heap-size[A]] heap-size[A]<-heap-size[A]-1 MIN-HEAPIFY(A,1) return min HEAP-DECREASE-KEY(A,i,key) if key>A[i] then error A[i]<-key while i>1 and A[PARENT(i)>A[i] do exchange A[i]<->A[PARENT(i)] i<-PARENT(i) MIN-HEAP-INSERT(A,key) heap-size[A]<-heap-size[A]+1 A[heap-size[A]]<-+∞ HEAP-DECREASE-KEY(A,heap-size[A],key)

算法导论习题答案 (5)

算法导论习题答案 (5)
c(j) = min {c(i − 1) + linecost(i, j)} .
Three-hole punch your paper on submissions. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of the essay should provide the following:
(a) Argue that this problem exhibits optimal substructure.
Solution: First, notice that linecost(i, j) is defined to be � if the words i through j do not fit on a line to guarantee that no lines in the optimal solution overflow. (This relies on the assumption that the length of each word is not more than M .) Second, notice that linecost(i, j) is defined to be 0 when j = n, where n is the total number of words; only the actual last line has zero cost, not the recursive last lines of subprob­ lems, which, since they are not the last line overall, have the same cost formula as any other line.

算法导论课程作业答案

算法导论课程作业答案

算法导论课程作业答案Introduction to AlgorithmsMassachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10Diagnostic Test SolutionsProblem1Consider the following pseudocode:R OUTINE(n)1if n=12then return13else 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.Problem2Give a short(1–2-sentence)description of each of the following data structures:(a)FIFO queueSolution:A dynamic set where the element removed is always the one that has been in the set for the longest time.(b)Priority queueSolution:A dynamic set where each element has anassociated priority value.The element removed is the element with the highest(or lowest)priority.(c)Hash tableSolution:A dynamic set where the location of an element is computed using a function of the ele ment’s key.Problem3UsingΘ-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)Problem4Describe 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).Problem5How 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 6Does 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 7It 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:YesProblem 8Consider 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 fromeach of the four corners.Proof (b):Partition the square into 4squares,each with a side of 1/2unit.If any two points areon 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)onlyProblem9Give 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.Problem10We 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 knowSolution:(b),(d)are both correctProblem11A deck of52cards is shuf?ed thoroughly.What is the probability that the4aces are all next to each other?(Circle theright answer.)(a)4!49!/52!(b)1/52!(c)4!/52!(d)4!48!/52!(e)None of the above(f)Don’t knowSolution:(a)Problem12The 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 twoprobabilities,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 rainduring 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)Problem13A player throws darts at a target.On each trial,independentlyof 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.Problem14Let 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])2Solution:(b)and(c)only。

《数学建模》习题及参考答案 第一章 建立数学模型

《数学建模》习题及参考答案 第一章 建立数学模型

第一章部分习题3(5). 决定十字路口黄灯亮的时间长度.4. 在1.3节“椅子能在不平的地面上放稳吗”的假设条件中,将四角的连线呈正方形改为长方形,其余不变,试构造模型并求解.5. 模仿1.4节商人过河问题中的状态转移模型,作下面这个众所周知的智力游戏:人带着猫、鸡、米过河,船除希望要人计划之外,至多能载猫、鸡、米三者之一,而当人不在场时猫要吃鸡、鸡要吃米,设计一个安全过河方案,并使渡河次数尽量地少.6. 利用1.5节表1和表3给出的1790-2000年的美国实际人口资料建立下列模型: (1) 分段的指数增长模型. 将时间分为若干段,分别确定增长率r. (2) 阻滞增长模型. 换一种方法确定固有增长率r 和最大容量x m .7. 说明1.5节中Logistic 模型(9)可以表示为()()01t t r mex t x --+=,其中t 0是人口增长出现拐点的时刻,并说明t 0与r ,x m 的关系.8. 假定人口的增长服从这样的规律:时刻t 的人口为x (t),t 到t +△t 时间内人口的增量与x m -x (t)成正比(其中为x m 最大容量). 试建立模型并求解. 作出解的图形并与指数增长模型、阻滞增长模型的结果进行比较.9(3). 甲乙两站之间有电车相通,每隔10分钟甲乙两站相互发一趟车,但发车时刻不一定相同。

甲乙之间一中间站丙,某人每天在随机的时刻到达丙站,并搭乘最先经过丙站的那趟车,结果发现100天中约有90天到达甲站,约有10天到达乙站。

问开往甲乙两站的电车经过丙站的时刻表是如何安排的。

参考答案3(5). 司机看到黄灯后停车要有一定的刹车距离1s ,设通过十字路口的距离为2s ,汽车行驶速度为v ,则黄灯的时间长度t 应使距停车线1s 之内的汽车能通过路口,即()vs s t 21+≈其中s 1可由试验得到,或按照牛顿第二定律解运动方程,进一步可考察不同车重、不同路面及司机反应灵敏程度等因素的影响.4. 相邻两椅脚与地面距离之和分别定义为()()θθg f 和,将椅子旋转ο180,其余作法与1.3节相同.5. 人、猫、鸡、米分别记为4,3,2,1=i ,当i 在此岸时记1=i x ,否则记0=i x ,则此岸的状态可用()4321,,,x x x x s =表示。

算法导论习题答案 (4)

算法导论习题答案 (4)
Mark the top of each sheet with your name, the course number, the problem number, your recitation section, the date and the names of any students with whom you collaborated.
The father now has to decide how to procure skis for his daughter for every weekly session (until she quits). One can buy skis at a one-time cost of B dollars, or rent skis at a weekly cost of R dollars. (Note that one can buy skis at any time—e.g., rent for two weeks, then buy.)
(a) Suppose that (starting from an empty queue) we do 3 insertions, then 2 removals,
then 3 more insertions, and then 2 more removals. What is the total cost of these 10
Problem Set 4 Solutions
Reading: Chapters 17, 21.1–21.3 Both exercises and problems should be solved, but only the problems should be turned in.

算法设计与分析试卷试题(A)(附答案)

算法设计与分析试卷试题(A)(附答案)

chengcheng算法分析考试试卷(A卷)课程名称算法分析编号题号一二三四总分得分评阅人一、填空题(每小题3分,共30分)1、一个算法的优劣可以用空间复杂度与时间复杂度来衡量。

2、这种不断回头寻找目标的方法称为回溯法。

3、直接或间接地调用自身的算法称为递归算法。

4、 记号在算法复杂性的表示法中表示紧致界。

5、由分治法产生的子问题往往是原问题较小模式,这就为使用递归技术提供了方便。

6、建立计算模型的目的是为了使问题的计算复杂性分析有一个共同的客观尺度。

7、下列各步骤的先后顺序是②③④①。

①调试程序②分析问题③设计算法④编写程序。

8、最优子结构性质的含义是问题最优解包含其子问题最优解。

9、贪心算法从初始阶段开始,每一个阶段总是作一个使局部最优的贪心选择。

10、拉斯维加斯算法找到的解一定是正确的。

二、选择题(每小题2分,共20分)1、哈夫曼编码可利用( C )算法实现。

A、分治策略B、动态规划法C、贪心法D、回溯法2、下列不是基本计算模型的是( B )。

A、RAMB、ROMC、RASPD、TM3、下列算法中通常以自顶向下的方式求解最优解的是( C)。

A、分治法B、动态规划法C、贪心法D、回溯法chengcheng 4、在对问题的解空间树进行搜索的方法中,一个活结点有多次机会成为活结点的是( A )A、回溯法B、分支限界法C、回溯法和分支限界法D、动态规划5、秦始皇吞并六国使用的远交近攻,逐个击破的连横策略采用了以下哪种算法思想? BA、递归;B、分治;C、迭代;D、模拟。

6、FIFO是( A )的一搜索方式。

A、分支界限法B、动态规划法C、贪心法D、回溯法7、投点法是( B )的一种。

A、分支界限算法B、概率算法C、贪心算法D、回溯算法8、若线性规划问题存在最优解,它一定不在( C )A.可行域的某个顶点上 B.可行域的某条边上 C.可行域内部 D.以上都不对9、在一般输入数据的程序里,输入多多少少会影响到算法的计算复杂度,为了消除这种影响可用( B )对输入进行预处理。

算法导论习题解答2.3-7

算法导论习题解答2.3-7
•2.3-7 请给出⼀个运⾏为Θ(nlgn)的算法(伪码),使之能在给定⼀个由n个整数构成的集合S和另⼀个整数x时,判断出S中是否存在有两个其和等于x的元素。

解:解题思路:先对集合S进⾏归并排序,然后新建⼀个数组S1,使得S1[i] = x – S[i],再将两个数组并起来。

如果在并的过程中发现有两个元素相等且两个元素⼀个来⾃S,⼀个来⾃S1,则可以确定S中存在有两个其和等于x的元素。

Find whether x exits
1、Sort(S)
2、for i <- 0 to Length(S) – 1
3、 do S1[i] <- x - S[i]
4、for i <- 0 to Length(S) – 1
5、 do Merge( S,S1 )
6、 if S[p] > S1[q]
7、 S0[k] <- S[p] p++ k++
8、 if S[p] < S1[q]
9、 S0[k] <- S[q] q++ k++
10、 if S[p] == S1[q]
11、 return true
12、return false
在第⼀⾏进⾏排序时,时间代价为Θ(nlgn),后来的合并过程时间代价为Θ(n),总的时间代价为Θ(nlgn)。

《算法导论(原书第3版)》第24章部分题目解答

《算法导论(原书第3版)》第24章部分题⽬解答第24章单源最短路径24.1 Bellman-Ford算法24.1-4思路:先做|V|-1遍松弛操作,然后再做⼀遍松弛操作,对于这次松弛操作中dist值被更新的点,必然包含了每个负环中的⾄少⼀个点。

对于这些点做dfs查找它们能够在图中到达哪些点,所有被搜索到的点即为题⽬要求找的点部分c++代码:#include <bits/stdc++.h>using namespace std;const int maxn = ...;const int inf = 0x3f3f3f3f;//正⽆穷struct E{int x,y,z;//三元组(x,y,z)表⽰⼀条有向边。

从x出发到y,权值为z。

}vector<E> es;//存边vector<int> e[maxn];//模拟邻接链表vector<int> vec;//存起始点void bellman(int s){for(int i = 1; i<=n; i++)d[i]=inf;d[s] = 0;for(int t = 1; t<n; t++){for(auto e:es){if(d[e.x]!=inf && d[e.x]+e.z<d[e.y])d[e.y] = d[e.x] + w;}}for(auto e:es){if(d[e.x]!=inf && d[e.x]+e.z<d[e.y]){vec.push_back(y);}}}int v[maxn];void dfs(int x){v[x] = 1;for(auto y: e){if(!v[y]) dfs(y);}}void solve(int s){bellman(s);for(auto x:vec){if(!v[x]) dfs(x);}for(int i = 1; i<=n; i++){if(v[i]) cout<<"负⽆穷"<<endl;else if(d[i]==inf) cout<<"不可达"<<endl;else cout<<d[i]<<endl;}}24.1-5思路:跑⼀遍Bellman-Ford算法,具体做法如下:1、初始化∀v∈V,d[v]=0。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1、设1S和2S的结构为,,,,,YQXT,其中RYQXT,),(TX和
qq)(

,1S和2S对应的分别为:

(1))(),(01,110ttqqtt
(2))exp(),(01,210ttqqtt
(3))(),(2021,110ttqqtt
试问上述哪些是时不变系统,哪些不是,为什么?

2、(1)负指数分布的概率密度函数0 00 )(xxexfx,试求负指数分布的随机
数(求逆法);
(2)若0-1均匀分布随机数采用如下的迭代公式(初始种子为S-4,S-3,S-2,S-1)
Si=aSi-2+bSi-4模m,r i= Si /m。求负指数分布的随机数产生模型的离散时间系统规
范;
(3)给出负指数分布的随机数产生模型的最终序列的模型描述语句。

3、对于离散时间模型 INPUTS=X1X2XP
STATES=Q1Q2Qn
OUTPUTS=Y1Y2Ym
转换函数=:STATESINPUTSSTATES
输出函数=:STATESOUTUTS
(1) 给出它的常态形序列的模型描述语句序列;
(2) 给出它的模型网络图;

4、对于6阶系统









153.004.0000
100015.001.00
01002.001.00
02.005.023170400
005.02.0100
000010

A
,它的特征值为

}52.0,7924.0,0816.0,97.4,08.8,94.9{}{A

,如把系统两个3阶子系统耦

合,问该系统是否弱耦合系统,为什么?

5、详细论述离散事件模型的同态关系及其条件。
6、从一般系统的七元组集合结构S=出发,给出系统行为
的形式化描述。

相关文档
最新文档