1. 程式人生 > >《演算法導論》第3版 第2章課後題答案(英文版)

《演算法導論》第3版 第2章課後題答案(英文版)

Solution to Exercise 2.2-2

    SELECTION-SORT(A)

    n = A.length

    for j = 1 to n - 1

        smallest = j

        for i = j + 1 to n

            if A[i] < A[smallest]

                smallest = i

        exchange A[j] with A[smallest]

    The algorithm maintains the loop invariant that at the start of each iteration of the outer for

loop, the subarray A[1..j - 1] consists of the j -1 smallest elements in the array A[1..n], and this subarray is in sorted order. After the first n - 1 elements, the subarray A[1..n-1] contains the smallest n - 1 elements, sorted, and therefore element A[n] must be the largest element.

     The running time of the algorithm is θ(n^2) for all cases.

Solution to Exercise 2.2-4

    Modify the algorithm so it tests whether the input satisfies some special-case condition and, if it does, output a pre-computed answer. The best-case running time is generally not a good measure of an algorithm.

Solution to Exercise 2.3-5

      Procedure BINARY-SEARCH takes a sorted array A, a value v, and a rang[low..high] of the array, in which we search for the value v. The procedure compare v to the array entry at the mid point of the range and decides to eliminate half the range from further consideration. We give both iterative and recursive versions, each of which returns either an index i such that A[i] = v, or NIL if no entry of A[low..high] contains the value v. The initial call to either version should have the parameters A, v, 1, n.

       ITERATIVE-BINARY-SEARCH(A, v, low, high)

       while low<=high

           mid = ⌊(low + high) / 2⌋

           if v == A[mid]

               return mid

           elseif v > A[mid]

                low = mid + 1

           else high = mid - 1

        return NIL

        RECURSIVE-BINARY-SEARCH(A, v, low, high)

        if low > high

            return NIL

        mid =⌊(low + high) / 2⌋

        if v == A[mid]

            return mid

        elseif v > A[mid]

            return RECURSIVE-BINARY-SEARCH(A, v, mid + 1, high)

        else return  RECURSIVE-BINARY-SEARCH(A, v, low, mid - 1)

    Both procedures terminate the search unsuccessfully when the range is empty (i.e., low > high) and terminate it successfully if the value v has been found. Based on the comparison of v to the middle element in the searched range, the search continues with the range halved. The recurrence for the these procedures is therefore T(n) = T(n/2) + θ(1), whose solution is T(n) = θ(lgn).

