0% found this document useful (0 votes)
15 views

Loops and Timecomplexity

Uploaded by

Ahmed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Loops and Timecomplexity

Uploaded by

Ahmed Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Discrete Structures

Loops And Time complexity


Ahsan Azhar
Algorithms
Setting up the appropriate mathematical model is only part of the solution. To
complete the solution, a method is needed that will solve the general problem
using the model. Ideally, what is required is a procedure that follows a sequence
of steps that leads to the desired answer. Such a sequence of steps is called an
algorithm.

DEFINITION
An algorithm is a finite sequence of precise instructions for performing a
computation or for solving a problem.
Algorithms
Example 1 Finding the Maximum Element in a Finite Sequence.

procedure

for
if
return
Algorithms
Properties Of Algorithms: There are several properties that algorithms generally
share. They are useful to keep in mind when algorithms are described. These properties are:
▶ Input. An algorithm has input values from a specified set.
▶ Output. From each set of input values an algorithm produces output values from a specified set. The
output values are the solution to the problem.
▶ Definiteness. The steps of an algorithm must be defined precisely.
▶ Correctness. An algorithm should produce the correct output values for each set of input values.
▶ Finiteness. An algorithm should produce the desired output after a finite (but perhaps
large) number of steps for any input in the set.
▶ Effectiveness. It must be possible to perform each step of an algorithm exactly and in a
finite amount of time.
▶ Generality. The procedure should be applicable for all problems of the desired form, not
just for a particular set of input values.
Loops
 Repetition statements allow us to execute a statement or a block of
statements multiple times
 Often they are referred to as loops
 Like conditional statements, they are controlled by boolean
expressions
 There usually three kinds of repetition statements:
i. while
ii. for
iii. do
 The programmer should choose the right kind of loop statement for
the situation
Loops
There are types of loops according to their applications

Loop

Single Nested

Independent Dependent
The Statement
 A while statement has the following syntax:

while ( condition )
statement;
 If the condition is true, the statement is executed
 Then the condition is evaluated again, and if it is still true, the
statement is executed again
 The statement is executed repeatedly until the condition becomes
false
The Statement

condition
evaluated

true false

statement
The Statement
 A for statement has the following syntax:

for (initialization; condition; update)


statements;

 initialization: This is the first component, which is used for the initialization of the
counter variable which is used to control the loop. This statement is only executed
once.
 condition: It is a boolean expression, which is evaluated before each iteration of the
loop. If the boolean expression is true, the loop body is executed.
 update: This statement is executed after each iteration of the loop. This statement
updates the counter variable or other values that are present in the loop.
The Statement

condition

update true false

statement
The Statement
 A do while statement has the following syntax:
do {
// body of do-while loop
} while (condition);
 the body of the loop is executed first and then the test condition/expression is
checked, unlike other loops where the test condition is checked first.
 When the test condition is evaluated as true, the program control goes to the start of
the loop and the body is executed once more.
 The above process repeats till the test condition is true.
 When the test condition is evaluated as false, the program controls move on to the
next statements after the do…while loop.
The Statement

Execute loop body

condition

true false
Time Complexity
The time complexity of an algorithm can be expressed in terms of the number
of operations used by the algorithm when the input has a particular size. The
operations used to measure time complexity can be the comparison of
integers, the addition of integers, the multiplication of integers, the division of
integers, or any other basic operation.

Time complexity is described in terms of the number of operations required


instead of actual computer time because of the difference in time needed for
different computers to perform basic operations.
Big- Notation
The growth of functions is often described using a special notation

DEFINITION
Let and be functions from the set of integers or the set of real numbers to the set of
real numbers. We say that is if there are constants and such that
whenever . [This is read as “ is big-oh of .”]

Intuitively, the definition that is says that grows slower than some fixed multiple of
as grows without bound.
Big- Notation
Example 1 How can big- notation be used to estimate the sum of the first
positive integers?

Solution: Because each of the integers in the sum of the first positive integers
does not exceed , it follows that

From this inequality it follows that is .


Big- Notation
Example 2 Give big- estimates for the factorial function and the logarithm of
the factorial function, where the factorial function is defined by

Solution: A big- estimate for can be obtained by noting that each term in the
product does not exceed . Hence,

This inequality shows that is .


Time Complexity Of Loops
A loop is repetition statements. Which means particular piece of
instructions run multiple times. To find the time complexity and big-of a
loop it is essential part to calculate the number of repetition of the loop.
Once the number of iterations are counted then the time complexity in the
body of a loop is the product of the number of counts and the time
complexity of a complete single iteration.

We, in this section only find the time complexity of a loop with no in body
statements.
Time Complexity Of Loops
We find the time complexity of a loop in six steps

i. Dry run of the loop


ii. Find the generative function of the sequence of the loop
iii. Find the value of the last iteration
iv. Find the range of the sequence
v. Count the total terms in the sequence
vi. Drive the Big- of the loop

