SlideShare a Scribd company logo
1 of 46
Lecture 2
Algorithm Part 1
23/10/2018 Lecture 2 Algorithm Part 1 1
Content Lecture 2 Part 1
ο‚— Performance
ο‚— Introduction
ο‚— Recursive
ο‚— Examples
ο‚— Recursion vs. Iteration
ο‚— Primitive Recursion Function
ο‚— Peano-Hilbert Curve
ο‚— Turtle Graphics
23/10/2018 Lecture 2 Algorithm Part 1 2
Performance
ο‚— Performance in computer system plays a significant rule
ο‚— The performance is general presented in the O-Notation
(invented 1894 by Paul Bachmann) called Big-O-Notation
ο‚— Big-O-Notation describes the limiting behavior of a
function when the argument tends towards a particular
value or infinity
ο‚— Big-O-Notation is nowadays mostly used to express the
worst case or average case running time or memory usage
of an algorithm in a way that is independent of computer
architecture
ο‚— This helps to compare the general effectiveness of an
algorithm
23/10/2018 Lecture 2 Algorithm Part 1 3
Performance
Definition
Be n the size of the data or any other problem related size
f(n) = O(g(n)) for n Ο΅ N,
if M, n0 Ο΅ N (element of the set N) exist such that
|f(n)| ≀ M|g(n)| for all n β‰₯ n0
Simpler:
f(n) = O(g(n)) for n οƒ  ∞
With other words: g(n) is a upper or lower bound for the
function f(n).
23/10/2018 Lecture 2 Algorithm Part 1 4
Performance
ο‚— The formal definition of Big-O-Notation is not used
directly
ο‚— The Big-O-Notation for a function f(x) is derived by the
following rules:
ο‚— If f(x) is a sum of several terms the one with the largest
growth rate is kept and all others are ignored
ο‚— If f(x) is a product of several factors, any constants
(independent of x) are ignored
23/10/2018 Lecture 2 Algorithm Part 1 5
Performance
Example
f(x) = 7x3 – 3x2 + 11
The function is the sum of three terms: 7x3, -3x2, 11
The one with the largest growth rate it the one with the
largest exponent: 7x3
This term is a product of 7 and x3
Because the factor 7 doesn’t depend on x it can be ignored
As a result you got: f(x) = O(g(x)) = O(x3) with g(x) = x3
23/10/2018 Lecture 2 Algorithm Part 1 6
Performance
List of standard Big-O Notations for comparing algorithm:
O(1)
Constant effort, independent from n
O(n)
Linear effort
23/10/2018 Lecture 2 Algorithm Part 1 7
Performance
O(n log(n))
Effort of good sort methods
O(n2)
Quadratic effort
23/10/2018 Lecture 2 Algorithm Part 1 8
Performance
O(n
k
)
Polynomial effort (with fixed k)
O(2
U
)
Exponential effort
23/10/2018 Lecture 2 Algorithm Part 1 9
Performance
O(n!)
All permutation of n elements
Worst case!
23/10/2018 Lecture 2 Algorithm Part 1 10
Performance
Other performance notations
ο‚— theta
ο‚— sigma
ο‚— small-o
23/10/2018 Lecture 2 Algorithm Part 1 11
Algorithm
What is an algorithm?
23/10/2018 Lecture 2 Algorithm Part 1 12
Algorithm
ο‚— A set of instructions done sequentially
ο‚— A list of well-defined instructions to complete a given task
ο‚— To perform a specified task in a specific order
ο‚— It is an effective method to solve a problem expressed as a
finite sequence of steps
ο‚— It is not limited to finite (nondeterministic algorithm)
ο‚— In computer systems an algorithm is defined as an instance
of logic written in software in order to intend the computer
machine to do something
23/10/2018 Lecture 2 Algorithm Part 1 13
Algorithm
ο‚— It is important to define the algorithm rigorously that
means that all possible circumstances for the given task
should be handled
ο‚— There is an initial state
ο‚— The introductions are done as a series of steps
ο‚— The criteria for each step must be clear and computable
ο‚— The order of the steps performed is always critical to the
algorithm
ο‚— The flow of control is from the top to the bottom (top-
down) that means from a start state to an end state
ο‚— Termination might be given to the algorithm but some
algorithm could also run forever without termination
23/10/2018 Lecture 2 Algorithm Part 1 14
Algorithm
ο‚— All algorithms are classified in 6 classes:
ο‚— Recursion vs. Iteration
ο‚— Logical
ο‚— Serial(Sequential)/Parallel/Distributed
ο‚— Deterministic/Non-deterministic
ο‚— Exact/Approximate
ο‚— Quantum
23/10/2018 Lecture 2 Algorithm Part 1 15
Algorithm
Recursion vs. Iteration
ο‚— Recursive algorithm makes references to itself repeatedly until a
finale state is reached
ο‚— Iterative algorithms use repetitive constructs like loops and
sometimes additional data structures
ο‚— More details later
Logical
ο‚— Algorithm = logic + control
ο‚— The logic component defines the axioms that are used in the
computation, the control components determines the way in
which deduction is applied to the axioms
ο‚— Example The programming language Prolog
23/10/2018 Lecture 2 Algorithm Part 1 16
Algorithm
Serial(Sequential)/Parallel/Distributed
ο‚— Serial Algorithm performing tasks serial that means one step by
one step; Each step is a single operation
ο‚— Parallel Algorithm performing multiple operations in each step
ο‚— Distributed Algorithm performing tasks distributed
ο‚— Examples
ο‚— Serial: Calculating the sum of n numbers
ο‚— Parallel: Network
ο‚— Distributed: GUI, E-mail
23/10/2018 Lecture 2 Algorithm Part 1 17
Algorithm
Deterministic/Non-deterministic
ο‚— Deterministic algorithm solve the problem with exact decision
at every step
ο‚— Non-deterministic solve problem by guessing through the use
of heuristics
ο‚— Example Shopping List
ο‚— Buy all items in any order οƒ  nondeterministic algorithm
ο‚— Buy all items in a given order οƒ  deterministic algorithm
Exact/Approximate
ο‚— Exact algorithms reach an exact solution
ο‚— Approximation algorithms searching for an approximation
close to the true solution
23/10/2018 Lecture 2 Algorithm Part 1 18
Algorithm
ο‚— Example: To find an approximate solutions to optimization
problems like storehouses for shops
Quantum
ο‚— Quantum algorithm running on a realistic model of quantum
computation
ο‚— It is a step-by-step procedure, where each of the steps can be
performed on a quantum computer
ο‚— It might be able to solve some problems faster than classical
algorithms
ο‚— Example: Shor's algorithm for integer factorization
23/10/2018 Lecture 2 Algorithm Part 1 19
Recursion
ο‚— Many problems, models and phenomenon have a self-reflecting
form in which the own structure is contained in different
variants
ο‚— This can be a mathematical formula as well as a natural
phenomenon
ο‚— If this structure is adopted in a mathematical definition, an
algorithm or a data structure than this is called a recursion
23/10/2018 Lecture 2 Algorithm Part 1 20
Recursion
Definition
Recursion is the process of repeating items in a self-similar way.
ο‚— Recursion definitions are only reasonable if something is only
defined by himself in a simpler form
ο‚— The limit will be a trivial case
ο‚— This case needs no recursion anymore
ο‚— A common joke is the following "definition" of recursion
(Catb.org. Retrieved 2010-04-07.):
Recursion
See "Recursion"
23/10/2018 Lecture 2 Algorithm Part 1 21
Recursion
Examples
Language
A child couldn't sleep, so her mother told a story about a little frog,
who couldn't sleep, so the frog's mother told a story about a little bear,
who couldn't sleep, so bear's mother told a story about a little weasel
...who fell asleep.
...and the little bear fell asleep;
...and the little frog fell asleep;
...and the child fell asleep.
23/10/2018 Lecture 2 Algorithm Part 1 22
Mathematical examples
Factorial
ο‚— The Factorial of a number if defined as n! = n*(n-1)*(n-2)*…*1
ο‚— Therefore:
F(n) = n! for n > 0 is defined: F(1) = 1
F(n) = n * F(n-1) for n > 1
ο‚— Program code in C/C++
int factorial(int number) {
if (number <= 1) //trivial case
return number;
return number * (factorial(number - 1)); //recursive call
}
23/10/2018 Lecture 2 Algorithm Part 1 23
Mathematical examples
Calculate F(5) = 5! = 120
23/10/2018 Lecture 2 Algorithm Part 1 24
Recursion
ο‚— Because of recursion it is possible that more than one
incarnation of the procedure exists at one time
ο‚— It is important that there is finiteness in the recursion (a
trivial case)
ο‚— For example a query decided if there is another recursion
call or not
ο‚— Otherwise you will have an endless recursive algorithm
calling itself again and again
ο‚— In the example it was the case that number <= 1
23/10/2018 Lecture 2 Algorithm Part 1 25
Recursion
Definition
The depth of a recursion is the number of recursion calls.
Example
For factorial the depth of F(n) is n.
depth(F(n)) = n
Because in every step you call the recursion only one time.
Therefore depth(F(5)) = 5
23/10/2018 Lecture 2 Algorithm Part 1 26
Recursion
ο‚— There are two ways to implement a recursion:
ο‚— Starting from an initial state and deriving new states
which every use of the recursion rules
ο‚— Starting from a complex state and simplifying successive
through using the recursion rules until a trivial state is
reached which needs no use of recursion (see Faculty)
ο‚— How to build a recursion depends mainly on:
ο‚— How readable and understandable the alternative
variants are
ο‚— Performance and memory issues
23/10/2018 Lecture 2 Algorithm Part 1 27
Mathematical examples
Greatest Common Divisor
ο‚— The Greatest Common Divisor gcd(x,y) of the integer x
and y s is the product of all common prime number factors
of x and y
ο‚— To calculate the gcd you can use the following formula:
gcd(x1, y1) = gcd(y1, x1 % y1) =gcd(x2, y2) =
= gcd(y2, x2 % y2) = … = gcd(xk, 0)
ο‚— The final gcd(xk, 0) = xk is the Greatest common divisor
23/10/2018 Lecture 2 Algorithm Part 1 28
Mathematical examples
Implementation in C
int mygcd(int x, int y) {
if (y == 0) //trivial case
return x;
else
return mygcd(y, x % y); //recursive call
}
gcd(34, 16) = gcd(16, 2) = gcd(2, 0) = 2
gcd(127, 36)=gcd(36, 19)=gcd(19, 17)=gcd(17, 2)=gcd(2, 1)=gcd(1, 0)=1
23/10/2018 Lecture 2 Algorithm Part 1 29
Mathematical examples
Fibonacci sequence
ο‚— Fibonacci sequence is one of the classical example of
recursion
ο‚— Leonardo Fibonacci was an Italian mathematician (around
1180 – 1240)
ο‚— The Fibonacci sequence can be found also in nature
(plants)
ο‚— The sequence is defined as:
ο‚— F(0) = 0 (base case)
ο‚— F(1) = 1 (base case)
ο‚— F(n) = F(n-1) + F(n-2) (recursion) for all n > 1 with n Ο΅ N
23/10/2018 Lecture 2 Algorithm Part 1 30
Mathematical examples
Calculate F(4) = 3
23/10/2018 Lecture 2 Algorithm Part 1 31
Mathematical examples
Ackermann function
ο‚— Wilhelm Ackermann was a German mathematician (1896 –
1962)
ο‚— The Ackermann function is used as a benchmark of the
ability of a compiler to optimize recursion
ο‚— The function is defined as:
𝐴 π‘š, 𝑛 =
𝑛 + 1 𝑖𝑓 π‘š = 0
𝐴 π‘š βˆ’ 1, 1 𝑖𝑓 π‘š > 0 π‘Žπ‘›π‘‘ 𝑛 = 0
𝐴 π‘š βˆ’ 1, 𝐴 π‘š, 𝑛 βˆ’ 1 𝑖𝑓 π‘š > 0 π‘Žπ‘›π‘‘ 𝑛 > 0
23/10/2018 Lecture 2 Algorithm Part 1 32
Mathematical examples
Implementation in C
int ackermann(int m, int n) {
if (m == 0) //trivial case
return n + 1;
else if (n == 0) //recursive call
return ackermann(m – 1, 1);
else //recursive call
return ackermann(m – 1, ackermann(m, n – 1));
}
23/10/2018 Lecture 2 Algorithm Part 1 33
Recursion vs. Iteration
ο‚— Use of recursion in an algorithm has both advantages and
disadvantages
ο‚— The main advantage is usually simplicity
ο‚— The main disadvantage is often that the algorithm may require
large amounts of memory if the depth of the recursion is very
high
ο‚— Many problems are solved more elegant and efficient if they are
implemented by using iteration
ο‚— This is especially the case for tail recursions which can be
replaced immediately by a loop, because no nested case exists
which has to be represented by a recursion
ο‚— The recursive call happens only at the end of the algorithm
ο‚— Recursion and iteration are not really contrasts because every
recursion can also be implemented as iteration
23/10/2018 Lecture 2 Algorithm Part 1 34
Recursion vs. Iteration
Tail recursion
int tail_recursion(…) {
if (simple_case) //trivial case
/*do something */;
else
/*do something */;
tail_recursion(…); // only recursive call
}
23/10/2018 Lecture 2 Algorithm Part 1 35
Recursion vs. Iteration
Examples
ο‚— The factorial algorithm is more efficient if you use an iterative
implementation
int factorial_iterative(int number) {
int result = 1;
while (number > 0) {
result *= number;
number--;
}
return number;
}
ο‚— Whereas the Ackermann function is an example where it is more
efficient and simpler to implement it with recursion
23/10/2018 Lecture 2 Algorithm Part 1 36
Primitive Recursion Function
Definition
The primitive recursive functions are among the number-
theoretic functions, which are functions from the natural
numbers (non negative integers) {0, 1, 2 , ...} to the natural
numbers
ο‚— These functions take n arguments for some natural
number n and are called n-ary
ο‚— Functions in number theory are primitive recursive
ο‚— Important also in proof theory (proof by induction)
23/10/2018 Lecture 2 Algorithm Part 1 37
Primitive Recursion Function
ο‚— The basic primitive recursive functions are given by these
axioms:
ο‚— Constant function: The 0-ary constant function 0 is
primitive recursive
ο‚— Successor function: The 1-ary successor function S,
which returns the successor of its argument, is primitive
recursive. That is, S(k) = k + 1
ο‚— Projection function: For every n β‰₯ 1 and each i with 1 ≀
i ≀ n, the n-ary projection function Pi
n, which returns
its ith argument, is primitive recursive
23/10/2018 Lecture 2 Algorithm Part 1 38
Primitive Recursion Function
Examples
ο‚— Addition: x + y
ο‚— Add(0, x) = x
ο‚— Add(y + 1, x) = Add(y, x) + 1
ο‚— Multiplication: x*y
ο‚— Exponentiation: xy,
ο‚— Factorial: n!
ο‚— Minimum: (n1, ... nn)
ο‚— Maximum: (n1, ... nn)
23/10/2018 Lecture 2 Algorithm Part 1 39
Peano-Hilbert curve
ο‚— The Peano-Hilbert curves were discovered by Peano and
Hilbert in 1890/1891
ο‚— They convert against a function which maps the interval
[0,1] of the real numbers surjective on the area [0,1] x [0,1]
and is in the same time constant
ο‚— That means through repeatedly execute the function rules
you will reach every point in a square
23/10/2018 Lecture 2 Algorithm Part 1 40
Peano-Hilbert curve
The Peano-Hilbert Curve can be expressed by a rewrite system (L-
system):
Alphabet: L, R
Constants: F, +, βˆ’
Axiom: L
Production rules:
L β†’ +RFβˆ’LFLβˆ’FR+
R β†’ βˆ’LF+RFR+FLβˆ’
F: draw forward
+: turn left 90Β°
-: turn right 90Β°
23/10/2018 Lecture 2 Algorithm Part 1 41
Peano-Hilbert curve
 First order: L  +F-F-F+ R -F+F+F-