Solution to Problem 2-4

    a. The inversions are (1,5), (2,5), (3,4), (3,5), (4,5). (Remember that inversions are specified by indices rather than by the values in the array.)

    b. The array with elements from {1, 2, . . . , n} with the most inversions is <n, n - 1, n - 2, . . . , 2, 1>. For all 1 <= i <= j <= n, there is an inversion (i, j). The number of such inversions is n(n - 1)/2.

    c. Suppose that the array A starts out with an inversion (k, j). Then k < j and A[k] > A[j]. At the time that the outer for loop of lines 1-8 sets key = A[j], the value that started in A[k] is still somewhere to the left of A[j]. That is, it's in A[i], where 1 <= i < j, and so the inversion has become (i, j). Some iteration of the while loop of lines 5-7 moves A[i] one position to the right. Line 8 will eventually drop key to the left of this element , thus eliminating the inversion. Because line 5 moves only elements that are less than key, it moves only elements that correspond to inversions. In other words, each iteration of the while loop of lines 5-7 corresponds to the elimination of one inversion.

    d. We follow the hint and modify merge sort to count the number of inversions in θ(n lgn) time.

        To start, let us define a merge-inversion as a situation within the execution of merge sort in which the MERGE procedure, after copying A[p . . q] to L and A[q+1 .. r] to R, has values x in L and y in R such that x > y, Consider an inversion (i, j), and let x = A[i] and y = A[j], so that i < j and x > y. We claim that if we were to run merge sort, there would be exactly one merge-inversion involving x and y. To see why, observe that the only way in which array elements change their position is within the MERGE procedure. Moreover, since MERGE keeps elements within L in the same relative order to each other, and correspondingly for R, the only way in which two elements can change their ordering relative to each other is for the greater one to appear in L and  the lesser one to appear in R. Thus, there is at least one merge-inversion involving x and y. To see that there is exactly one such merge-inversion, observe that after any call of MERGE that involves both x and y, they are in the same sorted subarray and will therefore both appear in L or both appear in R in any given call thereafter. Thus we have proven the claim.

       We have shown that every inversion implies one merge-inversion. In fact, the correspondence between inversions and merge-inversion is one-to-one. Suppose we have a merge-inversion involving values x and y, where x originally was A[i] and y was originally A[j]. Since we have merge-inversion, x > y. And since x is in L and y is in R, x must be within a subarray preceding the subarray containing y . Therefore x started out in a position i preceding y's original position j, and so (i, j) is and inversion.

        Having shown a one-to-one correspondence between inversions and merge-inversions, if suffices for us to count merge-inversions.

        Consider a merge-inversion involving  y in R. let z be the smallest value in L that is greater than y. At some point during the merging process, z and y will be the 'exposed' values in L and R, i.e., we will have z = L[i] and y = R[j] in line 13 of MERGE. At that time, there will be merge-inversions involving y and L[i], L[i + 1], L[i + 2], . . . , L[n1], and these n1 - i + 1 merge-inversions will be the only ones involving y. Therefore, we need to detect the first time that z and y become exposed during the MERGE procedure and add the value of n1 - i + 1 at that time to our total count of merge-inversions.

        The following pseudocode, modeled on merge sort, works as we have just described. It also sort the array A.

        COUNT-INVERSIONS(A, p, r)

        inversions = 0

        if p < r

            q = ⌊(p + r) / 2⌋

            inversions = inversions + COUNT-INVERSIONS(A, p, q)

            inversions = inversions + COUNT-INVERSIONS(A, q + 1, r)

            inversions = inversions + MERGE-INVERSION(A, p, q, r)

        return inversions

        MERGE-INVERSIONS(A, p, q, r)

        n1 = q - p + 1

        n2 = r - q

        let L[1 . . n1 + 1] and R[1 . . n2 + 1]

        for i = 1 to n1

            L[i] = A[p + i - 1]

        for j = 1 to n2

            R[i] = A[q + j]

        L[n1 + 1] = ∞

        R[n2 + 1] = ∞

        i = 1

        j = 1

        inversions = 0

        counted = FALSE

        for k = p to r

            if counted == FALSE and R[j] < L[i]

                inversions = inversion + n - i + 1

                counted = TRUE

            if L[i] <= R[j]

                A[k] = L[i]

                i = i + 1

            else A[k] = R[j]

                j = j + 1

               counted = FALSE

        return inversions

        This initial call is COUNT-INVERSIONS(A, 1, n)

        In MERGE-INVERSIONS, the boolean variable counted indicates whether we have counted the merge-inversions involving R[j]. We count them the first time that both R[j] is exposed and a value greater than R[j] becomes exposed in the L array. We set counted to FALSE upon each time that a new value becomes exposed in R. We don't have to worry about merge-inversions involving the sentinel ∞ in R, since no value in L will be greater than ∞.

        Since we have added only a constant amount of additional work to each procedure call and to each iteration of the last for loop of the merging procedure, the total running time of the above pseudocode is the same as for merge sort: θ(n lgn).

    

相關推薦

演算法導論3 2課後答案英文版

Solution to Exercise 2.2-2    SELECTION-SORT(A)    n = A.length    for j = 1 to n - 1        smallest = j        for i = j + 1 to n       

尚學堂 實戰java程式設計 1,2課後答案

第1章 一、選擇題 1.C 2.AD 3.D 4.B 5.A 二、簡答題 1.答:計算機語言總的來說分為機器語言,組合語言,高階語言三大類。這三種語言是計算機語言發展歷史的三個階段。 2.答:java在作業系統上加入了jvm虛擬機器,程式執行在jvm上,jvm會

《計算機演算法設計與分析 2+3+4 (王曉東) 》原書附答案pdf電子書附下載連結+30個總結JVM虛擬機器的技術文排版好收藏版

技術書閱讀方法論 一.速讀一遍(最好在1~2天內完成) 人的大腦記憶力有限,在一天內快速看完一本書會在大腦裡留下深刻印象,對於之後複習以及總結都會有特別好的作用。 對於每一章的知識,先閱讀標題,弄懂大概講的是什麼主題,再去快速看一遍,不懂也沒有關係,但是一定要在不懂的

《算法導論》讀書筆記--1、2課後

秦九韶 ons 全局變量 思考 end exc ray 存在 檢查 第一章 轉自http://www.cnblogs.com/batteryhp/p/4654860.html 思考題 1-1(運行時間的比較)確定時間t內求解的問題的最大規模。 上面是網上提供的答案。

