SLIDE 1
welcome to data structures and algorithms data structures and - - PowerPoint PPT Presentation
welcome to data structures and algorithms data structures and - - PowerPoint PPT Presentation
welcome to data structures and algorithms data structures and algorithms 2020 08 31 lecture 1 overview practicalities introduction insertion sort overview practicalities introduction insertion sort some practicalities the course is
SLIDE 2
SLIDE 3
- verview
practicalities introduction insertion sort
SLIDE 4
- verview
practicalities introduction insertion sort
SLIDE 5
some practicalities
the course is completely online for the exercise question hours we use two groups the midterm will be (online and) multiple choice the final exam will be online and the schedule might change keep an eye on the canvas pages for updates
SLIDE 6
material
excellent book! Introduction to Algorithms by Cormen, Leiserson, Rivest, Stein slides via canvas exercise sheets via canvas (most exercises from the book) some solutions via canvas
SLIDE 7
contact
email f.van.raamsdonk at vu.nl refer to the course in the subject (for example with ‘DSA’) for administrative matters: Robin van der Wiel, email r.r.vander.wiel at vu.nl
SLIDE 8
- verview
practicalities introduction insertion sort
SLIDE 9
data structures and algorithms: context
some problems cannot be solved some problems cannot be solved efficiently some problems can be solved efficiently for some problems we do not know whether they can be (efficiently) solved if P = NP then the NP-complete problems cannot be efficiently solved
SLIDE 10
what this course is about
we will study basic data structures and analysis and design of algorithms prerequisite: elementary programming but this is not a programming course prerequisite: elementary (discrete) mathematics and graph theory but this is not a pure theory course
SLIDE 11
important aspects of algorithms
algorithm: an algorithm is a list of instructions, the essence of a program correctness: does the algorithm meet the requirements for every input? termination: does the algorithm eventually produce an output? time complexity how much time does the algorithm use? time as function of the input size (T(n)) space complexity: how much space does the algorithm use? space as function of the input
SLIDE 12
what do we study?
we we focus on time (not so much on space) complexity we write algorithms in pseudocode we describe time complexity as a function T of the input size n we want to know what happens if n becomes very large we often consider the worst-case scenario
SLIDE 13
the function T ‘counts’ elementary steps
examples of elementary steps: comparison of two integers, addition, array assignment, ... let n be the size of the input, and let a function T of n give the number of steps compare the growth of different functions: n log n n n log n n2 n3 2n 8 3 8 24 62 512 256 16 4 16 64 256 4096 65536 32 5 32 160 1024 32768 4294967296 103 10 103 13000 106 109 10300 104 13 104 105 108 1012 103000 105 20 105 106 1010 1015 103000
SLIDE 14
- verview
practicalities introduction insertion sort
SLIDE 15
sorting: specification
input: a finite sequence of elements
- utput: an ordered or sorted permutation of the input-sequence
elements are usually integers or natural numbers sorted means usually in increasing order with respect to ≤ an element may occur more than once the input-sequence is assumed to be an array convention (of the book): an array starts at index 1
SLIDE 16
insertion sort: idea
you may think of sorting a hand of cards the sequence consists of a sorted part followed by a non-sorted part initially: the sorted part consists only of the first element loop: while the non-sorted part is non-empty insert (how?) the first element of the non-sorted part in the correct position of the sorted part
SLIDE 17
insertion sort: pseudocode
1
- Algorithm insertionSort(A, n):
2
- for j := 2 to n do
3
- key := A[j]
4
- i := j − 1
5
- while i > 0 and A[i] > key do
6
- A[i + 1] := A[i]
7
- i := i − 1
8
- A[i + 1] := key
SLIDE 18
general: pseudocode uses
arrays assignment := elementary arithmetic: comparisons such as >, ≥, <, ≤, + and − for-loops and while-loops indentation gives the block-structure attributes such as A.length for the length of array A
SLIDE 19
insertion sort: best and worse performance
best case performance: we execute the while-loop the least possible this happens if the comparison-test always fails worst case performance: we excute the while-loop as many times as possible this happens if the comparison-test always succeeds
SLIDE 20
time complexity is given as a function
T(n) where n is the size of the input for insertion sort: n is the length of the input-array how to find a description for T? analyze the (pseudocode of the) algorithm line by line elementary steps take constant time
SLIDE 21
insertion sort: worst-case time complexity
test for-loop: n assignment key: n − 1 assignment i: n − 1 worst case: A[i] > key always succeeds for fixed j: we do j times the while-tests and n
j=2 j = 1 2n(n + 1) − 1
for fixed j: we do j − 1 times the assignment A[i + 1] and n
j=2(j − 1) = 1 2(n − 1)n
for fixed j: we do j − 1 times the assignment i and n
j=2(j − 1) = 1 2(n − 1)n
assignment A[i + 1]: n − 1 times
SLIDE 22
insertion sort: worst-case time complexity
so T(n) is of the form an2 + bn + c for some a, b, c ≥ 0 we have an2 ≤ T(n) we can find a positive real number d such that T(n) ≤ dn2 for large n so T(n) is sandwiched between two polynomials of the form cn2 this is the intuition of T is in Θ(n2) eventually the growth of T is similar to the growth of n2 this is an asymptotic approximation where constant factors become irrelevant
SLIDE 23
insertion sort: best case time complexity
recall: the best case occurs if A[i] > key always fails this is the case if the input is sorted the best-case time complexity is a linear function of the input size n
SLIDE 24
insertion sort: worst-case time complexity
running time depends on the input: best case if input is sorted, worst case if input is inverse sorted running time depends on the size of the input: we want to express running time as function T of size of the input which running time? best case is useless; average case is interesting; worst case gives guarantee worst-case time complexity is quadratic in the size of the input T(n) in Θ(n2)
SLIDE 25
example: what if T has constant factors?
algorithm I has worst-case time complexity I(n) = 2 · n2 algorithm M has worst-case time complexity M(n) = 50 · n · log n for n = 26, we have I(26) = 2 · 212 = 213 and M(26) = 300 · 26 > 28 · 26 = 214 for n = 210, we have I(210) = 2 · 220 = 221 and M(210) = 500 · 210 < 29 · 210 = 219 for large inputs, M is faster than I see also exercise sheet 1
SLIDE 26