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

Digital Logic Design Week01Fall2022

This document discusses algorithms and their analysis. It defines an algorithm as a sequence of instructions to solve a problem in a finite number of steps. Key properties of algorithms are that they take input, produce output, and terminate. Performance is a common way to analyze algorithms by comparing their time and space requirements. Analysis focuses on how requirements grow as input size increases, with slower growth rates like linear being more efficient than faster ones like quadratic or exponential.

Uploaded by

Wasim Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Digital Logic Design Week01Fall2022

This document discusses algorithms and their analysis. It defines an algorithm as a sequence of instructions to solve a problem in a finite number of steps. Key properties of algorithms are that they take input, produce output, and terminate. Performance is a common way to analyze algorithms by comparing their time and space requirements. Analysis focuses on how requirements grow as input size increases, with slower growth rates like linear being more efficient than faster ones like quadratic or exponential.

Uploaded by

Wasim Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

DATA STRUCTURES AND ALGORITHMS

WEEK 01 (Fall 2022)

Dr. Sheraz Khan


Algorithm

The method of solving a problem is known as an
algorithm.


More precisely, an algorithm is a sequence of
instructions that act on some input data to produce
desired output in a finite number of steps.

<date/time> 2
P Properties of Algorithms
(a) Input – An algorithm must receive some input data
supplied externally.
(b) Output – An algorithm must produce at least one output
as the result.
(c) Finiteness – No matter what the input might be, the
algorithm must terminate after a finite number of steps. For
example, a procedure which goes on performing a series of
steps infinitely is not an algorithm.
P Properties of Algorithms
(d) Definiteness – The steps to be performed in the algorithm
must be clear and unambiguous.

(e) Effectiveness – One must be able to perform the steps in


the algorithm without applying any intelligence. For example,
the step—Select three numbers which form a Pythagorean
triplet—is not effective.
P Analysis of Algorithms
• Multiple algorithms may exist for solving a given problem.

• To be able to decide which algorithm to use, we need to


analyze algorithms.

• There can be multiple yardsticks to determine which


algorithm is better than the other.
P Yardsticks for Analyzing Algorithms
• (a) Robustness – Is the algorithm robust enough to
tackle all types of valid and invalid inputs.
• (b) Maintainability – Is it easy to alter the algorithm as
needs change in future.
• (c) Scalability – Can the algorithm deal with increase in
the number of inputs.
• (d) Modularity – Can the algorithm be broken down into
smaller sections (modules).
P Yardsticks for Analyzing Algorithms
• (e) Security – Can the algorithm deal with malicious attacks.

• (f) User-friendliness – Is it easy to use the algorithm.

• (g) Performance (efficiency) – Does the algorithm take less time


and memory space when implemented in a program and executed.

Of these, the most popular yardstick used to analyze algorithms is


performance. This is because it is easy to quantify time and space
requirements of an algorithm.
P Time and/or Space Analysis
• The analysis of algorithms is done by comparing the time and/or space
required for executing the algorithms.

• Often, there is a trade-off between time and space.

• While doing time-based analysis of algorithms we do not use


conventional time units like seconds or minutes required for executing
the algorithms.

• We are interested in relative efficiency of different algorithms rather


than the exact time for one.
Analysis of Searching/Sorting
Algorithms
• In Searching and Sorting algorithms the important task
being
• done is the comparison of two values.

• While searching, the comparison is done to check if the


value in a set matches the one, we are looking for.

• Whereas in sorting the comparison is done to see whether


values being compared are out of order.
Analysis of Arithematic Algorithms
• The arithmetic operations fall under two groups—additive
and multiplicative.

• Additive operators include addition, subtraction,


increment, and decrement.

• Multiplicative operators include multiplication, division,


and modulus.
Example: Count the number of
characters in a file
If there are 500 characters present in the file, then
number of times each step is performed would be as
follows:
Refer to Example on Previous Slides
• Conditional checks, the number of increments and get
next character and are far too many as compared to
numberof initialization and printing operations.

• The number of initialization and printing operations would


remain same for a file of any size and they become a
much smaller percentage of the total as the file size
increases.
Refer to Example on Previous Slides
• For a large file, the number of initialization and printing
operations would be insignificant as compared to the
number of increments and conditional checks.

• While analyzing this algorithm the initialization and


printing operation should be ignored and only steps 2, 4
and 5 should be considered.
Rates of Growth
Constant Growth Rate

• If the growth rate of an algorithm doesn’t change with the


size of the input, then the algorithm is called a constant
time algorithm.
Growth Rates
• constant (1),
• linear (n),
• logarithmic (log n),
• log linear (n log n),
• quadratic (n2),
• cubic (n3),
• polynomial (nc)
• exponential (cn)
An Allgorithm with Cobmbination of
more than one Growth Rates
• We can safely ignore the slower growing terms.
• For example, if the growth rate of an algorithm in n 2 +
3n, then as n increases the term n2 will grow much
faster than the term 3n.
• So, we can safely discard the term 3n.
• On discarding the lower order term, what we are left
with is called the order of the function or order of the
algorithm whose growth rate the function represents.
An Efficient Algorithm ?

• An algorithmis considered to be more efficient than


another if it has a lower order of growth.
Time & Space Growth Rates
• The discussion until now relates to behavior of an
algorithm as regards time for execution.

• The same discussion can be extended to the space


requirements (memory) of an algorithm.

• Thus, an algorithm whose space requirement grows at the


rate n2 is considered to be better than the one whose
space requirements grows at the rate n3.

You might also like