welcome to data structures and algorithms data structures
play

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


  1. welcome to data structures and algorithms

  2. data structures and algorithms 2020 08 31 lecture 1

  3. overview practicalities introduction insertion sort

  4. overview practicalities introduction insertion sort

  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

  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

  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

  8. overview practicalities introduction insertion sort

  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

  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

  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

  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

  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 2 n 3 2 n log n n log n n n 8 3 8 24 62 512 256 16 4 16 64 256 4096 65536 32 5 32 160 1024 32768 4294967296 10 3 10 3 10 6 10 9 10 300 10 13000 10 4 10 4 10 5 10 8 10 1 2 10 3000 13 10 5 10 5 10 6 10 10 10 15 10 3000 20

  14. overview practicalities introduction insertion sort

  15. sorting: specification input: a finite sequence of elements output: 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

  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

  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 � A [ i + 1] := A [ i ] 6 � 7 i := i − 1 � 8 A [ i + 1] := key

  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

  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

  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

  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 2 n ( 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

  22. insertion sort: worst-case time complexity so T ( n ) is of the form an 2 + bn + c for some a , b , c ≥ 0 we have an 2 ≤ T ( n ) we can find a positive real number d such that T ( n ) ≤ dn 2 for large n so T ( n ) is sandwiched between two polynomials of the form cn 2 this is the intuition of T is in Θ( n 2 ) eventually the growth of T is similar to the growth of n 2 this is an asymptotic approximation where constant factors become irrelevant

  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

  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 Θ( n 2 )

  25. example: what if T has constant factors? algorithm I has worst-case time complexity I ( n ) = 2 · n 2 algorithm M has worst-case time complexity M ( n ) = 50 · n · log n for n = 2 6 , we have I (2 6 ) = 2 · 2 12 = 2 13 and M (2 6 ) = 300 · 2 6 > 2 8 · 2 6 = 2 14 for n = 2 10 , we have I (2 10 ) = 2 · 2 20 = 2 21 and M (2 10 ) = 500 · 2 10 < 2 9 · 2 10 = 2 19 for large inputs, M is faster than I see also exercise sheet 1

  26. summary main concepts: algorithm, pseudocode, algorithm insertion sort: intuition, application ‘on the fly’, and pseudocode, ‘precise application’ we continue with chapter 2 in lecture 2 exercise class for questions about homework tomorrow

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend