Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

201 DSA Chapter 1 Introduction in Recursion and Complexity of Algorithms

The document provides an introduction to basic concepts on data structures and algorithms (DSA) including: - Key concepts related to data, algorithms, and pseudocode. - An overview of recursion, including the basic components of recursive algorithms, properties of recursion, and designing and implementing recursive algorithms. - A discussion of algorithm complexity analysis including asymptotic analysis and analyzing common complexities like P and NP problems. The document is intended as an introduction for a course on data structures and algorithms and outlines some of the key topics and learning outcomes for students in the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

201 DSA Chapter 1 Introduction in Recursion and Complexity of Algorithms

The document provides an introduction to basic concepts on data structures and algorithms (DSA) including: - Key concepts related to data, algorithms, and pseudocode. - An overview of recursion, including the basic components of recursive algorithms, properties of recursion, and designing and implementing recursive algorithms. - A discussion of algorithm complexity analysis including asymptotic analysis and analyzing common complexities like P and NP problems. The document is intended as an introduction for a course on data structures and algorithms and outlines some of the key topics and learning outcomes for students in the course.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 102

Basic concepts

on DSA

Dept. Computer
Science

Course introduction, Recursion and


Complexity of Algorithms DSA: Basic concepts
Some concepts on data
Algorithm
Pseudocode

Recursion
Data Structures and Algorithms Ngày 5 tháng 1 năm 2022 Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++
Dept. Computer Science
Complexity of
Faculty of Computer Science and Engineering Algorithms
Algorithm Efficiency
Ho Chi Minh University of Technology, VNU-HCM Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.1


Basic concepts
Overview on DSA

Dept. Computer
1 DSA: Basic concepts Science

Some concepts on data


Algorithm
Pseudocode
DSA: Basic concepts
2 Recursion Some concepts on data
Algorithm
Recursion and the basic components of recursive Pseudocode

algorithms Recursion
Recursion and the basic
Properties of recursion components of recursive
algorithms

Designing recursive algorithms Properties of recursion


Designing recursive algorithms
Recursion and backtracking Recursion and backtracking
Recursion implementation in
Recursion implementation in C/C++ C/C++

Complexity of
3 Complexity of Algorithms Algorithms
Algorithm Efficiency
Algorithm Efficiency Asymptotic Analysis
Problems and common
Asymptotic Analysis complexities
P and NP Problems
Problems and common complexities
P and NP Problems
Basic concepts on DSA.2
Basic concepts
Sources of Materials on DSA

Dept. Computer
Science

1 We would like to thank Dr. The-Nhan LUONG, a


former instructor of our Department, for the DSA: Basic concepts
composing of this document. Some concepts on data
Algorithm

2 This document also uses figure, sentences and demo Pseudocode

Recursion
source code from the following sources: Recursion and the basic
components of recursive
• The old presentation for course Data Structures and algorithms
Properties of recursion
Algorithms edited by other members in our Designing recursive algorithms

Department Recursion and backtracking


Recursion implementation in
• Book entitled Data Structures - A Pseudocode C/C++

Complexity of
Approach with C++ (first edition, 2001) written Algorithms
by Richard F. Gilberg and Behrouz A. Forouzan Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.3


Basic concepts
Course learning outcomes on DSA

Dept. Computer
Science
L.O.1 Determine the complexity of simple algorithms
(polynomial time - nested loop - no recursive)
L.O.1.1 Give definition of Big-O notation
L.O.1.2 Determine complexity of simple polynomial algorithms
DSA: Basic concepts
L.O.2 Manipulate basic data structures such as list, tree and graph Some concepts on data
Algorithm
L.O.2.1 Describe and present basic data structures such as: array, Pseudocode

linked list, stack, queue, tree, and graph Recursion


L.O.2.2 Implement basic methods for each of basic data structures: Recursion and the basic
components of recursive
array, linked list, stack, queue, tree, and graph algorithms
Properties of recursion
Designing recursive algorithms

L.O.3 Implement basic sorting and searching algorithms Recursion and backtracking
Recursion implementation in
L.O.3.1 Illustrate how searching algorithms work on data structures: C/C++

array, linked list, stack, queue, tree, and graph Complexity of


Algorithms
L.O.3.2 Illustrate how sorting algorithms work on an array Algorithm Efficiency
L.O.3.3 Implement necessary methods and proposed algorithms Asymptotic Analysis
Problems and common
on a given data structure for problem solving complexities
P and NP Problems

Basic concepts on DSA.4


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

Basic concepts Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.5


Basic concepts
What is Data? on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

