You are on page 1of 20

COMP-311: Algorithms and Data Structures Problem Set 4

Juan Duchimaza November 3, 2010

Contents
1 Book Problems 1.1 Problem 5.1 . 1.2 Problem 5.16 1.3 Problem 6.2 . 1.4 Problem 6.3 . 1.5 Problem 6.10 1.6 Problem 7.3 . 1.7 Problem 7.31 1.8 Problem 9.1 . 1.9 Problem 9.5 . 1.10 Problem 9.7 . 1.11 Problem 9.10 2 Appendix 2.1 Problem 6.2 . 2.1.1 Part a 2.1.2 Part b 2.2 Problem 6.3 . 3 3 6 6 7 8 8 9 10 11 13 14 16 16 16 18 19

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

This page intentionally left blank.

Part 1 Book Problems


1.1 Problem 5.1

Given input {4371, 1323, 6173, 4199, 4344, 9679, 1989} and a hash function h(x) = x(mod 10), show the resulting: a. Separate chaining hash table. A separate chaining hash table simply keeps a list of all elements that hash to the same value, almost like a 2-D array. There is no need to worry about collisions. Figure 1.1 shows the resulting hash table.

Figure 1.1: Separate chaining hash table for Problem 5.1 a.

b. Hash table using linear probing. Using linear probing, the numbers that experience collision are 6173 (with 1323), 4344 (with 6173), 9679 (with 4199), and 1989 (with 4199 and then with 9679). Figure 1.2 shows the resulting hash table.
After 4371 0 1 2 3 4 5 6 7 8 9 4371 After 1323 4371 1323 After 6173 4371 1323 6173 After 4199 4371 1323 6173 After 4344 4371 1323 6173 4344 After 9679 9679 4371 1323 6173 4344 After 1989 9679 4371 1989 1323 6173 4344

4199

4199

4199

4199

Figure 1.2: Hash table using linear probing. A red color means the number experienced one or more collisions when inserted.

c. Hash table using quadratic probing. With quadratic probing, after each successive probe you take a step equivalent to s = i2 , where i is the number of probes you have done so far. In this hash table, most of the collisions require only one probe, so the table for quadratic probing in Figure 1.3 resembles the table for linear probing in Figure 1.2. The main issue arises when 1989 is inserted. After i = 1 probes, it looks at index 0, which is already full. After i = 2 probes, it looks at index (0 + 22 ) mod 10 = 4, which is already full. After i = 3 probes, it looks at index (4 + 32 ) mod 10 = 3. After i = 4 probes, it looks at index (3+42 ) mod 10 = 9. After i = 5 probes, it looks at index (9+52 ) mod 10 = 4 again. After i = 6 probes, it looks at index (4 + 62 ) mod 10 = 0. After i = 7 probes, it looks at index 9. After i = 8 probes, it looks at index 3. After i = 9 probes, it looks at index 4. After i = 10 probes, it looks at index 4 again. After i = 11 probes, it looks at index 5. After i = 12 probes, it looks at index 9. Finally, after i = 13 probes, it looks at index 8, which it nds is empty. By this point, quadratic probing has taken a ridiculous amount of steps (818 steps!) to nd a good place to hash 1989 into.

After 4371 0 1 2 3 4 5 6 7 8 9 4371

After 1323 4371 1323

After 6173 4371 1323 6173

After 4199 4371 1323 6173

After 4344 4371 1323 6173 4344

After 9679 9679 4371 1323 6173 4344

After 1989 9679 4371 1323 6173 4344

4199

4199

4199

1989 4199

Figure 1.3: Hash table using quadratic probing. A red color means the number experienced one or more collisions when inserted. d. Hash table with second hash function h2 (x) = 7 (x mod 7). With a second hash function, after the hashing function h(x) = x(mod 10) nds a collision, it continues to use h2 (x) to properly hash the value. Thus, for example, when h(x) nds a collision when the attempt to insert 6173 is made, h2 (x) moves ahead 7 (6173 mod 7) = 7 6 = 1 index spots. The problem comes in when 1989 is hashed into the table. When it nds that index spot 9 is taken, the second hash function moves ahead 7 (1989 mod 7) = 7 1 = 6 index spots, to index number 5. From there, it moves ahead another 6 index spots to 1, which is already occupied. Again, it will move ahead another 6 slots to 7, and then to slot number 3, 9, and on to 5 again. Because the second hashing function will keep tying to hash in this cycle, the heap will at some point be deemed full and 1989 will not be hashed.
After 4371 0 1 2 3 4 5 6 7 8 9 4371 After 1323 4371 1323 After 6173 4371 1323 6173 After 4199 4371 1323 6173 After 4344 4371 1323 6173 After 9679 4371 1323 6173 9679 4344 4199 After 1989 4371 1323 6173 9679 4344 4199

