Programming Fundamentals 5 th lecture Szabolcs Papp.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

College of Information Technology & Design
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
CS 206 Introduction to Computer Science II 02 / 27 / 2009 Instructor: Michael Eckmann.
Space-for-Time Tradeoffs
The Assembly Language Level
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the five essential properties of an algorithm.
The Growth of Functions
Hardness Results for Problems P: Class of “easy to solve” problems Absolute hardness results Relative hardness results –Reduction technique.
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Lecture 2: Fundamental Concepts
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
Discrete Structures Chapter 2 Part B Mathematical Induction
Computer Science 1620 Programming & Problem Solving.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
- ABHRA DASGUPTA Solving Adhoc and Math related problems.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
Sequences Jordi Cortadella Department of Computer Science.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Lecture 6 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Programming Fundamentals 9 th lecture Szabolcs Papp.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Programming Fundamentals 6 th lecture Szabolcs Papp.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Lecture 11 Data Structures, Algorithms & Complexity Introduction Dr Kevin Casey BSc, MSc, PhD GRIFFITH COLLEGE DUBLIN.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
Algorithms 1.Notion of an algorithm 2.Properties of an algorithm 3.The GCD algorithm 4.Correctness of the GCD algorithm 5.Termination of the GCD algorithm.
Tirgul 11 Notes Hash tables –reminder –examples –some new material.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
ALGORITHMS.
Programming Fundamentals 3 rd lecture Szabolcs Papp.
ECE 8443 – Pattern Recognition ECE 8527 – Introduction to Machine Learning and Pattern Recognition Objectives: Elements of a Discrete Model Evaluation.
Programming Fundamentals 8 th lecture Szabolcs Papp.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 4 With Question/Answer Animations 1. Chapter Summary Divisibility and Modular Arithmetic - Sec 4.1 – Lecture 16 Integer Representations and Algorithms.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 The Role of Algorithms in Computing. 2 Computational problems A computational problem specifies an input-output relationship  What does the.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
COUNTING Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Advanced Data Structures Lecture 1
Introduction to Algorithms
IPO Lap Problem 1 Create a flow chart and IPO for the following application: A user is allowed to enter 5 numbers. The application should provide summary.
Problem Solving & Computer Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Algorithmic complexity: Speed of algorithms
GC211Data Structure Lecture2 Sara Alhajjam.
Hashing Alexandra Stefan.
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Algorithm Analysis and Design
Algorithmic complexity: Speed of algorithms
Algorithms.
Introduction to Algorithms
Presentation transcript:

Programming Fundamentals 5 th lecture Szabolcs Papp

ELTE 2/  Patterns of Algorithms – the essence Patterns of Algorithms  Calculation from a sequence (e.g. sum) Calculation from a sequence (e.g. sum)  Decision Decision  Pick Pick  Search Search  Count Count  Maximum pick Maximum pick  Patterns of Algorithms – summary Patterns of Algorithms Content

ELTE 3/ Patterns of Algorithms (PoA) – the essence Its aim: A proven template as a basis, on which we can build our solutions later. (Development will be quicker and more safe) Its structure: 1. abstract task specification [+program specification] 2. abstract algorithm

ELTE 4/ Patterns of Algorithms (PoA) – the essence How we use them: 1. create specification for the given task 2. suspect PoA from specification 3. map the parameters of the given task to the parameters of the corresponding abstract task 4. "generate" the task specific algorithm based on the algorithm of the PoA, by changing the parameters as per point create a more efficient algorithm using program transformations

ELTE 5/ Patterns of Algorithms (PoA) – the essence Task characteristics:  determine the right PoA – if the output of the specification is:  a boolean value  decisiondecision  a sequence with the same size as the input has  copy… ... if the post condition contains:  a sequence operation (Σ, Π …)  calculation from a sequence calculation from a sequence  …

ELTE 6/ Patterns of Algorithms (PoA) – the essence Task characteristics:  Combining PoAs:  output is a boolean value  the decision PoA can realize the boolean property of another PoAdecision  the output is a sequence  can be combined as a preprocessor with any other PoA  …

ELTE 7/ Patterns of Algorithms What is PoA? It is the general solution of a typical programming task.  sequence  single value  sequence  sequence  sequence  sequences  sequences  sequence

ELTE 8/ Calculation from a sequence Tasks (examples):  We know the monthly income and expenses of a person. Let's calculate by how much money his asset will change by the end of the year!  W know the lap times of a car racer. Let's determine his average lap time!  Let's calculate the factorial of N!

ELTE 9/ Calculation from a sequence What is in common? We have N "something", and we have to calculate a single "something" out of them! Eg.  – income/lap time;  – factiroal;  ; &

ELTE 10/ Calculation from a sequence Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: S:Something  Precondition: N  0  Post condition: S=F(X[1..N]) F:  – sum of N elements;  – product of N elements;;  – union of an N element set; & – concatenation of N texts …