We perform all these steps in ascending and descending value loops.


Time Complexity Of Ascending Loops

I- Dry Run:

𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant difference of 4, which means
the sequence is
Time Complexity Of Ascending Loops

III- Find the value of :

If If

Or we say
Time Complexity Of Ascending Loops

IV- Range of the sequence:

Range : Range :
Time Complexity Of Ascending Loops

V- Count of total terms of the sequence:

Count of : Count of :
Time Complexity Of Ascending Loops

VI- The Big of the loop:

Big : Big
Time Complexity Of Ascending Loops
Generic form:

I- Dry Run:

𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃 ⋯
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant addition of , which means the
sequence is
Time Complexity Of Ascending Loops

III- Find the value of :

If If

Or we say
Time Complexity Of Ascending Loops

IV- Range of the sequence:

Range : Range :
Time Complexity Of Ascending Loops

V- Count of total terms of the sequence:

Count of : Count of :
Time Complexity Of Ascending Loops

VI- The Big of the loop:

Big : Big
Time Complexity Of Descending Loops

I- Dry Run:

𝒏 𝒏− 𝟒𝒏− 𝟖𝒏− 𝟏𝟐 ⋯ 𝒏− 𝟒 𝒌
II- Generative Function: Now from the results of iterations find the
descending sequence. In this example there is a constant subtraction of 4,
which means the sequence is
Time Complexity Of Descending Loops

III- Find the value of :

If If

Or we say
Time Complexity Of Descending Loops

IV- Range of the sequence:

Range : Range :
Time Complexity Of Descending Loops

V- Count of total terms of the sequence:

Count of : Count of :
Time Complexity Of Descending Loops

VI- The Big of the loop:

Big : Big
Time Complexity Of Descending Loops
Generic form:

I- Dry Run:

𝒏 𝒏− 𝒃𝒏− 𝟐 𝒃 ⋯
II- Generative Function: Now from the results of iterations find the
sequence. In this example there is a constant difference of , which means
the sequence is
Time Complexity Of Descending Loops

III- Find the value of :

If If

Or we say
Time Complexity Of Descending Loops

IV- Range of the sequence:

Range : Range :
Time Complexity Of Descending Loops

V- Count of total terms of the sequence:

Count of : Count of :
Time Complexity Of Descending Loops

VI- The Big of the loop:

Big : Big
Time Complexity Of Nested Loops (Ind)
Terminates

I- Dry Run:
𝒏
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒
Outer loop 𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌 Inner loop

𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒 𝒌
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
Time Complexity Of Nested Loops (Ind)

II- Generative Function:


𝒏
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
Generative Function of 𝒏− 𝟒
outer loop
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌 Generative

… Function of
Inner loop
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
𝒏− 𝟒 𝒌
𝟎 𝟒 𝟖 𝟏𝟐 ⋯ 𝟒𝒌
Time Complexity Of Nested Loops (Ind)

III- Find the value of :

for inner loop for outer loop

Or we say Or we say
Time Complexity Of Nested Loops (Ind)

IV- Find the Range of :

for inner loop for outer loop


Time Complexity Of Nested Loops (Ind)

V- Count of total terms of the sequence:

for inner loop for outer loop


Time Complexity Of Nested Loops (Ind)

VI- The Big −𝑶 of the independent nested loops:

The big –O of independent nested loops is


Time Complexity Of Nested Loops (Ind)
Generic form Terminates

I- Dry Run:
𝒏− 𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝟐 𝒃
Outer loop 𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ Inner loop

𝒂 𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝒌𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯
Time Complexity Of Nested Loops (Ind)
Generic form

II- Generative Function:


𝒏− 𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
Generative Function of 𝒏− 𝟐 𝒃
outer loop
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯ Generative

… Function of
Inner loop
𝒂 𝒂+𝟑 𝒃 ⋯ 𝒂+𝒌𝒃
𝒏− 𝒌𝒃
𝒂 𝒂+ 𝒃𝒂+𝟐 𝒃𝒂+𝟑 𝒃 ⋯
Time Complexity Of Nested Loops (Ind)
Generic form

III- Find the value of :


for inner loop for outer loop

Or we say Or we say
Time Complexity Of Nested Loops (Ind)
Generic form

IV- Find the Range of :


for inner loop for outer loop
Time Complexity Of Nested Loops (Ind)
Generic form

V- Count of total terms of the sequence:


for inner loop for outer loop
Time Complexity Of Nested Loops (Ind)
Generic form

VI- The Big −𝑶 of the independent nested loops:

The big –O of independent nested loops is


Thanks!
Do you have any questions?
[email protected]
F301 - Cab 2- Building A
University of Central Punjab Lahore

CREDITS: This presentation template was created by Slidesgo, and includes


icons by Flaticon and infographics & images by Freepik

You might also like