4344 4199 4199

Figure 1.4: Hash table using second hashing. A red color means the number experienced one or more collisions when inserted. 5

1.2

Problem 5.16

Under certain assumptions, the expected cost of an insertion into a hash table with secondary clustering is given by 1/(1 ) ln(1 ). Unfortunately, this formula is not accurate for quadratic probing. However, assuming that it is, determine the following: a. The expected cost of an unsuccessful search. In a hash table with secondary clustering, the expected cost of an unsuccessful search is the same as the expected cost of an insertion. Thus, E (c) = 1 ln(1 ) (1 )

b. The expected cost of a successful search. The expected cost of a successful search is: 1 1 ln(1 ) 0 (1 ) 1 1 ln(1 )) E= ( 0 (1 ) 0 0 1 2 E = [ln(1 ) ((1 )(ln(1 ) 1))] 0 22 1 E = (ln(1 ) + (1 )(ln(1 ) 1) + 1) 2 E=

1.3

Problem 6.2

a. Show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2, one at a time, into an initially empty binary heap. The result of building the binary heap one by one is shown in Figure 1.5. A step-by-step solution is provided in the Appendix. b. Show the result of using the linear-time algorithm to build a binary heap using the same input. The result of building the binary heap by using the linear-time algorithm is shown in Figure 1.6. A step-by-step solution is provided in the Appendix.

1 3 6 15 14 12 7 9 10 5 11 13 2 4 8

Figure 1.5: Answer to problem 6.2a.

1 3 12 15 14 9 6 7 5 4 11 13 2 8 10

Figure 1.6: Answer to problem 6.2b.

1.4

Problem 6.3

Show the result of performing three deleteMin operations in the heap of the previous exercise. The result of deleting 1, 2, and 3 from the tree in Figure heap is shown in Figure 1.7 on the next page. A step-by-step solution is provided in the Appendix.

1.5

Problem 6.10

a. Give an algorithm to nd all nodes less than some value, X , in a binary heap. Your algorithm should run in O(K ), where K is the number of nodes output. The best approach to nd all the nodes less than X in a binary heap is to recursively check each node, starting with the root and following with the 7

4 6 13 15 14 12 7 9 10 11 5 8

Figure 1.7: Answer to problem 6.3.

children, and comparing the value in that node to X. If the value is greater than or equal to X, that nodes children will have a greater value, and that is as far as the search needs to go. Additionally, if it encounters a node that does not have children (ie, a leaf), then start collapsing the recursive calls. The proposed O(K ) algorithm is listed below.
1 2 3 4 5 6 7 v o i d l e s s T h a n ( Node c u r r ) i f ( curr i s null ) return i f (X < c u r r ) \\ add c u r r t o queue , s t a c k , p r i n t o u t s t r i n g , array , e t c lessThan ( curr . l e f t ) lessThan ( curr . r i g h t )

Figure 1.8: Answer to problem 6.10.

1.6

Problem 7.3

Suppose we exchange elements a[i] and a[i+k], which were originally out of order. Prove that at least 1 and at most 2k 1 inversions are removed. In any list of n elements where there exist x inversions, exchanging elements i and i+k, where a[i] > a[i+k], you will at least get rid of that one inversion, especially in the case where k = 1 (a[i] and a[i+k] are consecutive elements). In the worst case where the list sorted in a descending order (or an order that is completely opposite to how it should be sorted), you have 8

n(n + 1) x= inversions. By exchanging the elements, you will get rid of the 2 inversions coming from a[i] up until a[i+k], a total of k inversions: {(a[i],a[i+1]), (a[i], a[i+2]), (a[i], a[i+3]), . . . , (a[i], a[i+k])}. Additionally, by replacing a[i+k] with a larger value a[i], you will be removing the inversions from all elements from a[i+1] through a[i+k-1] for a total of k 1 inversions: {(a[i+1],a[i+k]), (a[i+2], a[i+k]), (a[i+3], a[i+k]), . . . , (a[i+(k-1)], a[i+k])}. Thus, you remove k + k 1 = 2k 1 inversions by exchanging elements a[i] and a[i+k].

1.7

Problem 7.31

A sorting algorithm is stable if elements with equal keys are left in the same order as they occur in the input. Which of the sorting algorithms in this chapter are stable and which are not? Why? Insertion sort is stable because it checks a key in a specic element with the keys in neighboring elements before swapping, and the only reasons for which it swaps elements is if it nds a dierence (ie, compareTo < 0). If the keys are the equal, then the sort does not swap elements. Mergesort is stable because, given the routine from the book, sorting begins from the left (earliest input) and move right. It stores an element in the nal array if it nds that the current element from the left array has a key that is lower than or equal to the current element from the right array (the array that is input later). Thus, the element that was input earlier is given preference. Bucket sort is stable. As it creates the new, sorted array, it traverses the input array beginning at the end. Meanwhile, the sort also populates the new array beginning from the last index spot at which an element with a given key may be found in the new array. Thus, by the end of sorting, the input order is maintained. Shellsort is not stable. As a counterexample, consider an array a = {5, 4, 31 , 32 , 33 , 34 }, where the subscript is used simply to distinguish the elements. After a 3-sort, the array will be a = {32 , 33 , 31 , 5, 4, 34 }. After the subsequent 1-sort, the sorted array will be a = {32 , 33 , 31 , 34 , 4, 5}. Thus, it is clear that the order of input elements is not maintained. 9

Heapsort is not stable because by performing a deleteMax, it tacks on the rst element with a given key to a position later in the array. In the event that elements have equal keys, heapsort will actually reverse the order of the elements, where the rst input will actually be the last output. Quicksort can be either, stable or unstable, depending on where the pivot lays in relation to elements with similar keys. If the pivot happens to be the rst or last instance of elements with that key, quicksort will be stable. If the pivot happens to be somewhere in the middle, quicksort may be unstable.

1.8

Problem 9.1
2 2 4 3 6 2 G D 1 6 3 2 H 6 E 2 2 3 1 3 1 I F 4

Find a topological ordering for the graph in Figure 1.9 A 1 s B C 4 3 t

Figure 1.9: Graph used in Exercise 9.1 ADG/ BE/ C/ t/ AE/ CFI/ Ct/ DEH/ EI/ Ft/ /

s A B C D E F G H I t

Table 1: Corresponding representation of the graph in Figure 1.9.

10

s A B 0 2 1 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

C 3 3 3 3 3 3 2 1 1 0 0 0

D E 2 4 1 4 0 3 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

F G H 2 1 1 2 0 1 2 0 0 2 0 0 2 0 0 2 0 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I 2 2 2 2 1 1 1 0 0 0 0 0

t 3 3 3 3 3 3 3 3 2 1 0 0

Enqueue Dequeue s s G G D, H D A H A B, E B E I I F F C C t t

Table 2. Application of topological sort algorithm to nd a potential topological order of the graph in Figure 1.9. A possible topological order for this graph is: s, G, D, H, A, B, E, I, F, C, t

1.9

Problem 9.5

a. Find the shortest path from A to all other vertices for the graph in Figure 1.10. Applying Dijkstras Algorithm to the graph: Visited A A, C A, C, B A, C, B, A, C, B, A, C, B, A, C, B, A B 0 5 0 5 0 5 0 5 0 5 0 5 0 5 C 3 3 3 3 3 3 3 D E 10 10 10 8 10 7 9 7 9 7 9 7 F G 6 6 8 6 8 6 8 6

G G, E G, E, F G, E, F, D

Turning this table into a graph, Figure 1.10 becomes:

11

1 G 3 1 7

2 D

7 6

2 F

Figure 1.10: Graph used in problem 9.5. b. Find the shortest unweighted path from B to all other vertices for the graph in the Figure 1.10. In this case, you simply add nodes to the path as you encounter them. Starting with B , you can instantly get to C , E , and G. From there, you can take one more step to reach D from C , and F from E . Finally, you can take the last step to connect to A from D, and all the nodes are now included in the path.

1.10

Problem 9.7

a. Give an example where Dijkstras algorithm gives the wrong answer in the presence of a negative edge but no negative-cost cycle. Take for example a tree as illustrated in Figure 1.10. Dijkstras algorithm, starting at A, gives a path from A to C with cost of 5, when path A-B-C has a cost of 7 + (4) = 3, meaning that it is indeed the shortest path to C. b. Show that the weighted shortest-path algorithm suggested in Section 9.3.3 works if there are negative-weight edges, but no negative-cost cycles, and that the running time of this algorithm 12

B 5 3

G 1

2 D F

is O(|E | |V |). This algorithm will go through each vertex for each edge in the graph, giving it an O(|E ||V |) asymptotic complexity. For simplicity, this algorithm will be used on Figure 1.10. This shows that you do get a constantly updated value for the distance to the node you are looking at, and may only update it if you nd a less costly path to it. v A A B v.dist w w.dist 0 C 0 B 7 C 5 cvw 5 7 -4 w.dist update? (value) T (5) T (7) T (3)

1.11

Problem 9.10

a. Explain how to modify Dijkstras algorithm to produce a count of the number of dierent minimum paths from v to w. Use an array to maintain the counter of paths from v to any other node in the graph, and associate each index with a given node. While you check adjacent nodes to nodes that have been taken o the queue (as you expand the visibility cloud), compare w.dist with v.dist + cvw. If the quantities are equal, add 1 to the element in the corresponding array index. If you nd that v.dist + cvw is less that w.dist, reset that element to 0 because you have found a new shortest path.

13

B 1

1 1

3 D

2 F

Figure 1.11: Answer to problem 9.5b b. Explain how to modify Dijkstras algorithm so that if there is more than one minimum path from v to w, a path with the fewest number of edges is chosen. Similar to maintaining a count of dierent minimum paths, another array can be used to keep a count of minimum edges. This will also require a variable that will keep a count of how many edges the path contains, assuming that all edges lead to a node, which will be incremented as a node is dequeued and decremented when w.dist is less than v.dist + cvw (you start going back to nd other, cheaper paths). Each time something is enqueued as a new path, you store this value in the respective array index.

14

C 5 A 7 B Figure 1.12: Answer to problem 9.7 -4

15

Part 2 Appendix
2.1
2.1.1

Problem 6.2
Part a

10

10 12 12 1 10 14 6 12 1 5 6 8 14 15 12 10 5 8 15 6 14 3 10 14 6

1 10 1 5 12 1 5 12 10 8 10

1 12 14 1 6 14 12 10

16

1 3 6 15 14 9 12 10 5 8 15 6 14 12 3 7

1 5 10 9 8

1 3 6 15 14 12 7 9 1 3 6 15 14 12 7 9 10 5 11 4 8 13 15 6 14 12 3 7 9 5 10 4 8 15 6 14 12 3 7 9 1

1 4 5 10 11 8

2 5 10 11 13 4 8

Figure 2.1: Full solution to problem 6.2a. In Figure 2.1, the blue nodes are the nodes that are added at each step.

17

2.1.2

Part b
10 12 1 6 3 9 7 10 3 1 6 14 9 7 5 4 11 13 2 8 15 12 14 9 3 6 7 5 4 11 13 4 5 11 13 8 2 15 3 14 9 12 6 7 1 2 8 10 5 4 11 13 10 1 2 8

14 15

12 15

Figure 2.2: Full solution to problem 6.2b.

In Figure 2.2, the green subtree is the current tree youreheapifying, the blue nodes are the nodes that are smaller than the absolute root in the minimum tree.

18

2.2

Problem 6.3
1 3 6 7 14 12 9 10 5 11 13 2 4 8 2 3 6 7 14 12 9 10 5 11 13 2 4 8 15 6 14 12 3 7 9 10 5 11 4 8 13

15

15

1 is deleted

2, 4, and 8 percolate up to ll up hole 3

3 6 15 14 12 7 9 10 5 11

4 8 13 15 13 14

6 7 12 9 10 5 11

4 8

3 is deleted

3, 6 percolate up, 13 moves to ll hole

4 6 13 15 14 12 7 9 10 5 11 4 8 15 13 14 12 6 7 9 10 11 5 8

3 deleted

4, 5, 10 percolate up, 11 moves

Figure 2.3: Full solution to problem 6.3. 19