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

Unit I

The syllabus outlines the fundamentals of algorithms, including their definition, properties, and characteristics, as well as the process of algorithm design and analysis. It covers various algorithm types, efficiency analysis, and methods for proving correctness, alongside empirical and visualization techniques. The document emphasizes the importance of understanding algorithms for problem-solving in computer science.

Uploaded by

OLGA RAJEE C
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)
3 views

Unit I

The syllabus outlines the fundamentals of algorithms, including their definition, properties, and characteristics, as well as the process of algorithm design and analysis. It covers various algorithm types, efficiency analysis, and methods for proving correctness, alongside empirical and visualization techniques. The document emphasizes the importance of understanding algorithms for problem-solving in computer science.

Uploaded by

OLGA RAJEE C
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/ 31

SYLLABUS

UNIT I – Introduction
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving –
Important Problem Types –Fundamentals of the Analysis of Algorithm
Efficiency – Analysis Framework - Asymptotic Notations and their properties
– Empirical analysis - Mathematical analysis of Recursive and Non-recursive
algorithms – Visualization.
INTRODUCTION

Unit I
WHAT IS AN ALGORITHM?

An algorithm is a sequence of unambiguous instructions for


solving a problem that transforms any legitimate input into required output
in a finite amount of time.
EXAMPLE
Euclid’s Algorithm for GCD Pseudocode Notation
Step 1: Divide m by n and Algorithm GCD – Euclid( m, n )
let r be the remainder. begin
while n ≠ 0
Step 2: If r=0, the algorithm begin
terminates and n is the
r  m mod n;
result.
m  n;
n  r;
Step 3: Otherwise, Let
current value of n be m and end
current value of r be n. Go return( n );
back to Step1.
end
EXAMPLE (continued….)

Consecutive integer checking algorithm Middle School Procedure for GCD( m,


– GCD( m, n ) n)
Step 1: Assign the value of min { m, n } to t. Step 1: Find the prime factors of m.

Step 2: Divide m by t. If remainder is 0, go to Step 2: Find the prime factors of n.


Step 3: Identify all the common factors in Step 1
Step 3; Otherwise go to Step 4.
and 2.
Step 3: Divide n by t. If remainder is 0, return t
Step 4: Compute the product of all the common
and stop; Otherwise, proceed to Step 4.
factors and return it as GCD.
Step 4: Decrease the value of t by 1 & Go to Step

2.
NOTION OF AN ALGORITHM

• Non ambiguity requirement cannot be compromised.


• range of inputs has to be specified carefully.
• same algorithm can be represented in several different ways.
• Several algorithms for solving the same problem may exist.
• Algorithms for the same problem can be based on different ideas.
• Different algorithms can solve the problem with different speeds.
PROPERTIES OF AN ALGORITHM
1. An algorithm takes zero or more inputs.
2. An algorithm results in one or more outputs.
3. All operations can be carried out in a finite amount of time.
4. An algorithm should be efficient and flexible.
5. It should use less memory space as much as possible
6. An algorithm must terminate after a finite number of steps.
7. Each step in the algorithm must be easily understood.
8. An algorithm should be concise and compact to facilitate verification of
their correctness.
CHARACTERISTICS OF AN
ALGORITHM
• Input – zero or more but finite number of inputs.
• Output – one or more outputs
at least one output is essential.
• Finiteness – terminate after finite number of steps.
each step must execute in finite amount of time.
• Definiteness – Each step must be precisely defined.
action must be rigorously & unambiguously specified.
• Effectiveness – Each operation must be basic and
completed in finite amount of time.
FUNDAMENTALS OF
ALGORITHMIC PROBLEM
SOLVING

Algorithms are procedural


solutions to problems

Sequence of steps in designing


and analysing an algorithm is
as follows
UNDERSTANDING THE PROBLEM

• First step in designing an algorithm.


