1 Introduction To Data Structure
1 Introduction To Data Structure
▪ Introduction:
▪ Its types
▪ Its Examples
▪ Data Structures
▪ A data structure is a way of organizing and storing
data so that it can be accessed and modified
Department of Computer Science and Engineering
efficiently.
▪ Purpose:
▪ Efficient data manipulation
▪ Logical data representation
▪ Better algorithm performance
Contents
▪ Its types
• Examples of DS
• Operations on DS
Data, Data Objects and Data Structures
information.
▪ e.g. : Group of employees, Group of Students etc.
Is this
sufficient
Department of Computer Science and Engineering
information?
Data Structures
Types of DS
Non-Primitive
Primitive DS
DS
Non-Linear
Int Linear DS
DS
Department of Computer Science and Engineering
Boolean Stack
Queue
Primitive Data Structures V/s
programming languages.
• All they are supported by
all programming • They can be implemented
languages. with the help of primitive
data types.
Structure
• Linear data structure ▪ Non-Linear data structure
grows linearly. does not grow linearly.
• Different operation which
can be performed are in ▪ They grows level wise.
a linear way.
▪ Examples of Non - Linear
• Examples of Linear DS:
Department of Computer Science and Engineering
DS:
– Array
▪ Tree
0 1 2 3 4 5
6
1 2 3 4 5 6
7 ▪ Graph
– Linked List
Node 0 Node 1 Node 2 Node 3
1 2 3 4
Linear Data Structure
▪ Array 0 1 2 3 4 5
6
1 2 3 4 5 6
7
▪ Tree -- Graph
Sequential v/s
Non-Sequential Data Structures
• Data structure utilizes
▪ Data structure utilizes memory which is not
memory which is allotted allotted sequentially.
sequentially.
• Memory allotted is
scattered in the memory
Department of Computer Science and Engineering
space.
▪ Example : • Example :
▪ Arrays (1D, 2D, nD) – Linked Lists
– Tree
▪ Char Array (Strings)
– Graph
▪ Array of Structures
Persistent v/s Ephemeral Data
Structures
▪ The data structure which ▪ The data structure which
persists its previous does not persists its
version even after previous version after
performing different performing different
operations. operations.
▪ Example ▪ Example
▪ Stack ▪ Queue Vrsn # 1 a
Vrsn # 2 a b
Change in Operation
30
Vrsn # 3 a b c
20 20 20
Change in Operation
Vrsn # 4 b c
10 10 10 10 10
▪ Definition:
Persistent data structures preserve all previous versions of the data structure, even after updates.
▪ Types of Persistence:
▪ Partial Persistence:
Department of Computer Science and Engineering
▪ Can access any version, but only the latest version can be modified.
▪ Full Persistence:
▪ Can access and modify any version.
▪ Confluent Persistence:
▪ Allows merging versions (less common, more advanced).
▪ Characteristics:
▪ Do not overwrite old data.
▪ Definition:
Ephemeral data structures are traditional structures where each update (insert, delete,
modify) destroys the previous version.
▪ Characteristics:
Department of Computer Science and Engineering
▪ Examples:
▪ Arrays
▪ Linked Lists
▪ Stacks
Multiple versions
Versioning Single version only
maintained
Example:
▪ Static: ▪ Dynamic:
▪ - Example: Array
▪ - Examples: Linked List,
Dynamic Arrays
Examples of DS
▪ Arrays ▪ Linked
Lists
▪ Matrix
▪ Tree
Department of Computer Science and Engineering
▪ Strings
▪ Graph
▪ Stack
▪ Tables
▪ Queues
▪ Files
Applications of DS
independent view.
▪ The process of providing only the essentials and hiding the details
is known as abstraction.
Example of ADT
▪ It focuses on
Stack
▪ What operations can be Size Top
performed (e.g., insert,
Type
delete, search)
Values
▪ Not on how the operations
are implemented internally
Stack
Department of Computer Science and Engineering
▪ Top of Stack
▪ Set of Operations:
▪ Push
Stack ADT
▪ Pop
▪ Display
Why Abstract Data Structures?
▪ Create DS
▪ Insert elements in DS
▪ Delete elements in DS
Department of Computer Science and Engineering
▪ Display elements of DS
▪ Modify elements in DS
Operations on DS
Hash Table insert, search, delete Fast lookups like DNS caching
Algorithm Content
▪ Algorithms:
▪ Space complexity,
▪ Quadratic,
▪ Cubic,
▪ Logarithmic.
Algorithm
▪ At the end of this session, every learner
will be able to understand
▪ Frequency Count
Department of Computer Science and Engineering
▪ Time Complexity
▪ Space Complexity
▪ Asymptotic Notations
Algorithm – Definition
▪ Algorithm is
▪ A fine
▪ Clearly specified
▪ Sequence of instructions
Department of Computer Science and Engineering
▪ Input ▪ steps
• Time
• Space
• Network
Department of Computer Science and Engineering
• Power Consumption
2. Space requirement
1. Time Complexity
• Write algorithm.
▪ x = y----------------------1 ▪ x = y-----------------------1
Department of Computer Science and Engineering
▪ y = Temp----------------1 ▪ y = Temp-----------------1
▪ -------------------- f(n) = 3 ▪ -------------------- s(n) = 3
Example
of Frequency Count
int p = 0; 1 int p = 0; 1
for (i=0; i<5; i++) 6 (5+1) for (i=0; i<n; i++) n+1
{ - { -
p = p + i; 5 int p = p + i; n
} - } -
Time
Name Example Description
Function
1 < logn < √n < n < nlogn < n² < n³ < …….. <2ⁿ <3ⁿ <nⁿ
Department of Computer Science and Engineering
Common
Time Complexities
Department of Computer Science and Engineering
Why is Time Complexity
Important?
Time
Name Example Scenario
Complexity
O(n log n) Linearithmic time Merge Sort, Quick Sort (average case)
Department of Computer Science and Engineering
▪ It includes
Department of Computer Science and Engineering
▪ Instruction space
▪ It includes
▪ Best Case
▪ Worst Case
Department of Computer Science and Engineering
▪ Average Case
▪ Average number of instructions executed.
Asymptotic Notations
▪ Longest time of
Computation • Shortest time of • Average time of
computation computation
Cases to Examine
if f(n) grows no faster than c*g(n) for all n >= 1 where c and
n0 are constants.
1 < logn < √n < n < nlogn < n² < n³ < …….. <2ⁿ <3ⁿ <nⁿ
Big O Notation
Department of Computer Science and Engineering
Big-Omega Ω Notation
Given two functions g(n) and f(n),
we say that f(n) = Ω(g(n)), if there exists constants c
>0 and n0 >= 0 such that
1 < logn < √n < n < nlogn < n² < n³ < …….. <2ⁿ <3ⁿ <nⁿ
Big-Omega Ω Notation
Department of Computer Science and Engineering
Θ (Theta) Notation
Let g and f be the function from the set of natural numbers
to itself.
1 < logn < √n < n < nlogn < n² < n³ < …….. <2ⁿ <3ⁿ <nⁿ
Θ (Theta) Notation
Department of Computer Science and Engineering
Department of Computer Science and Engineering