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

Module 1 Algorithm

Uploaded by

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

Module 1 Algorithm

Uploaded by

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

Algorithm

➢ An algorithm is a set of instructions for solving a problem or accomplishing a task.

➢ An Algorithm is any well-defined computational procedure that takes some value or


set of values as input and produces a set of values or some value as output. Thus
algorithm is a sequence of computational steps that transforms the i/p into the o/p.
➢ An Algorithm is a finite set of instructions that, if followed, accomplishes a particular
task.
➢ In addition, all algorithms should 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 very basic so that it can be carried
out, in principle, by a person using only pencil & paper.

Properties of Algorithm

➢ Non Ambiguity
Each step in an algorithm should be non-ambiguous. That means each instruction
should be clear and precise. This property also indicates the effectiveness of
algorithm.
➢ Range of Input
The range of input should be specified.
➢ Multiplicity
The same algorithm can be represented into several different ways.
➢ Speed
The algorithm is written using some specified ideas. Bus such algorithm should be
efficient and should produce the output with fast speed.
➢ Finiteness
The algorithm should be finite. That means after performing required operations it
should be terminate.

Algorithm Analysis

Analysis of algorithm is the process of analyzing the problem-solving capability of the


algorithm in terms of the time and size required (the size of memory for storage while
implementation).

Efficiency of an algorithm

➢ The efficiency of an algorithm defines the number of computational resources used by


an algorithm and time taken by an algorithm to produce the desired result.
➢ An algorithm must be analyzed to determine its resource usage.
➢ The efficiency of an algorithm can be measured based on the usage of different
resources.
➢ For maximum efficiency of algorithm we wish to minimize resource usage.
➢ The important resources such as time and space complexity cannot be compared
directly, so time and space complexity could be considered for an algorithmic
efficiency.
➢ There are two main measures for the efficiency of an algorithm.
➢ The complexity of an algorithm is divided into two types:

• Time complexity
• Space complexity

Time Complexity:

➢ The time complexity of an algorithm quantifies the amount of time taken by an


algorithm to run as a function of the length of the input.
➢ The actual running time depends on many factors:
• The speed of the computer: cpu (not just clock speed), I/O, etc.
• The compiler, compiler options .
• The quantity of data - ex. search a long list or short.
• The actual data - ex. in the sequential search if the name is first or last.
Space Complexity
➢ The space complexity of an algorithm or a computer program is the amount of memory
space required to solve an instance of the computational problem as a function of
characteristics of the input.
➢ It is the memory required by an algorithm until it executes completely.
➢ Space complexity is the amount of memory used by the algorithm (including the input
values to the algorithm) to execute and produce the result.
➢ To estimate the memory requirement we need to focus on two parts:
• A fixed part: It is independent of the input size. It includes memory for
instructions (code), constants, variables, etc.
• A variable part: It is dependent on the input size. It includes memory for recursion
stack, referenced variables, etc.
➢ While executing, algorithm uses memory space for four reasons:

1. Instruction Space

• It's the amount of memory used to save the compiled version of instructions.

2. Environmental Stack

• Sometimes an algorithm(function) may be called inside another


algorithm(function). In such a situation, the current variables are pushed onto
the system stack, where they wait for further execution and then the call to the
inside algorithm(function) is made.

3. Data Space

• Amount of space used by the variables and constants.


4. Recursion stack space

• Amount of space needed by recursive function


• For each recursive function, the space needed depends on local variables and
formal parameters.
• Space depends on maximum depth of recursion.

Classification of algorithms

Deterministic Algorithm Non-deterministic Algorithm

A non-deterministic algorithm is one in


A deterministic algorithm is one whose
which the outcome cannot be predicted
behavior is completely determined by its
with certainty, even if the inputs are
inputs and the sequence of its instructions.
known.

For a particular input the computer will


For a particular input, the computer will
give different outputs on different
give always the same output.
execution.

Example-searching and sorting algorithms Travelling salesperson problem.


Time space trade off

Definition: In computer science, a space-time or time-memory tradeoff is a way of solving a


problem in : 1.) Less time by using more memory) or, 2.) By solving a problem in very little
space by spending a long time.

Types of Trade Off:

• Compressed / Uncompressed Data


• Re-Rendering / Stored Images
• Smaller Code / Loop Unrolling

Compressed / Uncompressed Data

• A space -time trade off can be applied to the problem of data storage.
• If data is stored uncompressed,it takes more space but less time.
• If the data is stored compressed, it takes less space but more time to run the
decompression algorithm.

Re-Rendering / Stored Images

• Storing only the source and rendering it as an image everytime the page is requested
would be trading time for space. More time used but less space.
• Storing the images would be trading space for time. More space used but less time.

Smaller Code(with loop) / Larger Code (without loop)

• Smaller code occupies less space in memory but it requires high computation time
which is required for jumping back to the beginning of the loop at the end of each
iteration.
• Larger code or loop unrolling can be traded for higher program speed. It occupies
more space in memory but requires less computation time. (as we need not perform
jumps back and forth since there is no loop.)

You might also like