Uploaded by Rajesh Nigam

UNIT 1

advertisement
UNIT-1
Data: - Data is the basic fact or entity that is utilized in calculation or manipulation. There are
two different types of data such as numerical data and alphanumeric data and these two data
types specify the nature of data item that undergoes certain operations . Data may be a single or
it may be a set of values.
Data Structure:-The logical or mathematical model of a particular organization of data is called
a data structure.
Data structure is a way of organizing all data items that consider not only the element
stored but also their relationship to each other.
Classification of Data Structure: Data structure are normally divided into two broad categories
i.
Primitive data structure
ii.
Non – Primitive data structure
Primitive Data Structure:-These are basic structure and are directly operated upon by the
machine instructions .Ex. Integers, Floating point numbers, character constant, String constant,
pointers etc.
Non-Primitive Data structure:-These are derived from the primitive data structures. The non –
primitive data structures emphasize on structuring of a group of homogenous (same type) or
heterogeneous (different type, data items ex. Arrays, lists & files.)
ABSTRACT DATA TYPE
An Abstract Data type (ADT) is defined as a mathematical model of the objects that make up the data
type as well as the function that operate on these object
ADT= Type + Function name + Behavior of each function
Data Type:--which type of data is to be used (int, char)
Function name: -- Define the name of function.
Behavior of each function:--Define the behavior the function
ADT is useful to handle type correctly the data. Always what is to done is given in ADT
but who is to be done is not given in ADT.
OPERATION OF DATA STRUCTURE
1. Create: - This operation is use to declaration statement .This can be done by declaration
statement. The creation of data structure may take place either during compile time or run time
2. Insertion:-Adding a new element in the data structure as referred as insertion
3. Select: - This operation deals with accessing a particular data item with a data structure
4. Delete: - This operation is used to destroyed the memory space allocated for the specified data
structure
5. Update:-It is used to update and modified the data in the data structure
6. Searching: - This operation finds the presence of the desire item in the list of the data item
7. Sorting:-This process is use to arranging all data item in a data structure in a particular order
8. Merging:-This process of combining the data item of two different sorted list into a single
sorted list
9. Traversing:-In this we accessing each data exactly once in the data structure so that each data
item traverse and visit
Algorithm: - A clearly specified finite set of instructions a computer follows to solve a problem.
An algorithm’s performance depends on internal and external factors
Properties of Algorithms
•
Finiteness: An algorithm must always terminate after a finite number of steps
•
Definiteness: Each step must be precisely defined; the actions must be unambiguous
•
Input: An algorithm has zero or more inputs
Offline Algorithms: All input data is available before the execution of the algorithm begins
Online Algorithms: Input data is made available during the execution of the algorithm
•
Output: An algorithm has one or more outputs
•
Effectiveness: All operations must sufficiently basic to be done exactly and within a
finite length of time by a man using pencil and paper
Algorithm Complexity
It is the factor used for comparing different algorithms. Efficiency of an algorithm is measured
with respect to time and space used by the algorithm.
The time is measured by counting the no of keys operations in sorting algorithms like the no.
of comparisons in sorting as the operation, other than comparison require the less time than the
comparison .The space is measured by counting the maximum memory required by the algorithm
“The complexity of an algorithm is a function f(n) which gives the running time and/or storage
space requirement of the algorithm in term of the size n of the input data “
Time complexity
The time complexity of an algorithm is the amount of computer time it needs to execute the
program and get the intended result. The time T(p) , taken by the program P is the sum of the
compile time and the run time.
Space complexity
Analysis of algorithm is also defined as the process of determining a formula for prediction of
the memory requirement (primary memory) based on the problem size this is called the space
complexity of the algorithm that is space complexity is defined as the amount of memory
required for running an algorithm.
While analyzing the algorithm based on prior algorithm analysis principal 3 different cases are
identified. It is important to note that this classification is purely based on then nature of the
problem for which we are developing the algorithm
(i)
(ii)
(iii)
Worst case: Refer to maximum no. of instruction /operation that could be executed
by the algorithm in order to give the desire output.
Average case: Refer to no. of instruction operation that could be execute by the
algorithm an average in order to give the desire result
Best case ; Refer to the minimum no. of instruction/operation that could to be
execute by the algorithm in order to give the desireresult
Array
An array can be defined as an infinite collection of homogenous (Similar type) elements. This
means that an array can store either all integers ,all floating point numbers ,all characters (array
of character or string ) or any other complex data types ,but all of same type.
There are some important points about arrays .These are as follows:(i)
Arrays are always stored in consecution memory locations.
(ii)
An array can store multiple values which can be referenced by a single name unlike a
simple variable which store one value at a time followed by an index or subscript
specified inside a square bracket.
(iii) An array name is actually a pointer to the first location of the memory block allocated
to the name of the array.
(iv)
An array either be an integer, character or floating data type can be initialized only
during declaration time and not afterwards.
(v)
There is no bound checking concept for arrays in C .It means that one can attempt to
enter any number of values irrespective of the integer index specified inside square
bracket during declaration of arrays.
Types of Array : ONE DIMENSIONAL ARRAY
A one dimensional array is one in which only one subscript specification is needed to specify a
particular element of the array.
Data type var [index];
Int num[10];
Int num[10]={1,3,5,7,9,11,13,15,17,19};
char word[10]={‘h’,’e’,’l’,’l’,’o’,’\o’};
Accessing elements from one Dimensional array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
cout<”Enter the array”;
for(i=0;i<=9;i++)
{
cin>>a[i];
}
cout<<”the entered array is “;
for(i=0;i<=9;i++)
{
cout<<a[i]<<”\n”;
}
getch();
}
Size=(upperbound-lowerbound)+1
Address of element a[k]=B+WxK
B=Base address
W=Size of each element of the array
K=No. of required element in the array (index of element)
Que. If the box address of the first element of the array is 2000 and each element of the array
occupies 4 bytes in the memory, then address of fifth element of a one –dimensional array a[10]
A[5] =2000+5x4
=2020
TWO DIMENSIONAL ARRAY
void main()
{
int ,j,arr[3][3];
cout<<”Enter the elements “;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
Cin>>arr[i][j];
}
}
cout<<”Enter elements are “;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arr[i][j];
}
}
getch();
}
Inserting Element in an array at K positionAlgorithm:
INSERT(LA,N,K,ITEM)
LA= Linear array
N= Number of Elements
K= Positive Integer such that K<=N
ITEM= Element which we have to insert in the kth Position.
Initialize the value of J
1. Set j:=N-1
2. Repeat steps 3 and 4 while J>=K
3. [Move Jth element downward]
Set LA[J+1]=LA[J]
4.
5.
6.
7.
J=J-1 [END of step 2 loop]
Set LA[K]=ITEM
Set N=N+1
EXIT
Deleting a Element from Linear ArrayAlgorithm:
DELETE (LA, N, K, ITEM)
1. Set ITEM=LA[K]
2. Repeat for J=K to N-1 [move J+1st element upward]
Set LA[J]=LA[J+1]
3. j=j+1 [END of loop]
4. Reset the number N of elements in LA.
Set N=N-1
5. Exit
Array Representation of 2-D:
Row-Major implementation: In row major implementation elements of array are read from the
keyboard row-wise. It means that the complete first row is stored and then complete second row
is stored and so on.
a[2][4]
a[0][0]
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[1][2]
a[1][3]
a= a[2][4]=a[r1][r2] Then address of a[i1][i2]=a[1][2]
=Base+[i1*r2+i2]*size
=200+[1*4+2]*1
=206
Que. The Array Data [10, 15] is stored in memory in row major order. If base address is 200 and
column size is 1. Calculate the address of element data [7, 12].
=B+W [n(i-L1)+(j-L2)]
=200+1[15(7-0) + (12-0)]
=200+1[105+12]
=200+117
=317
Column-Major implementation: In this implementation the elements of an array are read from
the keyboard column wise. It means that the complete first column is stored and then complete
second column is stored and so on.
Address of element a[i][j] = B+W (m (j-L2)+(i-L1))
B = Base address
W = Size of element
m = No. of rows
L1 = lower bound of row
L2 = lower bound of column
n = no. of column
U1 = upper bound for rows
U2 = upper bound for column
In Row Major Implementation:
Address of D[i][j] = B+W(n(i-L1)+(j-L2))
Que. How is physical memory allocated for a 2-D array? If each element of an array D [20][50]
requires 4 bytes of storage, base address of DATA is 2000 determine the location of D [10][10],
when the array is stored. (i) Row major. (ii) Column major.
Given, B = 2000
W=4
I = 10
j = 10
L1 = 0
L2 = 0
U1 = 20
U2 = 50
m = 20
n = 50
In row majorD [10] [10] = B+W[n(i-L1)+(j-L2)]
= 2000+4[50(10-0) + (10-0)]
= 2000+4[500+10]
= 2000+4*510
= 2000+2040
=4040
In column major:
D[10][10]=B+W[m(j-L2)+(i-L1)]
=2000+4[20(10-0) + (10-0)]
=2000+4[200+10]
=2000+4*210
=2000+840
=2840
Array as parameters
main()
{
int a[10],i,sum=0;
cout<<”enter the array”;
for(i=0;i<=9;i++)
{
cin>>a[i];
}
sum=funarray(a,9);
cout<<”the sum of elements =”<<sum;
getch();
}
intfunarray(int p[],int n)
{
int s=0;,i;
for(i=0;i<=n-1;i++)
{
s=s+p[i];
}
return(s);
}
Sparse Matrices
Sparse Matrices - Matrices with a relatively high proportion of zero enteries are called Sparse
matrices. For ex- diagonal matrix where only diagonal values are non-zero. Triangular matrix
where below the diagonal or above the diagonal elements are non-zero.
1
0
0
0
2
0
0
0
3
Storage pool
Storage pool is a collection of all the memory blocks that are allocate by the application
programs which are in use. When an object allocates some blocks of mem0ry then that allocation
is returned to storage pool
A program in OS called memory manager which handle memory management part by memory
allocation
These types of strategies are
1. First-fit
2. Best-fit
3. Worst-fit
When a program request a new node, storage is obtain from the pool by a language library
function if insufficient storage is available in the pool, the library request additional pool space
from the operating System.
Best fit
The memory manager allocates the best suited hole to the process.
For example demand of 15 kb memory block then for a list of 10kb, 18kb, 16kb 25kb and 19kb
the memory manager will allocate16kb block unallocated block to the process.
Worst Fist
The memory manager places the process in a largest block of unallocated memory available .By
this storage the main advantages is that a large amount of free space gets available even after
allocation, so that even this running space can be future allocated to any new process demand of
15kb 18kb 16kb 25kb 19kb then 35kb block from a list will allocated.
First Fit
For example demand of 15 kb memory block then for a list of 10kb, 18kb, 16kb 25kb and 19kb
the memory manager will allocate16kb block unallocated block to the process then 18kb block
from a list will allocated.
Garbage collection
The memory allocation for the objects is done from the heap .If some object is created and which
is not been in use from a long time then .Such an object is called garbage.The garbage collection
is a technique in which all such garbage is collected and recycled.The garbage collector cleans
up the heap so that the memory occupied by the unused objects can be freed and can be used
allocating for the new object the garbage collection algorithm works in two steps as follow –
1. Mark-In the marking process all the live objects are located and marked them as non –
collected objects
2. Sweep-In this step all the unmarked objects are swept from the heap and the space that
has been allocating by those objects can be used for allocating new objects.
But the drawback of this mark & sweep algorithmis that multiple fragments of memory get
created hence the technique called compaction is used.
Advantages
1. The manual memory management done by the programmer (after malloc use of free or
delete at the end of the function is time consuming and error prone .hence this automatic
memory management is done.
2. Reusability of the memory can be achieved with the help of garbage collection.
Disadvantages
1. The execution of the program is paused or stopped during the process of garbage
collection.
Recursion
Recursion- A function is called recursive if a statement within the body of a function calls the
same function.
Recursion is an alternative to main() iteration in making a function execute
repeatedly.
{
Int a, fact;
printf(“enter any no.”);
scanf(“%d”,&a);
fact=rec(a);
printf(“Factorial value= %d”, fact);
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
Types of Recursion- Recursion is of 2 types depending on whether a function calls itself
from within itself or whether two function calls one another mutually.
1. Direct Recursion
2. Indirect Recursion
intabc()
{
….
abc()
}
Direct
intabc()
{
….xyz();
}
int xyz()
{
abc();
}
Indirect
Disadvantages of Recursion1. It consumes more storage space because the recursive calls along with automatic
variables are stored on the stack.
2. The computer may run out of memory if the recursive calls are not checked.
3. It is not more efficient in terms of speed and execution time.
4. If proper precautions are not taken, recursion mai result in non-terminating iterations.
5. Recursion is not advocated when the problem can be through iteration. Recursion may be
treated as a software tool to be applied carefully and selectively.
RECURSION PROGRAMME
Program1
#include<stdio.h>
void multi();
void main()
{
int i;
clrscr();
multi(10);
getch();
}
void multi(int n)
{
if(n<10000)
{
multi(n*10);
}
printf(“\n%d”,n);
}
Program2
int power(int ,int);
void main()
{
intn,p,result;
cout<<”enter the number “;
cin>>n;
cout<<”enter the power to be calculated “;
cin>>p;
result=power(n,p);
cout<<”the result”<<result;
getch();
}
int power (intn,int p)
{
staticint r=1;
if(p==0)
return(1);
else
r=r*n;
power(n,p-1);
return(r );
}
Difference between Iteration and RecursionIteration
Recursion
1
2.
3.
4.
It is a process of executing a statement or
a set of statement repeatedly, until some
specified condition is specified.
Iteration involbes four clear cut steps
initialization, condition, execution and
updation.
Any recursive problem can be solved
iteratively.
Iterative counter part of a problem is more
efficient in terms of memory utilization
and execution speed.
Recursion is the technique of defining anything
in terms of itself.
There must be an exclusive if statement
inside the recursion function, specifying
stopping condition.
Not all problems have recursive solution.
Recusion is generally a work option to go for
simple problems or problems not recursive in
nature.
Simulating Recursion
Recursion can be simulated using control structures such as while, do-while, for and goto.
Similar for simulating recursion a special data structure called stack is used.
Backtracking
Backtracking is a technique in which particular domain is searched for obtaining solution. If
desired solution is not found then another domain space is searched. For example N-Queen
Problem
Tail Recursion
Tail recursion is a kind of recursion in which there is no pending operation after returning from
the recursive function. This is special type of recursive approach which helps in improving the
space and time efficiency of recursive function.
Simple Recursion
void main()
{
int fact(int n);
int n;
cout<<”Enter any number”;
cin>>n;
cout<<”factorial=”<<fact(n);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
This program doesn’t have tail recursion because it contains a recursive function fact and on
return of this recursive call multiplication is performed. This multiplication operation is always a
pending operation even after return of recursive call. We can convert the above code into a tail
recursive form as followsvoid main()
{
int fact(int n);
int n;
cout<<”program for finding factorial using tail recursion”;
cout<<”Enter any number”;
cin>>n;
cout<<”factorial is=”<<fact(n);
getch();
}
int fact(int n)
{
int my_new_fact(int n,int f);
return my_new_fact(n,1);
}
int my_new_fact(int n,int f)
{
If(n==1)
return f;
my_new_fact (n-1,n*f);
}
Advantages
1. Optimizes the task of compiler for executing the recursive function.
2. Improves the space and time efficiency of the recursive code.
Tower of Hanoi
The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883.
It consists of three rods, and a number of disks of different sizes which can slide onto any rod.
The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest
at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following
rules:

Only one disk must be moved at a time.

Each move consists of taking the upper disk from one of the rods and sliding it onto another
rod, on top of the other disks that may already be present on that rod.

No disk may be placed on top of a smaller disk.
Recursive solution
A key to solving this puzzle is to recognize that it can be solved by breaking the problem down
into a collection of smaller problems and further breaking those problems down into even
smaller problems until a solution is reached. For example:

label the pegs A, B, C — these labels may move at different steps

let n be the total number of discs

number the discs from 1 (smallest, topmost) to n (largest, bottommost)
To move n discs from peg A to peg C:
1. move n−1 discs from A to B. This leaves disc n alone on peg A
2. move disc n from A to C
3. move n−1 discs from B to C so they sit on disc n
The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again
for n−1. The entire procedure is a finite number of steps, since at some point the algorithm will
be required for n = 1. This step, moving a single disc from peg A to peg B, is trivial. This
approach can be given a rigorous mathematical formalism with the theory of dynamic
programming, and is often used as an example of recursion when teaching programming.
Download