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

Analysis of Algorithm

Uploaded by

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

Analysis of Algorithm

Uploaded by

4q57rwt7hc
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA

STRUCTURES
AND
ALGORITHMS
Hafiz Ahsan Arshad
1. Initialize two integer variables, x and y, to 0.
2. Start an outer loop with i ranging from 0 to 2 (inclusive).
• Inside the outer loop, start an inner loop with j
ranging from 0 to 2 (inclusive).
• Check if j is even.
• If j is even, increment the value of x by 2.
• If j is odd (not even), increment the value of y by 4.
• End the inner loop.
3. End the outer loop.
4. Print the final values of x and y to the console.
What is Algorithm

■ An algorithm is a step-by-step procedure for solving a particular


problem in a finite amount of time.

Proble Algorithm: A sequence of


m instructions describing how
to do a task

High Level
Language
Program
Properties of Algorithms

■ It must be composed of an ordered sequence of precise steps.


■ It must have finite number of well-defined instructions /steps.
■ The execution sequence of instructions should not be
ambiguous.
■ It must be correct.
■ It must terminate.
Efficiency of Algorithm

■ The efficacy of an algorithm refers to how well it performs a specific task.


■ It is a measure of how efficiently an algorithm utilizes computational resources to achieve
its intended goal.
■ Two key aspects of algorithmic efficacy are
1. time complexity.
2. space complexity.
■ Time Complexity:
– Measures runtime efficiency.
– Quantifies the number of basic operations relative to input size.
– Lower time complexity indicates faster execution for larger inputs.
■ Space Complexity:
– Measures memory usage efficiency.
– Quantifies the additional memory space relative to input size.
– Lower space complexity indicates less memory usage.
Example of Efficiency of
Algorithm
■ Let us consider the following problem. The holiday season is
approaching and a gift shop is expecting sales to be double or even
triple the regular amount. They have hired extra delivery people to
deliver the packages on time. The company calculates the shortest
distance from the shop to a particular destination and hands the route
to the driver.
■ Suppose that 50 packages are to be delivered to 50 different houses.
The shop, while making the route, finds that the 50 houses are one
mile apart and are in the same area. (See Figure 1-1, in which each
dot represents a house and the distance between houses is 1 mile.)
Example of Efficiency of
Algorithm (Cont…)
■ To deliver 50 packages to their destinations, one of the drivers picks up all 50
packages, drives one mile to the first house and delivers the first package. Then
he drives another mile and delivers the second package, drives another mile and
delivers the third package, and so on. Figure 1-2 illustrates this delivery scheme.

■ It now follows that using this scheme, the distance driven by the driver to deliver
the packages is:
1+1+1+...+1 = 50 miles
■ Therefore, the total distance traveled by the driver to deliver the packages and
then getting back to the shop is:
50+50 = 100 miles
Example of Efficiency of
Algorithm (Cont…)
■ Another driver has a similar route to deliver another set of 50 packages. The
driver looks at the route and delivers the packages as follows:
■ The driver picks up the first package, drives one mile to the first house,
delivers the package, and then comes back to the shop. Next, the driver picks
up the second package, drives 2 miles, delivers the second package, and then
returns to the shop. And so on. Figure 1-3 illustrates this delivery scheme.

■ The driver delivers only one package at a time. After delivering a package, the
driver comes back to the shop to pick up and deliver the second package.
Using this scheme, the total distance traveled by this driver to deliver the
packages and then getting back to the store is:
2 * (1+2+3+...+50) = 2550 miles
Example of Efficiency of
Algorithm (Cont…)
■ Now suppose that there are n packages to be delivered to n houses,
and each house is one mile apart from each other, as shown in Figure
1-1.
■ If the packages are delivered using the first scheme, the following
equation gives the total distance traveled:
(1-1)
■ If the packages are delivered using the second method, the distance
traveled is:
2* (1+2+3+ ... + n) = 2 * () = + n (1-2)

Gauss Formula:
Sum from 1 to n =
Efficiency of Algorithm (Cont…)

■ Ways of measuring efficiency:


– Run the program and see how long it takes
– Run the program and see how much memory it uses
■ Lots of variables to control:
– Input Data:
• The efficiency of a program can vary significantly depending on the input data it
operates on. Some inputs may cause the program to execute faster or slower
than others.
– Hardware Platform:
• The performance of a program is affected by the underlying hardware, including
the processor, memory, and storage. Different hardware configurations can lead
to varying performance results.
– Programming Language/Compiler:
• The choice of programming language and compiler can influence program
efficiency. Some languages are inherently faster than others for specific tasks,
and different compiler optimizations can impact runtime and memory usage.
Time Complexity

■ Complexity is a function f (n) which yields the time required to execute