• Read problem description carefully & understand.
• Ask questions to clarify doubts.
• Identify the problem types to use a known algorithm.
• Often times, algorithm won’t be available readily.
• So, we need to design our own algorithm.
• Specify the range of inputs (instances) exactly.
• Correct algorithm is not the one works most of the time but works
correctly for all legitimate inputs.
DECISION MAKING
• Ascertaining the capabilities of a Sequential vs Parallel algorithms
computational device. Exact vs Approximation algorithms
3 Reasons to choose appr. algo’s
• Choosing between exact and • Some pbms can’t be solved exactly.
approximate problem solving.
• Avbl algo’s are slow.
• Appr. algo’s can b a part of exact
one’s.
• Deciding on appropriate data Algorithms + Data Structures =
structures. Programs.
Choice of proper DS is required.
• Algorithm design technique. Reasons to learn these techniques.
Provides guidance & classify algo’s
based on underlying idea.
DESIGNING AN ALGORITHM
Designing is a challenging task • Algorithm Design Technique
Reasons: “An algorithm design technique
• Some design techniques are (“strategy” or “paradigm”) is a
inapplicable. general approach to solve problems
algorithmically that is applicable to
• Several techniques need to be
a variety of problems from different
combined. areas of computing”.
• Hard to identify underlying design
Methods of specifying an algorithm:
techniques.
• Natural language
• Even for a particular design
technique, getting an algo. needs • Pseudocode
nontrivial ingenuity • Flow chart
PROVE CORRECTNESS
• Once an algorithm is specified, prove its correctness.
• Prove that the algorithm yields a required result for every legitimate input in
a finite amount of time.
• For some algorithms, it is easy; for others, it is complex.
• A common technique is to use mathematical induction.
• An algorithm’s iterations provide a sequence of steps needed for such
proofs.
• tracing the algorithm’s performance for a few specific inputs can be a
worthwhile activity, it cannot prove the algorithm’s correctness.
• But to show that an algorithm is incorrect, one instance of its input is
enough.
• The notion of correctness for approximation algorithms is less
straightforward than it is for exact algorithms.
• For an approximation algorithm, we usually show that the error produced by
the algorithm does not exceed a predefined limit.
ANALYSE THE ALGORITHM
• Two kinds of algorithm efficiency:
• time efficiency – indicates how fast the algorithm runs, and
• space efficiency – indicates how much extra memory it uses
• Another desirable characteristic of an algorithm is simplicity.
• simpler algorithms are easier to understand and to program.
• the resulting programs usually contain fewer bugs
• Yet another desirable characteristic of an algorithm is generality.
• two issues:
- generality of the problem the algorithm solves.
Sometimes, designing a more general algorithm is unnecessary or
difficult or even impossible.
- the set of inputs it accepts.
Design an algorithm that can handle a set of inputs that is natural
for the problem.
• If not satisfied with the algorithm’s efficiency, simplicity, or generality, we
should redesign the algorithm.
CODE THE ALGORITHM
• Programming an algorithm presents both a peril and an opportunity.
• The danger is in making the transition from an algorithm to a program
either incorrectly or very inefficiently.
• The validity of programs is established by testing.
• Implementing an algorithm correctly is necessary but not sufficient.
• Avoid inefficient implementation by using some standard tricks such as
• computing a loop’s invariant (an expression that does not change its value)
outside the loop,
• collecting common subexpressions,
• replacing expensive operations by cheap ones, and so on.
• A working program provides an opportunity in allowing an empirical
analysis of the underlying algorithm.
Conclusion:
“As a rule, a good algorithm is a result of repeated effort and
rework”.
IMPORTANT PROBLEM TYPES
• Sorting.
• Searching.
• String processing.
• Graph problems.
• Combinatorial problems.
• Geometric problems.
• Numerical problems.
MATHEMATICAL ANALYSIS OF
NON RECURSIVE ALGORITHMS
• General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation. (As a rule, it is located in the
innermost loop.)
3. Check whether the number of times the basic operation is executed
depends only on the size of an input.
4. Set up a sum expressing the number of times the algorithm’s basic
operation is executed.
5. Using standard formulas, either find a closed form formula for the count
or, establish its order of growth.
EXAMPLE 1
ALGORITHM MaxElement(A[0..n − 1]) • Let us denote C(n) the
//Determines the value of the largest element number of times the
in a given array comparison is executed.
//Input: An array A[0..n − 1] of real numbers • we get the following sum
for C(n):
//Output: The value of the largest element in
A
maxval ← A[0] • This is an easy sum to
compute because it is
for i ← 1 to n − 1 do nothing other than 1
if A[i] > maxval repeated n − 1 times. Thus,
maxval ← A[i]
return maxval
EXAMPLE 2
ALGORITHM UniqueElements(A[0..n − 1]) two kinds of worst-case
//Determines whether all the elements in a inputs:
given array are distinct does not exit the loop
//Input: An array A[0..n − 1] prematurely.
//Output: Returns “true” if all the elements in A arrays with no equal
are distinct elements or last two
elements are the only pair of
// and “false” otherwise equal elements.
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
if A[i] = A[j ]return false
return true
We also could have
computed the sum as
follows:
EXAMPLE 3
• the number of
ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − multiplications made for
1], B[0..n − 1, 0..n − 1]) every pair of specific values
//Multiplies two square matrices of order n by the of variables i and j is
definition-based algorithm • the total number of
//Input: Two n × n matrices A and B multiplications M(n) is
expressed by the following
//Output: Matrix C = AB
triple sum:
for i ← 0 to n − 1 do
for j ← 0 to n − 1 do • Starting with the innermost
C[i, j ]← 0.0 sum, we get
for k ← 0 to n − 1 do
C[i, j ]← C[i, j ] + A[i, k] ∗ B[k, j • On a particular machine
]
return C • more accurate estimate
MATHEMATICAL ANALYSIS OF
RECURSIVE ALGORITHMS
General Plan for Analyzing the Time Efficiency of Recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can
vary on different inputs of the same size; if it can, the worst-case,
average-case, and best-case efficiencies must be investigated
separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its
solution.
EXAMPLE 1(BACKWARD SUBSTITUTION)
ALGORITHM F(n) • the number of multiplications M(n)
//Computes n! recursively needed
//Input: A nonnegative integer n
• Such equations are called recurrence
//Output: The value of n! relations or, recurrences.
if n = 0 return 1
else return F (n − 1) ∗ n • recurrence relation and initial condition.
• method of backward substitutions.
• The basic operation of the
algorithm is multiplication, we
denote M(n)
• The function F (n) is computed
according to the formula • result of our backward substitutions:
• Tower of Hanoi puzzle
EXAMPLE 2
• the number of moves M(n)
depends on n only, and we get the
following recurrence equation for
it:

• we have the following recurrence


relation for the number of moves
M(n):
To move n > 1 disks from peg 1 to peg • We solve this recurrence by the
3, we first move recursively n − 1 disks method of backward substitutions:
from peg 1 to peg 2, then move the
largest disk directly from peg 1 to peg
3, and, finally, move recursively n − 1
disks from peg 2 to peg 3. Of course, if • we get the following formula for
n = 1, we simply move the single disk the solution to recurrence
directly from the source peg to the
destination peg.
EXAMPLE 2(TREE METHOD)
• When a recursive algorithm makes more than a single call to itself, it can
be useful for analysis purposes to construct a tree of its recursive calls.
• In this tree, nodes correspond to recursive calls, and we can label them
with the value of the parameter(s) of the calls.
• For the Tower of Hanoi example, the tree is given below:

• . By counting the number of nodes in the tree, we can get the total
number of calls made by the Tower of Hanoi algorithm:
EMPIRICAL ANALYSIS
• Definition:
Empirical analysis of algorithm means observing behavior of an
algorithm for certain set of input. Such an analysis is based on timing the
program on several inputs and then analyzing the results obtained.
• Empirical algorithmics is the practice of using empirical methods to study
the behavior of algorithms.
• The practice combines algorithm development and experimentation,
where algorithms are not just designed, but also implemented and tested
in a variety of situations.
• In this process, an initial design of an algorithm is analyzed so that the
algorithm may be developed in a stepwise manner.
• An empirical analysis of an algorithm is one based on actual
experimentation and observation of the results.
• Sometimes a mathematical analysis is difficult for even simple
algorithms.
PLAN FOR EMPIRICAL ANALYSIS
1. Understand the experiment’s purpose.
2. Decide on the efficiency metric M to be measured and the measurement
unit (an operation count vs. a time unit).
3. Decide on characteristics of the input sample (its range, size, and so on).
4. Prepare a program implementing the algorithm (or algorithms) for the
experimentation.
5. Generate a sample of inputs.
6. Run the algorithm (or algorithms) on the sample’s inputs and record the
data observed.
7. Analyze the data obtained.
ALGORITHM VISUALISATION
In addition to the mathematical and empirical analyses of
algorithms, there is yet a third way to study algorithms. It is called
algorithm visualization.

Definition:
Algorithm Visualization can be defined as the use of images to convey
some useful information about algorithms.

Two variations of algorithm visualization:


• Static algorithm visualization - Static algorithm visualization shows an
algorithm’s progress through a series of still images.
• Dynamic algorithm visualization(algorithm animation) - Algorithm
animation shows a continuous, movie-like presentation of an algorithm’s
operations.
ALGORITHM VISUALISATION

• Early efforts in the area of algorithm visualization go back to the 1970s.


• The watershed event happened in 1981 with the appearance of a 30-
minute color sound film titled Sorting Out Sorting.
• This algorithm visualization classic was produced at the University of
Toronto by Ronald Baecker with the assistance of D. Sherman
• It contained visualizations of nine well-known sorting algorithms.
• It provided quite a convincing demonstration of their relative speeds.
• Sorting Out Sorting used a visual presentation via vertical or horizontal
bars of different heights or lengths.
• For larger files, Sorting Out Sorting used a scatterplot of points on a
coordinate plane, with the first coordinate representing an item’s position
in the file and the second one representing the item’s value.
SORTING OUT SORTING(continued….)

Initial and final screens of a typical visualization of a sorting


algorithm using the bar representation.
SORTING OUT
SORTING(CONTINUED….)

Initial and final screens of a typical visualization of a sorting algorithm using


the scatterplot representation.
THANK YOU!!!!
THE END……

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