0% found this document useful (0 votes)
9 views68 pages

AoA-Segment-1 Introduction

The document outlines the course structure for 'Design & Analysis of Algorithms' taught by Dr. Abdus Salam at Abasyn University, including grading policies, textbooks, and a detailed course outline. It emphasizes the importance of algorithms in computer science, providing definitions, examples, and the mathematical background necessary for understanding algorithms. Additionally, it discusses the historical significance of algorithms and includes practical applications and problem-solving techniques.

Uploaded by

fahadrehmann07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views68 pages

AoA-Segment-1 Introduction

The document outlines the course structure for 'Design & Analysis of Algorithms' taught by Dr. Abdus Salam at Abasyn University, including grading policies, textbooks, and a detailed course outline. It emphasizes the importance of algorithms in computer science, providing definitions, examples, and the mathematical background necessary for understanding algorithms. Additionally, it discusses the historical significance of algorithms and includes practical applications and problem-solving techniques.

Uploaded by

fahadrehmann07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Segment # 1

Design & Analysis of Algorithms


by
Dr. Abdus Salam
Head, Department of Computing & Technology
Abasyn University, Peshawar
Class Policy
• Grading
– 5 – 6 assignments (10 marks)
• Programming component
• On LMS only
– 3 – 4 quizzes (10 marks)
• On LMS only
– Mid-term exam (30 marks)
• Closed books, closed notes
– Final exam (50 marks)
• Closed books, closed notes
– Attendance and class participation

Abasyn University Peshawar 2


Text and Reference
• Textbook
• Introduction to Algorithms
– Thomas H. Cormen, Charles E. Leiserson,
Ronald L. Rivest and Clifford Stein, 2nd Ed
• Algorithm Analysis & Design
– Harsh Bhasin, Oxford University Press, 2015
• Reference Book
• Computer Algorithms Introduction to Design &
Analysis
– Sara Baase, 3rd Edition

Abasyn University Peshawar 3


Course Outline
• Following are the topics that will be discussed, listed in the
approximate order in which they will be covered.
– Introduction
– Background, Review of Necessary Mathematics, Analyzing Algorithms
– Complexity Analysis
– Insertion Sort, Bubble Sort, Selection Sort
– Asymptotic Notations
– Growth of Functions
– Recurrences
– Analysis of Recursive Algorithms
– Design Paradigms
• Divide and conquer Algorithms
• Graph Algorithms
• Greedy Algorithms
• Dynamic Programming

Abasyn University Peshawar 4


Overview
• Let’s see how to solve a computer science problem
– Take a telephone directory
– You are looking to find the right page to find your friend’s telephone
number
• First approach
– Start looking from page 1, 2, 3 and so on until you find the required
number.
– Is it efficient? Why?
• Second approach
– Flip to the middle and tear the book into two
– Throw one part and continue with other part

Abasyn University Peshawar 5


How to count the number of people in a room?

1. Everyone, please standup in your place


2. Think you have a number “1”
3. Pair off with someone standing
4. Each pair shall add their numbers together
5. Take on the sum as your new number
6. One of a pair should sit down
7. Go to step 3
8. At the end there will be only ONE student left standing. Give me
your number.

Abasyn University Peshawar 6


Algorithms: Real Life Examples
• Many Real-Life Examples
– Cooking: Recipe for preparing a dish
– Origami: The Japanese art of paper folding
– Directions: How to go to Peshawar airport

Abasyn University Peshawar 7


Why Study Algorithms?
• Necessary in any computer programming problem
– Improve algorithm efficiency: run faster, process more data, do
something that would otherwise be impossible
– Solve problems of significantly large sizes
– Technology only improves things by a constant factor
• Compare algorithms
• Algorithms as a field of study
– Learn about a standard set of algorithms
– New discoveries arise
– Numerous application areas
• Learn techniques of algorithm design and analysis

Abasyn University Peshawar 8