the algorithm of a problem of size ‘n’.
■ Want to achieve platform-independence
– Use an abstract machine that uses steps or unit of time and
units of memory, instead of seconds or bytes.
– each elementary operation takes 1 step or 1 unit.
– each elementary instance occupies 1 unit of memory
■ Running Time of an Algorithm
– Running time is measured in terms of number of steps/primitive
operations performed.
– Generally time grows with size of input, so running time of an
algorithm is usually measured as function of input size.
– Independent from machine, OS.
Complexity of Algorithms:
Rule 1
■ A linear sequence of elementary operations is performed in constant
time.
■ constant time: f (n)=1
■ Example:
x=a .....................1
x= a+b*c/h-u ......5
a>b ......................1
Complexity of Algorithms:
Rule 2
■ Analyzing a group of consecutive statements
■ The statement taking the maximum time will be the one counted
– use the maximum rule

Block #1 t1
f (n) =
Block #2 t2 max(t1,t2)

■ Always analyze function calls first


Complexity of Algorithms:
Rule 3
■ If <test> then P1 else P2 structures are a little harder; conditional
loops.
■ The maximum rule can be applied here too:
– max(t1, t2), assuming t1, t2 are times of P1, P2
■ However, the maximum rule may prove too conservative
– if <test> is always true the time is t1
– if <test> is always false the time is t2
e.g. if (a>b) then ...........1
a=2......................1
b=b+3*t .............3
else
x=x+2*3 ................3 Block #1 t1 Block #2 t2 Max(t1,t2)

f (n)= 1+max(4,3) = 5
Complexity of Algorithms

■ The time complexity of the expression b = b + 3 * t is typically


considered to be O(1), not O(3),
■ Constant Time Operations:
– In computational complexity analysis, basic arithmetic operations
like addition, multiplication etc. are generally considered to be
constant time operations (O(1)).
– It means they take the same amount of time to execute
regardless of the values of b and t.
■ Time Complexity Analysis for a Single Line:
– When analyzing the time complexity of a single line of code or a
basic expression, we focus on the most significant operation or
operations that contribute to the time complexity.
Example 1

Algorithm: Number of times executed


1. n = read input from user 1
2. sum = 0 1
3. i = 0 1
4. while i < n n+1
5. number = read input from user n
6. sum = sum + number n
7. i=i+1 n
8. mean = sum / n 1
The computing time for this algorithm in terms on input size n is:
f (n) = 1 + 1 + 1 + n +1+ n + n + n + 1
f (n) = 4n + 5 or O(n)
Example 1 Explained
■ In time complexity, we focus on the dominant or most significant term(s) that have the
greatest impact on the overall runtime of an algorithm as the input size (n) grows to
infinity.
■ Other constant factors or lower-order terms, such as the 4 and +5 in the expression 4n
+ 5, are typically ignored because they become less significant as n becomes large.
■ Here's why these terms are ignored:
– Constant Factor (4):
o The constant factor 4 simply scales the overall time taken by the algorithm but doesn't
affect the growth rate with respect to the input size (n).
o As n increases, the difference between 4n and n (without the constant factor) becomes
less significant.
– Constant Term (+3):
o The constant term +5 is a fixed overhead that doesn't depend on the input size.
o It represents a one-time operation or a constant amount of time taken, regardless of
the input size.
o In calculating the time complexity, we're interested in how the algorithm's performance
scales with respect to the input size, so constant terms like +5 is ignored.
Example 2: Example 3:
i=1 .............................. 1 int Sum(int A[], int N){
while (i < n).................. n int s=0; 1
a=2+g.............. n-1 for (int i=0; i< N; i++ )
i=i+1 ............... n-1 2 4
s = s + A[i]; 3
if (i<=n)...................... 1 5 7
return s; 6
a=2 .................. 1 }
8
else
a=3.................... 1
1,2,8: Once
3,4,5,6,7: Once per each iteration
f (n) = 1 +n+ 2(n-1) + 1 + 1 of for loop, N iteration
Total: 5N + 3
f (n) = 3n + 1 or O(n) The complexity function of the
algorithm is : f(N) = 5N +3 or O(n)
Example 4: Example 5:
Add(A,B,n){ for (i=0; i<=n ; i++) ...... (N+2)
for (int i = 0; i < n; i++) ...... (N+1) {
{ g++; ...... N+1
for (int j = 0; j < n; j++) ...... N*(N+1) }
{ f (N) = 2N+3
result[i][j] = matrix1[i][j] + matrix2[i] O(N)
[j]; ...... N*N
} Example 6:
}} for (i=n; i > 0 ; i - -) ...... (N+1)
f (N) = 2N2+N+1 {
f (N) = O(N2) g++; ...... N
}
f (N) = 2N+1
O(N)
Example 7: Solution:
for (int i = 0; i < n; i++) i j Stateme
nt
{
0 0 X
for (int j = 0; j < i; j+
+) 1 0 1
1X
{
2 0 2
Statement 1
2X
}
3 0 3
} 1
f (N) = O(N2) 2
3X
. . .
. . .
As statement is running like
. . .
following:
n 0 n
. 1+2+3+4+……+n =
.
nX
Example 8:
Solution:
for (int i = 1; i < n; i=i*2) i
{ 1
1*2 = 2
Statement
2*2 = 22
}
Assume:
22*2 = 23
f (N) = O()
i≤
23*2 = 24n
2k ≤ n ∴i=2k
.
.
.
.
.
2k = n
k=
.

2k*2 = 2
k+1
Compare Type of Time
Function

You might also like