Skip to content

Commit

Permalink
CLRS C02
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis1992 committed Jun 14, 2015
1 parent a2b0aff commit 83d8be0
Show file tree
Hide file tree
Showing 18 changed files with 346 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.tex

images/
Tools/
37 changes: 37 additions & 0 deletions C02-Getting-Started/2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### Exercises 2.1-1
***
Using Figure 2.2 as a model, illustrate the operation of INSERTION-SORT on the array A = [31, 41, 59, 26, 41, 58].

### `Answer`
![pic](./repo/s1/1.png)


### Exercises 2.1-2
***
Rewrite the INSERTION-SORT procedure to sort into nonincreasing instead of nondecreasing order.

### `Answer`
![pic](./repo/s1/2.png)


### Exercises 2.1-3
***
Consider the searching problem:
* **Input**: A sequence of n numbers A = [a1, a2, . . . , an] and a value v.* **Output**: An index i such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudocode for **linear search**, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.

### `Answer`
![pic](./repo/s1/3.png)

### Exercises 2.1-4
***
Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n + 1)-element array C. State the problem formally and write pseudocode for adding the two integers.

### `Answer`
![pic](./repo/s1/4.png)



***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

Expand Down
42 changes: 42 additions & 0 deletions C02-Getting-Started/2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
### Exercises 2.2-1
***
Express the function ![](http://latex.codecogs.com/gif.latex?n^3/1000 - 100n^2 - 100n+3)
in terms of Θ-notation.

### `Answer`
Θ(n3)


### Exercises 2.2-2
***
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first n - 1 elements of A. Write pseudocode for this algorithm, which is known as **selection sort**. What loop invariant does this algorithm maintain? Why does it need to run for only the first n - 1 elements, rather than for all n elements? Give the best-case and worst-case running times of selection sort in Θ- notation.

### `Answer`
![pic](./repo/s2/1.png)

时间都是Θ(![](http://latex.codecogs.com/gif.latex?n^2))


### Exercises 2.2-3
***
Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in Θ-notation? Justify your answers.

### `Answer`
* 平均情况应该要查找n/2个元素
* 最坏情况是n个

所以都是Θ(n)


### Exercises 2.2-4
***
How can we modify almost any algorithm to have a good best-case running time?

### `Answer`
投机取巧,往最好情况去想



***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

70 changes: 70 additions & 0 deletions C02-Getting-Started/2.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
### Exercises 2.3-1
***
Using Figure 2.4 as a model, illustrate the operation of merge sort on the array A = [3, 41, 52, 26, 38, 57, 9, 49].

### `Answer`
![pic](./repo/s3/1.png)


### Exercises 2.3-2
***
Rewrite the MERGE procedure so that it does not use sentinels, instead stopping once either array L or R has had all its elements copied back to A and then copying the remainder of the other array back into A.

### `Answer`
[code](./exercise_code/merge-sort.py)


### Exercises 2.3-3
***
Use mathematical induction to show that when n is an exact power of 2, the solution of the recurrence
![](./repo/s3/2.png) is ![](http://latex.codecogs.com/gif.latex? T\(n\) = n\\lg{n})

1.定义 ![](http://latex.codecogs.com/gif.latex? F\(k\) = T\(2^k\))

2.当k = 1时,![](http://latex.codecogs.com/gif.latex? F\(1\) = T\(2\) = 2 = 2\\lg{2} = 2^1\\lg{2^1})

3.假设![](http://latex.codecogs.com/gif.latex? F\(k\) = 2^k \\lg{2^k})

4.![](http://latex.codecogs.com/gif.latex? F\(k+1\) = T\(2^{k+1}\) = 2T\(2^k\)+2^{k+1} = 2*2^k \\lg{2^k} + 2^{k+1} = 2^{k+1}\(\\lg{2^k}+1\) = 2^{k+1}\(\\lg{2^k}+\\lg{2}\) = 2^{k+1}\\lg{2^{k+1}})


### `Answer`
[code](./exercise_code/merge-sort.py)


### Exercises 2.3-4
***
Insertion sort can be expressed as a recursive procedure as follows. In order to sort A[1..n], we recursively sort A[1..n -1] and then insert A[n] into the sorted array A[1..n - 1]. Write a recurrence for the running time of this recursive version of insertion sort.

### `Answer`
![pic](./repo/s3/3.png)

### Exercises 2.3-5
***
Referring back to the searching problem (see Exercise 2.1-3), observe that if the sequence A is sorted, we can check the midpoint of the sequence against v and eliminate half of the sequence from further consideration. **Binary search** is an algorithm that repeats this procedure, halving the size of the remaining portion of the sequence each time. Write pseudocode, either iterative or recursive, for binary search. Argue that the worst-case running time of binary search is Θ(lg n).
### `Answer`
![pic](./repo/s3/4.png)

![](http://latex.codecogs.com/gif.latex? T\(n\) = T\(n/2\) + C)

[python code](./exercise_code/binary-search.py)


### Exercises 2.3-6
***
Observe that the while loop of lines 5 - 7 of the **INSERTION-SORT** procedure in Section 2.1 uses a linear search to scan (backward) through the sorted subarray A[1..j - 1]. Can we use a binary search (see Exercise 2.3-5) instead to improve the overall worst-case running time of insertion sort to Θ(n lg n)?

### `Answer`
不可以,查找可以达到对数级的,但是依然要移动元素,依然是线性的.

### Exercises 2.3-7
***
Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and another integer x,determines whether or not there exist two elements in S whose sum is exactly x.


先用mergesort进行排序,然后两根指针分别在集合的头和尾,往中间扫描~
这个题目利用hashtable可以达到O(n),[my code](https://github.com/gzc/leetcode/blob/master/cpp/001-010/Two%20Sum.cpp)

***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

Expand Down
19 changes: 19 additions & 0 deletions C02-Getting-Started/exercise_code/binary-search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# coding=utf-8

def binarysearch(items, v):
low = 0
high = len(items)-1;
while low <= high:
mid = (low+high)/2;
if(items[mid] == v):
return mid
elif(items[mid] < v):
low += 1
else:
high -= 1
return None

items = [1,2,3,4,5]
print binarysearch(items, 3)
print binarysearch(items, 6)
37 changes: 37 additions & 0 deletions C02-Getting-Started/exercise_code/inversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python
# coding=utf-8

def merge(items, p, q, r):
L = items[p:q+1]
R = items[q+1:r+1]
i = j = 0
k = p
inversions = 0;
while i < len(L) and j < len(R):
if(L[i] < R[j]):
items[k] = L[i]
i += 1
else:
items[k] = R[j]
j += 1
inversions += (len(L) - i)
k += 1
if(j == len(R)):
items[k:r+1] = L[i:]
return inversions



def mergesort(items, p, r):
inversions = 0;
if(p < r):
q = (p+r)/2
inversions += mergesort(items, p, q)
inversions += mergesort(items, q+1, r)
inversions += merge(items, p, q, r)
return inversions


items = [4,3,2,1,17]
inversions = mergesort(items, 0, len(items)-1)
print items,inversions
32 changes: 32 additions & 0 deletions C02-Getting-Started/exercise_code/merge-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python
# coding=utf-8

def merge(items, p, q, r):
L = items[p:q+1]
R = items[q+1:r+1]
i = j = 0
k = p
while i < len(L) and j < len(R):
if(L[i] < R[j]):
items[k] = L[i]
i += 1
else:
items[k] = R[j]
j += 1
k += 1
if(j == len(R)):
items[k:r+1] = L[i:]



def mergesort(items, p, r):
if(p < r):
q = (p+r)/2
mergesort(items, p, q)
mergesort(items, q+1, r)
merge(items, p, q, r)


items = [4,3,2,1,17]
mergesort(items, 0, len(items)-1)
print items
100 changes: 100 additions & 0 deletions C02-Getting-Started/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
### Problems 1 : Insertion sort on small arrays in merge sort
***
Although merge sort runs in Θ(n lg n) worst-case time and insertion sort runs in Θ(n^2) worst- case time, the constant factors in insertion sort make it faster for small n. Thus, it makes sense to use insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which n/k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism, where k is a value to be determined.
a. Show that the n/k sublists, each of length k, can be sorted by insertion sort in Θ(nk) worst-case time.
b. Show that the sublists can be merged in Θ(n lg (n/k) worst-case time.
c. Given that the modified algorithm runs in Θ(nk + n lg (n/k)) worst-case time, what isthe largest asymptotic (Θnotation) value of k as a function of n for which the modifiedalgorithm has the same asymptotic running time as standard merge sort?
d. How should k be chosen in practice?

### `Answer`
a. 总共有n/k个长度为k的列表,所以![](http://latex.codecogs.com/gif.latex?T\(n\) = n/k * \\Theta\(k^2\) = \\Theta\(nk\) )

b. 因为现在的层数为lg(n/k),所以是Θ(n lg (n/k))

c. 要么是Θ(nk) = Θ(nlgn),要么是Θ(nlg(n/k)) = Θ(nlgn).前者k = lgn,后者k = n^0.5.但是后者不成立,因为nk = n^1.5 > nlgn. 所以k = lgn.

d. [Exercise 1.2.2](https://github.com/gzc/CLRS/blob/master/C01-The-Role-of-Algorithms-in-Computing/1.2.md)当 n>43 mergesort 好于 insertion sort. 所以可以选40.



### Problems 2 : Correctness of bubblesort
***
Bubblesort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements that are out of order.

### `Answer`
a. 我们还需要证明数组里面的元素就是原来的那些元素

b. **每次循环前,子数组A[j..]的最小元素是A[j]** <br />
Initialization:刚开始的时候,子数组只有一个元素,是A的最后一个元素,循环不变式自然成立 <br />
Maintenance:每个迭代,会比较A[j]和A[j-1]的大小,并把小的往前放 <br />
Termination:迭代结束时,j = i,A[i]是子数组A[i..]最小的元素 <br />

c. **每次循环前,A[1,i-1]是一个排序好的数组,且小于等于A[i..]中的元素**
Initialization:刚开始的时候,子数组为空,循环不变式自然成立 <br />
Maintenance:每个迭代,A[i]会变成A[i..]中的最小元素 <br />
Termination:迭代结束时,i = n, 我们获得了一个排序好的数组 <br />

d. **bubblesort**的最坏运行时间与**insertion sort**一样都是Θ(n^2),但是一般来说**bubblesort**会慢点,因为它有许多的**swap**操作.


### Problems 3 : Correctness of Horner's rule
***
### `Answer`

**a.** Θ(n)

**b.**
Naive-Poly-Eval:

y = 0
for i = 0 to n
m = 1
for k = 1 to i
m = m·x
y = y + aᵢ·m
运行时间是Θ(n2),非常慢

**c.**

**Initialization:** 一开始没有项,y = 0

**Maintenance:**根据循环不变式,第i次迭代结束有

![](http://latex.codecogs.com/gif.latex? y = a_i + x\\sum_{k = 0}^{n-\(i+1\)}a_{k+i+1}x^k =
a_ix^0 + \\sum_{k = 0}^{n-i-1}a_{k+i+1}x^{k+1} =
\\sum_{k = -1}^{n-i-1}a_{k+i+1}x^{k+1} =
\\sum_{k = 0}^{n-i}a_{k+i}x^k )

**Termination:**循环结束时 i = -1, 将i = 0代入
![](http://latex.codecogs.com/gif.latex? y = \\sum_{k = 0}^{n}a_{k}x^k)

**d.**
前面已经证明了循环不变式,结论自然是成立的.


### Problems 4 : Inversions
***
Let A[1..n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A.

a. List the five inversions of the array 2, 3, 8, 6, 1.
b. What array with elements from the set {1, 2, . . . , n} has the most inversions? Howmany does it have?
c. What is the relationship between the running time of insertion sort and the number ofinversions in the input array? Justify your answer.
d. Give an algorithm that determines the number of inversions in any permutation on nelements in Θ(n lg n) worst-case time. (Hint: Modify merge sort.)

### `Answer`
**a.**
⟨2,1⟩, ⟨3,1⟩, ⟨8,6⟩, ⟨8,1⟩ 和 ⟨6,1⟩.

**b.**
数组[n,n-1,n-2,...,3,2,1]有最大的逆序数对. <br />
一共是(n-1) + (n-2) + …… + 3 + 2 + 1 = n(n-1)/2

**c.**
插入排序中移动元素的次数就是逆序数的对数.

**d.**
[code](./exercise_code/inversions.py)

***
Follow [@louis1992](https://github.com/gzc) on github to help finish this task.

Expand Down
Binary file added C02-Getting-Started/repo/s1/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s1/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s1/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s1/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s2/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s3/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s3/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s3/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added C02-Getting-Started/repo/s3/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
<td align="center"><a href="./C01-The-Role-of-Algorithms-in-Computing/problem.md"><font color="black">p</font></td>
</tr>

<tr>
<td align="center">II</td>
<td align="center"><a href="./C02-Getting-Started/2.1.md"><font color="black">1</font></td>
<td align="center"><a href="./C02-Getting-Started/2.2.md"><font color="black">2</font></td>
<td align="center"><a href="./C02-Getting-Started/2.3.md"><font color="black">3</font></td>
<td align="center"><a href="./C02-Getting-Started/problem.md"><font color="black">p</font></td>
</tr>

<tr>
<td align="center">IX</td>
<td align="center"><a href="./C09-Medians-and-Order-Statistics/9.1.md"><font color="black">1</font></td>
Expand Down

0 comments on commit 83d8be0

Please sign in to comment.