210 likes | 222 Views
Data Structures. Instructor: Dr. Leah Epstein TA: Miri Priesler (This lecture)Text book: pages 1-15, 22-30, 54-64. General. Leah Epstein email: lea@idc.ac.il tel: 381 Reception hour: before/after class Text book: Cormen, Leiserson, Rivest Introduction to Algorithms Final grade:
E N D
Data Structures Instructor: Dr. Leah Epstein TA: Miri Priesler (This lecture)Text book: pages 1-15, 22-30, 54-64
General • Leah Epstein • email: lea@idc.ac.il • tel: 381 • Reception hour: before/after class • Text book: • Cormen, Leiserson, Rivest Introduction to Algorithms • Final grade: • fg- final exam, mg- midterm exam, hw-homework
Why “Intro” is not enough • Because: • not every problem has a computational solution • or at least no practically efficient solution • devising efficient algorithm is difficult • evaluating the efficiency of a computational solution • well known techniques that may improve efficiency • proving the correctness of the solution
Why Study D.S ? • Because the efficiency of a computing solution often depends on the way the data is organized • for example searching in a pile of cards • unsorted • sorted ID 3459696 name- Hober Huchem Address: etc
Unsorted For i=1 to n do if pile[i]=k then return true return false • Takes approximately 3n steps in the worst case
Sorted (Binary Search) l=1, u=n while l<=u do m=(l+n)/2 if pile[m]=k then return true if pile[m]<k then l=m+1 else u=m-1 return false Takes approximately 6log2n steps
Sorting • Specification • input- a sequence of numbers • output- a permutation of the sequence such that • Sorting by insertion • start with an empty pile and insert each number in its correct position
Sorting • Insertion-Sort(A) for j=2 to length(A) do key = A[j] i = j-1 while i > 0 and A[i] > key do A[i+1]=A[i] i=i-1 A[i+1]=key
Analysis • Algorithm analysis- trying to predict the resources the algorithm requires as a function of the input size • Resources- • time, space (memory), etc.. • Input size • number of items in the input, number of bits in a number.
Running time for j=2 to length(A) do key = A[j] i = j-1 while i > 0 and A[i] > key do A[i+1]=A[i] i=i-1 A[i+1]=key
Running Time • Best case (when?) • Worst case (when?) • Average case?
Order of Growth • We are interested only in the order of growth of the running time (space) • best case • worst case • Most of the time, we are interested only in worst case analysis • and sometimes in average case analysis
Order of Growth • Definition- asymptotic tight bound • Convention • usually stands for Example
Order of Growth • Definition - asymptotic upper bound • Definition- asymptotic lower bound • Claim-
Examples also
Merge Sort • The idea: divide the array into 2 equal parts, sort each part separately and merge the two sorted parts into one array mergeSort(A, p, r) if (p<r) then q = (p+r)/2 mergeSort(A,p,q) mergeSort(A,q+1,r) merge(A, p, q, r)
Time analysis • The time required for mergeSort: • How to solve recurrence equations? • Iterate and see...
Master Theorem • Provides a cookbook for solving recurrences of the form: where and are constants • if for some constant then • if then • if for some constant and if for some constant and for sufficiently large then