MUHAMMAD BIN MUSA AL-KHWARIZMI
(Algorizm) (770 - 840 C.E.)
• Abu Abdullah Muhammad Ibn Musa al-Khwarizmi was born
at Khwarizm (Kheva). It has been established from his
contributions that he flourished under Khalifah (Calif) Al-
Mamun at Baghdad during 813 to 833 C.E. and died around
840 C.E. He is best known for introducing the mathematical
concept Algorithm, which is so named after his last name.
• Al-Khwarizmi was one of the greatest mathematicians ever
lived. He was the founder of several branches and basic
concepts of mathematics.
– Algebra, as he not only initiated the subject in a systematic
form but also developed it to the extent of giving analytical
solutions of linear and quadratic equations. The name Algebra
is derived from his famous book Al-Jabr wa-al-Muqabilah.
– He developed in detail trigonometric tables containing the sine
functions, which were later extrapolated to tangent functions.
– Al-Khwarizmi also developed the calculus of two errors, which
led him to the concept of differentiation. He also refined the
geometric representation of conic sections
• He adopted the use of zero, a numeral of fundamental
importance, leading up to the so-called arithmetic of
positions and the decimal system. His pioneering work on
the system of numerals is well known as "Algorithm," or
"Algorizm."
Abasyn University Peshawar 9
Algorithms
• A tool for solving a well-specified computational
problem

Input Algorithm Output


Example: sorting
input: A sequence of number
output: An ordered permutation of input
issues: correctness, efficiency, storage, etc.
Computer Program = Algorithm + Data Structures

Data Structure: An organized method of storing and


retrieving data.
Abasyn University Peshawar 10
What is an Algorithm?
 A sequence of computational step that transform the input
into the output.

A general step-by-step procedure for producing the solution to


each instance of a given problem.
 Algorithms are the ideas behind computer programs.

 An algorithm is the thing which stays the same whether


the program is in Java running on a Cray in New York or
is in Python running on a Macintosh in Kathmandu.

Abasyn University Peshawar 11


Formal Definition of Algorithm
• An algorithm for solving a problem
“a finite sequence of unambiguous, executable steps or instructions,
which, if followed would ultimately terminate and give the solution
of the problem”.
• Algorithm must have the following criteria:
– Input: Zero or more quantities is supplied
– Output: At least one quantity is produced.
– Finiteness:
• If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates
after a finite number of steps.
– Unambiguous;
• Each instruction is clear and unambiguous.
– Executable;
• Every instruction must be basic so that it can be carried out.
– Terminates;
• the execution of an algorithm must lead to an end

Abasyn University Peshawar 12


The Abstract Nature of Algorithms
• It is important to emphasize the distinction
between an algorithm and its representation—a
distinction that is analogous to that between a
story and a book.
• A story is abstract, or conceptual, in nature; a
book is a physical representation of a story.
• If a book is translated into another language or
republished in a different format, it is merely the
representation of the story that changes—the
story itself remains the same.

Abasyn University Peshawar 13


Review of Necessary

Mathematics
Mathematical Background
• Sets
– A set is a collection of distinct elements that we wish
to treat as a single object.
– Usually the elements are of the same type
– A set has no inherent order
– {a, b, c} and {b, c, a} are same
• Sequence
– A group of elements in a specified order is called a
sequence.
– (1, 2, 3), (1, 3, 5), and (a, c, b) are distinct sequences
• Series
– A series is the sum of a sequence.

Abasyn University Peshawar 15


Mathematical Background
• Floor and Ceiling
– For any real number x,  x  (read “floor of x”) is the largest integer
less than or equal to x.  x  (read “ceiling of x”) is the smallest
integer greater than or equal to x. e.g.  2.9 2, and 6.1 7.
• Functions
– A function f of one variable is a rule or law that associates with a
value x a unique value f(x) e.g. the function f that associates the
square of a real number with a given real number x is
f(x) = x2
A function determines the set of ordered pairs. e.g. the above
function determines all the ordered pairs (x, x2). A graph of a
function is the set of all ordered pairs determined by the function.
Draw function chart.

Abasyn University Peshawar 16


