Sie sind auf Seite 1von 19

DESIGN AND ANALYSIS OF ALGORITHMS

PREPARED BY: Sakshi Mehta

University Institute of Engineering (UIE)


Design and Analysis of Alogorithms (CST-302)

Lecture 1
Introduction to Algorithms

University Institute of Engineering (UIE)


ALGORITHMS: AN INTRODUCTION
An algorithm is a well defined computational procedure that
transforms inputs into outputs, achieving the desired input-output
relationship.

Algorithm has five important features:


Finiteness: Each instruction should be such that it can be performed
in a finite time.
Definiteness: Each step should be clear and unambiguous.
Effectiveness: Each instruction must be very basic so that it can be
carried out by a person using paper and pencil.
Input: Zero or more quantities are supplied.
Output: At least one quantity is produced.

University Institute of Engineering (UIE)


Algorithms
A tool for solving a well-specified computational
problem

Input Algorithm Output


An algorithm is said to be correct if, for every input
instance, it halts with the correct output
An incorrect algorithm might:
not halt at all on some input, or
halt with an answer other than the desired one
Algorithms must be:
Correct: For each input produce an appropriate output
Efficient: run as quickly as possible, and use as little
memory as possible

University Institute of Engineering (UIE)


FLOW CHART

A flowchart consists of a sequence of instructions linked


together by arrows to show the order in which the
instructions must be carried out. Each instruction is put into
a box. The boxes are of different shapes depending upon
what the instruction is.

University Institute of Engineering (UIE)


PSEUDOCODE

Pseudocode is a compact and informal high-level


description of a computer programming algorithm that
uses the structural conventions of programming
languages, but omits detailed subroutines, variable
declarations or language-specific syntax.
Pseudo code resembles, but should not be confused with,
skeletons programs including dummy code, which can be
compiled without errors. Flowcharts can be thought of as
a graphical form of pseudo code.

University Institute of Engineering (UIE)


PROPERTIES OF AN ALGORITHM

Independent of any Programming Language.


Each step must be clear and well understood.
Executes finite steps.
Terminates in finite time period.
Simple Algorithm cannot be considered as the most efficient
and most accurate.
Instructions in algorithm include: Arithmetic, Logical and
Control Transfer.

University Institute of Engineering (UIE)


Problems and Algorithms

We need to solve a computational problem


Convert a weight in pounds to Kg

An algorithm specifies how to solve it, e.g.:


1. Read weight-in-pounds
2. Calculate weight-in-Kg = weight-in-pounds * 0.455
3. Print weight-in-Kg

A computer program is a computer-executable


description of an algorithm

University Institute of Engineering (UIE)


PROBLEM SOLVING PROCESS

Analysis
Problem
specification
Design

Algorithm

Implementation

Program

Compilation
Executable
(solution)

University Institute of Engineering (UIE)


From Algorithms to Programs
Problem
Algorithm: A sequence of
instructions describing how to do a task (or
process)

C Program
University Institute of Engineering (UIE)
Computational problems

A computational problem specifies an input-output


relationship
What does the input look like?
What should the output be for each input?
Example:
Input: an integer number n
Output: Is the number prime?
Example:
Input: A list of names of people
Output: The same list sorted alphabetically
Example:
Input: A picture in digital format
Output: An English description of what the picture shows

University Institute of Engineering (UIE)


CLASSIFICATION OF ALGORITHMS

Serial
Parallel
Recursive
Logical
Deterministic/Non-Deterministic
Exact/Appropriate

University Institute of Engineering (UIE)


ANALYSIS OF ALGORITHMS

Is to check the efficiency of the algorithm


Is it Correct?
How much time it takes on input of n numbers?
Can we improve it?
Types
Benchmarking
Asymptotic Notation

University Institute of Engineering (UIE)


How do we check the efficiency of an Algorithm on a
Machine?

Insertion Merge
Sort Takes C1 n2 time to sort
n elements.
Sort Takes C2 n lg n time to
sort n elements.
Faster than Merge Sort Faster than Insertion
for small input size. Sort for large input size.

Note: Insertion Sort usually has a smaller constant factor than Merge Sort. C1< C2
Problem is to sort an array of 1 million numbers (106)

Computer A : 100 times faster than Computer B


Computer B Runs Merge Sort
Runs Insertion Sort It can execute 10million
It can execute 1 billion instructions instructions per second(107)
per second(109)Let C1=2 Let C2=50
2.(106)2 instructions / 109 50.106 lg 106 instructions / 107
instructions per second = 2000secs instructions per second = 100secs

University Institute of Engineering (UIE)


TIME AND SPACE COMPLEXITY

The time complexity of a program (for a given input) is the


number of elementary instructions that this program
executes. This number is computed with respect to the size n
of the input data

The space complexity of a program (for a given input) is the


number of elementary objects that this program needs to
store during its execution. This number depends on n.

University Institute of Engineering (UIE)


COMPLEXITY CONTD

Growth of Time/Space for a given problem depends on


Machine which executes the program
Compiler used to construct the program
Speed of CPU, bus , Peripherals
Language used and coding efficiency
Quality of Input.

University Institute of Engineering (UIE)


INPUT SIZE

Time and space complexity

This is generally a function of the input size


E.g., sorting, multiplication

How we characterize input size depends:


Sorting: number of input items
Multiplication: total number of bits
Graph algorithms: number of nodes & edges

University Institute of Engineering (UIE)


FAQ

How can algorithms be related to pseudo codes?


Give the notations required to denote complexity of an
algorithm.

University Institute of Engineering (UIE)


References

1. Introduction to Algorithms by Coreman,


Leiserson,Rivest, Stein.
2. Fundamentals of Algorithms by Ellis Horwitz,
Sartaj Sahni, Sanguthevar Rajasekaran
3. Different websites providing study material.

University Institute of Engineering (UIE)

Das könnte Ihnen auch gefallen