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

DA

An algorithm is a step-by-step process for solving problems, consisting of a finite number of steps that lead to a solution, and can be expressed in a human-readable format. It takes input values and produces output values, with characteristics such as finiteness, efficiency, and the ability to solve specific classes of problems. The document also covers basic terminologies related to data organization, data structures, operations on data structures, and analysis of algorithms in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DA

An algorithm is a step-by-step process for solving problems, consisting of a finite number of steps that lead to a solution, and can be expressed in a human-readable format. It takes input values and produces output values, with characteristics such as finiteness, efficiency, and the ability to solve specific classes of problems. The document also covers basic terminologies related to data organization, data structures, operations on data structures, and analysis of algorithms in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Basic Concept Of Algorithm

INTRODUCTION
An algorithm is a Step By Step process to solve a problem, where
each step indicates an intermediate task. Algorithm contains finite
number of steps that leads to the solution of the problem.

For example when we are doing a mathematical calculation like


addition, subtraction, multiplication or division. What do we do?
We start with pen and paper and do the computation in one or two
steps. If it is a bigger sum calculation of Simple interest or Profit
and loss then we write some more steps. Algorithm is like that.
But the language used in algorithm is somewhat similar to
programming languages and somewhat with our human
language(like English).

An algorithm takes some value as input and produces output value


in a finite time.

Lets see an Example

Adding two numbers Algorithm to add two numbers


Step 1: Start
Step 2: Declare the num1, num2, and sum variables.
Step 3: Read the num1 and num2 values.
Step 4: Add num1 and num2 and put the output in the
sum variable
Step 5: End

Here this algorithm is the written interpretation of a program that


is understandable to any person and it is understandable to any
person who can read English and it doesn’t matter whether he or
she knows programming or not. If we write the above algorithm in
c programming language then that will look like this

#include<stdio.h>
#include<conio.h>

void main()
{

int num1 = 64;


int num2 = 55;
int sum = num1+num2;
printf(“The sum is %d”,sum);
getch();
}

Thus it happens:

Features Of Algorithm

1. Algorithm is human readable form of a program.


2. An algorithm must terminate after a finite number of steps. This
is called fineness of an algorithm.
3. Algorithm must be able to solve all kinds of problems of a
particular class.
4. The algorithm should be simple and basic so that it can be
understood very easily. This is called the efficiency of the
program. Quality of algorithm depends on the efficiency of
algorithm.

5. Analgorithm must take some input value and produce output


value on that.
BASIC TERMINOLOGIES: ELEMENTARY DATA
ORGANISATION

Data: Data means raw facts that are entered stored into the
computer memory ex- numbers, single characters, strings etc

Data Items: Data Item is a single unit of data value ex – Student


Name is a data item
Data items can be further subdivided into sub-items. Ex- Student
Name can be divided into First Name, Middle Name and Last
Name.
These Data items that can be further divided into sub items are
called Group Items. On the Other hand Data Items that can not be
divided further are called Elementary Items. Ex – StuID

Entity: Entity is something that exist in real world. Or entities are


real world objects. Like Student, Employee, Faculty, Building,
Grocery etc.
Entities have some properties associated with them that are called
attributes. These attributes have certain values.
For example if Student is an entity then its attributes are
Student_Name, Age, Sex, StuID etc.
And we can assign values to these attributes like

Attributes Student_Name Age Sex StuID


Values Harvard 21 Male 014fss
Jonhson

Entities with similar attributes can be grouped together to form


Entity Sets
Attributes of entity set have range of values instead of a single
value
Data when gets processed becomes Information or Information
is meaningful data or processed data.
Field: Field is a single unit of information that contains a single
attribute value of an entity
Record: Record is a collection of all field values of a given entity
File: File is the collection of records of the entities in a given
entity set

Records may also be classified according to length.


A file can have fixed-length records or variable-length records.
 In fixed-length records, all the records contain the same data
items with the same amount
of space assigned to each data item.
 In variable-length records file records may contain different
lengths.
Example: Student records have variable lengths, since different
students take different
numbers of courses. Variable-length records have a minimum and
a maximum length.

Abstract Data Type (ADT)

Type: A type is a set of values of similar kind like integer, floating


decimal, alphanumeric characters etc
Data Type: Data Type is a type with a set of operations to alter the
type. For Example: List is a datatype. Two traditional
implementations of list data type are linked list and Arrays