Summations and Series
• Summations
– Often we need to refer to the sum of like terms. If we need to
refer to the sum of the first seven positive integers, we simply
write
1+2+3+4+5+6+7
If we need to refer to the sum of the squares of the first seven
positive integers, we simply write
12+22+32+42+52+62+72
It is not satisfactory if we need to refer to the sum of the first 100
positive integers. We could write
1+2+3+4+······+ 100
However, more concise method is to use Greek letter Σ (sigma).
100 100

i
i 1
i
i 1
2

Abasyn University Peshawar 17


Series
• Arithmetic Series n
n(n  1)
– The sum of consecutive integers: 
i 1
i
2
• Polynomial Series n
n(n  1)( 2n  1)
– Sum of the squares of integers: 
i 1
i  2

6
• Power of 2 k
– This is a frequently occurring case of a
geometric series: i 0
 2 i
2 k 1
1

• Geometric Series n
r n 1
1
– The general case of geometric series: 
i 0
i
r 
r1
Abasyn University Peshawar 18
Series
• Harmonic Series
n


i 1
1
i
ln( n)  .577
• Arithmetic-Geometric
Series n

 i 2
i 1
i
( k  1) 2 k 1
2
• Fibonacci Numbers
– Although this is not a
summation, the series
Fn Fn  1  Fn  2 for n 2,
occurs frequently in
analysis of algorithms. F0 0, F1 1.

Abasyn University Peshawar 19


Logarithms
• Logarithms
– Logarithms are the one of the mathematical tools
used most in the analysis of algorithms.
• Definition
– For b>1 and x>0, logb x (read “log to the base b of x”)
is that real number L such bL = x; that is, logb x is the
power to which b must be raised to get x.
• Some Examples
 log2 8 = 3 because 23 = 8
 log3 81 = 4 because 34 = 81
 log2 1/16 = -4 because 2-4 = (1/2)4 = (1/16)
 log2 7 ≈ 2.807 because 22.807 ≈ 7
Abasyn University Peshawar 20
Logarithms
• Properties of Logarithms
– loga 1 = 0
– logb ba = a
– loga (xy) = loga x + loga y
– loga x/y = loga x – loga y
– loga xy = y loga x
– loga x = logb x / logb a

Abasyn University Peshawar 21


PUZZLES

Abasyn University Peshawar 22


Magic Square
• Fill the 3 × 3 table with nine distinct integers from 1 to 9 so that the sum of the
numbers in each row, column, and corner-to-corner diagonal is the same.

• How many ways are there to fill such a table?


• Let us think of the table as filled with one number at a time, starting with
placing the 1 somewhere and ending with placing the 9. There are nine ways
to place 1, followed by eight ways to place 2, and so on until the last number 9
is placed in the only unoccupied cell of the table.
• Hence, there are 9! = 9 · 8 · . . . · 1 = 362,880 ways to arrange the nine
numbers in the cells of the 3 × 3 table.
• Therefore, solving this problem by exhaustive search would imply generating
all 362,880 possible arrangements of distinct integers from 1 to 9 in the table
and checking, for each of the arrangements, whether all its row, column, and
diagonal sums are the same.
• This amount of work is clearly impossible to do by hand.

Abasyn University Peshawar 23


Magic Square
• The first helpful step is to find the value of the common sum for the magic squares in
question. This sum, called sometimes the magic sum, is equal to the sum of all the
numbers in its rows divided by the number of rows: (1 + 2 +··· + 9)/3 = 15.
• Second, the central cell must contain 5. Indeed, denoting the numbers in the row 1, 2,
and 3 a, b, c; d, e, f; g, h, i, respectively, and adding the numbers in the second row,
the second column, and the two main diagonals, we obtain
• (d + e + f ) + (b + e + h) + (a + e + i) + (g + e + c)
• = 3e + (a + b + c) + (d + e + f ) + (g + h + i) = 3e + 3 · 15 = 4 · 15, which implies that e
= 5.
• What remains is to arrange pairs (1, 9), (2, 8), (3, 7), and (4, 6) around it.
• Taking into account the table’s symmetries, there are only two qualitatively different
ways to put 1 and hence 9: in the table’s corners and not in the table’s corners (see
Figure 4.21).

