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

Data Structures

dsa in c++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data Structures

dsa in c++
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Data

Data are basic raw facts .


For example name of a person, age of an employee, chair color , numbers.
Data structures

Data Structure is a way for structuring data i.e way of storing data in a computer so that it can be used
efficiently .
Why to learn data structures
As applications are getting complex and data rich, there are three common problems that
applications face now-a-days.
•Data Search − Consider an inventory of 1 million(106) items of a store. If the application is to
search an item, it has to search an item in 1 million(106) items every time slowing down the
search. As data grows, search will become slower.
•Processor speed − Processor speed although being very high, falls limited if the data grows to
billion records.
•Multiple requests − As thousands of users can search data simultaneously on a web server, even
the fast server fails while searching the data.
To solve the above-mentioned problems, data structures come to rescue. Data can be organized in
a data structure in such a way that all items may not be required to be searched, and the required
data can be searched almost instantly.
Operations on Data Structure

•Search − Algorithm to search an item in a data structure.


•Sort − Algorithm to sort items in a certain order.
•Insert − Algorithm to insert item in a data structure.
•Update − Algorithm to update an existing item in a data structure.
•Delete − Algorithm to delete an existing item from a data structure.
Types of data structures
Linear Data structures:-A data structure is linear if every item is related to its previous and next
items.In linear data structure data items are arranged in a linear sequence.
In linear data structure data items are arranged in a linear sequence.

Non –Linear Data structures:-A data structure is non – linear if every item is attached to many
other items in specific ways to reflect relationships. In non linear data structure data items are
not in a sequence.
Example of Linear and Non –Linear data structures
Linear:-
(a) Linked List
(b) Stack
(c) Queue
(d) Array
Non Linear
(a) Tree
(b) Sets
(C)Graphs
Array
Array is linear data structure and is a collection of similar data type and having a common
name.The elements of the array can be accessed with the help of index.Array is a static
collection of data.Once the memory is allocated for an array it cannot be altered while
running the program.
Example:-Consider a linear array A[6]consisting of roll numbers of 6 students such that:

0 10
1 11
2 12

3 13
4 14
5 15
Linked List
A linked list is a linear collection of data elements called nodes,where the linear order is given by
means of pointers.The node in the linked list is divided into two parts-the first part contain the
information and the second part contains the address node in the list.In the linked list data can
be inserted or deleted from any position.
Example:-Linear Linked list

Start

A B C D E X
Stack
A Stack is a collection of elements in which an element may be inserted or deleted at one end
called the top of stack ,so stack is called Last-in-first-out(LIFO)

D
A
B

C
Queues
A queue is a linear list of elements in which can be inserted only at one end called the rear and
the deletions can be taken place at other end called front.So queue is called first-in-first-
out(FIFO)list because the first element in a queue is the first element out of queue.
Example:-

Rear 10 20 30 40 Front
Trees
A tree is a non linear data structure which is used to represent a hierarchical relationship
between elements.Trees grows downwards from top to bottom.

B C

D E F G
Graph
A Graph G is a set of vertices and edges i.e. G=(V,E)
Where V is a finite and non empty set of vertices and E is the set of pairs of vertices.

v1 v3
E1 E3 E4

v2 v4
E2
Algorithms
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output.
Example of algorithm
Algorithm to add two numbers entered by the user:-

