Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Self-Organizing Linked List. 2 Motivation Searching for an element in a linked list takes O(N) worst case time –If we are lucky, and the element is.

Similar presentations


Presentation on theme: "1 Self-Organizing Linked List. 2 Motivation Searching for an element in a linked list takes O(N) worst case time –If we are lucky, and the element is."— Presentation transcript:

1 1 Self-Organizing Linked List

2 2 Motivation Searching for an element in a linked list takes O(N) worst case time –If we are lucky, and the element is at the beginning, then search is fast Our goal is to try to keep elements that are likely to be searched in the near future close to the head –Methods vary in how they determine the nodes that need to be close to the head –The worst case search time in still O(N), but the average time can be much better –All linked list features, except search remain unchanged

3 3 Common Methods for Self-Organization Move to front –After an element is found in a search, move it to the front Transpose –After an element is found in a search, move it one step closer to the front Count –Order the nodes by the number of times an element is searched Ordering –Order by some other criterion, such as lexicographic order

4 4 Example Element searched or inserted PlainMove to front TransposeCountLexical ordering AAAAAA CAC BACB ABC CACBCAB C-1ABABC DACBDCABD C-1ABDABCD AACBD C-1A-1BDABCD DACBDDACBACDBC-1A-1D-1BABCD AACBD???? C ???? A ???? Next, search for the following: C, C, E, E

5 5 Average Case Analysis Usually, the time taken is neither the worst case time nor the best case time –Average case analysis assumes a probability distribution for the set of possible inputs and gives the expected time t av =  input i probability(i)  time(i) –Average search time in a linked list t av = probability(key is not present)  n +  i=1 n probability(key is in node i)  i t av = n  [1 -  i=1 n probability(key is in node i)] +  i=1 n probability(key is in node i)  i

6 6 Average Case Analysis Example - 1 Probability(i) = 1/n, 1 < i < n t av = n  [1 -  i=1 n probability(key is in node i)] +  i=1 n probability(key is in node i)  i = n  [1 -  i=1 n 1/n] +  i=1 n 1/n  i = n  [0] + (1/n)  i=1 n i = (1/n) n(n+1)/2 = (n+1)/2 = O(n)

7 7 Average Case Analysis Example - 2 Probability(i) = 1/(2n), 1 < i < n t av = n  [1 -  i=1 n probability(key is in node i)] +  i=1 n probability(key is in node i)  i = n  [1 -  i=1 n 1/(2n)] +  i=1 n 1/(2n)  i = n  [1/2] + (1/(2n))  i=1 n i = n/2 + (1/2n) n(n+1)/2 = n/2 + (n+1)/4 = O(n)

8 8 Average Case Analysis Example - 3 Prob(1) = 0.5, prob(2) = 0.3, prob(3) = 0.2 t av = n  [1 -  i=1 n probability(key is in node i)] +  i=1 n probability(key is in node i)  i = n  [1 -  ] + 0.5  1 + 0.3  2 + 0.2  3 +  i=4 n 0  i = n  [0] + 0.5 + 0.6 + 0.6 + 0 = 1.7 = O(1)


Download ppt "1 Self-Organizing Linked List. 2 Motivation Searching for an element in a linked list takes O(N) worst case time –If we are lucky, and the element is."

Similar presentations


Ads by Google