SlideShare a Scribd company logo
Design and Analysis of Algorithms.pptx
What is an Algorithm?
• The word algorithm comes from the name of a
Persian author, Abu Ja'far Mohammed ibn Musa
al Khowarizmi(c.825A.D.), who wrote a textbook
on mathematics.
• This word has taken on a special significance in
computer science, where "algorithm" has come
to refer to a method that can be used by a
computer for the solution of a problem.
• This is what makes algorithm different from
words such as process, technique, or method.
Definition:
• An algorithm is a finite set of instructions that, if followed,
accomplishes a particular task. In addition, all algorithms
must satisfy the following criteria:
1. Input: Zero or more quantities are externally supplied.
2. Output: At least one quantity is produced.
3. Definiteness: Each instruction is clear and unambiguous.
4. Finiteness: If we trace out the instructions of an algorithm,
then for all cases, the algorithm terminates after a finite
number of steps.
5. Effectiveness: Every instruction must be very basic so that it
can be carried out, in principle, by a person using only pencil
and paper. It is not enough that each operation be definite as
in criterion3; it also must be feasible.
What is Pseudocode?
• One of the popular representation of
Algorithm
• Widely choosen because:
– easy to read and write
– allow the programmer to concentrate on the logic
of the problem
– Structured in English language
Pseudocode Convention
• Statement are written in simple English
• Each instruction is written on a separate line
• Keywords and indentation are used to signify
particular control structures.
• Each set of instructions is written from top to
bottom, with only one entry and one exit.
• Groups of statements may be formed into modules,
and that group given a name.
Six Basic Computer Operations
1. A computer can receive information
Read student name
Get system date
Read number_1,
number_2
Get tax_code
Verb used:
•Read  used when the algorithm is to receive the input from a record on a file
•Get  used when the algorithm is to receive input from the keyboard.
2. A computer can put out information
Print `Program Completed´
Write customer record to master file
Put out name, address and postcode
Output total_tax
Display ´End of data´
Prompt for student_mark
Get student_mark
Verb used:
•Print  used when the output is to be sent to the printer
•Write  used when the output is to be written to a file
•Put, Output, Display  used when the output is to be written to the screen
•Prompt  required before an input instruction Get, causes the message to be sent to
the screen which requires the user responds, usually by providing input.
3. A computer can perform arithmetic
Add number to total
Total = total + number
Divide total_marks by student_count
Sales_tax = cost_price * 0.10
Compute C = (F – 32) * 5/9
Verb used:
•Compute
•Calculate
•Symbols used:
+, -, *, /, ()
4. A computer can assign a value to a variable or
memory location
• Three cases :
1. To give data an initial value in pseudocode, the verbs
Initialise or Set are used
2. To assign a value as a result of some processing, the
symbols ´=´or ´´ are written
3. To keep a variable for later use, the verbs Save or
Store are used.
Initialize total_price to zero
Set student_count to 0
Total_price = cost_price + sales_tax
Total_price  cost_price + sales_tax
Store customer_num in last_customer_num
5. A computer can compare two variables and select one
of two alternate actions
IF student_attendance_status is part_time
THEN
add 1 to part_time_count
ELSE
Add 1 to full_time_count
ENDIF
Keyword used:
IF, THEN, ELSE
6. A computer can repeat a group of actions
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO
Keyword used:
DOWHILE, ENDDO
Meaningful names
• When designing a solution algorithm, a programmer
should introduce unique names, which are:
– Represent the variables or objects in the problem
– Meaningful
example: number1, number2, number3  more
meaningful than A, B, C
- Used word separator if more than one word
example: sales_tax, word_count
- Or Capital letter as separator
example: salesTax, wordCount
The Structure Theorem
• It is possible to write any computer program
by using only three basic control structures
that are easily represented in pseudocode:
»Sequence
»Selection
»Repetition
Sequence
• Add 1 to pageCount
• Print heading line 1
• Print heading line 2
• Set lineCount to zero
• Read customer record
Is the straightforward execution of one processing step after another.
Statement a
Statement b
Statement c
Selection
IF condition p is true THEN
statement(s) in true case
ELSE
statement(s) in false case
ENDIF
Presentation of condition and the choice between two actions
Example:
IF student_attendance_status is part_time THEN
add 1 to part_time_count
ELSE
add 1 to full_time_count
ENDI>f
Repetition
DOWHILE condition p is true
statement block
ENDDO
The presentation of a set of instructions to be performed repeatedly,
as long as a condition is true
Example:
Set student_total to zero
DOWHILE student_total < 50
Read student record
Print student name, address to report
Add 1 to student_total
ENDDO
DESIGNING A SOLUTION ALGORITHM
• The most challengin task in the life cycle of a
program
• First attempt usually doesnt result in a
finished product
• Keep altering the step and algorithms till
satesfied result achieved.
Point to be considered in Solution Algorithm
1. A name should be given to the algorithm, which is
describe the function of algorithm
2. An END statement used to indicate the algorithm is
complete
3. All processing steps between the algorithm name
and END statement should be indented for
readability.
4. Each processing step in the defining diagram
relates directly to one or more statements in the
algorithm.
Example 3.1. Solution Algorithm for example 2.1
 A program is required to read
three numbers, add them together
and print their total.
• Defining diagram
Input Processing Output
Number1
Number2
Number3
total
Solution Algorithm
• Add_three_numbers
Read number1, number2, number3
Total = number1 + number2 + number3
Print total
END
• Comments : //
• Blocks :{and }.
• Statements are delimited by ;.
• An identifier begins with a letter.
• The data types of variables are not explicitly
declared.
• Assignment of values to variables
(variable):=(expression);
• Elements of multidimensional arrays are
accessed using[ and ].
• Looping statements :for,while,and repeat-
until.
• The while loop takes the following form:
while(condition) do
{ (statement 1)
(statement n)
}
• The general form of a for loop is
for variable:=value l to value 2 step do
{ (statement 1)
(statement n)
}
• A repeat-until statement is constructed as
follows:
repeat
(statement 1)
(statement n)
Until(condition)
• A conditional statement has the following
forms:
– if (condition) then (statement)
– if (condition) then (statement1) else (statement2)
– case
{
: (condition 1): (statement1)
: (condition n): (statement n)
:else:(statement n + 1) }
Recursive Algorithms
• A recursive function is a function that is
defined in terms of itself.
• Similarly, an algorithm is said to be recursive if
the same algorithm is invoked in the body.
• An algorithm that calls itself is direct recursive.
• Algorithm A is said to be indirect recursive if it
calls another algorithm which in turn calls A.
Recursion
• In some problems, it may be natural to define
the problem in terms of the problem itself.
• Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
• Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
Tower of Hanoi
• The disks must be moved within one week.
Assume one disk can be moved in 1 second. Is
this possible?
• To create an algorithm to solve this problem, it
is convenient to generalize the problem to the
“N-disk” problem, where in our case N = 64.
Recursive Solution
Recursive Solution
Recursive Solution
Recursive Solution
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Recursive Algorithm
void Hanoi(int n, string a, string b, string c)
{
if (n == 1) /* base case */
Move(a,b);
else { /* recursion */
Hanoi(n-1,a,c,b);
Move(a,b);
Hanoi(n-1,c,b,a);
}
}
Performance Analysis
Any given problem may be solved by a number of algorithms.
To judge an algorithm there are many criteria. Some of
them are:
1. It must work correctly under all possible condition
2. It must solve the problem according to the given
specification
3. It must be clearly written following the top down strategy
4. It must make efficient use of time and resources
5. It must be sufficiently documented so that anybody can
understand it
6. It must be easy to modify, if required.
7. It should not be dependent on being run on a particular
computer.
• Performance analysis of an algorithm depends
upon two factors
• amount of memory used
• amount of compute time consumed on any
CPU.
• Formally they are notified as complexities in
terms of:
– Space Complexity.
– Time Complexity.
• Space Complexity of an algorithm is the amount of memory it
needs to run to completion i.e. from start of execution to its
termination. Space need by any algorithm is the sum of following
components:
• Fixed Component: This is independent of the characteristics of the
inputs and outputs. This part includes: Instruction Space, Space of
simple variables, fixed size component variables, and constants
variables.
• Variable Component: This consist of the space needed by
component variables whose size is dependent on the particular
problems instances(Inputs/Outputs) being solved, the space
needed by referenced variables and the recursion stack space is one
of the most prominent components. Also this included the data
structure components like Linked list, heap, trees, graphs etc.
• Therefore the total space requirement of any algorithm 'A' can be
provided as
• Space(A) = Fixed Components(A) + Variable Components(A)
{
int z = a + b + c;
return(z);
}
Space(A) = Fixed Components(A) + Variable Components(A)
Space = 3
int sum(int a[], int n)
{
int x = 0; // 4 bytes for x
for(int i = 0; i < n; i++) // 4 bytes for i
{
x = x + a[i];
}
return(x);
}
Space(A) = Fixed Components(A) + Variable Components(A)
Space = 3 + n
AlgorithmRSum(a,n)
{
if (n <0) thenreturn0.0;
Else return RSum (a,n-1)+ a[n];
}
Space(A) = Fixed Components(A) + Variable Components(A)
Space > = 3(n+1)
• Time Complexity of an algorithm(basically when
converted to program) is the amount of computer time
it needs to run to completion.
• The time taken by a program is the sum of the compile
time and the run/execution time .
• The compile time is independent of the
instance(problem specific) characteristics.
• following factors effect the time complexity:
– Characteristics of compiler used to compile the program.
– Computer Machine on which the program is executed and
physically clocked.
– Multiuser execution system.
– Number of program steps.
• Time(A) = Fixed Time(A) + Instance Time(A)
• Sum(a,b)
{
return a+b
}
Time(A) = Fixed Time(A) + Instance Time(A)
Time (A) = C+2
Statement
Steps per
execution
Frequency Total Steps
Algorithm Sum(number,size)
0 -
0
{ 0 - 0
result=0.0;
1 1 1
for count = 1 to size do
1 size+1 size + 1
result= result + number[count];
1 size size
return result;
1 1 1
} 0 - 0
Total 2size + 3
Design and Analysis of Algorithms.pptx
Asymptotic Analysis
• Asymptotic analysis of an algorithm refers to
defining the mathematical boundation
/framing of its run-time performance.
• Using asymptotic analysis, we can very well
conclude the best case, average case, and
worst case scenario of an algorithm.
• Asymptotic analysis refers to computing the
running time of any operation in
mathematical units of computation.
Why is Asymptotic Notation
Important?
1. They give simple characteristics of an
algorithm's efficiency.
2. They allow the comparisons of the
performances of various algorithms.
• Asymptotic Notation is a way of comparing function that ignores
constant factors and small input sizes. Three notations are used to
calculate the running time complexity of an algorithm:
• O (Big-oh)
• Ω (omega)
• ϴ (Theta)
• Usually, the time required by an algorithm falls under three types −
• Best Case − Minimum time required for program execution.
• Average Case − Average time required for program execution.
• Worst Case − Maximum time required for program execution.
O (Big-oh)
• Big-oh is the formal method of expressing the
upper bound of an algorithm's running time.
• It measures the worst case time complexity or
the longest amount of time an algorithm can
possibly take to complete.
For example, for a function f(n)
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
Ω (omega)
• The notation Ω(n) is the formal way to express
the lower bound of an algorithm's running
time.
• It measures the best case time complexity or
the best amount of time an algorithm can
possibly take to complete.
For example, for a function f(n)
Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
• The notation θ(n) is the formal way to express
both the lower bound and the upper bound of
an algorithm's running time.
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
Amortize Analysis
• Amortized analysis is used for algorithms that
have expensive operations that happen
only rarely.
• Amortized complexity analysis is most commonly
used with data structures that have state that
persists between operations.
• The basic idea is that an expensive operation can
alter the state so that the worst case cannot
occur again for a long time, thus amortizing its
cost.
• The aggregate method, where the total running time
for a sequence of operations is analyzed.
• The accounting (or banker's) method, where we
impose an extra charge on inexpensive operations and
use it to pay for expensive operations later on.
• The potential (or physicist's) method, in which we
derive a potential function characterizing the amount
of extra work we can do in each step. This potential
either increases or decreases with each successive
operation, but cannot be negative.
Dynamic Table
Design and Analysis of Algorithms.pptx
Accounting Method
• Increase cost of some operations to bet credit
for others.
• For example consider a stack operations
Cost Amortized
cost
Push 1 2
Pop 1 0
Multi pop Min(k,n) 0
Potential Method
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 Suggest splitting the inputs into k distinct sub
problems ( 1≤ k ≤ n).
 These sub problems must be solved.
 A method must be found to combine the sub
solutions into a solution of the whole problem.
• If the sub problems are still relatively large
then the divide and conquer strategy can
possibly be reapplied.
Design and Analysis of Algorithms.pptx
Detecting a Counterfeit Coin
• Given a set of coins
• Exactly one coin is defective: it has lesser weight
• Which one is it?
Detecting a Counterfeit Coin Example:
n= 16
After one comparison
After Two comparisons
After Three comparisons
After Four comparisons
Design and Analysis of Algorithms.pptx
• Time Complexity of many divide-and-conquer
algorithms is given by recurrences of the form
• where a,b are constants
• split the problem into a ≥ 1 sub problems of
size n/b where b >1.
Defective Chessboard
• Given a chess board of size n × n where n = 2k
• With exactly one defective square
• How do we cover it completely with L-shaped
tiles?
• L-shaped tiles : trominoes or triominoes
Defective Chessboard
K=0 k=1
K=2
A Divide Conquer Solution
• Size k=1 (i.e. 2×2) problem has a simple
solution.
• Divide larger boards into smaller ones.
• Combine solutions to smaller problems.
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
Ex: Tiling a 8×8 Defective Chessboard
• ai 1≤ i ≤ n is a list of elements that are sorted
in nondecreasing order.
• Determine whether a given element x is
present in the list.
• MID = low + high
2
• If x < mid, then high = mid-1
• If x > mid, then low = mid+1
Example
1 10 55 66 68 85 92 101 110
Number to Search
index 0 1 2 3 4 5 6 7 8
Mid = low + high
2
= 0 + 8
2
= 4
85 > 68 85 < 92
Example
1 10 55 66 68 85 92 101 110
Number to Search
index 0 1 2 3 4 5 6 7 8
Mid = low + high
2
= 5 + 8
2
= 6. 5
85 > 68 85 < 92
Example
1 10 55 66 68 85 92 101 110
Number to Search
index 0 1 2 3 4 5 6 7 8
Mid = low + high
2
= 5 + 7
2
= 6
85 < 92
Example
1 10 55 66 68 85 92 101 110
Number to Search
index 0 1 2 3 4 5 6 7 8
Mid = low + high
2
= 5 + 6
2
= 5.5
85 = 85
Pseudo Code for Binary Search
Procedure binary_search
A ← sorted array
n ← size of array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while x not found
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
end procedure
6 3 17 25 31 55 62 53 36 42 47 29 8 4 12
Finding the Maximum and Minimum
• Find the maximum and minimum items in a
set of n elements.
• Divide and Conquer Approach
 the array is divided into two halves.
 maximum and minimum numbers in each halves
are found.
 return the maximum of two maxima of each half
and the minimum of two minima of each half.
82 36 49 91 12 14 6 76 92 55
82 36 49 91 12 14 6 76 92 55
82 36 49 91 12 14 6 76 92 55
82 36 49 14 6 76
91 12 92 55
82 36 49 14 6 76
49 76
Max Min
92 55
Max Min
91 6
Max Min
82 36
Max
92
Min
6
Design and Analysis of Algorithms.pptx
Time Complexity
Merge Sort
• Merge Sort is a Divide and Conquer algorithm.
• It divides input array in two halves, calls itself
for the two halves and then merges the two
sorted halves.
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 5 3 9 4 1 7
2 8 3 5 4 9 1 7
2 3 5 8 1 4 7 9
1 2 3 4 5 7 8 9
Design and Analysis of Algorithms.pptx
Time Complexity
Quick Sort
• Sorting Algorithm
• It finds the element called pivot which divides
the array into two halves.
• Elements in the first half are smaller than the
pivot.
• Elements in the second half are greater than
the pivot.
Divide and Conquer Solution
• Find the pivot that divides the array into two
halves.
• Quick sort the left half.
• Quick sort the right half.
0 1 2 3 4 5
5 2 6 1 3 4
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Right = 4
Is Pivot < right?
No
0 1 2 3 4 5
4 2 6 1 3 5
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Left = 4
Is Pivot > left?
Yes
0 1 2 3 4 5
4 2 6 1 3 5
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Left =2
Is Pivot > left?
Yes
0 1 2 3 4 5
4 2 6 1 3 5
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Left =6
Is Pivot > left?
No
0 1 2 3 4 5
4 2 5 1 3 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Right =6
Is Pivot < Right?
Yes
0 1 2 3 4 5
4 2 5 1 3 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Right =3
Is Pivot < Right?
No
0 1 2 3 4 5
4 2 3 1 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Left =3
Is Pivot > Left?
Yes
0 1 2 3 4 5
4 2 3 1 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 5
Left =1
Is Pivot > Left?
Yes
0 1 2 3 4 5
4 2 3 1 5 6
Array Index
Array Elements
Left Right
Pivot
0 1 2 3 4 5
4 2 3 1 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 4
Right =1
Is Pivot < Right?
No
0 1 2 3 4 5
1 2 3 4 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 4
Left =1
Is Pivot > Left
Yes
0 1 2 3 4 5
1 2 3 4 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 4
Left =2
Is Pivot > Left
Yes
0 1 2 3 4 5
1 2 3 4 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 4
Left =3
Is Pivot > Left
Yes
0 1 2 3 4 5
1 2 3 4 5 6
Array Index
Array Elements
Left Right
Pivot
Pivot = 1
Right =3
Is Pivot < right
Yes
Design and Analysis of Algorithms.pptx
Selection Sort
• Sorting Algorithm
• Initially whole array is unsorted.
• Considered into two parts
 Unsorted
 Sorted
• Selection : select the lowest element in the
remaining array.
• Swapping : Bring it to the starting position.
• Counter shift: Change the counter for
unsorted array be one.
64 25 12 22 11
Sorted Array Unsorted Array
64 25 12 12 11
11 25 12 22 64
Sorted Array Unsorted Array
25 12
11 12 25 22 64
Sorted Array Unsorted Array
25 22
11 12 22 25 64
Sorted Array Unsorted Array
Design and Analysis of Algorithms.pptx
Strassen’s Matrix Multiplication

More Related Content

PPT
UNIT-1-PPTS-DAA.ppt
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PPT
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
PPT
UNIT 1- Design Analysis of algorithms and its working
PDF
UNIT-1-PdjfjfjfjfjfjfjfjfjfjfjPTS-DAA.pdf
PDF
UNIT-1-PPTS-DAA_cofjfjvjcjcncnfncmpressed.pdf
PPT
UNIT-1-PPTS-DAA_INTRODUCTION_TO_DAA_GH.ppt
PPT
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT 1- Design Analysis of algorithms and its working
UNIT-1-PdjfjfjfjfjfjfjfjfjfjfjPTS-DAA.pdf
UNIT-1-PPTS-DAA_cofjfjvjcjcncnfncmpressed.pdf
UNIT-1-PPTS-DAA_INTRODUCTION_TO_DAA_GH.ppt
UNIT-1-PPTS-DAA.ppt

Similar to Design and Analysis of Algorithms.pptx (20)

PPT
Introduction to Design Algorithm And Analysis.ppt
PPTX
Basic syntax : Algorithm,Flow chart
PPTX
vingautosaved-230525024624-6a6fb3b2.pptx
PPTX
01 Introduction to analysis of Algorithms.pptx
PPTX
Algorithm Design and Problem Solving [Autosaved].pptx
PPT
UNIT-2-PPTS-DAA.ppt
PPTX
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
PPTX
Design and Analysis of Algorithm ppt for unit one
PDF
Lecture 2 role of algorithms in computing
PPTX
session-1_Design_Analysis_Algorithm.pptx
PPTX
Unit-1_PPS_H6nS4p8J1iooinoinoinoink.pptx
PPTX
Introduction ,characteristics, properties,pseudo code conventions
PPT
DAA-Introduction-Divide and Conquer Technique
PPTX
Design and analysis of algorithms Module-I.pptx
PPTX
UNIT 1.pptx
PPTX
Algorithm & data structure lec2
PPT
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
PDF
As Level Computer Science Book -2
PPT
lec_4_data_structures_and_algorithm_analysis.ppt
PPT
lec_4_data_structures_and_algorithm_analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
Basic syntax : Algorithm,Flow chart
vingautosaved-230525024624-6a6fb3b2.pptx
01 Introduction to analysis of Algorithms.pptx
Algorithm Design and Problem Solving [Autosaved].pptx
UNIT-2-PPTS-DAA.ppt
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
Design and Analysis of Algorithm ppt for unit one
Lecture 2 role of algorithms in computing
session-1_Design_Analysis_Algorithm.pptx
Unit-1_PPS_H6nS4p8J1iooinoinoinoink.pptx
Introduction ,characteristics, properties,pseudo code conventions
DAA-Introduction-Divide and Conquer Technique
Design and analysis of algorithms Module-I.pptx
UNIT 1.pptx
Algorithm & data structure lec2
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
As Level Computer Science Book -2
lec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.ppt
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
PPT on Performance Review to get promotions
PDF
Well-logging-methods_new................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Project quality management in manufacturing
PPTX
web development for engineering and engineering
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
composite construction of structures.pdf
R24 SURVEYING LAB MANUAL for civil enggi
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
UNIT 4 Total Quality Management .pptx
PPT on Performance Review to get promotions
Well-logging-methods_new................
bas. eng. economics group 4 presentation 1.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
CH1 Production IntroductoryConcepts.pptx
Digital Logic Computer Design lecture notes
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Project quality management in manufacturing
web development for engineering and engineering
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
Ad

Design and Analysis of Algorithms.pptx

  • 2. What is an Algorithm? • The word algorithm comes from the name of a Persian author, Abu Ja'far Mohammed ibn Musa al Khowarizmi(c.825A.D.), who wrote a textbook on mathematics. • This word has taken on a special significance in computer science, where "algorithm" has come to refer to a method that can be used by a computer for the solution of a problem. • This is what makes algorithm different from words such as process, technique, or method.
  • 3. Definition: • An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition, all algorithms must satisfy the following criteria: 1. Input: Zero or more quantities are externally supplied. 2. Output: At least one quantity is produced. 3. Definiteness: Each instruction is clear and unambiguous. 4. Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps. 5. Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person using only pencil and paper. It is not enough that each operation be definite as in criterion3; it also must be feasible.
  • 4. What is Pseudocode? • One of the popular representation of Algorithm • Widely choosen because: – easy to read and write – allow the programmer to concentrate on the logic of the problem – Structured in English language
  • 5. Pseudocode Convention • Statement are written in simple English • Each instruction is written on a separate line • Keywords and indentation are used to signify particular control structures. • Each set of instructions is written from top to bottom, with only one entry and one exit. • Groups of statements may be formed into modules, and that group given a name.
  • 6. Six Basic Computer Operations 1. A computer can receive information Read student name Get system date Read number_1, number_2 Get tax_code Verb used: •Read  used when the algorithm is to receive the input from a record on a file •Get  used when the algorithm is to receive input from the keyboard.
  • 7. 2. A computer can put out information Print `Program Completed´ Write customer record to master file Put out name, address and postcode Output total_tax Display ´End of data´ Prompt for student_mark Get student_mark Verb used: •Print  used when the output is to be sent to the printer •Write  used when the output is to be written to a file •Put, Output, Display  used when the output is to be written to the screen •Prompt  required before an input instruction Get, causes the message to be sent to the screen which requires the user responds, usually by providing input.
  • 8. 3. A computer can perform arithmetic Add number to total Total = total + number Divide total_marks by student_count Sales_tax = cost_price * 0.10 Compute C = (F – 32) * 5/9 Verb used: •Compute •Calculate •Symbols used: +, -, *, /, ()
  • 9. 4. A computer can assign a value to a variable or memory location • Three cases : 1. To give data an initial value in pseudocode, the verbs Initialise or Set are used 2. To assign a value as a result of some processing, the symbols ´=´or ´´ are written 3. To keep a variable for later use, the verbs Save or Store are used. Initialize total_price to zero Set student_count to 0 Total_price = cost_price + sales_tax Total_price  cost_price + sales_tax Store customer_num in last_customer_num
  • 10. 5. A computer can compare two variables and select one of two alternate actions IF student_attendance_status is part_time THEN add 1 to part_time_count ELSE Add 1 to full_time_count ENDIF Keyword used: IF, THEN, ELSE
  • 11. 6. A computer can repeat a group of actions DOWHILE student_total < 50 Read student record Print student name, address to report Add 1 to student_total ENDDO Keyword used: DOWHILE, ENDDO
  • 12. Meaningful names • When designing a solution algorithm, a programmer should introduce unique names, which are: – Represent the variables or objects in the problem – Meaningful example: number1, number2, number3  more meaningful than A, B, C - Used word separator if more than one word example: sales_tax, word_count - Or Capital letter as separator example: salesTax, wordCount
  • 13. The Structure Theorem • It is possible to write any computer program by using only three basic control structures that are easily represented in pseudocode: »Sequence »Selection »Repetition
  • 14. Sequence • Add 1 to pageCount • Print heading line 1 • Print heading line 2 • Set lineCount to zero • Read customer record Is the straightforward execution of one processing step after another. Statement a Statement b Statement c
  • 15. Selection IF condition p is true THEN statement(s) in true case ELSE statement(s) in false case ENDIF Presentation of condition and the choice between two actions Example: IF student_attendance_status is part_time THEN add 1 to part_time_count ELSE add 1 to full_time_count ENDI>f
  • 16. Repetition DOWHILE condition p is true statement block ENDDO The presentation of a set of instructions to be performed repeatedly, as long as a condition is true Example: Set student_total to zero DOWHILE student_total < 50 Read student record Print student name, address to report Add 1 to student_total ENDDO
  • 17. DESIGNING A SOLUTION ALGORITHM • The most challengin task in the life cycle of a program • First attempt usually doesnt result in a finished product • Keep altering the step and algorithms till satesfied result achieved.
  • 18. Point to be considered in Solution Algorithm 1. A name should be given to the algorithm, which is describe the function of algorithm 2. An END statement used to indicate the algorithm is complete 3. All processing steps between the algorithm name and END statement should be indented for readability. 4. Each processing step in the defining diagram relates directly to one or more statements in the algorithm.
  • 19. Example 3.1. Solution Algorithm for example 2.1  A program is required to read three numbers, add them together and print their total.
  • 20. • Defining diagram Input Processing Output Number1 Number2 Number3 total
  • 21. Solution Algorithm • Add_three_numbers Read number1, number2, number3 Total = number1 + number2 + number3 Print total END
  • 22. • Comments : // • Blocks :{and }. • Statements are delimited by ;. • An identifier begins with a letter. • The data types of variables are not explicitly declared. • Assignment of values to variables (variable):=(expression);
  • 23. • Elements of multidimensional arrays are accessed using[ and ]. • Looping statements :for,while,and repeat- until. • The while loop takes the following form: while(condition) do { (statement 1) (statement n) }
  • 24. • The general form of a for loop is for variable:=value l to value 2 step do { (statement 1) (statement n) } • A repeat-until statement is constructed as follows: repeat (statement 1) (statement n) Until(condition)
  • 25. • A conditional statement has the following forms: – if (condition) then (statement) – if (condition) then (statement1) else (statement2) – case { : (condition 1): (statement1) : (condition n): (statement n) :else:(statement n + 1) }
  • 26. Recursive Algorithms • A recursive function is a function that is defined in terms of itself. • Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body. • An algorithm that calls itself is direct recursive. • Algorithm A is said to be indirect recursive if it calls another algorithm which in turn calls A.
  • 27. Recursion • In some problems, it may be natural to define the problem in terms of the problem itself. • Recursion is useful for problems that can be represented by a simpler version of the same problem. • Example: the factorial function 6! = 6 * 5 * 4 * 3 * 2 * 1 We could write: 6! = 6 * 5!
  • 28. Tower of Hanoi • The disks must be moved within one week. Assume one disk can be moved in 1 second. Is this possible? • To create an algorithm to solve this problem, it is convenient to generalize the problem to the “N-disk” problem, where in our case N = 64.
  • 41. Recursive Algorithm void Hanoi(int n, string a, string b, string c) { if (n == 1) /* base case */ Move(a,b); else { /* recursion */ Hanoi(n-1,a,c,b); Move(a,b); Hanoi(n-1,c,b,a); } }
  • 42. Performance Analysis Any given problem may be solved by a number of algorithms. To judge an algorithm there are many criteria. Some of them are: 1. It must work correctly under all possible condition 2. It must solve the problem according to the given specification 3. It must be clearly written following the top down strategy 4. It must make efficient use of time and resources 5. It must be sufficiently documented so that anybody can understand it 6. It must be easy to modify, if required. 7. It should not be dependent on being run on a particular computer.
  • 43. • Performance analysis of an algorithm depends upon two factors • amount of memory used • amount of compute time consumed on any CPU. • Formally they are notified as complexities in terms of: – Space Complexity. – Time Complexity.
  • 44. • Space Complexity of an algorithm is the amount of memory it needs to run to completion i.e. from start of execution to its termination. Space need by any algorithm is the sum of following components: • Fixed Component: This is independent of the characteristics of the inputs and outputs. This part includes: Instruction Space, Space of simple variables, fixed size component variables, and constants variables. • Variable Component: This consist of the space needed by component variables whose size is dependent on the particular problems instances(Inputs/Outputs) being solved, the space needed by referenced variables and the recursion stack space is one of the most prominent components. Also this included the data structure components like Linked list, heap, trees, graphs etc. • Therefore the total space requirement of any algorithm 'A' can be provided as • Space(A) = Fixed Components(A) + Variable Components(A)
  • 45. { int z = a + b + c; return(z); } Space(A) = Fixed Components(A) + Variable Components(A) Space = 3
  • 46. int sum(int a[], int n) { int x = 0; // 4 bytes for x for(int i = 0; i < n; i++) // 4 bytes for i { x = x + a[i]; } return(x); } Space(A) = Fixed Components(A) + Variable Components(A) Space = 3 + n
  • 47. AlgorithmRSum(a,n) { if (n <0) thenreturn0.0; Else return RSum (a,n-1)+ a[n]; } Space(A) = Fixed Components(A) + Variable Components(A) Space > = 3(n+1)
  • 48. • Time Complexity of an algorithm(basically when converted to program) is the amount of computer time it needs to run to completion. • The time taken by a program is the sum of the compile time and the run/execution time . • The compile time is independent of the instance(problem specific) characteristics. • following factors effect the time complexity: – Characteristics of compiler used to compile the program. – Computer Machine on which the program is executed and physically clocked. – Multiuser execution system. – Number of program steps. • Time(A) = Fixed Time(A) + Instance Time(A)
  • 49. • Sum(a,b) { return a+b } Time(A) = Fixed Time(A) + Instance Time(A) Time (A) = C+2
  • 50. Statement Steps per execution Frequency Total Steps Algorithm Sum(number,size) 0 - 0 { 0 - 0 result=0.0; 1 1 1 for count = 1 to size do 1 size+1 size + 1 result= result + number[count]; 1 size size return result; 1 1 1 } 0 - 0 Total 2size + 3
  • 52. Asymptotic Analysis • Asymptotic analysis of an algorithm refers to defining the mathematical boundation /framing of its run-time performance. • Using asymptotic analysis, we can very well conclude the best case, average case, and worst case scenario of an algorithm. • Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation.
  • 53. Why is Asymptotic Notation Important? 1. They give simple characteristics of an algorithm's efficiency. 2. They allow the comparisons of the performances of various algorithms.
  • 54. • Asymptotic Notation is a way of comparing function that ignores constant factors and small input sizes. Three notations are used to calculate the running time complexity of an algorithm: • O (Big-oh) • Ω (omega) • ϴ (Theta) • Usually, the time required by an algorithm falls under three types − • Best Case − Minimum time required for program execution. • Average Case − Average time required for program execution. • Worst Case − Maximum time required for program execution.
  • 55. O (Big-oh) • Big-oh is the formal method of expressing the upper bound of an algorithm's running time. • It measures the worst case time complexity or the longest amount of time an algorithm can possibly take to complete.
  • 56. For example, for a function f(n) Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
  • 57. Ω (omega) • The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. • It measures the best case time complexity or the best amount of time an algorithm can possibly take to complete.
  • 58. For example, for a function f(n) Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
  • 59. Theta Notation, θ • The notation θ(n) is the formal way to express both the lower bound and the upper bound of an algorithm's running time.
  • 60. θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
  • 61. Amortize Analysis • Amortized analysis is used for algorithms that have expensive operations that happen only rarely. • Amortized complexity analysis is most commonly used with data structures that have state that persists between operations. • The basic idea is that an expensive operation can alter the state so that the worst case cannot occur again for a long time, thus amortizing its cost.
  • 62. • The aggregate method, where the total running time for a sequence of operations is analyzed. • The accounting (or banker's) method, where we impose an extra charge on inexpensive operations and use it to pay for expensive operations later on. • The potential (or physicist's) method, in which we derive a potential function characterizing the amount of extra work we can do in each step. This potential either increases or decreases with each successive operation, but cannot be negative.
  • 65. Accounting Method • Increase cost of some operations to bet credit for others. • For example consider a stack operations Cost Amortized cost Push 1 2 Pop 1 0 Multi pop Min(k,n) 0
  • 70.  Suggest splitting the inputs into k distinct sub problems ( 1≤ k ≤ n).  These sub problems must be solved.  A method must be found to combine the sub solutions into a solution of the whole problem. • If the sub problems are still relatively large then the divide and conquer strategy can possibly be reapplied.
  • 72. Detecting a Counterfeit Coin • Given a set of coins • Exactly one coin is defective: it has lesser weight • Which one is it?
  • 73. Detecting a Counterfeit Coin Example: n= 16
  • 79. • Time Complexity of many divide-and-conquer algorithms is given by recurrences of the form • where a,b are constants • split the problem into a ≥ 1 sub problems of size n/b where b >1.
  • 80. Defective Chessboard • Given a chess board of size n × n where n = 2k • With exactly one defective square • How do we cover it completely with L-shaped tiles? • L-shaped tiles : trominoes or triominoes
  • 82. A Divide Conquer Solution • Size k=1 (i.e. 2×2) problem has a simple solution. • Divide larger boards into smaller ones. • Combine solutions to smaller problems.
  • 83. Ex: Tiling a 8×8 Defective Chessboard
  • 84. Ex: Tiling a 8×8 Defective Chessboard
  • 85. Ex: Tiling a 8×8 Defective Chessboard
  • 86. Ex: Tiling a 8×8 Defective Chessboard
  • 87. • ai 1≤ i ≤ n is a list of elements that are sorted in nondecreasing order. • Determine whether a given element x is present in the list. • MID = low + high 2 • If x < mid, then high = mid-1 • If x > mid, then low = mid+1
  • 88. Example 1 10 55 66 68 85 92 101 110 Number to Search index 0 1 2 3 4 5 6 7 8 Mid = low + high 2 = 0 + 8 2 = 4 85 > 68 85 < 92
  • 89. Example 1 10 55 66 68 85 92 101 110 Number to Search index 0 1 2 3 4 5 6 7 8 Mid = low + high 2 = 5 + 8 2 = 6. 5 85 > 68 85 < 92
  • 90. Example 1 10 55 66 68 85 92 101 110 Number to Search index 0 1 2 3 4 5 6 7 8 Mid = low + high 2 = 5 + 7 2 = 6 85 < 92
  • 91. Example 1 10 55 66 68 85 92 101 110 Number to Search index 0 1 2 3 4 5 6 7 8 Mid = low + high 2 = 5 + 6 2 = 5.5 85 = 85
  • 92. Pseudo Code for Binary Search Procedure binary_search A ← sorted array n ← size of array x ← value to be searched Set lowerBound = 1 Set upperBound = n while x not found if upperBound < lowerBound EXIT: x does not exists. set midPoint = lowerBound + ( upperBound - lowerBound ) / 2 if A[midPoint] < x set lowerBound = midPoint + 1 if A[midPoint] > x set upperBound = midPoint - 1 if A[midPoint] = x EXIT: x found at location midPoint end while end procedure
  • 93. 6 3 17 25 31 55 62 53 36 42 47 29 8 4 12
  • 94. Finding the Maximum and Minimum • Find the maximum and minimum items in a set of n elements. • Divide and Conquer Approach  the array is divided into two halves.  maximum and minimum numbers in each halves are found.  return the maximum of two maxima of each half and the minimum of two minima of each half.
  • 95. 82 36 49 91 12 14 6 76 92 55 82 36 49 91 12 14 6 76 92 55 82 36 49 91 12 14 6 76 92 55 82 36 49 14 6 76
  • 96. 91 12 92 55 82 36 49 14 6 76 49 76 Max Min 92 55 Max Min 91 6 Max Min 82 36 Max 92 Min 6
  • 99. Merge Sort • Merge Sort is a Divide and Conquer algorithm. • It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
  • 100. 2 8 5 3 9 4 1 7 2 8 5 3 9 4 1 7 2 8 5 3 9 4 1 7 2 8 5 3 9 4 1 7
  • 101. 2 8 5 3 9 4 1 7 2 8 3 5 4 9 1 7 2 3 5 8 1 4 7 9 1 2 3 4 5 7 8 9
  • 104. Quick Sort • Sorting Algorithm • It finds the element called pivot which divides the array into two halves. • Elements in the first half are smaller than the pivot. • Elements in the second half are greater than the pivot.
  • 105. Divide and Conquer Solution • Find the pivot that divides the array into two halves. • Quick sort the left half. • Quick sort the right half.
  • 106. 0 1 2 3 4 5 5 2 6 1 3 4 Array Index Array Elements Left Right Pivot Pivot = 5 Right = 4 Is Pivot < right? No
  • 107. 0 1 2 3 4 5 4 2 6 1 3 5 Array Index Array Elements Left Right Pivot Pivot = 5 Left = 4 Is Pivot > left? Yes
  • 108. 0 1 2 3 4 5 4 2 6 1 3 5 Array Index Array Elements Left Right Pivot Pivot = 5 Left =2 Is Pivot > left? Yes
  • 109. 0 1 2 3 4 5 4 2 6 1 3 5 Array Index Array Elements Left Right Pivot Pivot = 5 Left =6 Is Pivot > left? No
  • 110. 0 1 2 3 4 5 4 2 5 1 3 6 Array Index Array Elements Left Right Pivot Pivot = 5 Right =6 Is Pivot < Right? Yes
  • 111. 0 1 2 3 4 5 4 2 5 1 3 6 Array Index Array Elements Left Right Pivot Pivot = 5 Right =3 Is Pivot < Right? No
  • 112. 0 1 2 3 4 5 4 2 3 1 5 6 Array Index Array Elements Left Right Pivot Pivot = 5 Left =3 Is Pivot > Left? Yes
  • 113. 0 1 2 3 4 5 4 2 3 1 5 6 Array Index Array Elements Left Right Pivot Pivot = 5 Left =1 Is Pivot > Left? Yes
  • 114. 0 1 2 3 4 5 4 2 3 1 5 6 Array Index Array Elements Left Right Pivot
  • 115. 0 1 2 3 4 5 4 2 3 1 5 6 Array Index Array Elements Left Right Pivot Pivot = 4 Right =1 Is Pivot < Right? No
  • 116. 0 1 2 3 4 5 1 2 3 4 5 6 Array Index Array Elements Left Right Pivot Pivot = 4 Left =1 Is Pivot > Left Yes
  • 117. 0 1 2 3 4 5 1 2 3 4 5 6 Array Index Array Elements Left Right Pivot Pivot = 4 Left =2 Is Pivot > Left Yes
  • 118. 0 1 2 3 4 5 1 2 3 4 5 6 Array Index Array Elements Left Right Pivot Pivot = 4 Left =3 Is Pivot > Left Yes
  • 119. 0 1 2 3 4 5 1 2 3 4 5 6 Array Index Array Elements Left Right Pivot Pivot = 1 Right =3 Is Pivot < right Yes
  • 121. Selection Sort • Sorting Algorithm • Initially whole array is unsorted. • Considered into two parts  Unsorted  Sorted
  • 122. • Selection : select the lowest element in the remaining array. • Swapping : Bring it to the starting position. • Counter shift: Change the counter for unsorted array be one.
  • 123. 64 25 12 22 11 Sorted Array Unsorted Array 64 25 12 12 11
  • 124. 11 25 12 22 64 Sorted Array Unsorted Array 25 12
  • 125. 11 12 25 22 64 Sorted Array Unsorted Array 25 22
  • 126. 11 12 22 25 64 Sorted Array Unsorted Array