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

Data Structure Notes

The document discusses the need for data structures and algorithms to efficiently represent and process data in computers. It defines an algorithm as a set of steps to solve a problem and discusses properties like time and space complexity that measure algorithm efficiency. Several algorithm design techniques are described, including brute force, divide-and-conquer, decrease-and-conquer, transform-and-conquer, greedy algorithms, dynamic programming, and backtracking. Finally, it differentiates between primitive data structures like integers and characters, and non-primitive structures like arrays, files, and lists that are used to organize and store data.

Uploaded by

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

Data Structure Notes

The document discusses the need for data structures and algorithms to efficiently represent and process data in computers. It defines an algorithm as a set of steps to solve a problem and discusses properties like time and space complexity that measure algorithm efficiency. Several algorithm design techniques are described, including brute force, divide-and-conquer, decrease-and-conquer, transform-and-conquer, greedy algorithms, dynamic programming, and backtracking. Finally, it differentiates between primitive data structures like integers and characters, and non-primitive structures like arrays, files, and lists that are used to organize and store data.

Uploaded by

Exclusive Munda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NEED FOR DATA STRUCTURES

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.

ALGORITHM DESIGN TECHNIQUES


It is a general approach to solve problems algorithmically. It can be applied to a variety of
problems from different areas of computing. They provide us guidance in designing
algorithms for new problems. They represent a collection of tools useful for applications.

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.

Examples of divide-and-conquer algorithms:


Divide-before-processing: the bulk of the work is done while combining solutions to smaller
sub problems, e.g., merge sort.
Process-before-dividing: processing before a partition into sub problems, e.g., quick sort.

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.

STAGE 1: (Transformation stage): The problem’s instance is modified, more amenable to


solution
STAGE 2: (Conquering stage): The transformed problem is solved
The three major variations of the transform & conquer differ by the way a given instance is
transformed:

 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.

GREEDY ALGORITHMS "take what you can get now" strategy


The solution is constructed through a sequence of steps, each expanding a partially
constructed solution obtained so far. Basic principal of this technique is to construct a
solution sequentially by making the best local choice at each step.
Examples: 
Minimal spanning tree
Shortest distance in graphs
The coin exchange problem
Greedy techniques are mainly used to solve optimization problems. They do not always give
the best solution.

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

 Defining The operations that can be performed on that data.


 Representing the data in the memory.
 Determining the amount of memory required to store the structure and the amount of time needed
to process the structure.

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.

Types of Data Structures

Primitive Data Structures

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.

Examples: Integer, float, character, pointers.


Non-primitive Data Structures

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:

 Graphs: The Graph data structure is used to represent a network. It comprises of


vertices and edges (to connect the vertices). The graphs are very useful when it comes
to study a network.
 Trees: Tree data structure comprises of nodes connected in a particular arrangement
and they (particularly binary trees) make search operations on the data items easy. The
tree data structures consists of a root node which is further divided into various child
nodes and so on. The number of levels of the tree is also called height of the tree.

STATIC AND DYNAMIC DATA STRUCTURE

DYNAMIC STATIC

Memory is allocated to the data structure


Memory is allocated at compile time.
dynamically i.e. as the program executes. Fixed size.

Disadvantage: Because the memory


allocation is dynamic, it is possible for theAdvantage: The memory allocation is
fixed and so there will be no problem
structure to 'overflow' should it exceed its
with adding and removing data items.
allowed limit. It can also 'underflow'
should it become empty.

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.

Disadvantage: Harder to program as the


Advantage: Easier to program as there
software needs to keep track of its size
is no need to check on data structure
and data item locations at all times size at any point.

You might also like