ο‚— Second order: +RF-LFL-FR+
+-F+F+F-F-+F-F-F+F+F-F-F+-F-F+F+F-+
ο‚— Used in computer science
ο‚— For example to map the range of IP addresses used by computers into a
picture
23/10/2018 Lecture 2 Algorithm Part 1 42
Turtle Graphics
ο‚— Turtle Graphics are connected to the program language Logo
(1967)
ο‚— There is no absolute position in a coordination system
ο‚— All introductions are relative to the actual position
ο‚— A simple form would be:
Alphabet: X, Y
Constants: F, +, βˆ’
Production rules:
Initial Value: FX
X β†’ X+YF+
Y β†’ -FX-Y
23/10/2018 Lecture 2 Algorithm Part 1 43
F: draw forward
+: turn left 90Β°
-: turn right 90Β°
Turtle Graphics
ο‚— Level 1: FX = F+F+
ο‚— Level 2: FX = FX+YF+ = F+F++-F-F+
23/10/2018 Lecture 2 Algorithm Part 1 44
Turtle Graphics
Other turtle graphic
23/10/2018 Lecture 2 Algorithm Part 1 45
Any
questions?
23/10/2018 Lecture 2 Algorithm Part 1 46

More Related Content

What's hot

File handling in C
File handling in CFile handling in C
File handling in CKamal Acharya
Β 
File Management in C
File Management in CFile Management in C
File Management in CPaurav Shah
Β 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
Β 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTSoumen Santra
Β 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++Vineeta Garg
Β 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfixSelf-Employed
Β 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
Β 
Recursion in C++
Recursion in C++Recursion in C++
Recursion in C++Maliha Mehr
Β 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005vinaykhimsuriya1
Β 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c languagekiran Patel
Β 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++sai tarlekar
Β 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
Β 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ pptKumar
Β 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
Β 

