0% found this document useful (0 votes)
17 views65 pages

Module1 ADA - 4225 - BCS401 - 12-03-2025

The document provides an overview of algorithms, focusing on their definitions, components, and examples of different algorithms for computing the greatest common divisor (gcd). It also discusses pseudocode conventions, performance analysis including space and time complexity, and introduces sorting algorithms like selection sort and bubble sort. Additionally, it covers asymptotic notations used to analyze algorithm efficiency.

Uploaded by

aishwaryamc1817
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)
17 views65 pages

Module1 ADA - 4225 - BCS401 - 12-03-2025

The document provides an overview of algorithms, focusing on their definitions, components, and examples of different algorithms for computing the greatest common divisor (gcd). It also discusses pseudocode conventions, performance analysis including space and time complexity, and introduces sorting algorithms like selection sort and bubble sort. Additionally, it covers asymptotic notations used to analyze algorithm efficiency.

Uploaded by

aishwaryamc1817
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/ 65

Subject Code: BCS401

Subject Name: Analysis & Design of Algorithms


Module Number: 01
Name of the Module: Introduction, Fundamentals of the Analysis of Algorithm
Efficiency and Brute Force Approaches
Scheme: 2022
What is an Algorithm?

• An algorithm is a sequence of unambiguous instructions for solving a


problem, i.e., for obtaining a required output for any legitimate input in a
finite amount of time.
computing gcd(m, n) using three different
Algorithms.

1. Euclid's algorithm for computing gcd(m, n)

2. Consecutive integer checking algorithm for computing gcd(m, n)

3. Middle-school procedure for computing gcd(m, n)


Euclid's algorithm for computing gcd(m, n)
Consecutive integer checking algorithm for computing gcd(m, n)
Middle-school procedure for computing gcd(m, n)
• An algorithm can be divided into two main parts:
1. The name and
2. The body.

• Name: This part gives the algorithm a meaningful and concise


identifier, reflecting its purpose or functionality. It serves as a
reference for programmers and users.

• Body: This part contains the actual instructions and logic that
define how the algorithm solves a problem or performs a task.
It consists of a sequence of operations, computations,
conditionals, and control structures.
Pseudocode Convention

• Pseudocode is a way to express the logic of an algorithm


using a combination of natural language and programming
constructs.
• Although there is no strict standard for writing pseudocode,
there are some common conventions that can make it more
readable and understandable.
• Here are some conventions for writing pseudocode:
PSEUDO-CODE CONVENTIONS
1. Comments begin with // and continue until the end of line.

2. Blocks are indicated with matching braces { and }.

3. An identifier begins with a letter. The data types of variables are not
explicitly declared.
node= record
{
data type 1 data 1;
data type n data n;
node *link;
}
4. There are two Boolean values TRUE and FALSE.
Logical Operators
AND, OR, NOT
Relational Operators
<, <=,>,>=, =, !=
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;

6. Compound data types can be formed with records. Here is an example,


Node. Record
{
data type – 1 data-1;
.
.
.
data type – n data – n;
node * link;
}

Here link is a pointer to the record type node. Individual data items of
a record can be accessed with  and period.
Contd…

7. The following looping statements are employed.


For, while and repeat-until While Loop:
While < condition > do
{
<statement-1>
..
..
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do

{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:

repeat
<statement-1>
.
.
.
<statement-n>
until<condition>

8. A conditional statement has the following forms.

 If <condition> then <statement>


 If <condition> then <statement-1>
Else <statement-1>
Case statement:

Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}

9. Input and output are done using the instructions read & write. No
format is used to specify the size of input or output quantities
Contd…
10. There is only one type of procedure: Algorithm, the heading takes the form,
Algorithm Name (Parameter lists)

consider an example, the following algorithm fields & returns the maximum of
n given numbers:

1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for i:= 2 to n do
6. if A[i] > Result then
7. Result :=A[i];
8. return Result;
9. }
2. Performance Analysis

[Space/Time complexity]

• The space complexity of an algorithm is the amount of memory it


needs to run to completion.

• The time complexity of an algorithm is the amount of computer time


it needs to run to completion.
2. Performance Analysis

1.Space complexity

• Total amount of computer memory required by an algorithm to complete its


execution is called as space complexity of that algorithm.

• The space complexity of an algorithm refers to the amount of memory


space required by the algorithm to solve a problem. It measures the
maximum amount of memory used by the algorithm during its execution.
• The Space required by an algorithm is the sum of following components
1. A fixed part that is independent of the input and output. This includes
memory space for codes, variables, constants and so on.
2. A variable part that depends on the input, output and recursion stack.
( We call these parameters as instance characteristics)

Space requirement S(P) of an algorithm P,