Abasyn University Peshawar 24


Magic Square
• But the first of these arrangements cannot be completed to form a
magic square: if we put a number less than 5 in the upper right
corner, we will not be able to have the magic sum of 15 in the first
row, and if we put there a number larger than 5, we will have the
same problem with the last column.
• Thus, we can abandon the first partially filled table in Figure 4.21
and concentrate on the second one. There are also three other
ways to put 1 and 9 in the same row or the same column with 5.
These symmetric alternatives are shown in Figure 4.22.

Abasyn University Peshawar 25


Magic Square
• The line (row or column) containing 1 must then be filled
with 6 and 8, which can be done in two ways. The
numbers for the remaining cells are determined uniquely
afterward. All eight magic squares of order 3 are shown
in Figure 4.23. Of course, all of them are symmetric and
can be obtained from one of the eight by rotation and
reflection.

Abasyn University Peshawar 26


How to write an Algorithm?

Pseudocode Conventions
Pseudocode Conventions

Abasyn University Peshawar 28


Pseudocode Conventions

Abasyn University Peshawar 29


Pseudocode Conventions

Abasyn University Peshawar 30


Pseudocode Conventions
In general, a pseudo code is a notational system in which ideas can be
expressed informally during the algorithm development process.

• Pseudo code: • A Python code:


Problem: Given a list of positive
numbers, return the largest number def find_max (L):
on the list. max = 0
Inputs: A list L of positive numbers. for x in L:
This list must contain at least one
number. if x > max:
Outputs: A number n, which will be max = x
the largest number of the list. return max
Algorithm:
1. Set max to 0.
2. For each number x in the list L,
compare it to max.
3.If x is larger, set max to x.
4. max is now set to the largest
number in the list. Abasyn University Peshawar 31
Write an algorithm to add two numbers
entered by user.
Input: num1, num2
Output: sum
1: Declare variables num1, num2 and
sum.
2: Read the values of num1 and num2.
3: Add num1 and num2 and assign the
result to sum. or sum ← num1 + num2
4: Display sum
5: Stop

Abasyn University Peshawar 32


Algorithm Example
• Problem: Adding two n-digit numbers
• Input: Two positive m-digit decimal numbers (a and b)
am, am-1, …., a1
bm, bm-1, …., b1

• Output: The sum c = a + b


cm+1, cm, cm-1, …., c1

Abasyn University Peshawar 33


How to “derive” the algorithm
• Adding is something we all know
– done it a thousand times, know it “by heart”
• How do we give the algorithm?
– A step-by-step instruction
– to a dumb machine
• Try an example:
3492
8157

“Imagine you are looking at yourself solving it”

Abasyn University Peshawar 34


Algorithm: Adding two n-digit numbers
Input: a, b
Output: c
1: Set the value of carry to 0
2: Set the value of i to 1.
3: Repeat steps 4 through 6 until i > m.
4: Add ai and bi to the current value of carry, to get xi.
5: If xi < 10, then
let ci = xi and reset carry = 0.
else (/* i.e. xi  10 */)
let ci = xi - 10 and reset carry = 1.
6: Increase the value of i by 1.
7: Set cm+1 to the value of carry.
8: Print the final answer cm+1, cm, …., c1
9: Stop.

Abasyn University Peshawar 35