ELTE 11/ Calculation from a sequence  Task: F: operation with N parameters, where N can change dynamically  Solution: Decomposition to 2-parameter operations (e.g.  to +) and to a null-value (in case of + is 0). F(X[1..N])= f(F(X[1..N–1]),X[N]), whenever N>0 F(–)=F 0 … what is f/F 0 for the following operations:  – ?/?  – ?/? & – ?/?

ELTE 12/ Calculation from a sequence Specification (the final) :  Input: N:Integer, X:Array[1..N:Something]  Output: S:Something  Precondition: N  0  Post condition: S=F(X[1..N])  Definition:

ELTE 13/ Calculation from a sequence Algorithm: E.g. in case of  this looks like: S:=F 0 i=1,…,N S:=f(S,X[i]) S:=0 i=1,…,N S:=S+X[i]

ELTE 14/ Calculation from a sequence Specification (income and expenses) :  Input: N:Integer, In, Ex:Array[1..N:Integer]  Output: S:Integer  Precondition: N  0 and  i (1  i  N): In[i],Ex[i]  0  Post condition: S= In[i]–Ex[i] Algorithm: S:=0 i=1,…,N S:=S+In[i] – Ex[i]

ELTE 15/ Decision Tasks (examples): 1. Let's decide if an integer is prime or not! 2. Let's tell if a given word is the name of a month! 3. Based on the final marks of a student, let's determine if he/she fails the semester! 4. Let's find if a given word contains vowel! 5. Let's decide if a sequence is monotonically increasing! 6. Based on the final marks, let's determine if the student is excellent (all marks are the best).

ELTE 16/ Decision What is in common? Let's determine, if there is an item with given attribute among N "something"!

ELTE 17/ Decision Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: Exists:Boolean  Precondition: N  0  Post condition: Exists=  i(1  i  N): T(X[i]) Comment: T attribute can be defined as a boolean function. All elements can be tested against T attribute.

ELTE 18/ Decision Algorithm 1 : Algorithm 2 : i:=1 i  N and not T(X[i]) i:=i+1 Exists:=i  N i:=0 Exists:=False i<N and not Exists i:=i+1 Exists:=T(X[i])

ELTE 19/ Decision Variant: … all the items has attribute T … Specification (only the difference) :  Output: All:Boolean  Post condition : All=  i(1  i  N): T(X[i]) Algorithm: i:=1 i  N and not T(X[i]) i:=i+1 All:=i>N

ELTE 20/ Decision Specification (determine if fails the semester) :  Input: N:Integer, Mark:Array[1..N:Integer]  Output: Fails:Boolean  Precondition: N  0 and  i (1  i  N): Mark[i]  [1..5]  Post cond.: Fails=  i (1  i  N): Mark[i]=1 Algorithm: i:=1 i  N and Mark[i]  1 i:=i+1 Fails:=i  N

ELTE 21/ Pick Tasks: 1. We know the monthly income and expenses of a person. His assets grows by the end of the year. Let's give a month, when his assets grows! 2. Let's define the least divisor of a non-1 integer! 3. Let's find a vowel in an English word! 4. Let's define the order number of a month given by its name!

ELTE 22/ Pick What is in common? We have N "something", and we have to pick an element which has a given attribute, by knowing that at least one such element exists among N.

ELTE 23/ Pick Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: S:Integer  Precondition: N>0 and  i (1  i  N): T(X[i])  Post condition: 1  S  N and T(X[S]) Comment: T attribute can be defined as a boolean function. All elements can be tested against T attribute.

ELTE 24/ Pick Algorithm: Comment: We know more: this solution gives the very first element having the T attribute – so the program knows more than expected. How would we find the last one? i:=1 not T(X[i]) i:=i+1 S:=i

ELTE 25/ Pick Specification (find a vowel in a word) :  Input: Word:Text  Output: VW:Integer  Precondition: length(Word)>0 and  i (1  i  length(Word)): isVowel(Word[i])  Post condition: 1  VW  length(Word) and isVowel(Word[VW])  Definition: isVowel: Character  Boolean isVowel(c) := capital(c)  {'A',…,'U'}

ELTE 26/ Pick Algorithm: i:=1 not isVowel(Word[i]) i:=i+1 VW:=i

ELTE 27/ Search Tasks (examples): 1. We know the monthly income and expenses of a person. His assets grows by the end of the year. Let's give a month, when his assets didn't grow! 2. Let's define a non-1 and non-N divisor of the integer N! 3. Let's search for letter "a" in the name of a person! 4. Let's search for a course, in which the student failed! 5. Let's find and element in a sequence, which is greater than the previous element!

ELTE 28/ Search What is in common? We have N "something", and we have to search for an element which has a given attribute, and we do not know whether such an element exists among N.

