Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.

Similar presentations


Presentation on theme: "ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049."— Presentation transcript:

1 ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec 2620a.htm Office: TEL 3049

2 Complexity Estimation

3 3 Key Points Estimation of non-recursive algorithms –Best, Worst, and Average cases –How many times? –What does it cost?

4 4 Best, Worst, and Average Cases Why do people buy lottery tickets? –Win a million dollars Case?  Best Why should you invest in the stock market? –Best long term investment (compared to bonds and cash) Case?  Average Lottery –Average case  waste your money Stock market –Worst case  crash – lose everything

5 5 Which case should we use to make decisions Average case  most often Best case  when reasonable expectation exists Worst case  when guarantees are required

6 6 Best, Worst, and Average cases in Algorithm Analysis Is there a conditional execution? –while loops –branching with different cost alternatives Best Case –minimal execution Worst Case –maximal execution Average Case –median (50-50) case –not necessarily (Best + Worst)/2 !

7 7 Using Complexity The most important thing about algorithm analysis is to determine big- Oh – the approximate complexity –why? –need to be able to estimate program running times Do we need all the math? –not really

8 8 Complexity Estimation and Complexity Analysis Complexity Estimation –Two key questions How many times? What does it cost? Relating Complexity Estimation to Complexity Analysis –“How many times?” is the summation –“What does it cost?” is what’s inside the summation More accurate questions –What does it cost each time? –What does it cost on average? Example

9 Linked List

10 10 Key Points Maintaining data records Inserting into sorted arrays Dynamic data structures Linked-lists

11 11 Inserting into Sorted Arrays How is this done? –loop – larger values get moved into the next slot –when done, copy new value into next slot What is the complexity for inserting into a sorted array? –Is there a best, worst, and average case? do you always insert into the same location? –How many times, what does it cost? Best  0 moves  O(1) Worst  n moves, 1 swap per  O(n) Avg  n/2 moves, 1 swap per  O(n)

12 12 Inserting into Full Arrays What happens if we want to insert another element into this array? –Copy entire array into a larger array (time consuming) How do we avoid this? –Store data in an array with extra space Time-Space trade-off –What saves time can often waste space –What saves space can often waste time

13 13 Dynamic Data Structures Would like to create extra memory slots one at a time… Creating Links Inserting Links

14 14 Arrays vs. Linked-Lists Arrays –fixed length –contiguous memory locations –direct access Linked-Lists –dynamic length –arbitrary memory locations –access by following links

15 15 Comparing Arrays and Linked- Lists Find and delete a value in a sorted array and a linked-list. –Arrays find value with binary search (assume value is in array) shuffle all higher values down what is the complexity for find? –Best  O(1) –Worst  O( log n) –Avg  O( log n)

16 16 Comparing Arrays and Linked- Lists (Cont’d) Array –What is the complexity for delete? Best  last element  O(1) Worst  first element  O(n) Avg  middle element  O(n) –What is the overall complexity? Best  last element  O( log n)+O(1) = O( log n) Worst  first element  O( log n)+O(n) = O(n) Avg  mid-area element  O(logn)+O(n) = O(n)

17 17 Comparing Arrays and Linked- Lists (Cont’d) Linked-Lists –find value by following links –link past target link (garbage collecting will remove it) –what is the complexity for find? Best  first element  O(1) Worst  last element  O(n) Avg  middle element  O(n)

18 18 Comparing Arrays and Linked- Lists (Cont’d) What is the complexity for delete? –Always  O(1) What is the overall complexity? –Best  first element  O(1)+O(1) = O(1) –Worst  last element  O(n)+O(1) = O(n) –Avg  middle element  O(n)+O(1) = O(n)


Download ppt "ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049."

Similar presentations


Ads by Google