锅炉信息网 > 锅炉知识 > 锅炉百科

CS 130B HW 2 Type up

发布时间:

Question 1:     Rmk:Question 2:Input:  dollars, bank  ,  , and fraction of amount kept for each bank  Output: maximum am

Question 1:

Rmk:

Question 2:

  • Input: dollars, bank , , and fraction of amount kept for each bank
  • Output: maximum amount of money that can be transferred.
  • The algorithm is then going to be a slight modification of Dijkstra's Algorithm, with the only difference being how the weight of each vertex is calculated.
  • In our algorithm, the weight of a vertex is calculated by .
  • Additionally, while the classical Dijkstra pops the vertex with the minimum weight from the priority queue, here we pop the vertex with the maximum weight (because it corresponds to our remaining balance).
  • return

b) The algorithm terminates because for each round, it adds a vertex to the list of vertices that has been visited. So the unvisited vertices will eventually run out and there's nothing to be added to the priority queue.

Correctness: It suffices to show our algorithm never makes a mistake (in the sense that always labels the vertex it visits--not the ones whose distance are upper bounded, but the ones that have been actually visited-- with the correct distance). The base case holds trivially. Then by induction, suppose it hasn't made a mistake on the i-th round, by the greedy nature of our algorithm, the (i+1)-th vertex it chooses to label must be correct . Since if it is not correct, then there must be some path to it that cost less the one which our algorithm has chosen, but the reason our algorithm didn't choose that path is because no unvisited vertex has cost smaller than that between our starting point and the (i+1)-th vertex (which is chosen by our algorithm at this round).

c) It should be clear that the complexity of our algorithm is the same as Dijkstra. .

Question 4:

a)Alg: ( )

  • Input: numbers , distance .
  • Output: number of pairs of people who live at most apart.
  • Store in an array and sort it.
  • Let be the counter of pairs of people that are at most distance apart
  • Let (as global variables)
  • Loop until :
    • Increase by one each time as long as and . Then decrease by one (because it is one beyond the distance range).
    • if : . (this is the situation when our window of size at most cannot include any person other than the person at its left endpoint).
    • else:
  • Output

b) Proof:

For each loop, we start with the person and keep increasing by one until . After decreasing by one from there, we know for sure that all people that are both 1)to the right of 2) is at most distance from , has been added to our output . The people who are 1)to the left of 2) at most distance from , has been previously counted, so there's no need to count them.

In a slightly concise way, we could express the above paragraph in one sentence:

where means the th person instead of the distance.

Since the loops terminates only when , we have cover the entire range , so we have included every pair possible and have not over-counted. Therefore, the algorithm is correct.

c)

Sorting the array:

Going through loop:

Total:

上一篇:运行KEGG和GSEA时报错

下一篇:wkl后续zb

精选推荐

  • 711关东煮供应商
    711关东煮供应商

    今天给大家介绍三位,奶粉,全家、罗森这些便利店里关东煮的供应商。店里卖三四块钱一串的关东煮,在网上买不到,一块钱就搞定。首先关东

  • 健康日历|高压锅容易爆炸的4个原因
    健康日历|高压锅容易爆炸的4个原因

    来源:医药养生保健报设计:李雅琴医学审核:姜峰出品人:胡丽丽

  • 高炉
    高炉

    今天这活却是个白事,等到了时辰,那家人便准备火化,本来准备送普炉,我却心中一动,便对那家人说道:“这老人走也不要省,还是送高炉吧。”

  • 高压锅和电压力锅的区别,推荐几款点压力锅
    高压锅和电压力锅的区别,推荐几款点压

    记得之前有一次去朋友家玩,他正在用高压锅煮小米粥,是的,高压锅压小米粥,大概煮了半小时,高压锅突然爆炸了,现场惨不忍睹啊,幸好厨房里没

0