ELTE 29/ Search Specification:  Input: N:Integer, X:Array[1..N:Something]  Output Exists:Boolean, S:Integer  Precondition: N  0  Post cond.: Exists=  i (1  i  N): T(X[i]) and Exists  1  S  N and T(X[S])  Note: this specification comes half from decision and half from pick.

ELTE 30/ Search Algorithm 1 : Note: We know that the solution provides the first element having the given attribute. i:=1 i  N and not T(X[i]) i:=i+1 Exists:=i  N Exists S:=i  Y N

ELTE 31/ Search Algorithm 2 : Note: We know that the solution provides the first element having the given attribute. i:=0 Exists:=False i<N and not Exists i:=i+1 Exists:=T(X[i]) Exists S:=i  Y N

ELTE 32/ Search Specification (course in which student fails) :  Input: N:Integer, Mark:Array[1..N:Integer]  Output: Fails:Boolean, T:Integer  Precondition: N  0 and  i (1  i  N): Mark[i]  [1..5]  Post cond.: Fails=  i (1  i  N): Mark[i]=1 and Fails  1  T  N and Mark[T]=1

ELTE 33/ Search Algorithm: i:=1 i  N and Mark[i]  1 i:=i+1 Fails:=i  N Fails T:=i  Y N

ELTE 34/ Count Tasks (examples): 1. We know the monthly income and expenses of a person. Let's count the number of months where his assets grows! 2. Let's calculate the number of divisors of an integer! 3. Let's determine how many letter "a" can be found in the name of a person! 4. Based on the annual daily statistics, let's count the number of days when frozen! 5. We have birth date (month) data from N people. Let's count how many of them have birthday during the winter!

ELTE 35/ Count What is in common? We have N "something", and we have to count how many of them have a given attribute.

ELTE 36/ Count Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: Cnt:Integer  Precondition: N  0  Post cond.: Cnt=

ELTE 37/ Count Algorithm: Cnt:=0 i=1,…,N T(X[i]) Cnt:=Cnt+1  Y N

ELTE 38/ Count Specification (people having birthday in winter) :  Input: N:Integer, Mon:Array[1..N:Integer]  Output: Cnt:Integer (born in winter)  Precondition: N  0 and  i (1  i  N): Mon[i]  [1..12]  Post cond.: Cnt =

ELTE 39/ Count Algorithm: Note, that all the PoAs discussed so far work fine with N=0, expect for Search (it has this limitation in its precondition). Cnt:=0 i=1,…,N Mon[i]<3 or Mon[i]=12 Cnt:=Cnt+1  Y N

ELTE 40/ Maximum pick Tasks (examples): 1. We know the monthly income and expenses of a person. Let's determine the month, where his assets grow the most! 2. Let's pick the name of the person who is the last in the alphabetical order! 3. Let's find the person, who likes the most number of foods! 4. Based on the annual daily statistics, let's define the warmest day during the year! 5. We have birthdates from N people, let's find who has birthday at first during the year!

ELTE 41/ Maximum pick What is in common? We have to pick the greatest (or least) something from N elements. Important: If we have at least 1 element, then we know there is a greatest exists!

ELTE 42/ Maximum pick Specification:  Input: N:Integer, X:Array[1..N:Something]  Output: Max:Integer  Precondition: N>0  Post cond.: 1  Max  N and  i (1  i  N): X[Max]  X[i] Comments:  We suppose the following sort operator exists:  :Someting  Something  Boolean;  The sequence number of an element is a more general information, thus we provide that instead of the element itself.

ELTE 43/ Maximum pick Algorithm: Note: If there are more elements equal to the greatest, than this algorithm will find the first one. Questions: How can we find the last greatest one? How can we find the (first) least element? Max:=1 i=2,…,N X[i]>X[Max] Max:=i  Y N

ELTE 44/ Maximum pick (returning maximum value) MaxVal:=X[1] i=2,…,N X[i]>MaxVal MaxVal:=X[i]  Specification (only modifications) :  Output: MaxVal:Something  Post cond.:  i (1  i  N): MaxVal=X[i] and  i (1  i  N): MaxVal  X[i] Algorithm: Y N

ELTE 45/ Maximum pick Specification (warmest day during the year) :  Input: N:Integer, Temp:Array[1..N:Integer]  Output: Warmest:Integer  Precondition: N>0 and  i(1  i  N): Temp[i]  [ ]  Post condition: 1  Warmest  N and  i (1  i  N): Temp[Warmest]  Temp[i]

ELTE 46/ Maximum pick Algorithm: Warmest:=1 i=2,…,N Temp[i]>Temp[Warmest] Warmest:=i  Y N

ELTE 47/ Patterns of Algorithms (PoAs) – summary 1. Count from a sequence (e.g. sum) Count from a sequence 2. Decision Decision 3. Pick Pick 4. Search Search 5. Count Count 6. Maximum pick Maximum pick

Programming Fundamentals End of 5 th lecture