Step 1: Start
Step2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2
Step 5: Display sum
Step 6: Stop
Characteristics of algorithms
•Unambiguous − Algorithm should be clear and unambiguous. Each of its steps
(or phases), and their inputs/outputs should be clear and must lead to only one
meaning.
•Input − An algorithm should have 0 or more well-defined inputs.
•Output − An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
•Finiteness − Algorithms must terminate after a finite number of steps.
•Feasibility − Should be feasible with the available resources.
•Independent − An algorithm should have step-by-step directions, which should
be independent of any programming code.
Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before
implementation and after implementation. They are the following −
•A Priori Analysis − This is a theoretical analysis of an algorithm. 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.
•A Posterior Analysis − This is an empirical analysis of an algorithm. The
selected algorithm is implemented using programming language. This is then
executed on target computer machine. In this analysis, actual statistics like
running time and space required, are collected.
Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used by
the algorithm X are the two main factors, which decide the efficiency of X.
•Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
•Space Factor − Space is measured by counting the maximum memory space
required by the algorithm.
Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the
algorithm in its life cycle.
Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed
part and S(I) is the variable part of the algorithm.
Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3.
Now, space depends on data types of given variables and constant Here types and it
will be multiplied accordingly.
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop

In this example we have three variables A, B, and C and one constant. Hence S(P) =
1 + 3. Now, space depends on data types of given variables and constant types and it
will be multiplied accordingly.
Time Complexity

Time complexity of an algorithm represents the amount of time required by the algorithm
to run to completion
For example, addition of two n-bit integers takes n steps. Consequently, the total
computational time is T(n) = c ∗ n, where c is the time taken for the addition of two bits.
Asymptotic analysis
Asymptotic analysis refers to computing the running time of any operation in
mathematical units of computation.
Usually, the time required by an algorithm falls under three types −
•Best Case − Minimum time required for program execution.
•Average Case − Average time required for program execution.
•Worst Case − Maximum time required for program execution.
Asymptotic Notations
•Ο Notation
•Ω Notation
•θ Notation
Big Oh Notation, Ο

The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time. It measures the worst case time complexity or
the longest amount of time an algorithm can possibly take to complete.
Omega Notation, Ω

The notation Ω(n) is the formal way to express the lower bound of an algorithm's running
time. It measures the best case time complexity or the best amount of time an algorithm
can possibly take to complete.
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper
bound of an algorithm's running time.
Abstract Data type
The Data Type is basically a type of data that can be used in different computer
program. It signifies the type like integer, float etc, the space like integer will take 4-
bytes, character will take 1-byte of space etc.
The abstract datatype is special kind of datatype, whose behavior is defined by a set of
values and set of operations. The keyword “Abstract” is used as we can use these
datatypes, we can perform different operations. But how those operations are working
that is totally hidden from the user. The ADT is made of with primitive datatypes, but
operation logics are hidden.
Abstract data type
Some examples of ADT are Stack, Queue, List etc.
Let us see some operations of those mentioned ADT −
• Stack −
• isFull(), This is used to check whether stack is full or not
• isEmpry(), This is used to check whether stack is empty or not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top of the stack
• peek(), This is used to get the top most element of the stack
• size(), this function is used to get number of elements present into the stack

• Queue −
• isFull(), This is used to check whether queue is full or not
• isEmpry(), This is used to check whether queue is empty or not
• insert(x), This is used to add x into the queue at the rear end
• delete(), This is used to delete one element from the front end of the queue
• size(), this function is used to get number of elements present into the queue
Abstract Data type
•List −
• size(), this function is used to get number of elements present into the list
• insert(x), this function is used to insert one element into the list
• remove(x), this function is used to remove given element from the list
• get(i), this function is used to get element at position i
• replace(x, y), this function is used to replace x with y value
Programming Methodology
When programs are developed to solve real-life problems like inventory management,
payroll processing, student admissions, examination result processing, etc. they tend to
be huge and complex. The approach to analyzing such complex problems, planning for
software development and controlling the development process is called programming
methodology.
Types of Programming Methodologies