組合語言(3) 11相關內容及實驗11

第十一章 標誌暫存器 文章目錄 第十一章 標誌暫存器 章節內容 概述 ZF標誌 PF標誌 SF標誌 CF標誌 OF標誌

組合語言(3) 10相關內容及實驗10和課程設計1

第十章 CALL和RET指令 文章目錄 第十章 CALL和RET指令 章節內容 概述 ret/retf call ret和call配合

《實用電子元器件與電路基礎》 原書中文3+2+英文4等5本書 (美)舍茨(美)莫克著

資源連結:https://pan.baidu.com/s/1TOpfjWyodb9Cqe3kybW1iw分享電路設計重要參考資料5本:《實用電子元器件與電路基礎 原書第3版 (美)舍茨,莫克著》《實用電子元器件與電路基礎 原書第2版》《Practical Electronics for Inventors 1

深入理解Linux核心3--筆記-2.pdf

Chapter 8. Memory Management 8.1. Page Frame Management 8.1.1. Page Descriptors State information of a page frame is kept in a page des

C++ Primer Plus62課後習題答案

//第1題 #include<iostream> int main() { using namespace std; cout << "Enter your name:wahaha" << endl; cout << "Enter your a

分享《Python 遊戲程式設計快速上手(3)》高清中文版PDF+高清英文版PDF+原始碼

下載:https://pan.baidu.com/s/1n4hyQq1YFkuLiL2G3388fw 更多資料分享:http://blog.51cto.com/3215120 《Python 遊戲程式設計快速上手(第3版)》高清中文版PDF+高清英文版PDF+原始碼高清中文版,帶目錄和書籤,文字能夠複製貼

分享《Python 遊戲編程快速上手(3)》高清中文版PDF+高清英文版PDF+源代碼

type 粘貼 mage 技術分享 image 復制 快速 pan follow 下載:https://pan.baidu.com/s/1n4hyQq1YFkuLiL2G3388fw 更多資料分享:http://blog.51cto.com/3215120 《Python

2 RFID基礎知識

1.RFID即射頻識別。(常稱為電子標籤)RFID射頻識別是一種非接觸式的自動識別技術,識別高速運動物體並可同時識別多個標籤,識別距離可達幾十米。 2.RFID的組成:一套完整的RFID系統必須由標籤、閱讀器和天線組成。 3.電子標籤的分類:標籤類、注塑類、卡片類。 4.閱讀器與電子標籤之間的射頻訊

尚學堂 實戰java程式設計 3、4課後答案

尚學堂 實戰java程式設計 第3、4章課後題答案 第3章 一、選擇題 1.A 2.BD 解析:switch的的判斷表示式的資料型別:byte short int char 3.A 解析:0+3+5=8 4.BD 解析:函式過載時對返回資料型別不做檢查,但形引數量或型別必須變化,B和

Java語言程序設計第二課後習題答案僅供參考

[] main 是否 支付 都去 port span 時區 div 2.1 註意不同類型轉換 1 import java.util.Scanner; 2 3 public class Ch02 { 4 public static void main

《Javascript 高階程式設計()》筆記0x2 JavaScript基本概念1

目錄 語法 變數 資料型別     typeof:     Undefined型別      Null型別     Boolean型別     Numbe

計算機網路學習筆記——課後答案詳解

1、假定站點A和B在同一個10Mb/s乙太網網段上。這兩個站點之間的傳播時延為225位元時間。現假定A開始傳送一幀,並且在A傳送結束之前B也傳送一幀。如果A傳送的是乙太網所容許的最短的幀,那麼A在檢測到和B發生碰撞之前能否把自己的資料傳送完畢?換言之,如果A在傳送完畢之前並沒有檢測到碰撞,那麼能否肯定A所傳送

《統計學習方法》1 課後答案

1.1 說明伯努利模型的極大似然估計以及貝葉斯估計中的統計學習方法三要素。伯努利模型是定義在取值為0與1的隨機變數上的概率分佈。假設觀測到伯努利模型n次獨立的資料生成結果,其中k次的結果為1,這時可以用

c++ primer plus 課後答案

#include<iostream> #include<string> using namespace std; int main() { string first_name; string last_name; char grade;

c++ primer plus 課後答案

#include <iostream> using namespace std; int main() { int num_1,num_2,sum=0; cout << "Please enter two number: "; cin >&g

c++ primer plus 課後答案

#include <iostream> #include <cctype> using namespace std; int main() { char in_put; do { cout << "Please ente