• Keine Ergebnisse gefunden

Sample Solution

N/A
N/A
Protected

Academic year: 2022

Aktie "Sample Solution"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

University of Freiburg Dept. of Computer Science Prof. Dr. F. Kuhn

Algorithms and Data Structures Summer Term 2021

Sample Solution Exercise Sheet 4

Exercise 1: Hashing - Collision Resolution with Open Addressing

(a) Leth(s, j) :=h1(s)−2j modm and let h1(x) =x+ 2 mod m. Insert the keys 51, 13, 21, 30, 23, 72 into the hash table of sizem= 7 using linear probing for collision resolution (the table should show the final state).

0 1 2 3 4 5 6

(b) Leth(s, j) :=h1(s) +j·h2(s) mod mand leth1(x) =x mod mandh2(x) = 1 + x mod (m−1) . Insert the keys 28, 59, 47, 13, 39, 69, 12 into the hash table of size m = 11 using the double hashing probing technique for collision resolution. The hash table below should show the final state.

0 1 2 3 4 5 6 7 8 9 10

Sample Solution

(a)

30 13 21 72 51 23

0 1 2 3 4 5 6

(b)

69 13 47 59 39 28 12

0 1 2 3 4 5 6 7 8 9 10

Exercise 2: Application of Hashtables

Consider the following algorithm:

Algorithm 1 algorithm . Input: ArrayA of lengthn with integer entries

1: fori= 1 to n−1 do

2: for j= 0 to i−1do

3: fork= 0 to n−1do

4: if |A[i]−A[j]|=A[k]then

5: return true

6: return false

(2)

(a) Describe what algorithm computes and analyse its asymptotical runtime.

(b) Describe a different algorithm B for this problem (i.e., B(A) =algorithm(A) for each inputA) which uses hashing and takes time O(n2).

You may assume that inserting and finding keys in a hash table needs O(1) if α=O(1) (α is the load of the table).

(c) Describe another algorithm for this problem without using hashing which takes timeO(n2logn).

Sample Solution

(a) The algorithm checks if there are two entries in the array whose distance (absolute value of the difference) equals some entry in the array. If so, it returns “true”, otherwise “false”. In case it returns “false”, the algorithm runs completely through all three loops. It considers

n−1

X

i=1

i= n(n−1)

2 =O(n2)

many pairs (i, j) and for each of this pair it checks n times the if-condition in line 4. Therefore, the runtime isO(n3).

(b) We compute an arrayB of sizeO(n2) which contains an entry|A[i]−A[j]|for each pair (i, j) with 0≤j < i < n. This takes time O(n2). Afterwards we allocate a hash table of sizeO(n2), choose a suitable hash function h and hash the values from B into the table H (this takes O(n2) under ther assumption that one insert operation takesO(1)). Finally, we test for each entry inAif it is contained inH, taking ntimes O(1). Hence the overall runtime is O(n2).

(c) We sort A, takingO(nlogn). Afterwards we compute arrayB as in part (b), takingO(n2). Now we test for each entry in B if it is in A using binary search. This takes n2 times O(logn). The overall runtime is dominated by the last step and equalsO(n2logn).

Referenzen

ÄHNLICHE DOKUMENTE

If no damage is apparent, open the shipping container and remove documentation, mounting hardware, and cables.. Check materials received in option

IN GLOBAL RE' ER£NCE ILLEGAL NOT ON GLOBAL ?ILE DISK ROUTINE NOT ON DISK DISK I/O ERROR ROUTINE IS UNNAMED ROUTL1E ALREAD'Í j;q LIBRARY REMOVE OR SAVE ROUTINÍ COMMAND ONLY USED

Now that you are sure that the system comes up correctly under AMOS/L 1.3, you can make your LSYS.MON file the default so that the system will use this file to boot when you

COmmunications Management User's Guide binder for more specific information about remote access connections and setups using the optional AT&amp;T UNIX PC

MegaPAC's cooling system employs a temperature sensing and alarm module that contro ls the speed of its three high capacity fans to maintain proper internal

Of course most applications are sharable so the savings would not be as great if for example you had one user going directly into Fortune:Word for example

This is the stack overflo~ condition (STKOVF). In either case the information normally saved on the stack must be saved in a different location. The VISION

The parameters you can change in set up include communication and printing features, and also such functions as terminal control mode, display characteristics,