Chapter1 Data Structures
Chapter1 Data Structures
Chapter-1
INTRODUCTION TO DATA STRUTURES
Definition: Data Structure is the way of collecting and organizing the data in such a
way that we can perform operation on these data in an effective way.
Popularly used organized data structures are arrays, stack, queues, graphs, tress and
linked list
2. Storing data: Data structures are used for efficient data persistence, such as
specifying the collection of attributes and corresponding structures used to stores
records in a database management system.
5.Searching: Indexed creates using binary search trees, B-trees or hash tables speed
the ability to find a specific sought-after item.
Ordering and sorting: Data structures such as binary search tree, also known as an
ordered or sorted tree provide efficient methods of sorting objects, such as character
strings used as tags.
Data types: Data types of a variable specifies the range of values and amount of storage
space that a variable required. The basic data types includes the following:
1. Integer – We use these for storing various whole numbers, such as 5, 8, 67, 2390,
etc.
2. Character – It refers to all ASCII character sets as well as the single alphabets, such
as ‘x’, ‘Y’, etc.
3. Double – These include all large types of numeric values that do not come under
either floating-point data type or integer data type. Visit Double Data Type in C to know
more.
4. Floating-point – These refer to all the real number values or decimal points, such as
40.1, 820.673, 5.9, etc.
Static data structures have fixed formats and sizes along with memory locations. The
static characteristic shows the compilation of the data.
Time Complexity
The time factor should be very punctual. The running time or the execution time of a
program should be limited. The running time should be as less as possible. The less the
running time, the more accurate the device is.
Correctness
Each data must definitely have an interface. Interface depicts the set of data structures.
Data Structure should be implemented accurately in the interface.
Space Complexity
The Space in the device should be managed carefully. The memory usage should be
used properly. The space should be less occupied, which indicates the proper function
of the device.
for n.
Linear Data structures are kind of data structure that has homogeneous elements.
The data structure in which elements are in a sequence and form a liner series.
Linear data structures are very easy to implement, since the memory of the computer is
also organized in a linear fashion.
Some commonly used linear data structures are Stack, Queue and Linked Lists.
Deletion: The process of removing an existing data element from the given
collection of data elements is called deletion.
Searching: The process of finding the location of a data element in the given
collection of data elements is called as searching.
Sorting: The process of arrangement of data elements in ascending or
descending order is called
Introduction to Algorithm
The word Algorithm means ” A set of finite rules or instructions to be followed
in calculations or other problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps
that frequently involves recursive operations”.
Algorithm comes from the name of a mathematician Abu Jafar Muhammed
Ibn Musa Khwarizmi.
3.Well-Defined Outputs: The algorithm must clearly define what output will
be yielded and it should be well-defined as well. It should produce at least 1
output.
4. Finite-ness: The algorithm must be finite, i.e. it should terminate after a
finite time.
5. Feasible: The algorithm must be simple, generic, and practical, such that
it can be executed with the available resources. It must not contain some
future technology or anything.
6.Language Independent: The Algorithm designed must be language-
independent, i.e. it must be just plain instructions that can be implemented
in any language, and yet the output will be the same, as expected.
7.Input: An algorithm has zero or more inputs. Each that contains a
fundamental operator must accept zero or more inputs.
8. Output: An algorithm produces at least one output. Every instruction that
contains a fundamental operator must accept zero or more inputs.
9. Definiteness: All instructions in an algorithm must be unambiguous,
precise, and easy to interpret. By referring to any of the instructions in an
algorithm one can clearly understand what is to be done. Every fundamental
operator in instruction must be defined without any ambiguity.
10.Finiteness: An algorithm must terminate after a finite number of steps in
all test cases. Every instruction which contains a fundamental operator must
be terminated within a finite amount of time. Infinite loops or recursive
functions without base conditions do not possess finiteness.
11.Effectiveness: An algorithm must be developed by using very basic,
simple, and feasible operations so that one can trace it out by using just
paper and pencil.
Analyze an Algorithm
For a standard algorithm to be good, it must be efficient. Hence the efficiency
of an algorithm must be checked and maintained. It can be in two stages:
1. Priori Analysis:
“Priori” means “before”. Hence Priori analysis means checking the algorithm
before its implementation. In this, the algorithm is checked when it is written
in the form of theoretical steps. This Efficiency of an algorithm is measured
by assuming that all other factors, for example, processor speed, are
constant and have no effect on the implementation. This is done usually by
the algorithm designer. This analysis is independent of the type of hardware
and language of the compiler. It gives the approximate answers for the
complexity of the program.
2. Posterior Analysis:
“Posterior” means “after”. Hence Posterior analysis means checking the
algorithm after its implementation. In this, the algorithm is checked by
implementing it in any programming language and executing it. This analysis
helps to get the actual and real analysis report about correctness(for every
possible input/s if it shows/returns correct output or not), space required,
time consumed, etc. That is, it is dependent on the language of the compiler
and the type of hardware used.
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...
Example 2
int sum(int A[ ], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
In the above piece of code it requires
'n*2' bytes of memory to store array variable 'a[ ]'
2 bytes of memory for integer parameter 'n'
4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each)
2 bytes of memory for return value.
Consider function f(n) as time complexity of an algorithm and g(n) is the most
significant term. If C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and
n0 >= 1. Then we can represent f(n) as Θ(g(n)).
f(n) = Θ(g(n))
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <=
C2 g(n) for all values of C1 > 0, C2 > 0 and n0>= 1
C1 g(n) <= f(n) <= C2 g(n)
⇒C1 n <= 3n + 2 <= C2 n
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 2.
By using Big - Theta notation we can represent the time compexity as follows...
3n + 2 = Θ(n)
Difference between Posteriori analysis and A Priori analysis:
Recursion
Recursion is the process of calling a function itself repeatedly until a particular
condition is met. A function that calls itself directly or indirectly is called a
recursive function and such kind of function calls are called recursive calls.
Fundamentals of Recursion in C
The fundamental of recursion consists of two objects which are essential parts
of any recursive function. These are:
1.Recursion Case
2. Base Condition
1. Recursion Case
The recursion case refers to the recursive call present in the recursive function.
It decides what type of recursion will occur and the problem will be divided into
smaller sub problems.
2. Base Condition
The base condition specifies when the recursion is going to terminate. It is the
condition that determines the exit point of the recursion. We have to be careful
while defining the base condition because the incorrect or incomplete base
condition may lead the recursion to run for infinite times.
Types of Recursion
The recursion can be classified into different types based on what kind of
recursive case is present in the recursion.
Direct Recursion
Head Recursion
Tail Recursion
Tree Recursion
Indirect Recursion
1. Direct Recursion
Direct recursion is the most common type of recursion, where a function calls
itself directly within its own body. The recursive call can occur once or multiple
times within the function due to which we can further classify the direct
recursion
2.Indirect Recursion
Indirect recursion is an interesting form of recursion where a function calls
another function, which eventually calls the first function or any other function
in the chain, leading to a cycle of function calls. In other words, the functions
are mutually recursive. This type of recursion involves multiple functions
collaborating to solve a problem.
Tower of Hanoi
Towers, or simply pyramid puzzle[3]) is a mathematical game or puzzle
consisting of three rods and a number of disks of various diameters, which can
slide onto any rod. The puzzle begins with the disks stacked on one rod in
order of decreasing size, the smallest at the top, thus approximating a conical
shape.
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of
moves required to solve a Tower of Hanoi puzzle is 2 n − 1, where n is the
number of disks.
These rings are of different sizes and stacked upon in an ascending order, i.e.
the smaller one sits over the larger one. There are other variations of the puzzle
where the number of disks increase, but the tower count remains the same.
Rules
The mission is to move all the disks to some another tower without violating
the sequence of arrangement. A few rules to be followed for Tower of Hanoi are
−
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP