Data Structure Notes
Data Structure Notes
The computers are electronic data processing machines.In order to solve a particular problem we need
to know
How to represent the data in computer,
How to access them
What are the steps to be performed to get the needed output.
These tasks can be achieved with the knowledge of data structures and algorithms.
What is Algorithm ?
An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined
task. Algorithm is not the complete code or program, it is just the core logic(solution) of a problem,
which can be expressed either as an informal high level description as pseudocode or using
a flowchart.
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following properties :
1. Time Complexity
2. Space Complexity
Space Complexity
Its the amount of memory space required by the algorithm, during the course of its execution. Space
complexity must be taken seriously for multi-user systems and in situations where limited memory is
available.
Time Complexity
Time Complexity is a way to represent the amount of time needed by the program to run to
completion. We will study this in details in our section.
Characteristics of Algorithms:-
There are following characteristics of any algorithms as given below.
1. Input:-An algorithm should have one or more inputs.
2. Output:-An algorithm must have at least one output.
3. Definiteness:- Every statement in any algorithm should be definiteness.It means.Every
statement in algorithm should have unambiguous.Every statement should have clear and
there should not be more than one way to interprate the single statement in given algorithm.
4. Finiteness:-An algorithm should have a finite number of steps(instructions) to solve the
problems and get a valid output.
5. Effectiveness:-An algorithm should have effectiveness and produce well defined output of any
programs. Effectiveness means, an algorithms should have good method which produce
effective output with less time and less storage capacity.
BRUTE FORCE
Refers to a programming style that does not include any shortcuts to improve performance,
but instead relies on sheer computing power to try all possibilities until the solution to a
problem is found. A classic example is the traveling salesman problem (TSP).Suppose a
salesman needs to visit 10 cities across the country. How does one determine the order in
which cities should be visited such that the total distance traveled is minimized? The brute
force solution is simply to calculate the total distance for every possible route and then
select the shortest one. This is not particularly efficient because it is possible to eliminate
many possible routes through clever algorithms.
Some examples of brute force algorithms are:
Selection sort
Bubble sort
Sequential search
Exhaustive search: Traveling Salesman Problem, Knapsack problem.
DIVIDE-AND-CONQUER
This technique consists in
Dividing the problem in sub problems: A problem of size n is divided in several sub
problems. These sub problems should be independent and of about the same size.
Solving the sub problems: All sub problems are solved by applying the same technique
Combining the results: The results obtained by solving the sub problems are combined in
order to obtain the solution of the initial problem.
DECREASE-AND-CONQUER
Decrease and Conquer technique is a way of solving a problem by changing the instance
into a smaller instance, solving the smaller instance and then converting the solution for
smaller instance into a solution for bigger instance.
Examples of decrease-and-conquer algorithms:
Binary Tree traversals: in-order, preorder and post-order (recursion)
Computing the length of the longest path in a binary tree (recursion)
Computing Fibonacci numbers (recursion)
TRANSFORM-AND-CONQUER
Transform & Conquer is a general algorithm design technique which works in two stages.
Instance simplification: the instances of the problem can be transformed into an easier
instance to solve.
Representation change: the data structure can be transformed so that it is more efficient.
Problem reduction: the problem can be transformed to an easier problem to solve.
DYNAMIC PROGRAMMING
Dynamic Programming is a Bottom-Up Technique in which the smallest sub-instances are
explicitly solved first and the results of these used to construct solutions to progressively
larger sub-instances.
In contrast, Divide-and-Conquer is a Top-Down Technique which logically progresses from
the initial instance down to the smallest sub-instance via intermediate sub-instances.
Examples:
Fibonacci numbers computed by iteration.
Warshall’s algorithm implemented by iterations
BACKTRACKING
The method is used for state-space search problems. State-space search problems are
problems, where the problem representation consists of:
initial state
goal state(s)
a set of intermediate states
a set of operators that transform one state into another. Each operator has pre-
conditions and post-conditions.
a cost function – evaluates the cost of the operations (optional)
a utility function – evaluates how close is a given state to the goal state (optional)
Example:
Eight Queens Problem
Graph Coloring
Knapsack Problem
The following problems can be solved using state-space search techniques:
A farmer has to move a goat, a cabbage and a wolf from one side of a river to the other side
using a small boat. The boat can carry only the farmer and one more object (either the goat,
or the cabbage, or the wolf). If the farmer leaves the goat with the wolf alone, the wolf
would kill the goat. If the goat is alone with the cabbage, it will eat the cabbage. How can
the farmer move all his property safely to the other side of the river?"
You are given two jugs, a 4-gallon one and a 3-gallon one. Neither has any measuring
marker on it. There is a tap that can be used to fill the jugs with water. How can you get
exactly 2 gallons of water into the 4-gallon jug?
DATA STRUCTURE
Data structure is the way of organizing the data along with the relationship among the data. The study
of data structure includes
Data structures are a very important programming concept. They provide us with a means to
store, organize and retrieve data in an efficient manner. The data structures are used to make
working with our data, easier.
These are the structures which are supported at the machine level, they can be used to make
non-primitive data structures. These are integral and are pure in form. They have predefined
behavior and specifications.
The non-primitive data structures cannot be performed without the primitive data structures.
Although, they too are provided by the system itself yet they are derived data structures and
cannot be formed without using the primitive data structures.
The Non-primitive data structures are further divided into the following categories:
1. Arrays
Arrays are a homogeneous and contiguous collection of same data types. They have a static
memory allocation technique, which means, if memory space is allocated for once, it cannot be
changed during runtime. The arrays are used to implement vectors, matrices and also other
data structures. If we do not know the memory to be allocated in advance then array can lead
to wastage of memory. Also, insertions and deletions are complex in arrays since elements are
stored in consecutive memory allocations.
2. Files
A file is a collection of records. The file data structure is primarily used for managing large
amounts of data which is not in the primary storage of the system. The files help us to process,
manage, access and retrieve or basically work with such data, easily.
3. Lists
The lists support dynamic memory allocation. The memory space allocated, can be changed at
run time also. The lists are of two types:
a) Linear Lists
The linear lists are those which have the elements stored in a sequential order. The insertions
and deletions are easier in the lists. They are divided into two types:
Stacks: The stack follows a “LIFO” technique for storing and retrieving elements. The
element which is stored at the end will be the first one to be retrieved from the stack.
The stack has the following primary functions:
Push(): To insert an element in the stack.
Pop(): To remove an element from the stack.
Queues: The queues follow “FIFO” mechanism for storing and retrieving elements. The
elements which are stored first into the queue will only be the first elements to be
removed out from the queue. The “ENQUEUE” operation is used to insert an element
into the queue whereas the “DEQUEUE” operation is used to remove an element from
the queue.
b) Non Linear Lists
The non linear lists do not have elements stored in a certain manner. These are:
DYNAMIC STATIC
Advantage: Makes the most efficient use Disadvantage: Can be very inefficient as
the memory for the data structure has
of memory as the data structure only uses
been set aside regardless of whether it
as much memory as it needs is needed or not whilst the program is
executing.