week 1
play

Week 1 Oliver Kullmann Organisation Computationa Getting Started - PowerPoint PPT Presentation

CS 270 Algorithms Week 1 Oliver Kullmann Organisation Computationa Getting Started problems and algorithms Insertion Organisation 1 sort Algorithm Computational problems and algorithms analysis 2 Exactly analysing Insertion sort


  1. CS 270 Algorithms Week 1 Oliver Kullmann Organisation Computationa Getting Started problems and algorithms Insertion Organisation 1 sort Algorithm Computational problems and algorithms analysis 2 Exactly analysing Insertion sort 3 Insertion- Sort Algorithm analysis Discrete 4 mathematics Exercises Exactly analysing Insertion-Sort 5 Discrete mathematics 6 Exercises 7

  2. CS 270 Organisation Algorithms Oliver Kullmann Organisation Computationa problems Lecturer Dr Oliver Kullmann and algorithms Tuesday 9-10 Vivian 404 Insertion sort Tuesday 15-16 Glyndwr E Lectures Algorithm Wednesday 10-11 Vivian 404 analysis Thursday 11-12 Glyndwr E Exactly analysing Labs (choose one of the following two sessions) Insertion- Sort Thursday 12-13 Faraday 217 (Linux Lab) Discrete Thursday 13-14 Faraday 217 (Linux Lab) mathematics Exercises

  3. CS 270 Course home page Algorithms Oliver Kullmann Organisation Computationa problems http://www.cs.swan.ac.uk/~csoliver/Algorithms201213/index.html and algorithms Insertion sort with Algorithm analysis messages Exactly analysing general information Insertion- Sort up-to-date versions of the slides (which serve as the script) Discrete mathematics lab sheets Exercises course works.

  4. CS 270 Assessment Algorithms Oliver Kullmann Organisation Computationa problems and algorithms There will be two pieces of coursework, each worth 10%. Insertion sort Labs are an essential part of the course. Successful Algorithm analysis participations is worth 10%. Exactly The written examination counts 70%, it will take place in analysing Insertion- January. Sort Discrete mathematics Exercises

  5. CS 270 Lectures Algorithms Oliver Kullmann Organisation Computationa problems The script will usually be handed out at the beginning of and algorithms each week. Corrected version of the script are available on Insertion the course home-page. Usually, they will not be printed out sort again, except in cases where substantial changes have Algorithm analysis occurred. Exactly Left-over print-outs of the script can be found in the analysing Insertion- student’s office (Faraday Building Room 206) for those who Sort could not attend the lecture. Discrete mathematics You also have to sign the module attendance form. Exercises

  6. CS 270 References Algorithms Oliver Kullmann There are very many algorithms textbooks in print. In this module, we will follow (and refer to as CLRS ) Organisation Computationa problems Introduction to Algorithms (Third Edition) and algorithms by Cormen, Leiserson, Rivest and Stein, Insertion The MIT Press, 2009. sort Algorithm LIS Call Number: QA76.6. I58 2009 analysis (10 copies on 1 week loan, 2 copies on 1 night loan) Exactly analysing Insertion- Sort http://en.wikipedia.org/wiki/Introduction_to_Algorithms Discrete mathematics Exercises Reading from CLRS for week 1 Chapter 1 Chapter 2, Sections 2.1, 2.2

  7. CS 270 Computational problems Algorithms Oliver Kullmann Computational Problem: A specification in general terms of Organisation inputs and outputs and the desired input/output relationship. Computationa problems Problem Instance: An actual set of inputs for a given problem. and algorithms Insertion Example 1 sort Algorithm Problem: Sorting (numbers). analysis Input: A sequence of n numbers � a 1 , a 2 , . . . , a n � . Exactly analysing Output: A permutation (reordering) � a ′ 1 , a ′ 2 , . . . a ′ n � Insertion- Sort of the input such that a ′ 1 ≤ a ′ 2 ≤ . . . ≤ a ′ n . Discrete mathematics Example instance: Exercises Input � 10 , − 15 , 3 , 3 , 70 , − 17 , − 30 � Output �− 30 , − 17 , − 15 , 3 , 3 , 10 , 70 �

  8. CS 270 Algorithmic solution Algorithms Oliver Kullmann Organisation Algorithm: a well-defined computational procedure that takes Computationa problems some value, or sequence of values, as input and produces and algorithms some values, or sequence of values, as output. Insertion sort Algorithm There will always be many different algorithms for any analysis given problem. Exactly analysing A program is a particular implementation of some Insertion- Sort algorithm. Discrete A program is not the same as an algorithm. mathematics Exercises There are many different implementations of any given algorithm.

  9. CS 270 Expressing algorithms Algorithms Oliver We express algorithms in whatever way is the clearest and most Kullmann concise. Organisation Computationa English is sometimes the best way. problems and algorithms When issues of control need to be made perfectly clear, we often Insertion use pseudocode. sort Algorithm Pseudocode is similar to any typical imperative analysis programming language, such as Java, C, C++, Pascal, . . . Exactly analysing Insertion- Pseudocode expresses algorithms to humans. Sort Software engineering issues of data abstraction, modularity, Discrete mathematics error handling are often ignored. Exercises We sometimes embed English statements into pseudocode. Therefore, unlike for “real” programming language, we cannot create a compiler that translates pseudocode to machine code.

  10. CS 270 Insertion Sort Algorithms Oliver Kullmann Example 2 Organisation Insertion Sort is a good algorithm for sorting a small number of Computationa problems elements. and algorithms It works the way you might sort a hand of playing cards. Insertion sort Insertion-Sort ( A ) Algorithm analysis 1 for j = 2 to A . length Exactly 2 key = A [ j ] analysing Insertion- 3 / Insert A [ j ] into sorted sequence A [1 . . j − 1]. / Sort 4 i = j − 1 Discrete mathematics 5 while i > 0 and A [ i ] > key Exercises 6 A [ i +1] = A [ i ] 7 i = i − 1 8 A [ i +1] = key

  11. CS 270 Algorithm analysis Algorithms Oliver Kullmann Organisation We want to predict the resources that the algorithm requires. Computationa problems Typically, we are interested in and algorithms Insertion runtime sort memory Algorithm analysis number of basic operations, such as: Exactly analysing arithmetic operations (eg, for multiplying matrices) Insertion- Sort bit operations (eg, for multiplying integers) comparisons (eg, for sorting and searching) Discrete mathematics Exercises In order to predict resource requirements, we need a computational model.

  12. CS 270 Random-access machine (RAM) model Algorithms Oliver Kullmann Organisation Instructions are executed one after another. No concurrent Computationa operations. problems and algorithms It’s too tedious to define each of the instructions and their Insertion associated time costs. sort Instead, we recognise that we’ll use instructions commonly Algorithm analysis found in real computers: Exactly Arithmetic: add, subtract, multiply, divide, remainder, floor, analysing Insertion- ceiling. Also, shift left/shift right (good for Sort multiplying/dividing by 2 k ). Discrete Data movement: load, store, copy. mathematics Control: conditional/unconditional branch, subroutine call Exercises and return. Each of these instructions takes a constant amount of time.

  13. CS 270 Random-access machine (RAM) model (cont’d) Algorithms Oliver Kullmann Organisation Computationa problems and The RAM model uses integer and floating-point types algorithms Insertion We don’t worry about precision, although it is crucial in sort certain numerical applications. Algorithm analysis There is a limit on the word size: when working with inputs Exactly analysing of size n , assume that integers are represented by c lg n bits Insertion- Sort for some constant c ≥ 1. (lg n is a very frequently used Discrete shorthand for log 2 n .) mathematics Exercises

  14. CS 270 How do we analyse running time? Algorithms Oliver Kullmann The time taken by an algorithm depends on the input. Organisation Sorting 1000 numbers takes longer than sorting 3 numbers. Computationa problems A given sorting algorithm may even take differing amounts and algorithms of time on two inputs of the same size. Insertion For example, we’ll see that insertion sort takes less time to sort Algorithm sort n elements when they are already sorted than when analysis they are in reverse sorted order. Exactly analysing Insertion- The input size depends on the problem studied. Sort Discrete Usually the number of items in the input. Like the size n of mathematics Exercises the array being sorted. But could be something else. If multiplying two integers, could be the total number of bits in the two integers. Could be described by more than one number.

  15. CS 270 Algorithms Oliver The running time on a particular input is the number of Kullmann primitive operations (steps) executed. Organisation Want to define steps to be machine-independent. Computationa problems and Each line of pseudocode requires a constant amount of algorithms time. Insertion sort One line may take a different amount of time than another, Algorithm analysis but each execution of line i takes the same amount of time Exactly c i . analysing Insertion- Assumption: lines consist only of primitive operations. Sort If the line is a subroutine call, then the actual call takes Discrete mathematics constant time, but the execution of the subroutine being Exercises called might not. If the line specifies operations other than primitive ones,then it might take more than constant time.

Recommend


More recommend