Write an algorithm to find the largest among
three different numbers entered by user.
Input: a, b, c
Output: largest
1: Start
2: Declare variables a, b and c.
3: Read variables a, b and c.
4: if a > b
5: if a > c
6: Display a is the largest number.
7: else
8: Display c is the largest number.
9: else
10: if b > c
11: Display b is the largest number.
12: else
13: Display c is the largest number.
14: Stop
Abasyn University Peshawar 36
Write an algorithm to find all roots of a
quadratic equation ax2+bx+c=0.
Input: a, b, c
Output: r1, r2, rp, ip
1: Start
2: Declare variables a, b, c, D, r1, r2, rp and ip
3: Read the values for a, b and c
4: Calculate the discriminant D ← b^2 - 4ac
5: if D ≥ 0
6: r1←(-b+√D)/2a
7: r2←(-b-√D)/2a
8: Print r1 and r2 as roots.
9: else
10: #Calculate real and imaginary parts
11: rp←b/2a
12: ip←√(-D)/2a
13: Print rp+i(ip) and rp-i(ip) as roots
14: Stop
Abasyn University Peshawar 37
Write an algorithm to find the factorial of a
number entered by user.
Input: n
Output: factorial
1: Start
2: Declare variables n, factorial and i
3: Read the value of n
4: #Initialize variables
5: factorial←1
6: i←1
7: Repeat the steps until i = n
8: factorial ← factorial * i
9: i ← i + 1
10: Print factorial
11: Stop

Abasyn University Peshawar 38


Write an algorithm to find the Fibonacci
series till term ≤ 1000.
Input: Nil
Output: second_term
1: Start
2: Declare variables first_term, second_term and
temp.
3: Initialize variables first_term←0 second_term←1
4: Display first_term and second_term
5: Repeat the steps until second_term ≤ 1000
6: temp←second_term
7: second_term ← second_term + first term
8: first_term ← temp
9: Display second_term
10: Stop

Abasyn University Peshawar 39


Write an algorithm to check whether a
number entered by user is prime or not.
Input: n
Output: Yes or No
1: Start
2: Declare variables n, i, flag.
3: #Initialize variables
4: flag ← 1
5: i ← 2
6: Read n
7: Repeat the steps until i<(n/2)
8: if (n % i) = 0
9: flag ← 0
10 Go to step 12
11: i ← i + 1
12: if flag=0
13: Display No, n is not prime
14: else
15: Display Yes, n is prime
16: Stop
Abasyn University Peshawar 40
Write algorithms for some given problems

ASSIGNMENT NO. 1

Abasyn University Peshawar 41


Analyzing Algorithms
Correctness, Complexity, Space usage,
Simplicity, & Optimality
Analyzing Algorithms
• Analyzing Algorithms
We analyze algorithms with the intention of
improving them, if possible, and for choosing among
several available for a problem. We will use the
following criteria:
1. Correctness
2. Complexity
3. Space usage
4. Simplicity, clarity
5. Optimality

Abasyn University Peshawar 43


Analyzing Algorithms
• Correctness
– An algorithm is said to be correct if, for every input
instance, it halts with the correct output.
– Can incorrect algorithms be sometimes useful?
– Three major steps are involved in establishing the
correctness of an algorithm.
1. What “correct” means?
2. A precise statement about the characteristics of the inputs
(preconditions), and what result it is to produce for each
input (postconditions).
3. If the preconditions are satisfied, the postconditions will be
true when the algorithm terminates.
• Loop invariants are used to help us understand why
an algorithm is correct.

Abasyn University Peshawar 44


Analyzing Algorithms
• Complexity (Amount of work done)
– A measure of work that tells something about the
efficiency of an algorithm independent of
• the computer
• programming language
• programmer
• implementation details
– A simple algorithm consists of
• initialization instructions
• a loop
• fundamental operation
– Time complexity analysis
• is the determination of how many times the basic operation is
done for each value of the input size.

Abasyn University Peshawar 45


Analyzing Algorithms
• Complexity Analysis Techniques
– Best-Case Time Complexity Analysis
• For a given algorithm, B(n) is defined as the minimum number of times
the algorithm will ever do its basic operation for an input size of n.
• Example, sequential search B(n) = 1

– Worst-Case Time Complexity Analysis


• For a given algorithm, W(n) is defined as the maximum number of times
the algorithm will ever do its basic operation for an input size of n.
• Example, sequential search W(n) = n

– Average-Case Time Complexity Analysis


• For a given algorithm, A(n) is defined as the average of the number of
the times the algorithm does the basic operation for an input of size n.
• Example, sequential search A(n) = (n+1)/2

