Skip to content

Commit

Permalink
Merge pull request gzc#175 from kestory/master
Browse files Browse the repository at this point in the history
Modify 11.4
  • Loading branch information
Zhenchao Gan authored Jan 15, 2018
2 parents 20d9f93 + 9b6f464 commit 00a8769
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
55 changes: 29 additions & 26 deletions C11-Hash-Tables/11.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ Write pseudocode for HASH-DELETE as outlined in the text, and modify HASH-INSERT

### `Answer`

HASH-DELETE(T, k):
i <- 0
repeat j <- h(k, i)
if T[j] == k:
T[j] = "DELETED"
return
else if T[j] == NIL:
break
else:
i <- i + 1
until i == m
error "k is not in T"
HASH-DELETE(T, k)
i = 0
repeat
j = h(k, i)
if T[j] == k
temp = T[j]
T[j] = DELETED
return temp
i = i + 1
until T[j] == NIL or i == m
return NIL
HASH-INSERT(T, k):
i <- 0
repeat j <- h(k, i)
if T[j] == NIL || T[j] == "DELETED":
HASH-INSERT(T, k)
i = 0
repeat
j = h(k, i)
if T[j] == NIL or T[j] == DELETED
T[j] = k
rteurn j
else i <- i + 1
else i = i + 1
until i == m
error "hash table overflow"
Expand Down Expand Up @@ -80,26 +80,29 @@ Suppose that we use double hashing to resolve collisions; that is, we use the ha
对这个题目来说,如果d = 1,则要检查全部的散列.因为要遍历m个位置.

如果d > 1,那么h2(k)和m同时除d后又互质.可能要遍历m/d个位置.

### Exercises 11.4-4
***
Consider an open-address hash table with uniform hashing. Give upper bounds on the expected number of probes in an unsuccessful search and on the expected number of probes in a successful search when the load factor is 3/4 and when it is 7/8.

### `Answer`
Theorem 11.6. Given an open address hash table with load factor α = n/m < 1, the
expected number of probes in an unsuccessful search is at most 1/(1-α), assuming
uniform hashing.
Theorem 11.6. Given an open address hash table with load factor ![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{n}{m}<&space;1), the
expected number of probes in an unsuccessful search is at most ![](https://latex.codecogs.com/gif.latex?\frac{1}{1-\alpha}), assuming uniform hashing.

![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{3}{4}&space;\quad&space;\frac{1}{1-\alpha}&space;=&space;\frac{1}{1-\frac{3}{4}}&space;=&space;4probes)

α = ¾, then the upper bound on the number of probes = 1 / (1 - ¾ ) = 4 probes
![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{7}{8}&space;\quad&space;\frac{1}{1-\alpha}&space;=&space;\frac{1}{1-\frac{7}{8}}&space;=&space;8probes)

α = 7/8, then the upper bound on the number of probes = 1 / (1-7/8) = 8 probes

Theorem 11.8. Given an open address hash table with load factor α = n/m < 1, the
expected number of probes in a successful search is at most (1/α) ln (1/(1-α)), assuming
Theorem 11.8. Given an open address hash table with load factor ![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{n}{m}<&space;1), the
expected number of probes in a successful search is at most ![](https://latex.codecogs.com/gif.latex?\frac{1}{\alpha}ln\frac{1}{1-\alpha}), assuming
uniform hashing and assuming that each key in the table is equally likely to be searched
for.

α = ¾. (1/ ¾) ln (1/ (1 – ¾)) = 1.85 probes
α = 7/8. (1/ .875) ln (1/ (1 – .875)) = 2.38 probes
![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{3}{4}&space;\quad&space;\frac{1}{\alpha}ln\frac{1}{1-\alpha}&space;=&space;\frac{1}{\frac{3}{4}}ln\frac{1}{1-\frac{3}{4}}&space;\approx&space;1.85probes)

![](https://latex.codecogs.com/gif.latex?\alpha=&space;\frac{7}{8}&space;\quad&space;\frac{1}{\alpha}ln\frac{1}{1-\alpha}&space;=&space;\frac{1}{\frac{7}{8}}ln\frac{1}{1-\frac{7}{8}}&space;\approx&space;2.37probes)


### Exercises 11.4-5
***
Expand Down
2 changes: 0 additions & 2 deletions C11-Hash-Tables/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
UNSOLVED

[11.1.4](./11.1.md#exercises-111-4)

[11.3.5](./11.3.md#exercises-113-5)
[11.3.6](./11.3.md#exercises-113-6)

Expand Down
2 changes: 1 addition & 1 deletion C16-Greedy-Algorithms/16.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The optimal strategy is the obvious greedy one. Starting with a full tank of gas
Describe an efficient algorithm that, given a set {x1, x2, ...,xn} of points on the real line, determines the smallest set of unit-length closed intervals that contains all of the given points. Argue that your algorithm is correct.

### `Answer`
Consider the following very simple algorithm: Sort the points obtaining a new array {y1,...,yn}. The first interval is given by[y1,y1+1]. If yi is the leftmost point not contained in any existing interval the next interval is [y1,y1+1] and so on.
Consider the following very simple algorithm: Sort the points obtaining a new array {y1,...,yn}. The first interval is given by[y1,y1+1]. If yi is the leftmost point not contained in any existing interval the next interval is [yi,yi+1] and so on.

Thie greedy algorithm does the job since the rightmost element of the set must be contained in an interval and we can do no better than the interval [y1,y1+1]. Additionally, any subproblem to the optimal solution must be optimal. This is easily seen by considering the problem for the points greater than y1+1 and arguing inductively.

Expand Down

0 comments on commit 00a8769

Please sign in to comment.