Image Of A List
Linked List

Array Figure 1

Array Figure 2

There is a difference need to be made between the logical


representation of data type and the physical representation of
datatype in computer memory. For example, there are two
traditional implementations for the list data type: the linked list
and the array-based list. The list data type can therefore be
implemented using a linked list or an array. But we don’t need to
know how the list is implemented when we wish to use a list to
help in a more complex design. For example, a list might be used
to help implement a graph data structure.

Abstract Datatype: An abstract datatype is a datatype that is


represented with some attributes and functions. It is a datatype
specification within some language and independent of
implementation.
The interface for the ADT is defined in terms of a type and a set of
operations on that type. The behavior of each operation is
determined by its inputs and outputs. An ADT does not specify
how the data type is implemented. These implementation details
are hidden from the user of the ADT and protected from outside
access, a concept referred to as encapsulation.

Data Structure: Data Structure is a way or format of storing,


organizing and retrieving data in an efficient manner.
Data Structure is the implement of abstract datatype. In an object-
oriented language, an ADT and its implementation together make
up a class. Each operation associated with the ADT is
implemented by a member function or method. The variables that
define the space required by a data item are referred to as data
members. An object is an instance of a class, that is, something
that is created and takes up storage during the execution of a
computer program.
File Structure means the organization of data on storage devices
like Hard Disc, CD etc

Data Structure deals with not only the stored elements but also the
relationship among them.
Data structure mainly specifies the following
four things:-
i) Organization of data
ii) Accessing methods
iii) Degree of associativity
iv) Processing alternatives for information

Classification of Data Structure

Data Structure
PRIMITIVE DATA STRUCTURE

Primitive data structure is the data structure that is


atomic or indivisible. ex- int, real, float, Boolean etc

NON PRIMITIVE DATA STRUCTURES

Non Primitive data structures are composite data


structures that can be divided into groups of
homogeneous or heterogeneous data items.

Example: Records, Arrays, Strings etc

LINEAR DATA STRUCTURES


In Linear data structure data items are arranged in
linear way. Example: Arrays

NON LINEAR DATA STRUCTURES

In Non Linear data structure the data items are not


arranged in linear way. Example : tree

HOMOGENEOUS DATA STRUCTURES

In homogeneous data structure all the data items


are of same data type. Example – Arrays – int arr[] =
{5,4.3,2}
Here all the data items are of same data type that is
integer

NON HOMOGENEOUS DATA STRUCTURE

In Non homogeneous data structure all the data


items are not of the same data type. Example-
Records

STATIC DATA STRUCTURE

static data structures are those whose size, structure


and memory location are fixed during compile time.

DYNAMIC DATA STRUCTURE

In Dynamic data structure the size and memory


capacity of the data structure can be updated,
increased or decreases during compile time.
ARRAYS: Arrays are data structures that contain
finite number of data items of the same data type.

For example – int a[10] can contain values of only


integer type

int → datatype
a → variable Name
10 → size of the array-based

Concept of array:
• The individual element of an array can be
accessed by
specifying name of the array, followed by index or
subscript
inside square bracket.
Exp. to access the 10th element statement will be
a[9].
• The first element of the array has index zero[0]. So
in an
array of 10 elements the first array will be a[0] and
last a[9]
• The elements of array will always be stored in
consecutive
memory location.

The size of an array can be calculate by this


equation
(upper bound- lower bound )+1
(9-0)+1
• Arrays can always be read or written through
loop. In case of one dimensional array it required
one loop for reading and one loop for writing and
in case of two dimensional array it requires two
loops for each operation.

LIST:

List is a collection of variable number of data items.


List is the linear representation of array.

An element of list must contain at least two


fields, one for storing data or information and
other for storing address of next element.

STACK:

Stack is an ordered collection of data items. In stack


insertion and deletion occur at one end only and that
is the top of the stack.

Stack have the following features:

• It is a non primitive data structure.


• It is also called as last in first out type of data
structure(LIFO).
• Exp. Train, stack of trays etc.
• At the time of insertion or removal of an
element base of stack remains same.
• Insertion of element is called Push
• Deletion of element is called Pop

QUEUES:

In queues operations occur in First In First Out basis.


The insertion and deletions take place at front end
and rear end respectively.