Abasyn University Peshawar 46


Average, best and worst case
complexity - Example

Abasyn University Peshawar 47


Analyzing Algorithms
• Space Usage
– A program requires storage space for the instructions,
the constants and variables, and the input data. It
may also use some extra space for manipulating the
data and storing information needed to carry out its
computations.
– If the amount of extra space is constant, the algorithm
is said to work in place.
– If the input can be represented in various forms, then
we will consider the space required for the input itself
as well as any extra spce used.

Abasyn University Peshawar 48


Analyzing Algorithms
• Simplicity
– It is not always the case that the simplest way to
solve a problem is not the most efficient. Yet simplicity
is a desirable feature. It makes writing, debugging &
modifying a program easier.
– If the program is to be used very often, its efficiency
will probably be the determining factor.

Abasyn University Peshawar 49


Analyzing Algorithms
• Optimality
– Each problem has inherent complexity, that is, there is
some minimum amount of work required to solve it.
Therefore, an algorithm can’t be improved beyond a
certain point.
– An algorithm is optimal (in the worst case) if there is
no algorithm in the class under study that performs
fewer basic operations (in the worst case).
– A “tight” lower bound, one that matches the upper
bound for an algorithm, assures that further
improvements cannot be found.
– An algorithm is optimal, if W(n) = A(n) = B(n)
– e.g. the binary search algorithm is optimal in the class
of algorithms that perform comparisons only.
Abasyn University Peshawar 50
Analyzing Algorithms

Abasyn University Peshawar 51


Algorithm Efficiency vs. Speed
E.g.: sorting n numbers Sort 106 numbers!

Friend’s computer = 109 instructions/second


Friend’s algorithm = 2n2 instructions

Your computer = 107 instructions/second


Your algorithm = 50nlgn instructions

 6 2
Your friend = 2  10 instructio ns 2000seconds
109 instructio ns / second
You = 50  106 lg106 instructio ns
7
100seconds
10 instructio ns / second

20 times better!!
Abasyn University Peshawar 52
Analysis of Algorithm

Measuring Time Complexity


Algorithm Analysis: Example
• Alg.: MIN (a[1], …, a[n])
1: m ← a[1] 1
2: for i ← 2 to n n
3: if a[i] < m then n-1
4: m ← a[i] n-1
• Running time:
– Total number of primitive operations (steps) executed
before termination

– T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] + (n-1)
[the assignment]
– T(n) = 3n - 1

Abasyn University Peshawar 54


Measuring Time
• The absolute running time of an algorithm can never
really be predicted, since this depends on the
programming language used to implement the algorithm,
the computer the program runs on, and many other
factors.
• What we usually can do is find a measure of an
algorithm's relative running time, as a function of the
"size" of its input, often called n. The n could be
– The number of items in a container
– The length of a string or file
– The degree of a polynomial
• We count the number of abstract operations as a
function of n.
Abasyn University Peshawar 55
Algorithm: Printing each element of an array

1: A=[n] 1
2: for i ← 1 to len(A) n+1
3: print (A[i]) n

Here n = length(A) and we have n abstract


operations.
We write T(n) = 1+n+1+n = 2n+2.

Abasyn University Peshawar 56


Finding the index of a given value in an array

1: a =[n] 1
2: for i ← 1 to len(a) n+1
3: if (a[i] == x) n
4: return (i);

Here we need to introduce the B (best-case) and W


(worst-case) functions:
B(n) = 1 and
W(n) = n +1.

(What about the average case? It's usually difficult and


sometimes impossible to compute an average case. )

Abasyn University Peshawar 57


An Interesting Nested Loop
1: for i ← 1 to n step i *= 2) log n
2: for j ← 1 to n
3: count++

o i takes values 1, 2, 4, … until it exceeds n


o there are 1 + log2n iterations
o outer loop complexity (log n)
o Outer loop is done log n times and the inner loop is done
n+1 times, so
T(n) = log n+ log n (n+1) + n log n.
(Note that the default base for logarithms in Computer Science is 2.)