What's hot (20)

File handling in C
File handling in CFile handling in C
File handling in C
Β 
File handling in c
File handling in cFile handling in c
File handling in c
Β 
File Management in C
File Management in CFile Management in C
File Management in C
Β 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Β 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
Β 
C functions
C functionsC functions
C functions
Β 
Stack
StackStack
Stack
Β 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Β 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Β 
Stacks
StacksStacks
Stacks
Β 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
Β 
Function
FunctionFunction
Function
Β 
Recursion in C++
Recursion in C++Recursion in C++
Recursion in C++
Β 
Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005Skip list vinay khimsuriya_200430723005
Skip list vinay khimsuriya_200430723005
Β 
Dynamic memory Allocation in c language
Dynamic memory Allocation in c languageDynamic memory Allocation in c language
Dynamic memory Allocation in c language
Β 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Β 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
Β 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Β 
Space complexity
Space complexitySpace complexity
Space complexity
Β 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
Β 

Similar to Lecture2a algorithm

Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
Β 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
Β 
A review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementationA review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementationssuserfa7e73
Β 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithmsMyMovies15
Β 
ALGO.ppt
ALGO.pptALGO.ppt
ALGO.pptPidoonEsm
Β 
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsCP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsSheba41
Β 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdfGOWTHAMR721887
Β 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.pptSenthil Vit
Β 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis Shaista Qadir
Β 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy AlgorithmsAmrinder Arora
Β 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introductionLow Ying Hao
Β 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
Β 