Queues have the following features:

• It is a non primitive data structure.


• It is also called as last in first out type of data
structure(LIFO).
• Exp. Train, stack of trays etc.
• At the time of insertion or removal of an
element base of stack remains same.
• Insertion of element is called Push
• Deletion of element is called Pop

DATA STRUCTURE OPERATIONS:

The basic operations that can be performed on a Data Structure


are:
i) Insertion: - Operation of storing a new data element in a Data
Structure.
ii) Deletion: - The process of removal of data element from a Data
Structure.
iii) Traversal: - It involves processing of all data elements present
in a Data Structure.
iv) Merging: - It is a process of compiling the elements of two
similar Data Structures to form a new Data Structure of the same
type.
v) Sorting: - It involves arranging data element in a Data
Structure in a specified order.
vi) Searching: - It involves searching for the specified data
element in a Data Structure

ANALYSIS ON AN ALGORITHM:

There are two kinds of algorithm efficiency: time and space


efficiency. Time efficiency indicates how fast the algorithm runs;
space efficiency indicates how much extra memory the algorithm
needs.

Complexity
Performance of a program: The performance of a program is
measured based on the amount of computer memory and time
needed to run a program.
The two approaches which are used to measure the performance of
the program are:
1. Analytical method → called the Performance Analysis.
2. Experimental method → called the Performance Measurement.
Space Complexity
Space complexity: The Space complexity of a program is defined
as the amount of memory it needs to run to completion.
As said above the space complexity is one of the factor which
accounts for the performance of the program. The space
complexity can be measured using experimental method, which is
done by running the program and then measuring the actual space
occupied by the program during execution. But this is done very
rarely. We estimate the space complexity of the program before
running the program

The reasons for estimating the space complexity before running


the program even for the first time are:
(1) We should know in advance, whether or not, sufficient
memory is present in the computer. If this is not known and the
program is executed directly, there is possibility that the program
may consume more memory than the available during the
execution of the program. This leads to insufficient memory error
and the system may crash, leading to severe damages if that was a
critical system.
(2) In Multi user systems, we prefer, the programs of lesser size,
because multiple copies of the program are run when multiple
users access the system. Hence if the program occupies less
space during execution, then more number of users can be
accommodated.
Space complexity is the sum of the following components:
(i) Instruction space:
The program which is written by the user is the source program.
When this program is compiled, a compiled version of the
program is generated. For executing the program an executable
version of the program is generated. The space occupied by these
three when the program is under execution, will account for the
instruction space.

The instruction space depends on the following factors:


Compiler used – Some compiler generate optimized code which
occupies less space.
Compiler options – Optimization options may be set in the
compiler options.
Target computer – The executable code produced by the compiler
is dependent on the processor used.
(ii) Data space:
The space needed by the constants, simple variables, arrays,
structures and other data structures will account for the data space.
The Data space depends on the following factors:Structure size –
It is the sum of the size of component variables of the structure.
Array size – Total size of the array is the product of the size of the
data type and the number of array locations.
(iii) Environment stack space:
The Environment stack space is used for saving information
needed to resume execution of partially completed functions. That
is whenever the control of the program is transferred from one
function to another during a function call, then the values of the
local variable of that function and return address are stored in the
environment stack. This information is retrieved
when the control comes back to the same function.
The environment stack space depends on the following factors:
Return address Values of all local variables and formal parameters.
The Total space occupied by the program during the execution of
the program is the sum of the fixed space and the variable space.
(i) Fixed space - The space occupied by the instruction space,
simple variables and constants.
(ii) Variable space – The dynamically allocated space to the
various data structures and the environment stack space varies
according to the input from the user.
Space complexity S(P) = c + Sp
c -- Fixed space or constant space
Sp -- Variable space

We will be interested in estimating only the variable space because


that is the one which varies according to the user input.
Consider the following piece of code...
int square(int a)
{
return a*a;
}
That means, totally it requires 4 bytes of memory to complete its
execution. And this 4 bytes of memory is fixed for any input value
of 'a'. This space complexity is said to be Constant Space
Complexity.
If any algorithm requires a fixed amount of space for all input
values then that space complexity is said to be Constant Space
Complexity.
Consider the following piece of code...
int sum(int A[], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}