(Source: Basic concepts on DSA.6


Basic concepts
What is Data? on DSA

Dept. Computer
Science
Data
Data is information that has been translated into a form
that is more convenient to calculate, analyze.
DSA: Basic concepts
Some concepts on data
Example Algorithm
Pseudocode

• Numbers, words, measurements, observations or Recursion


Recursion and the basic
descriptions of things. components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++
• Qualitative data: descriptive information, Complexity of
• Quantitative data: numerical information (numbers). Algorithms
Algorithm Efficiency

• Discrete data can only take certain values (like whole Asymptotic Analysis
Problems and common
numbers) complexities
P and NP Problems
• Continuous data can take any value (within a range)

Basic concepts on DSA.7


Basic concepts
Data type on DSA

Dept. Computer
Science

Class of data objects that have the same properties.


Data type

1 A set of values DSA: Basic concepts


Some concepts on data

2 A set of operations on values Algorithm


Pseudocode

Recursion
Recursion and the basic
Example components of recursive
algorithms

Type Values Operations Properties of recursion


Designing recursive algorithms
Recursion and backtracking
integer −∞, ..., −2, −1, ∗, +, −, %, /, Recursion implementation in
C/C++
0, 1, 2, ..., ∞ ++, −−, ... Complexity of
floating point −∞, ..., 0.0, ..., ∞ ∗, +, −, /, ... Algorithms
Algorithm Efficiency

character \0, ..., ‘A’, ‘B’, ..., <, >, ... Asymptotic Analysis
Problems and common
‘a’, ‘b’, ..., ∼ complexities
P and NP Problems

Basic concepts on DSA.8


Basic concepts
Data structure on DSA

Dept. Computer
Science

What is a data structure?


1 A combination of elements in which each is either a
data type or another data structure DSA: Basic concepts
Some concepts on data

2 A set of associations or relationships (structure) that Algorithm


Pseudocode

holds the data together Recursion


Recursion and the basic
components of recursive
algorithms

Example Properties of recursion


Designing recursive algorithms

An array is a number of elements of the same type in a Recursion and backtracking


Recursion implementation in
C/C++
specific order.
Complexity of
Algorithms
Algorithm Efficiency

1 2 3 5 8 13 21 34 Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.9


Basic concepts
Abstract data type on DSA

Dept. Computer
Science

The concept of abstraction:

• Users know what a data type can do.


DSA: Basic concepts
• How it is done is hidden. Some concepts on data
Algorithm
Pseudocode

Definition Recursion
Recursion and the basic

An abstract data type is a data declaration packaged components of recursive


algorithms

together with the operations that are meaningful for the Properties of recursion
Designing recursive algorithms

data type. Recursion and backtracking


Recursion implementation in
C/C++

Complexity of
1 Declaration of data Algorithms
Algorithm Efficiency
2 Declaration of operations Asymptotic Analysis
Problems and common

3 Encapsulation of data and operations complexities


P and NP Problems

Basic concepts on DSA.10


Basic concepts
Abstract data type on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities

Figure: Abstract data type model (source: Slideshare) P and NP Problems

Basic concepts on DSA.11


Basic concepts
Example: List on DSA

Dept. Computer
Science

Interface

• Data: sequence of elements of a


DSA: Basic concepts
particular data type Some concepts on data
Algorithm

• Operations: accessing, insertion, deletion Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

Implementation C/C++

Complexity of
Algorithms
• Array Algorithm Efficiency
Asymptotic Analysis

• Linked list
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.12


Basic concepts
Algorithm on DSA

Dept. Computer
Science

What is an algorithm?
DSA: Basic concepts
The logical steps to solve a problem. Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
What is a program? Designing recursive algorithms
Recursion and backtracking

Program = Data structures + Algorithms Recursion implementation in


C/C++

Complexity of
(Niklaus Wirth) Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.13


Basic concepts
Pseudocode on DSA

Dept. Computer
Science

• The most common tool to define


algorithms
DSA: Basic concepts
Some concepts on data

• English-like representation of the Algorithm


Pseudocode

Recursion

algorithm logic Recursion and the basic


components of recursive
algorithms
Properties of recursion
Designing recursive algorithms

• Pseudocode = English + code


Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
instructions using basic Problems and common
relaxed syntax being easy control structures (sequen-
complexities
P and NP Problems
to read tial, conditional, iterative)

Basic concepts on DSA.14


Basic concepts
Pseudocode on DSA

Dept. Computer
Science
Algorithm Header
• Name
• Parameters and their types
• Purpose: what the algorithm does DSA: Basic concepts
Some concepts on data

• Precondition: precursor requirements for the parameters Algorithm


Pseudocode

• Postcondition: taken action and status of the parameters Recursion


• Return condition: returned value Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Algorithm Body Recursion and backtracking
Recursion implementation in
C/C++
• Statements
Complexity of
• Statement numbers: decimal notation to express levels Algorithms
Algorithm Efficiency

• Variables: important data Asymptotic Analysis


Problems and common
complexities
• Algorithm analysis: comments to explain salient points P and NP Problems

• Statement constructs: sequence, selection, iteration

Basic concepts on DSA.15


Basic concepts
Pseudocode: Example on DSA

Dept. Computer
Science
Algorithm average
Pre nothing
Post the average of the input numbers is printed
DSA: Basic concepts
1 i=0 Some concepts on data

2 sum = 0 Algorithm
Pseudocode

3 while all numbers not read do Recursion


4 i=i+1 Recursion and the basic
components of recursive
algorithms
5 read number Properties of recursion
Designing recursive algorithms
6 sum = sum + number Recursion and backtracking
Recursion implementation in
7 end C/C++

8 average = sum / i Complexity of


Algorithms
9 print average Algorithm Efficiency
Asymptotic Analysis
10 End average Problems and common
complexities
P and NP Problems
Algorithm 1: How to calculate the average

Basic concepts on DSA.16


Basic concepts
on DSA

Dept. Computer
Science

Recursion and the basic DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

components of Recursion
Recursion and the basic
components of recursive
algorithms

recursive algorithms Properties of recursion


Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.17


Basic concepts
Recursion on DSA

Dept. Computer
Science
Definition
Recursion is a repetitive process in which an algorithm
calls itself.
• Direct : A → A DSA: Basic concepts
Some concepts on data

• Indirect : A → B → A Algorithm
Pseudocode

Recursion

Example Recursion and the basic


components of recursive
algorithms
Properties of recursion
Factorial " Designing recursive algorithms

1 if n = 0 Recursion and backtracking

F actorial(n) = Recursion implementation in

n × (n − 1) × ... × 2 × 1 if n > 0 C/C++

Complexity of
Algorithms

Using recursion: " Algorithm Efficiency


Asymptotic Analysis
Problems and common
1 if n = 0 complexities
F actorial(n) = P and NP Problems
n × F actorial(n − 1) if n > 0

Basic concepts on DSA.18


Basic concepts
Basic components of recursive algorithms on DSA

Dept. Computer
Science

Two main components of a Recursive Algorithm


DSA: Basic concepts
1 Base case (i.e. stopping case) Some concepts on data
Algorithm
Pseudocode

2 General case (i.e. recursive case) Recursion


Recursion and the basic
components of recursive
algorithms
Properties of recursion
Example Designing recursive algorithms
Recursion and backtracking
Factorial " Recursion implementation in
C/C++
1 if n = 0 base
F actorial(n) = Complexity of
n × F actorial(n − 1) if n > 0 general Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.19


Basic concepts
Recursion on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Hình: Factorial (3) Recursively Algorithm Efficiency

(Source: Data Structure - A pseudocode Approach with C++ Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.20


Basic concepts
Recursion on DSA

Dept. Computer
Science

Factorial: Iterative Solution


1 Algorithm iterativeFactorial(n)
2 Calculates the factorial of a number using a loop.
DSA: Basic concepts
3 Pre: n is the number to be raised factorially Some concepts on data

4 Post: n! is returned - result in factoN Algorithm


Pseudocode

Recursion
5 i=1 Recursion and the basic
components of recursive

6 factoN = 1 algorithms
Properties of recursion

7 while i <= n do Designing recursive algorithms


Recursion and backtracking

8 factoN = factoN * i Recursion implementation in


C/C++

9 i=i+1 Complexity of
Algorithms
10 end Algorithm Efficiency

11 return factoN Asymptotic Analysis


Problems and common
complexities
12 End iterativeFactorial P and NP Problems

Basic concepts on DSA.21


Basic concepts
Recursion on DSA

Dept. Computer
Science

Factorial: Recursive Solution


1 Algorithm recursiveFactorial(n)
2 Calculates the factorial of a number using a recursion. DSA: Basic concepts
Some concepts on data

3 Pre: n is the number to be raised factorially Algorithm


Pseudocode
4 Post: n! is returned Recursion
Recursion and the basic
components of recursive
5 if n = 0 then algorithms
Properties of recursion
6 return 1 Designing recursive algorithms
Recursion and backtracking
7 else Recursion implementation in
C/C++

8 return n * recursiveFactorial(n-1) Complexity of


9 end Algorithms
Algorithm Efficiency

10 End recursiveFactorial Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.22


Basic concepts
Recursion on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

labelformat=empty
Hình: Calling a Recursive Algorithm (source: Data Structure - A Basic concepts on DSA.23
Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

Properties of recursion Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.24


Basic concepts
Properties of all recursive algorithms on DSA

Dept. Computer
Science

• A recursive algorithm solves the large


DSA: Basic concepts
problem by using its solution to a simpler Some concepts on data
Algorithm

sub-problem Pseudocode

Recursion
Recursion and the basic
components of recursive

• Eventually the sub-problem is simple algorithms


Properties of recursion
Designing recursive algorithms

enough that it can be solved without Recursion and backtracking


Recursion implementation in

applying the algorithm to it recursively. C/C++

Complexity of

→ This is called the base case. Algorithms


Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.25


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts

Designing recursive
Some concepts on data
Algorithm
Pseudocode

Recursion

algorithms Recursion and the basic


components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.26


Basic concepts
The Design Methodology on DSA

Dept. Computer
Science

Every recursive call must either solve a part of


the problem or reduce the size of the problem.
DSA: Basic concepts
Some concepts on data
Algorithm

Rules for designing a recursive algorithm Pseudocode

Recursion

1 Determine the base case (stopping case). Recursion and the basic
components of recursive
algorithms
Properties of recursion

2 Then determine the general case Designing recursive algorithms


Recursion and backtracking

(recursive case). Recursion implementation in


C/C++

Complexity of
3 Combine the base case and the general Algorithms
Algorithm Efficiency

cases into an algorithm. Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.27


Basic concepts
Limitations of Recursion on DSA

Dept. Computer
Science

• A recursive algorithm generally runs more DSA: Basic concepts


Some concepts on data

slowly than its nonrecursive Algorithm


Pseudocode

implementation. Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms

• BUT, the recursive solution shorter and Recursion and backtracking


Recursion implementation in
C/C++

more understandable. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.28


Basic concepts
Print List in Reverse on DSA

Dept. Computer
Science

93 19 8 26 DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
26 8 19 93 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.29


Basic concepts
Print List in Reverse on DSA

Dept. Computer
Science

93 19 8 26 DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
26 8 19 93 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.30


Basic concepts
Print List in Reverse on DSA

Dept. Computer
Science

1 Algorithm printReverse(list)
2 Prints a linked list in reverse. DSA: Basic concepts
Some concepts on data
3 Pre: list has been built Algorithm
Pseudocode
4 Post: list printed in reverse
Recursion
Recursion and the basic
components of recursive
5 if list is null then algorithms
Properties of recursion
6 return Designing recursive algorithms
Recursion and backtracking
7 end Recursion implementation in
C/C++
8 printReverse (list -> next) Complexity of
9 print (list -> data) Algorithms
Algorithm Efficiency
10 End printReverse Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.31


Basic concepts
Greatest Common Divisor on DSA

Dept. Computer
Science

Definition
DSA: Basic concepts
" a if b = 0 Some concepts on data
Algorithm
gcd(a, b) = b if a = 0 Pseudocode

gcd(b, a mod b) otherwise Recursion


Recursion and the basic
components of recursive
algorithms

Example Properties of recursion


Designing recursive algorithms
Recursion and backtracking
gcd(12, 18) = 6 Recursion implementation in
C/C++

gcd(5, 20) = 5 Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.32


Basic concepts
Greatest Common Divisor on DSA

Dept. Computer
Science

1 Algorithm gcd(a, b)
2 Calculates greatest common divisor using the Euclidean
algorithm. DSA: Basic concepts

3 Pre: a and b are integers Some concepts on data


Algorithm

4 Post: greatest common divisor returned Pseudocode

Recursion
Recursion and the basic

5 if b = 0 then components of recursive


algorithms

6 return a Properties of recursion


Designing recursive algorithms

7 end Recursion and backtracking


Recursion implementation in

8 if a = 0 then C/C++

Complexity of
9 return b Algorithms

10 end Algorithm Efficiency


Asymptotic Analysis

11 return gcd(b, a mod b) Problems and common


complexities

12 End gcd P and NP Problems

Basic concepts on DSA.33


Basic concepts
Fibonacci Numbers on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data

Definition Algorithm
Pseudocode

0 if n = 0
"
Recursion
F ibo(n) = 1 if n = 1 Recursion and the basic
components of recursive

F ibo(n − 1) + F ibo(n − 2) otherwise algorithms


Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.34


Basic concepts
Fibonacci Numbers on DSA

Dept. Computer
Science

Fibo(n)
DSA: Basic concepts
Some concepts on data
Algorithm
Pseudocode

Fibo(n-1) + Fibo(n-2) Recursion


Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Fibo(n-2) + Fibo(n-3) + Fibo(n-4) Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Fibo(n-3) + Fibo(n-4) Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.35


Basic concepts
Fibonacci Numbers on DSA

Dept. Computer
Science

Fibo(4) 3

DSA: Basic concepts


Fibo(3) 2 + Fibo(2) 1 Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic

Fibo(2) 1 + Fibo(1) 1 + Fibo(0) 0 components of recursive


algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++
Fibo(1) 1 + Fibo(0) 0 Complexity of
Algorithms
Algorithm Efficiency

Result Asymptotic Analysis


Problems and common
complexities
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... P and NP Problems

Basic concepts on DSA.36


Basic concepts
Fibonacci Numbers on DSA

Dept. Computer
Science

1 Algorithm Fibo(n)
DSA: Basic concepts
2 Calculates the nth Fibonacci number. Some concepts on data
Algorithm
3 Pre: n is postive integer Pseudocode

4 Post: the nth Fibonnacci number returned Recursion


Recursion and the basic
components of recursive
algorithms
5 if n = 0 or n = 1 then Properties of recursion
Designing recursive algorithms
6 return n Recursion and backtracking
Recursion implementation in
7 end C/C++

8 return Fibo(n-1) + Fibo(n-2) Complexity of


Algorithms
9 End fib Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.37


Basic concepts
Fibonacci Numbers on DSA

Dept. Computer
Science

No Calls Time No Calls Time


1 1 < 1 sec. 11 287 < 1 sec.
DSA: Basic concepts
2 3 < 1 sec. 12 465 < 1 sec. Some concepts on data

3 5 < 1 sec. 13 753 < 1 sec. Algorithm


Pseudocode

4 9 < 1 sec. 14 1,219 < 1 sec. Recursion


5 15 < 1 sec. 15 1,973 < 1 sec. Recursion and the basic
components of recursive
algorithms
6 25 < 1 sec. 20 21,891 < 1 sec. Properties of recursion
Designing recursive algorithms
7 41 < 1 sec. 25 242,785 1 sec. Recursion and backtracking
Recursion implementation in
8 67 < 1 sec. 30 2,692,573 7 sec. C/C++

9 109 < 1 sec. 35 29,860,703 1 min. Complexity of


Algorithms
10 177 < 1 sec. 40 331,160,281 13 min. Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.38


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science
Move disks from Source to Destination using Auxiliary:
1 Only one disk could be moved at a time.
2 A larger disk must never be stacked above a smaller
one. DSA: Basic concepts
Some concepts on data
3 Only one auxiliary needle could be used for the Algorithm
Pseudocode
intermediate storage of disks. Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
1 Algorithms
2 Algorithm Efficiency
Asymptotic Analysis

3 Problems and common


complexities
P and NP Problems

Source Auxiliary Destination

Basic concepts on DSA.39


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

2 Recursion
Recursion and the basic
3 1 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 1 to pole 3. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.40


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
3 2 1 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 1 to pole 2. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.41


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

1 Recursion
Recursion and the basic
3 2 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 3 to pole 2. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.42


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

1 Recursion
Recursion and the basic
2 3 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 1 to pole 3. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.43


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
1 2 3 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 2 to pole 1. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.44


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

2 Recursion
Recursion and the basic
1 3 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 2 to pole 3. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.45


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
1 Pseudocode

2 Recursion
Recursion and the basic
3 components of recursive
algorithms
Properties of recursion

Source Auxiliary Destination Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Moved disc from pole 1 to pole 3. Complexity of


Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.46


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

move(3,
A, C, B)

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
move(2, move(1, move(2, components of recursive
A, B, C) A, C, B) B, C, A) algorithms
Properties of recursion

A -> C Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis

move(1, move(1, move(1, move(1, move(1, move(1, Problems and common


complexities
A, C, B) A, B, C) C, B, A) B, A, C) B, C, A) A, C, B) P and NP Problems

A -> C A -> B C -> B B -> A B -> C A -> C

Basic concepts on DSA.47


Basic concepts
The Towers of Hanoi : General on DSA

Dept. Computer
Science

move(n, A, C, B)
DSA: Basic concepts
Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion

move(n-1, A, B, C) move(1, A, C, B) move(n-1, B, C, A) Designing recursive algorithms


Recursion and backtracking
Recursion implementation in
C/C++

Complexity Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis

T (n) = 1 + 2T (n − 1) Problems and common


complexities
P and NP Problems

Basic concepts on DSA.48


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

Complexity

T (n) = 1 + 2T (n − 1)
DSA: Basic concepts
=> T (n) = 1 + 2 + 22 + ... + 2n−1 Some concepts on data
Algorithm

=> T (n) = 2n − 1 Pseudocode

Recursion
=> T (n) = O(2n ) Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms

• With 64 disks, total number of moves: Recursion and backtracking


Recursion implementation in
C/C++

264 − 1 ≈ 24 × 260 ≈ 24 × 1018 = 1.6 × 1019 Complexity of


Algorithms
• If one move takes 1s, 264 moves take Algorithm Efficiency
Asymptotic Analysis

about 5 × 1011 years (500 billions years). Problems and common


complexities
P and NP Problems

Basic concepts on DSA.49


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science
1 Algorithm move(val disks <integer>, val source
<character>, val destination <character>, val
auxiliary <character>)
2 Move disks from source to destination. DSA: Basic concepts
Some concepts on data
3 Pre: disks is the number of disks to be moved Algorithm

4 Post: steps for moves printed Pseudocode

Recursion
5 print("Towers: ", disks, source, destination, auxiliary) Recursion and the basic
components of recursive
6 if disks = 1 then algorithms
Properties of recursion
7 print ("Move from", source, "to", destination) Designing recursive algorithms
Recursion and backtracking
8 else Recursion implementation in
C/C++
9 move(disks - 1, source, auxiliary, destination) Complexity of
10 move(1, source, destination, auxiliary) Algorithms
Algorithm Efficiency

11 move(disks - 1, auxiliary, destination, source) Asymptotic Analysis


Problems and common
12 end complexities
P and NP Problems

13 return
14 End move
Basic concepts on DSA.50
Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts

Recursion and
Some concepts on data
Algorithm
Pseudocode

Recursion

backtracking Recursion and the basic


components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.51


Basic concepts
Backtracking on DSA

Dept. Computer
Science

Definition
DSA: Basic concepts
A process to go back to previous steps to try unexplored Some concepts on data
Algorithm
alternatives. Pseudocode

Recursion
Recursion and the basic
components of recursive

backtracking.jpg algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
Hình: Goal seeking C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.52


Basic concepts
Eight Queens Problem on DSA

Dept. Computer
Science

Place eight queens on the chess board in such a way that


no queen can capture another.
DSA: Basic concepts
Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.53


Basic concepts
Eight Queens Problem on DSA

Dept. Computer
Science
1 Algorithm putQueen(ref board <array>,
val r <integer>)
2 Place remaining queens safely from a row DSA: Basic concepts

of a chess board. Some concepts on data


Algorithm
Pseudocode

Recursion
3 Pre: board is nxn array representing a Recursion and the basic
components of recursive
algorithms

chess board Properties of recursion


Designing recursive algorithms

4 r is the row to place queens onwards Recursion and backtracking


Recursion implementation in
C/C++

Complexity of
Algorithms
5 Post: all the remaining queens are safely Algorithm Efficiency
Asymptotic Analysis

placed on the board; or backtracking to Problems and common


complexities
P and NP Problems

the previous rows is required


Basic concepts on DSA.54
Basic concepts
Eight Queens Problem on DSA

1 for every column c on the same row r do Dept. Computer


Science

2 if cell r,c is safe then


3 place the next queen in cell r,c
4 if r < n-1 then DSA: Basic concepts
Some concepts on data

5 putQueen (board, r + 1) Algorithm


Pseudocode

6 else Recursion
Recursion and the basic

7 output successful placement components of recursive


algorithms
Properties of recursion

8 end Designing recursive algorithms


Recursion and backtracking
Recursion implementation in

9 remove the queen from cell r,c C/C++

Complexity of
10 end Algorithms
Algorithm Efficiency

end
Asymptotic Analysis
11 Problems and common
complexities

12 return P and NP Problems

13 End putQueen
Basic concepts on DSA.55
Basic concepts
Eight Queens Problem on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.56


Basic concepts
on DSA

Dept. Computer
Science

Recursion DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

implementation in Recursion
Recursion and the basic
components of recursive
algorithms

C/C++ Properties of recursion


Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.57


Basic concepts
Fibonacci Numbers on DSA

#i n c l u d e <i o s t r e a m > Dept. Computer


Science
u s i n g namespace s t d ;

long f i b ( long num ) ;

DSA: Basic concepts


i n t main ( ) { Some concepts on data
i n t num ; Algorithm
Pseudocode
c o u t << "What␣ F i b o n a c c i ␣ number
Recursion
␣␣␣ ␣␣␣␣␣␣ ␣ do ␣ you ␣ want ␣ t o ␣ c a l c u l a t e ? ␣ " ; Recursion and the basic

c i n >> num ; components of recursive


algorithms

c o u t << "The␣ " << num << " t h ␣ F i b o n a c c i ␣ number Properties of recursion
Designing recursive algorithms

␣␣␣ ␣␣␣␣␣␣ ␣ i s : ␣" << f i b (num) << e n d l ; Recursion and backtracking


Recursion implementation in
return 0; C/C++

} Complexity of
Algorithms
Algorithm Efficiency

long f i b ( long num) { Asymptotic Analysis


Problems and common
i f (num == 0 | | num == 1 ) complexities
P and NP Problems
r e t u r n num ;
r e t u r n f i b ( num−1) + f i b ( num−2);
} Basic concepts on DSA.58
Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;

DSA: Basic concepts


v o i d move ( i n t n , char s o u r c e , Some concepts on data

char d e s t i n a t i o n , char a u x i l i a r y ) ; Algorithm


Pseudocode

Recursion
i n t main ( ) { Recursion and the basic
components of recursive
algorithms
i n t numDisks ; Properties of recursion
Designing recursive algorithms
c o u t << " P l e a s e ␣ e n t e r ␣ number ␣ o f ␣ d i s k s : ␣ " ; Recursion and backtracking

c i n >> numDisks ; Recursion implementation in


C/C++

c o u t << " S t a r t ␣ Towers ␣ o f ␣ Hanoi " << e n d l ; Complexity of


Algorithms
move ( numDisks , ’A ’ , ’C ’ , ’B ’ ) ; Algorithm Efficiency
Asymptotic Analysis
return 0; Problems and common
complexities
} P and NP Problems

Basic concepts on DSA.59


Basic concepts
The Towers of Hanoi on DSA

Dept. Computer
Science

v o i d move ( i n t n , char s o u r c e ,
char d e s t i n a t i o n , char a u x i l i a r y ) {
static int step = 0; DSA: Basic concepts
Some concepts on data
Algorithm
i f ( n == 1 ) Pseudocode

c o u t << " S t e p ␣ " << ++s t e p << " : ␣Move␣ from ␣ " Recursion
<< s o u r c e << " ␣ t o ␣ " << d e s t i n a t i o n << e n d l Recursion
; and the basic
components of recursive

else { algorithms
Properties of recursion
move ( n −1, s o u r c e , a u x i l i a r y , d e s t i n a t i o n ) ; Designing recursive algorithms
Recursion and backtracking
move ( 1 , s o u r c e , d e s t i n a t i o n , a u x i l i a r y ) ; Recursion implementation in

move ( n − 1 , a u x i l i a r y , d e s t i n a t i o n , s o u r c e ) ; C/C++
Complexity of
} Algorithms
return ; Algorithm Efficiency
Asymptotic Analysis
} Problems and common
complexities
P and NP Problems

Basic concepts on DSA.60


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

Algorithm Efficiency Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.61


Basic concepts
Compare Algorithms on DSA

Dept. Computer
Science

• Given 2 or more algorithms to solve the


same problem, how do we select the best
DSA: Basic concepts
one? Some concepts on data

• Some criteria for selecting an algorithm Algorithm


Pseudocode

• Is it easy to implement, understand, modify? Recursion


Recursion and the basic
• How long does it take to run it to completion? components of recursive
algorithms
• How much of computer memory does it use? Properties of recursion
Designing recursive algorithms

• Software engineering is primarily Recursion and backtracking


Recursion implementation in
C/C++

concerned with the first criteria Complexity of


Algorithms

• In this course we are interested in the Algorithm Efficiency


Asymptotic Analysis
Problems and common

second and third criteria complexities


P and NP Problems

Basic concepts on DSA.62


Basic concepts
Algorithm Efficiency on DSA

Dept. Computer
Science

• Time complexity: the amount of time


that an algorithm needs to run to
completion. DSA: Basic concepts
Some concepts on data

• Space complexity: the amount of Algorithm


Pseudocode

memory an algorithm needs to run. Recursion


Recursion and the basic
components of recursive

• We will occasionally look at space algorithms


Properties of recursion
Designing recursive algorithms

complexity, but we are mostly interested Recursion and backtracking


Recursion implementation in

in time complexity in this course. C/C++

Complexity of
Algorithms
• Thus in this course the better algorithm is Algorithm Efficiency
Asymptotic Analysis

the one which runs faster (has smaller Problems and common
complexities
P and NP Problems

time complexity).
Basic concepts on DSA.63
Basic concepts
How to Calculate Running time on DSA

• Most algorithms transform input objects Dept. Computer


Science

into output objects.


sorting
5 3 1 2 1 2 3 5
algorithm
input object output object DSA: Basic concepts
Some concepts on data
Algorithm

• The running time of an algorithm typically Pseudocode

Recursion
grows with the input size f (n). Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
9000
Recursion and backtracking
8000
Recursion implementation in
7000 C/C++

6000 Complexity of
Time (ms)

5000 Algorithms
4000 Algorithm Efficiency
Asymptotic Analysis
3000
Problems and common
2000 complexities
P and NP Problems
1000
0
0 20 40 60 80 100
Input Size
Basic concepts on DSA.64
Basic concepts
How to Calculate Running Time on DSA

Dept. Computer
Science

• Even on inputs of the same size,


running time can be very different.
DSA: Basic concepts
• Example: algorithm that searches an array Some concepts on data

containing n integers to find the one with a Algorithm


Pseudocode

particular value K (assume that K appears Recursion


Recursion and the basic
exactly once in the array) components of recursive
algorithms

• Idea: analyze running time in the Properties of recursion


Designing recursive algorithms
Recursion and backtracking

• best case Recursion implementation in


C/C++

• worst case Complexity of


Algorithms
Algorithm Efficiency

• average case Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.65


Basic concepts
How to Calculate Running Time on DSA

Dept. Computer
Science

• Best case running time is usually


useless DSA: Basic concepts
Some concepts on data

• Average case time is very useful but Algorithm


Pseudocode

Recursion
often difficult to determine Recursion and the basic
components of recursive
algorithms

• We focus on the worst case running


Properties of recursion
Designing recursive algorithms
Recursion and backtracking

time Recursion implementation in


C/C++

• Easier to analyze Complexity of


Algorithms
Algorithm Efficiency

• Crucial to applications such as games, Asymptotic Analysis


Problems and common
complexities

finance and robotics P and NP Problems

Basic concepts on DSA.66


Basic concepts
Evaluations of Running Time on DSA

Dept. Computer
Science

DSA: Basic concepts

• There are two ways to compare Some concepts on data


Algorithm

running time between agorithms: Pseudocode

Recursion

• Experimental evaluation Recursion and the basic


components of recursive
algorithms

• Theoretical evaluation Properties of recursion


Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.67


Basic concepts
Experimental Evaluation of Running Time on DSA

Dept. Computer
Science

• Write a program 9000


DSA: Basic concepts
implementing the 8000
Some concepts on data

7000 Algorithm
algorithm Pseudocode
6000

Time (ms)
• Run the program with 5000
Recursion
Recursion and the basic

inputs of varying size 4000


components of recursive
algorithms
Properties of recursion
and composition 3000
Designing recursive algorithms

2000 Recursion and backtracking


• Measure accurately the 1000
Recursion implementation in
C/C++

actual running time 0 Complexity of


0 50 100 Algorithms
• Plot the results Input Size Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.68


Basic concepts
Limitations of Experiments on DSA

Dept. Computer
Science

• It is necessary to implement the


algorithm, which may be difficult DSA: Basic concepts
Some concepts on data
Algorithm

• Results may not be indicative of the Pseudocode

Recursion
running time on other inputs not included Recursion and the basic
components of recursive
algorithms

in the experiment Properties of recursion


Designing recursive algorithms

• In order to compare two algorithms, the Recursion and backtracking


Recursion implementation in
C/C++

same hardware and software environments Complexity of


Algorithms
must be used Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.69


Basic concepts
Theoretical Analysis of Running Time on DSA

Dept. Computer

• Uses a pseudo-code description of Science

the algorithm instead of an


implementation DSA: Basic concepts
Some concepts on data

• Characterizes running time as a


Algorithm
Pseudocode

Recursion

function of the input size, n Recursion and the basic


components of recursive
algorithms
Properties of recursion

• Takes into account all possible Designing recursive algorithms


Recursion and backtracking
Recursion implementation in

inputs C/C++

Complexity of
Algorithms

• Allows us to evaluate the speed of Algorithm Efficiency


Asymptotic Analysis
Problems and common

an algorithm independent of the complexities


P and NP Problems

hardware/software environment
Basic concepts on DSA.70
Basic concepts
Primitive Operations on DSA

Dept. Computer
Science
• For theoretical analysis, we will
count primitive or basic
operations, which are simple DSA: Basic concepts
Some concepts on data
Algorithm

computations performed by an Pseudocode

Recursion

algorithm Recursion and the basic


components of recursive
algorithms

• Example: Properties of recursion


Designing recursive algorithms
Recursion and backtracking

• Evaluating an expression: x2 + 3x Recursion implementation in


C/C++

• Assigning a value to a variable: x = y Complexity of


Algorithms


Algorithm Efficiency

Indexing into an array: a[10] Asymptotic Analysis


Problems and common

• Calling a function: mySwap(a, b)


complexities
P and NP Problems

• Returing from a function: return(x)


Basic concepts on DSA.71
Basic concepts
Counting Primitive Operations on DSA

Dept. Computer
Science

• By inspecting the pseudocode, we can determine the


maximum number of primitive operations executed by
an algorithm, as a function of the input size DSA: Basic concepts
Some concepts on data
Algorithm

Algorithm findMax(a, n) Pseudocode

Recursion
Recursion and the basic
currentMax = a [0] // 2 components of recursive
algorithms
i = 1 // 2 Properties of recursion
Designing recursive algorithms
while ( i < n ) // n Recursion and backtracking
if ( currentMax < a [ i ]) // 2n -2 Recursion implementation in
C/C++
currentMax = a [ i ] // 2n -2
Complexity of
i = i + 1 // 2n -2 Algorithms
return currentMax // 1 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.72


Basic concepts
Estimating Running Time on DSA

Dept. Computer
Science

• Algorithm findMax() executes 7n − 1


primitive operations in the worst case.
Define: DSA: Basic concepts
• a = Time taken by the fastest primitive Some concepts on data
Algorithm

operation Pseudocode

Recursion
• b = Time taken by the slowest primitive Recursion and the basic
components of recursive
operation algorithms
Properties of recursion

• Let f(n) be worst-case time of arrayMax.


Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

Then a(7n − 1) ≤ f (n) ≤ b(7n − 1)


C/C++

Complexity of
Algorithms
• Hence, the running time f(n) is bounded Algorithm Efficiency
Asymptotic Analysis

by two linear functions Problems and common


complexities
P and NP Problems

Basic concepts on DSA.73


Basic concepts
Growth Rate of Running Time on DSA

Dept. Computer
Science

• Changing the hardware/software


environment
DSA: Basic concepts
• Affects f (n) by a constant factor, but Some concepts on data

• Does not alter the growth rate of f (n) Algorithm


Pseudocode

Recursion
• Thus we focus on the big-picture which is Recursion and the basic
components of recursive
algorithms

the growth rate of an algorithm Properties of recursion


Designing recursive algorithms

• The linear growth rate of the running Recursion and backtracking


Recursion implementation in
C/C++

time f (n) is an intrinsic property of Complexity of


Algorithms
algorithm findMax() Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.74


Basic concepts
Linear Loops on DSA

Dept. Computer
Science

for ( i = 0; i < n ; i ++)


application code

The number of times the body of the loop is replicated is DSA: Basic concepts
n. Some concepts on data
Algorithm
Pseudocode

f(n) = n Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
for ( i = 0; i < n ; i += 2) C/C++

application code Complexity of


Algorithms

The number of times the body of the loop is replicated is Algorithm Efficiency
Asymptotic Analysis

n/2. Problems and common


complexities
P and NP Problems

f(n) = n/2
Basic concepts on DSA.75
Basic concepts
Logarithmic Loops on DSA

Dept. Computer
Science
Multiply loops

i = 1
while ( i <= n )
application code DSA: Basic concepts
i = i x 2 Some concepts on data
Algorithm
Pseudocode

Recursion
Divide loops Recursion and the basic
components of recursive
algorithms
Properties of recursion
i = n Designing recursive algorithms

while ( i >= 1) Recursion and backtracking


Recursion implementation in
application code C/C++

i = i / 2 Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
The number of times the body of the loop is replicated is Problems and common
complexities
P and NP Problems

f(n) = log2 n

Basic concepts on DSA.76


Basic concepts
Nested Loops on DSA

Dept. Computer
Science

Iterations = Outer loop iterations × Inner loop iterations

Example DSA: Basic concepts


Some concepts on data
Algorithm

i = 1 Pseudocode

while ( i <= n ) Recursion


Recursion and the basic
j = 1 components of recursive
algorithms
while ( j <= n ) Properties of recursion

application code Designing recursive algorithms


Recursion and backtracking
j = j * 2 Recursion implementation in
C/C++
i = i + 1
Complexity of
Algorithms
Algorithm Efficiency
The number of times the body of the loop is replicated is Asymptotic Analysis
Problems and common
complexities

f (n) = n log2 n P and NP Problems

Basic concepts on DSA.77


Basic concepts
Quadratic Loops on DSA

Dept. Computer
Science

Example

i = 1 DSA: Basic concepts


while ( i <= n ) Some concepts on data
Algorithm
j = 1 Pseudocode

while ( j <= n ) Recursion


application code Recursion and the basic
components of recursive
j = j + 1 algorithms
Properties of recursion
i = i + 1 Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++
The number of times the body of the loop is replicated is Complexity of
Algorithms
f (n) = n2 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.78


Basic concepts
Dependent Quadratic Loops on DSA

Dept. Computer
Science

Example

i = 1 DSA: Basic concepts


while ( i <= n ) Some concepts on data
Algorithm
j = 1 Pseudocode

while ( j <= i ) Recursion


application code Recursion and the basic
components of recursive
j = j + 1 algorithms
Properties of recursion
i = i + 1 Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++
The number of times the body of the loop is replicated is Complexity of
Algorithms
1 + 2 + . . . + n = n(n + 1)/2 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.79


Basic concepts
Constant Factors on DSA

Dept. Computer
Science

• The growth rate is not affected by


• constant factors or
• lower-order terms DSA: Basic concepts
Some concepts on data

• Examples Algorithm
Pseudocode

Recursion
• 103 n + 105 is a linear function Recursion and the basic
components of recursive

• 10n2 + 104 n is a quadratic function algorithms


Properties of recursion
Designing recursive algorithms

• How do we get rid of the constant


Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
factors to focus on the essential part Algorithms
Algorithm Efficiency

of the running time? Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.80


Basic concepts
Asymptotic Analysis on DSA

Dept. Computer
Science

• Algorithm efficiency is considered


with only big problem sizes. DSA: Basic concepts
Some concepts on data

• We are not concerned with an exact Algorithm


Pseudocode

Recursion
measurement of an algorithm’s Recursion and the basic
components of recursive
algorithms

efficiency. Properties of recursion


Designing recursive algorithms
Recursion and backtracking

• Terms that do not substantially


Recursion implementation in
C/C++

Complexity of
Algorithms
change the function’s magnitude are Algorithm Efficiency
Asymptotic Analysis

eliminated. Problems and common


complexities
P and NP Problems

Basic concepts on DSA.81


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

Asymptotic Analysis Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.82


Basic concepts
Asymptotic Notations on DSA

Dept. Computer
Science

There are three common notations for DSA: Basic concepts


Some concepts on data

asymptotic analysis: Algorithm


Pseudocode

Recursion
• Big-Oh: O(.) Recursion and the basic
components of recursive
algorithms

• Big-Omega: Ω(.)
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

• Big-Theta: Θ(.) C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.83


Basic concepts
Big-Oh notation on DSA

Dept. Computer
Science

• The big-Oh notation is used widely


to characterize running times and DSA: Basic concepts
Some concepts on data

space bounds Algorithm


Pseudocode

Recursion
• The big-Oh notation allows us to Recursion and the basic
components of recursive
algorithms

ignore constant factors and lower Properties of recursion


Designing recursive algorithms
Recursion and backtracking

order terms and focus on the main Recursion implementation in


C/C++

Complexity of

components of a function which Algorithms


Algorithm Efficiency
Asymptotic Analysis

affect its growth Problems and common


complexities
P and NP Problems

Basic concepts on DSA.84


Basic concepts
Big-Oh notation on DSA

Dept. Computer
Science

• Given functions f (n) and g(n), we DSA: Basic concepts

say that f (n) is O(g(n)) if there are Some concepts on data


Algorithm
Pseudocode

positive constants c and n0 such Recursion


Recursion and the basic
components of recursive

that: f (n) ≤ c.g(n) for n ≥ n0 algorithms


Properties of recursion
Designing recursive algorithms

• Example: 2n + 10 is O(n) Recursion and backtracking


Recursion implementation in
C/C++

Complexity of
• Why? Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.85


Basic concepts
Big-Oh Rules on DSA

Dept. Computer
Science
Example
f (n) = c.n ⇒ f (n) = O(n)
f (n) = n(n + 1)/2 = n2 /2 + n/2 ⇒ f (n) = O(n2 )
DSA: Basic concepts
Some concepts on data

• Set the coefficient of the term to Algorithm


Pseudocode

Recursion
one. Recursion and the basic
components of recursive
algorithms

• Keep the largest term and discard Properties of recursion


Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

the others. C/C++

Complexity of
Algorithms
Some example of Big-O: Algorithm Efficiency
Asymptotic Analysis

1 log2 n n n log2 n n2 ... nk ... 2n


Problems and common
complexities
P and NP Problems

n!
Basic concepts on DSA.86
Basic concepts
Big-Oh and Growth Rate on DSA

Dept. Computer

• The big-Oh notation gives an upper Science

bound on the growth rate of a


function DSA: Basic concepts
Some concepts on data
Algorithm

• The statement “f (n) is O(g(n))” Pseudocode

Recursion

means that the growth rate of f (n) Recursion and the basic
components of recursive
algorithms
Properties of recursion

is no more than the growth rate of Designing recursive algorithms


Recursion and backtracking
Recursion implementation in

g(n) C/C++

Complexity of
Algorithms
• We can use the big-Oh notation to Algorithm Efficiency
Asymptotic Analysis
Problems and common

rank functions according to their complexities


P and NP Problems

growth rate
Basic concepts on DSA.87
Basic concepts
Standard Measures of Efficiency on DSA

Dept. Computer
Science

Efficiency Big-O Iterations Est. Time


logarithmic O(log2 n) 14 microseconds
linear O(n) 10 000 0.1 seconds DSA: Basic concepts
Some concepts on data

linear log O(n log2 n) 140 000 2 seconds Algorithm


Pseudocode

quadratic O(n2 ) 100002 15-20 min. Recursion


polynomial O(nk ) 10000k hours Recursion and the basic
components of recursive
algorithms
exponential O(2n ) 210000 intractable Properties of recursion
Designing recursive algorithms
factorial O(n!) 10000! intractable Recursion and backtracking
Recursion implementation in
C/C++

Assume instruction speed of 1 microsecond and 10 Complexity of


Algorithms
instructions in loop. Algorithm Efficiency

n = 10000 Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.88


Basic concepts
Standard Measures of Efficiency on DSA

time n2 n log n Dept. Computer


2 Science
n

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
log2 n Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

n Basic concepts on DSA.89


Basic concepts
Relatives of Big-Oh on DSA

Dept. Computer
Science

• big-Omega
• f (n) is Ω(g(n)) if there is a constant c > 0 DSA: Basic concepts
and an integer constant n0 ≥ 1 such that Some concepts on data
Algorithm

f (n) ≥ c.g(n) for n ≥ n0 Pseudocode

Recursion
Recursion and the basic

• big-Theta
components of recursive
algorithms
Properties of recursion

• f (n) is Θ(g(n)) if there are constants c > 0


0 Designing recursive algorithms
Recursion and backtracking

and c00 > 0 and an integer constant n0 ≥ 1 Recursion implementation in


C/C++

such that c0 .g(n) ≥ f (n) ≥ c00 .g(n) for Complexity of


Algorithms
n ≥ n0 Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.90


Basic concepts
Intuition for Asymptotic Notation on DSA

Dept. Computer
Science

• Big-Oh: f (n) is O(g(n)) if f (n) is


asymptotically less than or equal to DSA: Basic concepts
Some concepts on data

g(n) Algorithm
Pseudocode

• Big-Omega: f (n) is Ω(g(n)) if f (n) Recursion


Recursion and the basic
components of recursive
algorithms

is asymptotically greater than or Properties of recursion


Designing recursive algorithms
Recursion and backtracking

equal to g(n) Recursion implementation in


C/C++

Complexity of

• Big-Theta: f (n) is Θ(g(n)) if f (n) Algorithms


Algorithm Efficiency
Asymptotic Analysis

is asymptotically equal to g(n) Problems and common


complexities
P and NP Problems

Basic concepts on DSA.91


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts

Problems and common


Some concepts on data
Algorithm
Pseudocode

Recursion

complexities Recursion and the basic


components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.92


Basic concepts
Binary search on DSA

Dept. Computer
Science

Recurrence Equation (Phương trình hồi quy)


DSA: Basic concepts
An equation or inequality that describes a Some concepts on data
Algorithm

function in terms of its value on smaller input. Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking

1 2 3 5 8 13 21 34 55 89 Recursion implementation in
C/C++

Complexity of
Algorithms
T (n) = 1 + T (n/2) ⇒ T (n) = O(log2 n) Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.93


Basic concepts
Binary search on DSA

Dept. Computer
Science

• Best case: when the number of steps DSA: Basic concepts

is smallest. T (n) = O(1) Some concepts on data


Algorithm
Pseudocode

• Worst case: when the number of Recursion


Recursion and the basic
components of recursive
algorithms

steps is largest. T (n) = O(log2 n) Properties of recursion


Designing recursive algorithms
Recursion and backtracking

• Average case: in between. Recursion implementation in


C/C++

Complexity of
T (n) = O(log2 n) Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.94


Basic concepts
Sequential search on DSA

Dept. Computer
Science

8 5 21 2 1 13 4 34 7 18

DSA: Basic concepts


Some concepts on data
Algorithm

• Best case: T (n) = O(1) Pseudocode

Recursion

• Worst case: T (n) = O(n) Recursion and the basic


components of recursive
algorithms
Properties of recursion

• Average case: T (n) = ni=1 i.pi


P Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
pi : probability for theP
target being at a[i] C/C++

pi = 1/n ⇒ T (n) = ( ni=1 i)/n =


Complexity of
Algorithms
Algorithm Efficiency

O(n(n + 1)/2n) = O(n) Asymptotic Analysis


Problems and common
complexities
P and NP Problems

Basic concepts on DSA.95


Basic concepts
Quick sort on DSA

Dept. Computer
Science

19 8 3 15 28 10 22 4 12 83

DSA: Basic concepts


Some concepts on data
Algorithm
Recurrence Equation Pseudocode

Recursion
T (n) = O(n) + 2T (n/2) Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

• Best case: T (n) = O(n log2 n) C/C++

Complexity of

• Worst case: T (n) = O(n2 ) Algorithms


Algorithm Efficiency
Asymptotic Analysis

• Average case: T (n) = O(n log2 n) Problems and common


complexities
P and NP Problems

Basic concepts on DSA.96


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

P and NP Problems
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.97


Basic concepts
P and NP Problems on DSA

Dept. Computer
Science

• P: Polynomial (can be solved in poly-


DSA: Basic concepts
nomial time on a deterministic ma- Some concepts on data
Algorithm
Pseudocode

chine). Recursion
Recursion and the basic
components of recursive
algorithms

• NP: Nondeterministic Polynomial (can


Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in

be solved in polynomial time on a non- C/C++

Complexity of
Algorithms
deterministic machine). Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.98


Basic concepts
P and NP Problems on DSA

Dept. Computer
Travelling Salesman Problem: Science

A salesman has a list of cities, each of which he must visit


exactly once. There are direct roads between each pair of
cities on the list. DSA: Basic concepts
Find the route the salesman should follow for the shortest Some concepts on data
Algorithm

possible round trip that both starts and finishes at any one Pseudocode

of the cities. Recursion


Recursion and the basic
components of recursive
algorithms

8 Properties of recursion

b c Designing recursive algorithms


Recursion and backtracking

7 Recursion implementation in
C/C++

9 5 Complexity of
15 Algorithms
a d Algorithm Efficiency

6 9 Asymptotic Analysis
Problems and common
complexities
8 P and NP Problems
11
e f

Basic concepts on DSA.99


Basic concepts
P and NP Problems on DSA

Dept. Computer
Science

Travelling Salesman Problem:


Deterministic machine:
f (n) = n(n − 1)(n − 2) . . . 1 = O(n!) DSA: Basic concepts

⇒ NP problem Some concepts on data


Algorithm
Pseudocode

Recursion
8
b c Recursion and the basic
components of recursive
algorithms
7 Properties of recursion
Designing recursive algorithms
9 5 Recursion and backtracking
15 Recursion implementation in
a d C/C++

Complexity of
6 9 Algorithms
8 Algorithm Efficiency

11 Asymptotic Analysis

e f Problems and common


complexities
P and NP Problems

Basic concepts on DSA.100


Basic concepts
P and NP Problems on DSA

Dept. Computer
Science
NP-complete: NP and every other problem in NP is
polynomially reducible to it.

DSA: Basic concepts


Some concepts on data
Algorithm
Pseudocode
P
Recursion
Recursion and the basic
components of recursive

NP algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking

NP-complete Recursion implementation in


C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common

P = NP? complexities
P and NP Problems

Basic concepts on DSA.101


Basic concepts
on DSA

Dept. Computer
Science

DSA: Basic concepts


Some concepts on data
Algorithm

THANK YOU.
Pseudocode

Recursion
Recursion and the basic
components of recursive
algorithms
Properties of recursion
Designing recursive algorithms
Recursion and backtracking
Recursion implementation in
C/C++

Complexity of
Algorithms
Algorithm Efficiency
Asymptotic Analysis
Problems and common
complexities
P and NP Problems

Basic concepts on DSA.102

You might also like