Solution of Assignment 4 CSC2100B 4.1.1 Open Hash Table 0 1 2 3 4 5 6 7 8 9 4371 1323 6173 4344 4199 9679 1989 Advantage: 1. Space can be saved if the records are large compared with the pointers 2. Simple and efficient collision handling 3. The size of the hash table can be smaller than the number of records 4. Deletion is easy and quick Disadvantage: 1. All links require space 2. Space is wasted if the records are small 4.1.2 Linear Probing 0 1 9679 4371 2 3 4 1989 1323 6173 5 6 7 8 4344 9 4199 Advantage: 1. Simple implementation 2. Insertion and search are efficient when the table is less than half full 3. All space can be used up before insertion failure happens (though clustering may occur) Disadvantage: 1.When the table is more than half full, insertion and search may be slow as expected number of probes is large 4.1.3 Quadratic Probing 0 1 2 3 4 5 6 7 8 9 9679 4371 1323 6173 4344 1989 4199 Advantage: 1. Clustering is reduced (compared with linear probing) Disadvantage: 1.Insertion may fail if the table is more than half full 2.Deletion is more complicated (needs lazy deletion) 4.1.4 Double Hashing 0 1 4371 2 3 4 1323 6173 5 6 7 8 9679 9 4199 1989 mod 10 = 9 but address 9 is already occupied. On the other hand, 7 - (1989 mod 7) = 6 but (9 + i*6) mod 10 is odd for all integer i and all odd addresses have already been used. So insertion will fail Advantage of double hashing: 1. Good for avoiding clustering Disadvantage: 1. Maybe slower than the other methods 4344 2. Need to choose the second hash function carefully so all cells can be probed. 4.2 Rehashing 0 1 10 11 1989 21 2 12 1323 22 3 13 4199 4 14 5 15 6 16 7 17 8 18 9 4371 20 6173 19 9679 4344 The size of the new hash table should be chosen as the smallest prime that is larger than twice that of the original table. The smallest prime >2*10 is 23 so the new table has size 23 and the new hash function is x mod 23 5.1.1 Insertion to Min-Heap 1 6 14 1 5 12 10 3 8 15 6 15 5 12 14 6 15 14 12 5 9 10 3 8 6 15 14 12 8 A new element is inserted to the rightmost position of the bottommost level. If some node is larger than its parent, swap them. 1 Repeat until the heap property is restored. 1 3 10 5 7 10 9 8 5.1.1 Insertion to Min-Heap 1 1 3 6 4 7 15 14 12 5 9 3 8 10 6 4 7 15 14 12 5 9 1 15 14 12 4 7 5 9 10 11 1 3 6 8 3 8 10 11 13 6 15 14 12 2 7 5 9 4 10 11 13 8 5.1.2 Build Heap 10 Initially the keys are inserted from left to right, top to 12 bottom 14 15 3 10 1 6 9 5 7 4 12 8 11 13 3 2 15 14 1 6 9 4 7 10 15 14 1 6 9 4 7 2 11 13 8 1 3 12 5 After the first 4 Percolate_Down operations, these become 4 heaps 5 3 2 11 13 After 2 more Percolate_Down, these become 2 heaps 12 8 15 14 2 6 9 4 7 5 8 11 13 10 The heap is built after the last Percolate_Down 5.1.1 Deletion in Min-Heap 1 2 3 6 2 7 15 14 12 5 9 3 4 10 11 13 6 8 4 7 15 14 12 5 9 3 15 14 12 4 7 5 9 10 11 13 4 6 13 8 10 11 6 8 13 15 14 12 5 7 10 9 11 8 6.1 Inversions 6.1.1 An O(n log n) time algorithm (modified from merge sort) for finding the number of inversions can be found here (you don’t need to know it) 6.1.2 #inversion is maximized when the sequence is decreasing. So Max#inversion = (n-1)n/2 6.1.3 No, it depends on the algorithm you use. If you use selection sort, the times taken for sorting A and B are the same. 6.2.1 Bubble Sort Original 3 1 4 1 5 9 2 6 5 P=1 1 3 1 4 5 2 6 5 9 P=2 1 1 3 4 2 5 5 6 9 P=3 1 1 3 2 4 5 5 6 9 P=4 1 1 2 3 4 5 5 6 9 6.2.1 Insertion Sort Original P=1 P=2 P=3 P=4 P=5 P=6 P=7 P=8 3 1 1 1 1 1 1 1 1 1 3 3 1 1 1 1 1 1 4 4 4 3 3 3 2 2 2 1 1 1 4 4 4 3 3 3 5 5 5 5 5 5 4 4 4 9 9 9 9 9 9 5 5 5 2 2 2 2 2 2 9 6 5 6 6 6 6 6 6 6 9 6 5 5 5 5 5 5 5 5 9 6.6.3 Quick Sort 1 2 3 4 5 6 7 8 9 10 11 m n 3 3 1 1 1 1 1 1 1 1 1 1 1 1 4 3 3 3 3 3 3 1 1 3 3 3 3 3 5 3 3 3 3 3 3 9 9 9 9 5 5 4 3 5 5 5 5 5 5 6 6 6 6 6 4 5 5 5 5 5 5 5 5 3 4 4 4 4 6 6 5 5 5 5 9 9 9 1 11 1 4 1 3 6 11 6 10 6 8 10 10 6.6.3 Quick Sort If different implementation is used, the intermediate results may be different. As long as the pivot is chosen according o the requirement and all elements < pivot are on its left and all elements > pivot are on its right (equal elements can be on either side), the answer will be accepted. 7.1 Topological Sort A s B D C E A F t B D C E G H I G H I A B C A B C D E H F I t E H Colored vertices have zero indegrees and are inserted into a queue. At the beginning of each iteration, one vertex is dequeued. This vertex and all its incident edges are removed from the graph. I F t F t 7.1 Topological Sort A B C E B F t C E F I C I t t E I C F C F I C F t t t t 7.3.1 Single Source Shortest Path Initial State A known C known B known v Known dv pv Known dv pv Known dv pv Known dv pv A 0 0 0 1 0 0 1 0 0 1 0 0 B 0 ∞ 0 0 5 A 0 5 A 1 5 A C 0 ∞ 0 0 3 A 1 3 A 1 3 A D 0 ∞ 0 0 ∞ 0 0 10 C 0 10 C E 0 ∞ 0 0 ∞ 0 0 10 C 0 8 B F 0 ∞ 0 0 ∞ 0 0 ∞ 0 0 ∞ 0 G 0 ∞ 0 0 ∞ 0 0 ∞ 0 0 6 B 7.3.1 Single Source Shortest Path (Dijkstra’s Algorithm G known E known F known D known v Known dv pv Known dv pv Known dv pv Known dv pv A 1 0 0 1 0 0 1 0 0 1 0 0 B 1 5 A 1 5 A 1 5 A 1 5 A C 1 3 A 1 3 A 1 3 A 1 3 A D 0 10 C 0 ∞ E 0 10 E 1 10 E E 0 7 G 1 ∞ G 1 10 G 1 8 G F 0 ∞ 0 0 ∞ E 1 ∞ E 1 ∞ E G 1 6 B 1 ∞ B 1 ∞ B 1 6 B 7.3.1 Single Source Shortest Path ∞ 1 B 2 0 3 2 7 2 E 6 1 B G 2 0 3 2 7 10 D ∞ ∞ 7 2 6 2 E 1 F ∞ ∞ 1 ∞ 3 C 7 2 E 1 6 F ∞ 1 B G 6 1 3 5 2 0 A G 7 D 5 10 3 C 3 A 1 B 2 0 F ∞ 1 3 5 1 3 5 A ∞ C ∞ D 5 ∞ 7 5 1 3 5 A G ∞ 3 2 7 10 D 8 3 C 7 2 6 E 1 F ∞ 7.3.1 Single Source Shortest Path 1 5 B 2 0 3 A 2 7 10 D 7 2 B G 2 0 3 2 9 D 1 F 8 7 2 E 1 6 F 8 B G 6 1 3 5 2 0 A 1 1 5 E 6 7 3 C 7 9 D 1 7 2 6 2 7 3 C 7 3 6 3 5 2 A F ∞ 1 5 E G 3 0 1 6 B 5 7 3 C 1 5 1 3 5 A G 6 3 2 7 9 D 7 3 C 7 2 6 E 1 F 8 7.3.2 Unweighted Shortest Path Initial State v Known A 0 B 0 C dv B dequeued pv Known 0 0 0 1 0 0 0 D 0 0 0 E 0 0 0 F 0 0 0 G 0 0 0 Q B 0 C,E,G dv C dequeued pv Known 0 0 0 0 1 1 B 1 1 pv Known 0 0 0 0 1 0 0 1 1 B 1 1 B 0 0 2 C 0 2 C B 0 1 B 1 1 B 0 0 0 0 2 E B 0 B 0 1 B E,G,D dv E dequeued 1 G,D,F dv pv 0 7.3.2 Unweighted Shortest Path G dequeued v Known A 0 B 1 C dv D dequeued F dequeued A dequeued pv Known dv pv Known dv pv Known dv pv 0 0 3 D 0 3 D 1 3 D 0 0 1 0 0 1 0 0 1 0 0 1 1 B 1 1 B 1 1 B 1 1 B D 0 2 C 1 2 C 1 2 C 1 2 C E 1 1 B 1 1 B 1 1 B 1 1 B F 0 2 E 0 2 E 1 2 E 1 2 E G 1 1 B 1 1 B 1 1 B 1 1 B Q D,F F,A A 7.3.2 Unweighted Shortest Path Initial state ∞ ∞ A ∞ C After C is dequeued E D F ∞ 0 B G 1 ∞ ∞ 1 A D ∞ G 1 1 A 1 C After E is dequeued E F ∞ 0 B E D F ∞ 0 B G 1 ∞ 1 C 2 After B is dequeued G ∞ 0 B ∞ 1 A 1 C 2 D E F 2 7.3.2 Unweighted Shortest Path After G is dequeued 0 B ∞ 1 A 1 C After F is dequeued E D F 2 0 B G 1 2 3 1 A D 3 G 1 1 A 1 C After A is dequeued E F 2 0 B E D F 2 0 B G 2 2 1 C 2 After D is dequeued G 1 3 1 A 1 C 2 D E F 2 7.5 Prim’s 3 A 4 4 5 D 6 2 11 2 1 4 4 4 5 6 H 10 3 11 1 4 2 7 2 2 4 5 D 8 6 H C A 2 11 G 8 J 2 11 1 4 5 6 H 4 2 11 11 8 C 6 1 2 F 3 I G J 10 3 1 1 7 2 2 6 3 B E C F I 3 4 D 3 E 4 10 B 2 J 1 F 7 4 G 6 3 I 1 11 3 A 6 3 B E C F I 3 A D 3 E H 10 B 11 7 G 8 J 7.5 Prim’s 3 A 4 4 5 D 6 2 11 2 1 4 4 4 5 6 H 10 3 11 1 4 2 7 2 2 4 5 D 8 6 H C A 2 11 G 8 J 2 11 1 4 5 6 H 4 2 11 11 8 C 6 1 2 F 3 I G J 10 3 1 1 7 2 2 6 3 B E C F I 3 4 D 3 E 4 10 B 2 J 1 F 7 4 G 6 3 I 1 11 3 A 6 3 B E C F I 3 A D 3 E H 10 B 11 7 G 8 J 7.5 Kruskal’s 3 A 4 4 5 D 6 2 11 2 1 4 4 4 5 6 H 10 3 11 1 4 2 7 2 2 4 5 D 8 6 H C A 2 11 G 8 J 2 11 1 4 5 6 H 4 2 11 11 8 C 6 1 2 F 3 I G J 10 3 1 1 7 2 2 6 3 B E C F I 3 4 D 3 E 4 10 B 2 J 1 F 7 4 G 6 3 I 1 11 3 A 6 3 B E C F I 3 A D 3 E H 10 B 11 7 G 8 J 7.5 Kruskal’s 3 A 4 4 5 D 6 2 11 2 1 4 4 4 5 6 H 10 3 11 1 4 2 7 2 2 4 5 D 8 6 H C A 2 11 G 8 J 2 11 1 4 5 6 H 4 2 11 11 8 C 6 1 2 F 3 I G J 10 3 1 1 7 2 2 6 3 B E C F I 3 4 D 3 E 4 10 B 2 J 1 F 7 4 G 6 3 I 1 11 3 A 6 3 B E C F I 3 A D 3 E H 10 B 11 7 G 8 J