ASYMPTOTIC NOTATIONS

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 is input bound i.e., if there's no input to the
algorithm, it is concluded to work in a constant time. Other than
the "input" all other factors are considered constant. Asymptotic
analysis refers to computing the running time of any operation in
mathematical units of computation. For example, the running time
of one operation is computed as f(n) and may be for another
operation it is computed as g(n2). This means the first operation
running time will increase linearly with the increase in n and the
running time of the second operation will increase exponentially
when n increases. Similarly, the running time of both operations
will be nearly the same if n is significantly small.

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.
Asymptotic Notations
Following are the commonly used asymptotic notations to
calculate the running time
complexity of an algorithm.
 Ο Notation
 Ω Notation
 θ Notation
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express 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 Notation, Ω
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. It is represented as follows −
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for
all n > n0. }

TIME SPACE TRADE OFF:

Time space tradeoff is a technique to solve problem in less time


using more memory or by spending more time with a little space.

TYPES OF TRADEOFF

The following are the types of tradeoff:

1.Compressed/Uncompressed
2. Re-rendering/Stored Images
3. Smaller code/Loop Unrolling
4. Lookup Table/Recalculation

1.Compressed/Uncompressed: Here time-space trade off is


applied and if the stored data is uncompressed then it takes more
space but less time. While if the data is compressed then it takes
more time but less space to run the algorithm.

2. Re-rendering/Stored Images:

When only the source is stored and rendered as an image more


time will be used with less space.

When the images are stored more space will be used and less time
will be spent.

3. Smaller code/Loop Unrolling:

When smaller code is used it occupies less space but takes more
time to execute.
When the program is bigger and complicated with loops occupy
more space in memory but takes less time to be executed.

4. Lookup Table/Recalculation:

Lookup table includes the entire table so it occupies more space


but takes little time for computation.

If it is recalculated as computer table entries are needed then


computing time increases but less memory is needed.

LINEAR SEARCH AND BINARY SEARCH TECHNIQUE


AND THEIR COMPLEXITY ANALYSIS:

Linear Search:

Basic Concept
• Basic idea:
– Start at the beginning of the array.
– Inspect elements one by one to see if it matches the key.
• Time complexity:
– A measure of how long an algorithm takes to run.
– If there are n elements in the array:
• Best case: match found in first element (1 search operation)
• Worst case: no match found, or match found in the last element
(n search operations)
• Average case: (n + 1) / 2 search operations

Let us explain this through a c program

#include <stdio.h>
int linear_search (int a[], int size, int key)
{
for (int i=0; i<size; i++)
if (a[i] == key) return i;
return -1;
}
int main()
{
int x[]={12,-3,78,67,6,50,19,10}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, linear_search (x,8,val);
}

Linear Search Properties:

It searches the array for the number to be searched


element by element.
– If a match is found, it returns the array index.
– If not found, it returns -1.

BINARY SEARCH:

Binary search works if the array is sorted.


– Look for the target in the middle.
– If you don’t find it, you can ignore half of the array,
and repeat the process with the other half.
• In every step, we reduce the number of
elements to search by half.

Lets Explain this through a program:

Iterative
#include <stdio.h>
int bin_search (int a[], int size, int key)
{
int L, R, mid;
L = 0; R = size – 1;
while (L <= R) {
mid = (L + R) / 2;
if (a[mid] < key) L = mid + 1;
else if (a[mid] > key) R = mid -1;
else return mid; /* FOUND AT INDEX mid */
}
return -1; /* NOT FOUND */
}
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, bin_search (x,8,val);
}

Recursive
#include <stdio.h>
int bin_search (int a[], int L, int R, int key)
{
int mid;
if (R < L) return -1; /* NOT FOUND */
mid = (L + R) / 2;
if (a[mid] < key) return (bin_search(a,mid+1,R,key));
else if (a[mid] > key) return (bin_search(a,L,mid-1,key));
else return mid; /* FOUND AT INDEX mid */
}
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, bin_search (x,0,7,val);
}

Time Complexity:
• If there are n elements in the array.
– Number of searches required in the worst case: log 2 n
• For n = 64 (say).
– Initially, list size = 64. 2 k = n, where k is the
– After first compare, list size = 32. number of steps.
– After second compare, list size = 16. k = log 2n
– After third compare, list size = 8.
– ……. log2 64 = 6
– After sixth compare, list size = 1. log2 1024 = 10

You might also like