Similar to Lecture2a algorithm (20)

Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Β 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
Β 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
Β 
A review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementationA review of automatic differentiationand its efficient implementation
A review of automatic differentiationand its efficient implementation
Β 
Lec7
Lec7Lec7
Lec7
Β 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
Β 
Lec7.ppt
Lec7.pptLec7.ppt
Lec7.ppt
Β 
analysis of algorithms
analysis of algorithmsanalysis of algorithms
analysis of algorithms
Β 
ALGO.ppt
ALGO.pptALGO.ppt
ALGO.ppt
Β 
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsCP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
Β 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
Β 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
Β 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
Β 
Big Oh.ppt
Big Oh.pptBig Oh.ppt
Big Oh.ppt
Β 
Unit i
Unit iUnit i
Unit i
Β 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Β 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
Β 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
Β 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
Β 
Unit i
Unit iUnit i
Unit i
Β 

More from mbadhi barnabas

Lecture4b dynamic data_structure
Lecture4b dynamic data_structureLecture4b dynamic data_structure
Lecture4b dynamic data_structurembadhi barnabas
Β 
Lecture4a dynamic data_structure
Lecture4a dynamic data_structureLecture4a dynamic data_structure
Lecture4a dynamic data_structurembadhi barnabas
Β 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searchingmbadhi barnabas
Β 
Lecture2b algorithm
Lecture2b algorithmLecture2b algorithm
Lecture2b algorithmmbadhi barnabas
Β 
Lecture1b data types
Lecture1b data typesLecture1b data types
Lecture1b data typesmbadhi barnabas
Β 
Lecture1a data types
Lecture1a data typesLecture1a data types
Lecture1a data typesmbadhi barnabas
Β 
Data struture and aligorism
Data struture and aligorismData struture and aligorism
Data struture and aligorismmbadhi barnabas
Β 
Data structures and algorithm
Data structures and algorithmData structures and algorithm
Data structures and algorithmmbadhi barnabas
Β 

