Introduction to Data Structure &
Algorithms
Qurra-tul-ann
[email protected]
Definition
Data structure is representation of the logical relationship existing
between individual elements of data.
A data structure is a way to store and organize data in a computer so that
it can be used efficiently
Why do we need data structure?
Why are Data Structures and
Algorithms Important?
If we use the Data Structures and Algorithms efficiently, then we
can increase the execution speed of the program and reduce the
time.
For example, consider a program that needs to search for a
particular item in a large dataset. Using an efficient search
Algorithm like Binary Search can significantly reduce the search
time and improve the performance of the program.
What is Data Structure?
Place data contiguously
Place data here and there with “links”
Place data with “formula”
Abstract Data Types
It is an abstraction of a data structure that provides only the interface to which the
data structure must adhere. The interface does not give any specific details about
something should be implemented or in what programming language.
Defines data and operations but no implementation
Eg: linked list, stack ,queue
For example, we use primitive values like int, float, and char with the
understanding that these data types can operate and be performed on without any
knowledge of their implementation details. ADTs operate similarly by defining
what operations are possible without detailing their implementation.
Classification of Data Structure
Data structure are normally divided into two broad categories:
Primitive Data Structure
Non-Primitive Data Structure
Classification of Data Structure
Data structure
Primitive DS Non-Primitive DS
Integer Float Character Pointer
Classification of Data Structure
Non-Primitive DS
Linear List Non-Linear List
Array Queue Graph Trees
Link List Stack
Primitive Data Structure
Primitive data structure is a data structure that can hold a single value
in a specific location.
The primitive data structures are nothing but the predefined data
structures, which are already defined .The value to the primitive data
structure is provided by the programmer
There are basic structures and directly operated upon by the machine
instructions.
In general, there are different representation on different computers.
Example: Integer, Floating-point number, Character constants,
string constants, pointers
Non-Primitive Data Structure
Data structure that can hold multiple values either in a contiguous or
random location. non-primitive data types are defined by the
programmer.
There are more sophisticated data structures.
These are derived from the primitive data structures.
The non-primitive data structures emphasize on structuring of a group of
homogeneous (same type) or heterogeneous (different type) data items.
Example : Lists, Stack, Queue, Tree, Graph
Non-Primitive Data Structure
The non-primitive data structure is further classified into two categories,
i.e., linear and non-linear data
Linear Data Structure :data organized in sequential order.eg:
Arrays ,Linked List
Non-Linear Data Structure: data organized in random order. Eg: Trees,
Graphs,Dictionaries,Heaps
Linear data structure
the data is stored in a sequence, i.e., one data after another data. When we
access the data from the linear data structure, we just need to start from
one place and will find other data in a sequence.
Each element is referred to by index
Linear data structures are easy to implement because computer memory is
arranged linearly. Its examples are array, stack, queue, linked list, etc.
Non-linear data structure
A non-linear data structure is another important type in which data
elements are not arranged sequentially; mainly, data elements are
arranged in random order without forming a linear structure.
Data elements are present at the multilevel, for example, tree.
In trees, the data elements are arranged in the hierarchical form
in graphs, the data elements are arranged in random order, using the edges
and vertex.
Multiple runs are required to traverse through all the elements
completely. Traversing in a single run is impossible to traverse the whole
data structure.
graph tree
Non-Primitive Data Structure
The most commonly used operation on data structure are broadly
categorized into following types:
Create
Selection
Updating
Searching
Sorting
Merging
Destroy or Delete
Algorithms
Data structure affects the design of both structural & functional aspects of
a program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step procedure to solve a
particular function.
Introduction
That means, algorithm is a set of instruction written to carry out certain
tasks & the data structure is the way of organizing the data with their
logical relationship retained.
To develop a program of an algorithm, we should select an appropriate
data structure for that algorithm.
Therefore algorithm and its associated data structures from a program.
Algorithm and Algorithm Analysis
How to access data for a result
Scan data sequentially
Scan data according to the sequence of a structure
Scan data with “formula”
How to provide a smart solution
Algorithm Analysis
To understand how good or how bad an algorithm is
Theoretically evaluate an algorithm
Execution time
Storage efficiency
Experimentally evaluate an algorithm
Use system clock to measure the execution time
Algorithm Analysis
Efficiency as a function of input size
The number of bits in an input number
Number of data elements (number, points)
Measuring the Running Time
Experimental Study
Use system clock to measure the execution time in c++ program
Use method like System.currentMillis() to get an accurate measure of actual running
time in java
Limitations of Experimental Study
It is necessary to implement and test the algorithm in order to determine its running
time.
Experiments can be done only on a limited set of inputs, and may not be indicative of
the running time on other inputs not included in the experiments.
In order to compare two algorithms the same hardware and software environment
should be used
Role of Data Structure
Data structures help us to organize the data in the computer, resulting in more
efficient programs.
An efficient program executes faster and helps minimize the usage of resources like
memory, disk.
Revolution in technology is not the first choice – write algorithm that can run
efficiently in existing resources. Software solution.
Selection of Data Structure
Before selecting a data structure Analyse the problem. Selection of Data Structure is
very much dependent on the nature of problem.
What operations are to be performed on data
Always select data structure that meets maximum requirement
Philosophical View of Data Structure
Each data structure has costs and benefits.
Cost is in term of space and time
A single data structure (e.g Array) is not good for all problems. In different situations,
different situations different data structures will be suitable.
There are three basic things associated with data structures. A data structure requires:
space for each data item it stores
time to perform each basic operation
programming effort