Unit 1 Introduction To Data Structure
Unit 1 Introduction To Data Structure
Data Types, Concept of data structure, Abstract Data Type, Implementation of Data Structure, Introduction to
algorithms, Basics of asymptotic notations.
Definition, Stack as an ADT, Stack operations, Stack Application: Evaluation of infix, postfix, and prefix
expression, Conversion of infix to postfix/prefix expression.
Definition, Queue as an ADT, Primitive operations in Queue, Linear and Circular queue and their application,
Array Based queue implementation, Priority Queue.
1
Unit 4: Lists [9 hours]
Definition, Static and dynamic list structure, Array implementation of lists, Queues as list. Linked List and
linked list as an ADT, Types of Linked List: Singly Linked List, Circular Linked List, Doubly Linked Lists,
Basic operations in linked list: node insertion, deletion, insertion and deletion after and before nodes, Linked
stacks and queues using linked lists.
Principle of Recursion, Comparison between recursion and iteration, Recursion example, Factorial, Fibonacci
sequence, GCD and Tower of Hanoi (TOH) Applications of Recursion.
Introduction, Internal and External Sorting, Insertion Sort, Selection Sort, Bubble Sort, Merge Sort and Quick
Sort, Efficiency of Sorting algorithms.
Introduction to Searching, Search algorithms: Sequential search, Binary search, Efficiency of different search
algorithms, Hashing: hash function, hash table, collision resolution techniques.
Concepts of tree, Basic operation in Binary tree,Tree height, level and depth, Tree search and
insertion/deletions, Binary tree traversals (pre-order, post-order and in-order), AVL balanced tree, Balancing
algorithm.
Represention and applications, Graphs as an ADT, Graph types, Transitive closure, Warshall's algorithm,
Graph traversal, Minimum spanning tree: Kruskal's and Prims algorithms, Shortest-path algorithm Dijkstra's
Algorithm.
2
Laboratory Work:
1. Implementation of Stack
2. Implementation of Linear and Circular Queues
3. Implementation of list and link list: Singly and Doubly
4. Factorial, Fibonacci and TOH using Recursion
5. Implementation of different Sorting Algorithms
6. Implementation of Searching Algorithms
7. Implementation of Binary and AVL tree.
8. Implementation of Tree Traversals
9. Implementation of Graph and its Traversal.
Reference Books:
1. Y. Langsam, M.J. Augenstein and A. M. Tenenbaum, Data Structures using C and C++ 2 nd Edition.
2. G. W. Rowe, Introduction to Data Structure and Algorithms with C and C++.
3. Rajesh K. Shukla, Data Structures using C and C++.
3
Unit 1: Introduction to Data Structure [3 hours]
Data Types, Concept of data structure, Abstract Data Type, Implementation of Data Structure, Introduction to
algorithms, Basics of asymptotic notations.
Data Types:
A data type is one of the forms of a variable to which the value can be assigned of a given type only. There are
many types of data, such as integer, floating, double, long, short, Boolean, character. These data types are also
known as primitive data structures. Each data type has specific memory allocations. Different types of data can
be used to store in memory to implement the data structure efficiently.
Data structure can be defined as the group of data elements which provides an efficient way of storing and
organizing data in the computer so that it can be used efficiently. It is basically a technique of organizing and
storing of different types of data items in computer memory. Data structures are arrays, Linked Lists, Stack,
Queue, etc. Data structures are widely used in almost every aspect in computer science i.e. operating system,
compiler design, artificial intelligence, graphics and many more.
To develop a program of an algorithm, we should select an appropriate data structure for algorithm.
Processor Speed: To handle very large amount of data, high speed processing is required, but as the
data is growing day by day to the billions of files per entity, processor may fail to deal with that much
amount of data.
Data Search: Consider an insurance policy which has 500 customers; if our application needs to search
for a particular customer, it needs to traverse 500 customers every search time, which results in slowing
down the search process.
Multiple requests: If thousands of users are searching the data simultaneously on a web server, then
there are the chances that a very large server can be failed during that process.
4
Data Structure Classification:
Data Structure
Float Array
Tree
Double Stack
Graph
Char and others Queue
List, etc
Data structures can be classified into two main types: primitive data structures and non-primitive data
structures.
Primitive data structures: These are the most basic data structures and are usually built into programming
languages. Examples include:
Integer
Float
Character
Boolean
Double
Void
Non-primitive data structures: These are complex data structures that are built using primitive data types.
User creates non-primitive data types using primitive data type. Non-primitive data structures can be further
categorized into the following types:
Arrays: A collection of elements of the same data type, stored in contiguous memory locations.
Linked lists: A collection of elements that are connected by links or pointers.
Stacks: A collection of elements that follow the Last-In-First-Out (LIFO) principle.
Queues: A collection of elements that follow the First-In-First-Out (FIFO) principle.
Trees: A hierarchical data structure consisting of nodes connected by edges.
Graphs: A non-linear data structure consisting of nodes and edges.
5
Linear data structures are one-dimensional and can be traversed sequentially from the first to the last
element.
Elements are arranged in one dimension, also known as linear dimension.
Example: lists, stack, queue, etc.
Data type defines a type of data which a variable stores. E.g. int a=20;
Abstraction refers to the act of representing essential features without including background details. E.g. how
'int a' variable allocated in memory of 4 bytes in background? This is abstraction.
ADT is a mathematical model that logically represents a data type. It only tells the essential features without
including background details. It specifies set of data (of any type) and set of operations that can be performed
on the data.
Note: All above ADT contain data and set of operations without background details. We do not know how the
processes are performed in background but have knowledge of data and operations on it.
Interface - Each data structure has an interface. Interface represents the set of operations that a data structure
supports. An interface only provides the list of supported operations, type of parameters they can accept and
return type of these operations.
Implementation provides the internal representation of a data structure. Implementation also provides the
definition of the algorithms used in the operations of the data structure.
6
Space Complexity − Memory usage of a data structure operation should be as little as possible.
Implementation time of different algorithms used by data structures are measured in different scenarios as
follows;
1. Worst Case − It is the scenario where a particular data structure operation takes maximum time it can take.
If an operation's worst case time is ƒ(n) then this operation will not take more than ƒ(n) time where ƒ(n)
represents function of n.
2. Average Case − It is the scenario describing the average execution time of an operation of a data structure.
If an operation takes ƒ(n) time in execution, then m operations will take mƒ(n) time.
3. Best Case − It is the scenario depicting the least possible execution time of an operation of a data structure.
Introduction to Algorithms:
An algorithm is a finite set or sequence of instructions for solving a specified problem. Or task. A specified
problem is a well-defined task that has to be performed and must be solvable by algorithms.
A computer algorithm is a detailed step-by-step method for solving a problem using a computer. A program is
an implementation of one or more algorithms. To solve problem or perform computation by giving some value
or set of values as input to yield or produce some value or set of values as output.
Algorithms are not dependent on any particular machine, programming languages or compilers. Implies,
algorithms run in same manner everywhere.
A problem can have many algorithms and our main objective is to analyze algorithms and find the algorithms
among many, which takes less time and memory and later on implemented in any language, as shown in figure
below.
Problem
A1 A4
A2 A3
Implemented
7
Properties of Algorithm:
• Inputs/Outputs: There must be some inputs from the standard set of input(s) and an algorithm’s
execution must produce output(s).
• Correctness: Correct set of output values must be produced from each set of inputs.
Step 1: Start
Step 2: Input num
Step 3: Find modulus of num as, rem=num%2
Step 4: if rem==0
Print (num is even)
Else
Print (num is odd)
Step 5: Stop
Step 1: Start
Step 2: Input N1, N2, N3
Step 3: if (N1>N2) then
if (N1>N3) then
MAX N1 [N1>N2, N1>N3]
else
MAX N3 [N3>N1>N2]
endif Is there any other algorithm to find the largest number among three??
else If yes Write an algorithm and implement it on C-programming.
if (N2>N3) then
MAX N2 [N2>N1, N2>N3]
else
MAX N3 [N3>N2>N1]
endif
endif
Step 4: Print “The largest number is”, MAX
Step 5: Stop
8
Analysis of Algorithms:
Algorithm analysis is a step of obtaining the time complexity and space complexity of any algorithm. Time
complexity is the time taken to execute any algorithm and space complexity is the memory or space utilized or
consumed by any algorithm. Whenever we want to perform analysis of algorithm, we need to calculate the
complexity of that algorithm without executing that particular algorithm. But when we calculate complexity of
an algorithm, it does not provide exact amount of resource required, so instead of taking exact amount of
resources we represent that complexity in a general form which produces the basic nature of that algorithm.
Complexity analysis of an algorithm is very hard to analyze exactly. We know that the complexity (worst, best
or average) of an algorithm is the mathematical function of the size of the input. So, we analyze the algorithms
in terms of bounds (lower and upper), then it would be easier. For this purpose, asymptotic notations are used,
which gives lower and upper bound concept.
1 < logn < sqrt(n) < n < nlogn < n2 < n3 < …….. < 2n < 3n < …….. <nn
Types of asymptotic notations:
n0 n
Figure : Geometric interpretation of Big Oh Notation.
For all values of n>=n0, plot shows clearly that f(n) lies below or on the curve of c*g(n).
Example 1: Find the Big Oh of the given function f(n) = 5n 2 + 7n + 7.
Solution: We have f(n) = 5n2 + 7n +7 <= 5n2 + 7n2 + 7n2 <= 19n2.
Implies, f(n) < = 19n2
Where, c = 19, g(n) = n2, thus f(n) = O(g(n)).
9
2. Big Omega (Ω) notation:
Big Omega gives asymptotic lower bound.
If f and g are two functions from set of integers to set of integers then the function f(n) is said to be big
omega of g(n), i.e. f(n)= Ω (g(n)) if and only if there exists two positive constants c and n0, such that for
all n>= n0, f(n)>=c*g(n).
f(n)
time c*g(n)
n0 n
Example:
Find big omega of the given function f(n) = 6n2 + 3n +5
6n2 + 3n +5 >= 6n2
Implies, f(n) >=6n2
Where, c=6, g(n) = n2 , thus f(n) = Ω (g(n)) = Ω (n2).
When we need asymptotically tight bound then we use this notation. If f and g are any two functions
from set of integers to set of integers then function f(n) is said to be big theta of g(n). i.e. f(n) = θ(g(n)) if
and only if there exists three positive constants c1, c2, n0 such that for all n>= n0,
c1*g(n)<=f(n)<=c2*g(n). c1*g(n)
f(n)
time c1*g(n)
n0 n
10
Example: if f(n) = 6n2 + 3n +5, g(n) = n2, then prove that f(n) = θ(g(n)).
Solution:
Let us choose c1, c2 and n0 values as 1, 14 and 1 respectively. Then we can have,
f(n)<=c2*g(n), n>=n0 as, 6n2 + 3n +5 >= 6n2 + 3n2 + 5n2 <= 14n2
Time complexity:
By counting the total statements takes the total time complexity of given algorithm.
11
Now total time complexity: 1. Algorithm:
MIN (a[1], …, a[n])
m = a[1];
T(n) = 1+1+1+(n+1)+n+n+n+1+1 for i = 2 to n
if a[i] < m
then m = a[i];
T(n) = 5 + 4n <= 5n + 4n Class Task:
Analyze the given algorithm.
T(n) = 9n
Exercise Questions:
1. What do you mean by ADT? Show that data type int as ADT.
2. What is the main concept behind the asymptotic notations? Explain Big Oh in detail with suitable
example.
3. What do you mean by linear data structure and non-linear data structure? Differentiate between them
with suitable example.
4. What are the characteristic of data structure?
5. What are the differences between primitive and non-primitive data structure?
6. What is algorithm? Write down the characteristics of algorithm?
7. Why do we need asymptotic notations? How many types of asymptotic notations are used to analysis
algorithms?
8. Find big oh, big omega and big theta of the following function.
a. F(x) = 3x2 + 9x2 + 8x + 6.
9. In what areas, data structures are applied?
10. What are the various applications of Data Structures?
11. Write any algorithm for sorting the integers and analyze it and find the time complexity.
12