More from mbadhi barnabas (9)

Lecture4b dynamic data_structure
Lecture4b dynamic data_structureLecture4b dynamic data_structure
Lecture4b dynamic data_structure
Β 
Lecture4a dynamic data_structure
Lecture4a dynamic data_structureLecture4a dynamic data_structure
Lecture4a dynamic data_structure
Β 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
Β 
Lecture3a sorting
Lecture3a sortingLecture3a sorting
Lecture3a sorting
Β 
Lecture2b algorithm
Lecture2b algorithmLecture2b algorithm
Lecture2b algorithm
Β 
Lecture1b data types
Lecture1b data typesLecture1b data types
Lecture1b data types
Β 
Lecture1a data types
Lecture1a data typesLecture1a data types
Lecture1a data types
Β 
Data struture and aligorism
Data struture and aligorismData struture and aligorism
Data struture and aligorism
Β 
Data structures and algorithm
Data structures and algorithmData structures and algorithm
Data structures and algorithm
Β 

Recently uploaded

BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
Β 
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Online
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service OnlineCALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Online
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Onlineanilsa9823
Β 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
Β 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxolyaivanovalion
Β 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
Β 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
Β 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
Β 
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...amitlee9823
Β 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
Β 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
Β 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
Β 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
Β 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
Β 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
Β 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
Β 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
Β 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
Β 
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...amitlee9823
Β 

Recently uploaded (20)

BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
Β 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Β 
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Online
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service OnlineCALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Online
CALL ON βž₯8923113531 πŸ”Call Girls Chinhat Lucknow best sexual service Online
Β 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Β 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
Β 
ALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptxALSO dropshipping via API with DroFx.pptx
ALSO dropshipping via API with DroFx.pptx
Β 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
Β 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
Β 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
Β 
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore...
Β 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Β 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
Β 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
Β 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
Β 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
Β 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
Β 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
Β 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
Β 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
Β 
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: πŸ“ 7737669865 πŸ“ High Profile Model Escorts | Bangalore ...
Β 