Abasyn University Peshawar 58


Algorithm: Multiplying two square matrices
1: a, b, c = [n][n] 1
2: for i ← 1 to n n+1
3: for j ← 1 to n n(n+1)
4: sum = 0 n*n
5: for k ← 1 to n n2(n+1)
6: sum += a[i][k] * b[k][j] n3
7: c[i][j] = sum n2

T(n)=1+(n+1)+(n(n+1))+(n2)+(n2(n+1))+n3+n2
T(n)=2n3+4n2+2n+2

Abasyn University Peshawar 59


Analysing an Algorithm
• Loop index depends on outer loop index
for j ← 1 to n
for k ← 1 to k < j
count++
Distinguish this case -
– Inner loop executed where the iteration count
• 1, 2, 3, …., n times increases (decreases) by a
constant ç O(nk)
from the previous one -
n(n+1
nS i where it changes by a factor
)
i= = 2 ç O(log n)
1
\ Complexity O(n2)

Abasyn University Peshawar 60


Contains Duplicates
• // Loop over all of the array's items except the last one.
for i = 1 to n – 1 n
// Loop over the items after item i.
for j = i + 1 to n
if (array[i] == array[j]) Then
return True
next j
next i
// If we get to this point, there are no duplicates.
return False

Abasyn University Peshawar 61


Analysis of Algorithm

The Complexity Classes


Time Complexity Classes
• 1 (constant running time):
– Instructions are executed once or a few times

• log n (logarithmic)
– A big problem is solved by cutting the original problem in smaller
sizes, by a constant fraction at each step
• n (linear)
– A small amount of processing is done on each input element

• n log n
– A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution

Abasyn University Peshawar 63


Typical Running Time Functions
• n2 (quadratic)
– Typical for algorithms that process all pairs of data items (double
nested loops)

• n3 (cubic)
– Processing of triples of data (triple nested loops)

• nK (polynomial)
• 2n (exponential)
– Few exponential algorithms are appropriate for practical use

Abasyn University Peshawar 64


Why Faster Algorithms?

Abasyn University Peshawar 65


Growth Rate

Functions Growth Rate


n lg n n nlg n n2 n3 2n nn
1 0 1 0 1 1 2 1 1000000000

2 1 2 2 4 8 4 4
4 2 4 8 16 64 16 256 100000000
8 3 8 24 64 512 256 16777216
1.84467E+1
16 4 16 64 256 4096 65536 9 10000000

32 5 32 160 1024 32768 4294967296 1.4615E+48


1.84467E+1 3.9402E+11 1000000
64 6 64 384 4096 262144 9 5

No. of basic operations


1638 209715 3.40282E+3 5.2829E+26
128 7 128 896 4 2 8 9 100000

10000

1000

100

10

1
10 100 1000
Time (micro sec)

lg n n nlg n n2 n3

Abasyn University Peshawar 66


Comparison of Complexity Classes
T(n) 10 20 50 100 1000 1000000
1 1 us 1 us 1 us 1 us 1 us 1 us
log n 3.32 us 4.32 us 5.64 us 6.64 us 9.97 us 19.9 us
n 10 us 20 us 50 us 100 us 1 msec 1 sec
n log n 33.2 us 86.4 us 282 us 664 us 9.97msec 19.9 sec
n2 100 us 400 us 2.5 msec 10 msec 1 sec 11.57days
1000 n2 100 msec 400msec 2.5 sec 10 sec 16.7mts 31.7years
n3 1 msec 8msec 125msec 1sec 16.7 min 317cent.
0.000001 * 2n 1.02 ns 1.05msec 18.8minutes 4 * 108 cent.
1.05 35.7 4 * 1014
2n
1.02 msec
sec years cent.

n! 3.63 Sec 771cent. 9 * 1050 cent.

Abasyn University Peshawar 67


Readings

• Chapter 1
• Appendix A
• Chapter 2: Section 2.1, 2.2

Abasyn University Peshawar 68

You might also like