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

Unit 1 Introduction To Data Structure

The document outlines a course structure covering various data structures and algorithms topics over 9 units totaling around 50 hours. The units cover concepts like stacks, queues, lists, trees, graphs and algorithms like searching, sorting and recursion. Programming assignments involve implementing many of these common data structures.

Uploaded by

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

Unit 1 Introduction To Data Structure

The document outlines a course structure covering various data structures and algorithms topics over 9 units totaling around 50 hours. The units cover concepts like stacks, queues, lists, trees, graphs and algorithms like searching, sorting and recursion. Programming assignments involve implementing many of these common data structures.

Uploaded by

Lakpa Nuru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Course Structure and Contents:

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.

Unit 2: The Stack [4 hours]

Definition, Stack as an ADT, Stack operations, Stack Application: Evaluation of infix, postfix, and prefix
expression, Conversion of infix to postfix/prefix expression.

Unit 3: Queue [3 hours]

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.

Unit 5: Recursion [3 hours]

Principle of Recursion, Comparison between recursion and iteration, Recursion example, Factorial, Fibonacci
sequence, GCD and Tower of Hanoi (TOH) Applications of Recursion.

Unit 6: Sorting [7 hours]

Introduction, Internal and External Sorting, Insertion Sort, Selection Sort, Bubble Sort, Merge Sort and Quick
Sort, Efficiency of Sorting algorithms.

Unit 7: Searching and Hashing [5 hours]

Introduction to Searching, Search algorithms: Sequential search, Binary search, Efficiency of different search
algorithms, Hashing: hash function, hash table, collision resolution techniques.

Unit 8: Trees [6 hours]

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.

Unit 9: Graphs [8 hours]

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.

Concept of Data Structure:

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.

Algorithm + Data Structure = Program

Need of Data Structure:

 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

Primitive Data Structure Non-Primitive Data Structure

Integer Linear Data Structure Non-Linear Data Structure

Float Array
Tree
Double Stack
Graph
Char and others Queue

List, etc

Figure 1: Classification of Data Structures

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.

Linear Data Structure:


 A linear data structure is a type of data structure in which data elements are arranged in a sequential
order, and each element has a unique predecessor and successor, except for the first and last elements.

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.

Non-Linear Data Structure


 A Non-linear data structure is a type of data structure in which data elements are not arranged in a
sequential order, and each element may have one or more predecessors and successors. Non-linear data
structures can represent complex relationships between data elements, such as hierarchies, networks, and
graphs.
 Elements are arranged in one-many, many-one and many-many dimensions.
 Example: tree, graph.

Abstract Data Type [ADT]:

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.

Example: ADT and Set of Operations

i) List ADT (create, display, insert, deletion, search)


ii) Stack ADT (push, pop, peek)
iii) Queue ADT (enqueue, dequeue, peek, display), etc.

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.

Foundation of Data Structures:

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.

Characteristics of a Data Structure:

 Correctness − Data structure implementation should implement its interface correctly.


 Time Complexity − Running time or the execution time of operations of data structure must be as small
as possible.

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.

Computer Algorithm or Program

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

Programs in C, C++, java,


C#, etc.

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

• Definiteness: Each step must be clear and unambiguous.

• Finiteness: Algorithm must terminate after finite steps.

• Correctness: Correct set of output values must be produced from each set of inputs.

• Effectiveness: Each step must be carried out in finite time.

Example 1: Algorithm to test whether the entered number is Even or Odd.

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

Example 2: Algorithm to find the greatest among three numbers.

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.

Why asymptotic notations are useful?

 They give a simple characterization of an algorithm's efficiency.


 They allow the comparison of the performance of various algorithms.

Relationship between different terms:

1 < logn < sqrt(n) < n < nlogn < n2 < n3 < …….. < 2n < 3n < …….. <nn
Types of asymptotic notations:

1. Big Oh (O) notation:


When we have only asymptotic upper bound then we use O notation.
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
Oh of g(n), i.e. f(n)=O(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).
c*g(n)
time f(n)

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

Figure : Geometric interpretation of Big Omega Notation.

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

3. Big Theta (θ) notation

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

Figure : Geometric interpretation of Big Theta Notation.

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)>=c1*g(n), n>=n0 as, 6n2 + 3n +5 >= 6n2 and

f(n)<=c2*g(n), n>=n0 as, 6n2 + 3n +5 >= 6n2 + 3n2 + 5n2 <= 14n2

for all, n>=1 (in both cases).

So, c1*g(n) <= f(n) <= c2*g(n),

Let's analyze, sequential search algorithm by counting the steps as below;

Sequential_Search(A, n, Key) // A is an array of elements, n is array-size, and key is item to search.


{
int i, flag=0;
for(i=0;i<n;i++){
if(A[i]==Key){
flag = 1;
}
}
if(flag==1){
print "Search successful";
}
else{
print "Search unsuccessful";
}
}

Time complexity:

By counting the total statements takes the total time complexity of given algorithm.

o Declaration statements take 1 step.


o Within for loop,
o i=0 takes 1 step time.
o i<n takes n+1 step time
o i++ takes n step time
o if condition takes n step time
 flag statement takes at most n step time
o if statement outside the for loop takes 1 step time
o print statement takes 1 step time

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

 T(n) = O(n) (Worst Case Analysis). Class Task.


2. Write an algorithm to find the factorial of a given number using
iterative method and Analyze it. Also write an implementation code for it.
2. Write an algorithm to find the fibonacci series up-to given number
using iterative method and Analyze it. Also write an implementation
code for it.

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

You might also like