Lecture2a algorithm

  • 1. Lecture 2 Algorithm Part 1 23/10/2018 Lecture 2 Algorithm Part 1 1
  • 2. Content Lecture 2 Part 1 ο‚— Performance ο‚— Introduction ο‚— Recursive ο‚— Examples ο‚— Recursion vs. Iteration ο‚— Primitive Recursion Function ο‚— Peano-Hilbert Curve ο‚— Turtle Graphics 23/10/2018 Lecture 2 Algorithm Part 1 2
  • 3. Performance ο‚— Performance in computer system plays a significant rule ο‚— The performance is general presented in the O-Notation (invented 1894 by Paul Bachmann) called Big-O-Notation ο‚— Big-O-Notation describes the limiting behavior of a function when the argument tends towards a particular value or infinity ο‚— Big-O-Notation is nowadays mostly used to express the worst case or average case running time or memory usage of an algorithm in a way that is independent of computer architecture ο‚— This helps to compare the general effectiveness of an algorithm 23/10/2018 Lecture 2 Algorithm Part 1 3
  • 4. Performance Definition Be n the size of the data or any other problem related size f(n) = O(g(n)) for n Ο΅ N, if M, n0 Ο΅ N (element of the set N) exist such that |f(n)| ≀ M|g(n)| for all n β‰₯ n0 Simpler: f(n) = O(g(n)) for n οƒ  ∞ With other words: g(n) is a upper or lower bound for the function f(n). 23/10/2018 Lecture 2 Algorithm Part 1 4
  • 5. Performance ο‚— The formal definition of Big-O-Notation is not used directly ο‚— The Big-O-Notation for a function f(x) is derived by the following rules: ο‚— If f(x) is a sum of several terms the one with the largest growth rate is kept and all others are ignored ο‚— If f(x) is a product of several factors, any constants (independent of x) are ignored 23/10/2018 Lecture 2 Algorithm Part 1 5
  • 6. Performance Example f(x) = 7x3 – 3x2 + 11 The function is the sum of three terms: 7x3, -3x2, 11 The one with the largest growth rate it the one with the largest exponent: 7x3 This term is a product of 7 and x3 Because the factor 7 doesn’t depend on x it can be ignored As a result you got: f(x) = O(g(x)) = O(x3) with g(x) = x3 23/10/2018 Lecture 2 Algorithm Part 1 6
  • 7. Performance List of standard Big-O Notations for comparing algorithm: O(1) Constant effort, independent from n O(n) Linear effort 23/10/2018 Lecture 2 Algorithm Part 1 7
  • 8. Performance O(n log(n)) Effort of good sort methods O(n2) Quadratic effort 23/10/2018 Lecture 2 Algorithm Part 1 8
  • 9. Performance O(n k ) Polynomial effort (with fixed k) O(2 U ) Exponential effort 23/10/2018 Lecture 2 Algorithm Part 1 9
  • 10. Performance O(n!) All permutation of n elements Worst case! 23/10/2018 Lecture 2 Algorithm Part 1 10
  • 11. Performance Other performance notations ο‚— theta ο‚— sigma ο‚— small-o 23/10/2018 Lecture 2 Algorithm Part 1 11
  • 12. Algorithm What is an algorithm? 23/10/2018 Lecture 2 Algorithm Part 1 12
  • 13. Algorithm ο‚— A set of instructions done sequentially ο‚— A list of well-defined instructions to complete a given task ο‚— To perform a specified task in a specific order ο‚— It is an effective method to solve a problem expressed as a finite sequence of steps ο‚— It is not limited to finite (nondeterministic algorithm) ο‚— In computer systems an algorithm is defined as an instance of logic written in software in order to intend the computer machine to do something 23/10/2018 Lecture 2 Algorithm Part 1 13
  • 14. Algorithm ο‚— It is important to define the algorithm rigorously that means that all possible circumstances for the given task should be handled ο‚— There is an initial state ο‚— The introductions are done as a series of steps ο‚— The criteria for each step must be clear and computable ο‚— The order of the steps performed is always critical to the algorithm ο‚— The flow of control is from the top to the bottom (top- down) that means from a start state to an end state ο‚— Termination might be given to the algorithm but some algorithm could also run forever without termination 23/10/2018 Lecture 2 Algorithm Part 1 14
  • 15. Algorithm ο‚— All algorithms are classified in 6 classes: ο‚— Recursion vs. Iteration ο‚— Logical ο‚— Serial(Sequential)/Parallel/Distributed ο‚— Deterministic/Non-deterministic ο‚— Exact/Approximate ο‚— Quantum 23/10/2018 Lecture 2 Algorithm Part 1 15
  • 16. Algorithm Recursion vs. Iteration ο‚— Recursive algorithm makes references to itself repeatedly until a finale state is reached ο‚— Iterative algorithms use repetitive constructs like loops and sometimes additional data structures ο‚— More details later Logical ο‚— Algorithm = logic + control ο‚— The logic component defines the axioms that are used in the computation, the control components determines the way in which deduction is applied to the axioms ο‚— Example The programming language Prolog 23/10/2018 Lecture 2 Algorithm Part 1 16
  • 17. Algorithm Serial(Sequential)/Parallel/Distributed ο‚— Serial Algorithm performing tasks serial that means one step by one step; Each step is a single operation ο‚— Parallel Algorithm performing multiple operations in each step ο‚— Distributed Algorithm performing tasks distributed ο‚— Examples ο‚— Serial: Calculating the sum of n numbers ο‚— Parallel: Network ο‚— Distributed: GUI, E-mail 23/10/2018 Lecture 2 Algorithm Part 1 17
  • 18. Algorithm Deterministic/Non-deterministic ο‚— Deterministic algorithm solve the problem with exact decision at every step ο‚— Non-deterministic solve problem by guessing through the use of heuristics ο‚— Example Shopping List ο‚— Buy all items in any order οƒ  nondeterministic algorithm ο‚— Buy all items in a given order οƒ  deterministic algorithm Exact/Approximate ο‚— Exact algorithms reach an exact solution ο‚— Approximation algorithms searching for an approximation close to the true solution 23/10/2018 Lecture 2 Algorithm Part 1 18
  • 19. Algorithm ο‚— Example: To find an approximate solutions to optimization problems like storehouses for shops Quantum ο‚— Quantum algorithm running on a realistic model of quantum computation ο‚— It is a step-by-step procedure, where each of the steps can be performed on a quantum computer ο‚— It might be able to solve some problems faster than classical algorithms ο‚— Example: Shor's algorithm for integer factorization 23/10/2018 Lecture 2 Algorithm Part 1 19
  • 20. Recursion ο‚— Many problems, models and phenomenon have a self-reflecting form in which the own structure is contained in different variants ο‚— This can be a mathematical formula as well as a natural phenomenon ο‚— If this structure is adopted in a mathematical definition, an algorithm or a data structure than this is called a recursion 23/10/2018 Lecture 2 Algorithm Part 1 20
  • 21. Recursion Definition Recursion is the process of repeating items in a self-similar way. ο‚— Recursion definitions are only reasonable if something is only defined by himself in a simpler form ο‚— The limit will be a trivial case ο‚— This case needs no recursion anymore ο‚— A common joke is the following "definition" of recursion (Catb.org. Retrieved 2010-04-07.): Recursion See "Recursion" 23/10/2018 Lecture 2 Algorithm Part 1 21
  • 22. Recursion Examples Language A child couldn't sleep, so her mother told a story about a little frog, who couldn't sleep, so the frog's mother told a story about a little bear, who couldn't sleep, so bear's mother told a story about a little weasel ...who fell asleep. ...and the little bear fell asleep; ...and the little frog fell asleep; ...and the child fell asleep. 23/10/2018 Lecture 2 Algorithm Part 1 22
  • 23. Mathematical examples Factorial ο‚— The Factorial of a number if defined as n! = n*(n-1)*(n-2)*…*1 ο‚— Therefore: F(n) = n! for n > 0 is defined: F(1) = 1 F(n) = n * F(n-1) for n > 1 ο‚— Program code in C/C++ int factorial(int number) { if (number <= 1) //trivial case return number; return number * (factorial(number - 1)); //recursive call } 23/10/2018 Lecture 2 Algorithm Part 1 23
  • 24. Mathematical examples Calculate F(5) = 5! = 120 23/10/2018 Lecture 2 Algorithm Part 1 24
  • 25. Recursion ο‚— Because of recursion it is possible that more than one incarnation of the procedure exists at one time ο‚— It is important that there is finiteness in the recursion (a trivial case) ο‚— For example a query decided if there is another recursion call or not ο‚— Otherwise you will have an endless recursive algorithm calling itself again and again ο‚— In the example it was the case that number <= 1 23/10/2018 Lecture 2 Algorithm Part 1 25
  • 26. Recursion Definition The depth of a recursion is the number of recursion calls. Example For factorial the depth of F(n) is n. depth(F(n)) = n Because in every step you call the recursion only one time. Therefore depth(F(5)) = 5 23/10/2018 Lecture 2 Algorithm Part 1 26
  • 27. Recursion ο‚— There are two ways to implement a recursion: ο‚— Starting from an initial state and deriving new states which every use of the recursion rules ο‚— Starting from a complex state and simplifying successive through using the recursion rules until a trivial state is reached which needs no use of recursion (see Faculty) ο‚— How to build a recursion depends mainly on: ο‚— How readable and understandable the alternative variants are ο‚— Performance and memory issues 23/10/2018 Lecture 2 Algorithm Part 1 27
  • 28. Mathematical examples Greatest Common Divisor ο‚— The Greatest Common Divisor gcd(x,y) of the integer x and y s is the product of all common prime number factors of x and y ο‚— To calculate the gcd you can use the following formula: gcd(x1, y1) = gcd(y1, x1 % y1) =gcd(x2, y2) = = gcd(y2, x2 % y2) = … = gcd(xk, 0) ο‚— The final gcd(xk, 0) = xk is the Greatest common divisor 23/10/2018 Lecture 2 Algorithm Part 1 28
  • 29. Mathematical examples Implementation in C int mygcd(int x, int y) { if (y == 0) //trivial case return x; else return mygcd(y, x % y); //recursive call } gcd(34, 16) = gcd(16, 2) = gcd(2, 0) = 2 gcd(127, 36)=gcd(36, 19)=gcd(19, 17)=gcd(17, 2)=gcd(2, 1)=gcd(1, 0)=1 23/10/2018 Lecture 2 Algorithm Part 1 29
  • 30. Mathematical examples Fibonacci sequence ο‚— Fibonacci sequence is one of the classical example of recursion ο‚— Leonardo Fibonacci was an Italian mathematician (around 1180 – 1240) ο‚— The Fibonacci sequence can be found also in nature (plants) ο‚— The sequence is defined as: ο‚— F(0) = 0 (base case) ο‚— F(1) = 1 (base case) ο‚— F(n) = F(n-1) + F(n-2) (recursion) for all n > 1 with n Ο΅ N 23/10/2018 Lecture 2 Algorithm Part 1 30
  • 31. Mathematical examples Calculate F(4) = 3 23/10/2018 Lecture 2 Algorithm Part 1 31
  • 32. Mathematical examples Ackermann function ο‚— Wilhelm Ackermann was a German mathematician (1896 – 1962) ο‚— The Ackermann function is used as a benchmark of the ability of a compiler to optimize recursion ο‚— The function is defined as: 𝐴 π‘š, 𝑛 = 𝑛 + 1 𝑖𝑓 π‘š = 0 𝐴 π‘š βˆ’ 1, 1 𝑖𝑓 π‘š > 0 π‘Žπ‘›π‘‘ 𝑛 = 0 𝐴 π‘š βˆ’ 1, 𝐴 π‘š, 𝑛 βˆ’ 1 𝑖𝑓 π‘š > 0 π‘Žπ‘›π‘‘ 𝑛 > 0 23/10/2018 Lecture 2 Algorithm Part 1 32
  • 33. Mathematical examples Implementation in C int ackermann(int m, int n) { if (m == 0) //trivial case return n + 1; else if (n == 0) //recursive call return ackermann(m – 1, 1); else //recursive call return ackermann(m – 1, ackermann(m, n – 1)); } 23/10/2018 Lecture 2 Algorithm Part 1 33
  • 34. Recursion vs. Iteration ο‚— Use of recursion in an algorithm has both advantages and disadvantages ο‚— The main advantage is usually simplicity ο‚— The main disadvantage is often that the algorithm may require large amounts of memory if the depth of the recursion is very high ο‚— Many problems are solved more elegant and efficient if they are implemented by using iteration ο‚— This is especially the case for tail recursions which can be replaced immediately by a loop, because no nested case exists which has to be represented by a recursion ο‚— The recursive call happens only at the end of the algorithm ο‚— Recursion and iteration are not really contrasts because every recursion can also be implemented as iteration 23/10/2018 Lecture 2 Algorithm Part 1 34
  • 35. Recursion vs. Iteration Tail recursion int tail_recursion(…) { if (simple_case) //trivial case /*do something */; else /*do something */; tail_recursion(…); // only recursive call } 23/10/2018 Lecture 2 Algorithm Part 1 35
  • 36. Recursion vs. Iteration Examples ο‚— The factorial algorithm is more efficient if you use an iterative implementation int factorial_iterative(int number) { int result = 1; while (number > 0) { result *= number; number--; } return number; } ο‚— Whereas the Ackermann function is an example where it is more efficient and simpler to implement it with recursion 23/10/2018 Lecture 2 Algorithm Part 1 36
  • 37. Primitive Recursion Function Definition The primitive recursive functions are among the number- theoretic functions, which are functions from the natural numbers (non negative integers) {0, 1, 2 , ...} to the natural numbers ο‚— These functions take n arguments for some natural number n and are called n-ary ο‚— Functions in number theory are primitive recursive ο‚— Important also in proof theory (proof by induction) 23/10/2018 Lecture 2 Algorithm Part 1 37
  • 38. Primitive Recursion Function ο‚— The basic primitive recursive functions are given by these axioms: ο‚— Constant function: The 0-ary constant function 0 is primitive recursive ο‚— Successor function: The 1-ary successor function S, which returns the successor of its argument, is primitive recursive. That is, S(k) = k + 1 ο‚— Projection function: For every n β‰₯ 1 and each i with 1 ≀ i ≀ n, the n-ary projection function Pi n, which returns its ith argument, is primitive recursive 23/10/2018 Lecture 2 Algorithm Part 1 38
  • 39. Primitive Recursion Function Examples ο‚— Addition: x + y ο‚— Add(0, x) = x ο‚— Add(y + 1, x) = Add(y, x) + 1 ο‚— Multiplication: x*y ο‚— Exponentiation: xy, ο‚— Factorial: n! ο‚— Minimum: (n1, ... nn) ο‚— Maximum: (n1, ... nn) 23/10/2018 Lecture 2 Algorithm Part 1 39
  • 40. Peano-Hilbert curve ο‚— The Peano-Hilbert curves were discovered by Peano and Hilbert in 1890/1891 ο‚— They convert against a function which maps the interval [0,1] of the real numbers surjective on the area [0,1] x [0,1] and is in the same time constant ο‚— That means through repeatedly execute the function rules you will reach every point in a square 23/10/2018 Lecture 2 Algorithm Part 1 40
  • 41. Peano-Hilbert curve The Peano-Hilbert Curve can be expressed by a rewrite system (L- system): Alphabet: L, R Constants: F, +, βˆ’ Axiom: L Production rules: L β†’ +RFβˆ’LFLβˆ’FR+ R β†’ βˆ’LF+RFR+FLβˆ’ F: draw forward +: turn left 90Β° -: turn right 90Β° 23/10/2018 Lecture 2 Algorithm Part 1 41
  • 42. Peano-Hilbert curve ο‚— First order: L οƒ  +F-F-F+ Rοƒ  -F+F+F- ο‚— Second order: +RF-LFL-FR+ +-F+F+F-F-+F-F-F+F+F-F-F+-F-F+F+F-+ ο‚— Used in computer science ο‚— For example to map the range of IP addresses used by computers into a picture 23/10/2018 Lecture 2 Algorithm Part 1 42
  • 43. Turtle Graphics ο‚— Turtle Graphics are connected to the program language Logo (1967) ο‚— There is no absolute position in a coordination system ο‚— All introductions are relative to the actual position ο‚— A simple form would be: Alphabet: X, Y Constants: F, +, βˆ’ Production rules: Initial Value: FX X β†’ X+YF+ Y β†’ -FX-Y 23/10/2018 Lecture 2 Algorithm Part 1 43 F: draw forward +: turn left 90Β° -: turn right 90Β°
  • 44. Turtle Graphics ο‚— Level 1: FX = F+F+ ο‚— Level 2: FX = FX+YF+ = F+F++-F-F+ 23/10/2018 Lecture 2 Algorithm Part 1 44
  • 45. Turtle Graphics Other turtle graphic 23/10/2018 Lecture 2 Algorithm Part 1 45
  • 46. Any questions? 23/10/2018 Lecture 2 Algorithm Part 1 46

Editor's Notes

  1. Betragsfunktion = Absolute value function
  2. Axiom = something that I just true – no proof
  3. For each y exist an x with f(x) = x