Procedural Programming
Problem is broken down into procedures, or blocks of code that perform one task each.
All procedures taken together form the whole program. It is suitable only for small
programs that have low level of complexity.
Example − For a calculator program that does addition, subtraction, multiplication,
division, square root and comparison, each of these operations can be developed as
separate procedures. In the main program each procedure would be invoked on the
basis of user’s choice.
Types of Programming
Methodologies
Object-oriented Programming
Here the solution revolves around entities or objects that are part of problem. The
solution deals with how to store data related to the entities, how the entities behave and
how they interact with each other to give a cohesive solution.
Example − If we have to develop a payroll management system, we will have entities
like employees, salary structure, leave rules, etc. around which the solution must be
built.
Types of Programming
Methodologies
Functional Programming
Here the problem, or the desired solution, is broken down into functional units. Each unit
performs its own task and is self-sufficient. These units are then stitched together to form
the complete solution.
Example − A payroll processing can have functional units like employee data
maintenance, basic salary calculation, gross salary calculation, leave processing, loan
repayment processing, etc.
Types of Programming
Methodologies
Logical Programming
Here the problem is broken down into logical units rather than functional
units. Example: In a school management system, users have very defined roles like
class teacher, subject teacher, lab assistant, coordinator, academic in-charge, etc. So
the software can be divided into units depending on user roles. Each user can have
different interface, permissions, etc.
Software developers may choose one or a combination of more than one of these
methodologies to develop a software.. To do this, developers use any of the following
two approaches −
•Top-down approach
•Bottom-up approach
Types of Programming
Methodologies
Top-down or Modular Approach
The problem is broken down into smaller units, which may be further broken down into
even smaller units. Each unit is called a module. Each module is a self-sufficient unit
that has everything necessary to perform its task.
The following illustration shows an example of how you can follow modular approach to
create different modules while developing a payroll processing program.
Types of Programming
Methodologies
Bottom-up Approach
In bottom-up approach, system design starts with the lowest level of components, which
are then interconnected to get higher level components. This process continues till a
hierarchy of all system components is generated. However, in real-life scenario it is very
difficult to know all lowest level components at the outset. So bottoms up approach is
used only for very simple problems.
Time space trade off
The best algorithm, hence best program to solve a given problem is one that requires less
space in memory and takes less time to execute its instruction or to generate output. But in
practice, it is not always possible to achieve both of these objectives. As said earlier, there
may be more than one approaches to solve a same problem. One such approach may require
more space but takes less time to complete its execution. Thus we may have to sacrifice one
at the cost of the other. That is what we can say that there exists a time space trade off
among algorithms.
Therefore, if space is our constraints then we have to choose a program that requires less
space at the cost of more execution time. Other than that, if time is our constraint, then we
have to choose a program that takes less time to complete its execution of statements at the
cost of more space.
In the analysis of algorithms, we are interested in the average case, the amount of time
a program might be expected to take on typical input data and in the worst case the
total time required by the program or the algorithm would take on the worst possible
inputs of that algorithm.
Types of Trade -off
1)Lookup tables Vs Recalculation

•In lookup table, an implementation can include the entire table which reduces computing
time but increases the amount of memory needed.
• It can recalculate i.e. compute table entries as needed, increasing computing time but
reducing memory requirements.

2)Compressed Vs Un compressed data


A space -time trade off can be applied to the problem of data storage.
● If data is stored uncompressed,it takes more space but less time.
● If the data is stored compressed, it takes less space but more time to run the
decompression algorithm.
Types of Trade -off
(3)Smaller Code(with loop) / Larger Code (without loop)
● Smaller code occupies less space in memory but it requires high computation time
which is required for jumping back to the beginning of the loop at the end of each
iteration.
● Larger code or loop unrolling can be traded for higher program speed. It occupies
more space in memory but requires less computation time. (as we need not perform
jumps back and forth since there is no loop.)
Identify the data structure that resemble the following example:
(a) Set of bangles worn by woman:-Stack
(b) Organization structure of company:-Tree
(c) Group of people lined up at ticket counter:-Queue
Q Draw the figures of Big Oh,Theta Omega with their equation
Big Oh Notation,O
Figure and equation

Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤


c.g(n) for all n > n0. }
Omega Notation, Ω

Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }

You might also like