S(P) = c + Sp
where c is a constant depends on the fixed part,
Sp is the instance characteristics
Example 1: consider abc() Algorithm.

fixed component depends on a,b and c. The space needed by each of these
algorithms is seen to be the sum of the following component.

the space needed by abc Algorithm is independent of the instance


characteristics. so,
Sp(instance characteristics) =0.
Example 1: Iterative function for sum Algorithm.
Time Complexity

• The time T(P) taken by a program P is the sum of the compile time and the
run (or execution)time. The compile time does not depend on the instance
characteristic.
• This run time is denoted by tp (instance characteristic ).
• The execution time or run-time of the program is refereed as its time
complexity denoted by tp (instance characteristics). This is the sum of the
time taken to execute all instructions in the program.
• Exact estimation of runtime is a complex task, as the number of instruction
executed is dependent on the input data. Also different instructions will take
different time to execute. So for the estimation of the time complexity we
count only the number of program steps.
• We can determine the steps needed by a program to solve a
particular problem instance in two ways.

• In the first method –


• we introduce a new variable count to the program which is initialized
to zero.

• We also introduce statements to increment count by an appropriate


amount into the program. (count = count +1)

• So when each time original program executes, the count also


incremented by the step count.
• The second method to determine the step count of an
algorithm is to build a table in which we list the total number of
steps contributed by each statement.
• The step count for the entire algorithm is obtained using
following parameter
• s/e- The number of Steps per execution(s/e) of the statement.
• Frequency - The total number of times each statement is executed
• By combining these two quantities, the total contribution of
each statement is obtained.
• By adding the contributions of all statements, the step count
for the entire algorithm is obtained.
selection sort
• Selection sort is a simple sorting algorithm that repeatedly selects the
smallest (or largest) element from an unsorted list and swaps it with
the element at the beginning of the sorted portion of the list.
• It gradually builds up a sorted portion of the list from left to right.
Here's how selection sort works:
1. Find the minimum (or maximum) element in the unsorted
portion of the list.

2. Swap the minimum (or maximum) element with the first


element of the unsorted portion of the list.

3. Move the boundary of the sorted portion one element to


the right.

4. Repeat steps 1 to 3 until the entire list is sorted.


• The algorithm divides the list into two portions: the sorted portion
and the unsorted portion.

• Initially, the sorted portion is empty, and the entire list is considered
as the unsorted portion.

• In each iteration, the smallest (or largest) element from the unsorted
portion is found and placed at the end of the sorted portion.
• The selection sort algorithm has a time complexity of O(n^2), where n is
the number of elements in the list.

• It is not suitable for large lists, as its performance is relatively poor


compared to more efficient sorting algorithms like quicksort or
mergesort.
• However, it can be useful for small lists or as a simple educational
example of a sorting algorithm.
Bubble sort
• Bubble sort is a simple sorting algorithm that repeatedly steps through a list or array,
compares adjacent elements, and swaps them if they are in the wrong order. This process
is repeated until the entire list is sorted.
Here's a step-by-step explanation of how bubble sort works:
1. Start at the beginning of the list or array.
2. Compare the first element with the second element.
3. If the first element is greater than the second element, swap them.
4. Move to the next pair of adjacent elements (second and third) and repeat steps 2 and 3.
5. Continue this process, comparing and swapping adjacent elements until you reach the
end of the list or array. At this point, the largest element will be in its correct position
at the end of the list.
6. Repeat steps 1-5 for the remaining unsorted portion of the list, excluding the last
element that was already sorted.
7. Keep repeating steps 1-6 until the entire list is sorted.
Asymptotic notations
• Asymptotic notations are mathematical notations used in computer science and
mathematics to describe the behavior and performance characteristics of
algorithms. They provide a way to analyze the efficiency and scalability of
algorithms by focusing on how their running time or space requirements grow as
the input size increases.
The three commonly used asymptotic notations are:
1. Big O notation (O): It represents the upper bound or worst-case scenario of an
algorithm's running time or space complexity. In simpler terms, it describes
the maximum amount of resources an algorithm will require.
2. Omega notation (Ω): It represents the lower bound or best-case scenario of
an algorithm's running time or space complexity. In other words, it describes
the minimum amount of resources an algorithm will require.
3. Theta notation (Θ): It represents both the upper and lower bounds of an
algorithm's running time or space complexity.
Asymptotic notations
Big O notation (O):
Omega notation (Ω):
Theta notation (Θ):
Mathematical analysis of Non-Recursive Algorithm
Summation formula and Rules
Uniqueness Problem
Matrix Multiplication
Computing number of bits in
integer
Mathematical analysis of Recursive Algorithm
Factorial of N
Tower of Hanoi

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy