Data Structure and Algorithms
Data Structure: Introduction
Puneet Kumar Jain
CSE Department
National Institute of Technology Rourkela
This lecture: outlines
What is Data?
What is Data type?
What is abstract data type?
What is Data Structure?
Why Data Structure?
What are different types of Data Structure?
Why different data structure?
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Data
Data etymologically derives from the Latin word “datum,” which
roughly translates to “something given”.
Data: (oxford dictionary)
facts and statistics collected together for reference or analysis
Things known or assumed as facts, making the basis of reasoning or
calculation.
Data is plain facts, observations, statistics, characters, symbols,
images, numbers, and more that are collected and can be
used for analysis. (TechDifferences)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Data Type
Data Type:
A data type is a collection of values and a set of operation that act
on those values
Defines the behavior and operations associated with data type
Primitive
Integer, Character, Float
Non-primitive (Complex)
String
Arrays etc.
Img ref: http://java.meritcampus.com/core-java-topics/java-data-types
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Abstract data type
Abstract data type:
Only provides concept (rules)
One has to implement their own
ADT is a specification that describes a data set and the operation
on that data. for example using lists (insert, delete), queues
(enqueue, dequqeue), stacks (push and pop), etc.
The process of hiding implementation details is known as
encapsulation.
In contrast, a Data Structure is an implementation of an ADT
within a programming language.
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Data Structure
Data structure: Data structure is a representation/organization of
data and the operations allowed on that data efficiently*
Storing,
Accessing, and
Modifying data
Ex:
Data Structure are the method of representing of logical
relationships between individual data elements related to the
solution of a given problem
*Efficiently: low time complexity and space complexity.
Img ref: https://medium.com/@Adi_Wang1476/stack-and-queue-1823effb6cc
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Data structure and algorithm
Data Structure:
A data structure is a systematic way of organizing and accessing data
Algorithm:
An algorithm is a step-by-step procedure for solving a problem
Algorithms and Data Structures go hand-in-hand:
Certain Algorithms require certain data structures to run efficiently
and vice-versa in a finite amount of time.
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Different Data structure
Data
Structure
Linear Non-linear
Array, Linked list, Stack, Queue Tree, Graph
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Linear vs Non-linear
Reference: https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure
There is no ultimate data structure...
The choice depends on our requirements
1. Application based: It must enrich enough in structure to
represent the relationship between data elements
Write to-do list (Array, List)
Queue at ATMs (Queue)
Arrange stack of books (Stack)
Look-up words in dictionary (Look-up table, hash map)
Organize directories in computers (Tree)
Plan road map to go somewhere (Graph)
Ref:https://slideplayer.com/slide/1450967/
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure
2. Frequent operation: The structure should be simple enough that
one can electively process the data when necessary
Traversing: to access every record at once
Searching: to find a location of the record using key
Insertion: adding a new record
Deletion: remove a record from the structure
Sorting: arranging items in a sequence ordered by some criterion
Merging: combine
Operations
Special operations
Traversing Searching Insertion Deletion Sorting Merging
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure
Structures and algorithms are required for efficient work in all
walks of daily life.
Examples: Hostel-room analogy: –
A “cluttered” room: quick to store but takes time to find items
An “organized” room: quick to find items but takes time to store
Points: – For each “application” a different structure may be
required – Depending on the structure, the process of using it
efficiently will be different
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure: example
Suppose you have a list of integers (some arbitrary values)
You have to read these integers one by one and store them in
some “data structure”, A, for the following purpose:
I should be able to find and remove the max integer from this list
Functions
add_number (A,i) – adds number i to A
find_max (A) - returns the max from A
remove_max (A) – removes max from A
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Cont…
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure
Consider accessing the kth entry in an array or linked list
In an array, we can access it using an index array[k]
Array[k]= Base_address+(k*size)
We must step through the first k – 1 nodes in a linked list
Consider searching for an entry in a sorted array or linked list
In a sorted array, we use a fast binary search
We must step through all entries less than the entry we’re looking for
array [0] [1] [2]
Array A B C
node
linked
Linked list A B C
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Why different data structure
However, consider inserting a new entry to the start of an array or a
linked list
An array requires that you copy all the elements in the array over
A linked list allows you to make the insertion very quickly
Very fast regardless of size
Ref: https://ece.uwaterloo.ca/~dwharder/aads/, Douglas Wilhelm Harder, M.Math, LEL, University of Waterloo
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Operations on Sorted array
Given an sorted array, we have the following run times:
Arbitrary
Front/1st Back/nth
Location
Find Good Okay Good
Insert Bad Bad Good
Erase Bad Bad Good
ref: ouglas Wilhelm Harder, M.Math. LEL, University of Waterloo
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Operations on Sorted array
If the array is not sorted, only one operations changes:
Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Bad Bad Good
Erase Bad Bad Good
Dref: ouglas Wilhelm Harder, M.Math. LEL, University of Waterloo
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Operations on Lists
However, for a singly linked list where we a head and tail
pointer, we have:
Arbitrary
Front/1st Back/nth
Location
Find Good Bad Good
Insert Good Bad Good
Erase Good Bad Bad
Dref: ouglas Wilhelm Harder, M.Math. LEL, University of Waterloo
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Designing Data Structures
Each data structure has costs and benefits.
Rarely is one data structure better than another in all situations.
A data structure requires:
space for each data item it stores,
time to perform each basic operation,
programming effort.
Each problem has constraints on available time and space.
Only after a careful analysis of problem characteristics and
solution requirements can we know the best data structure for
the task
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Selecting Data Structures
Select a data structure as follows
Analyze the problem to determine the resource constraints a
solution must meet.
Determine basic operations that must be supported. Quantify
resource constraints for each operation.
Select the data structure that best meets these requirements.
Some questions to ask: –
Are all the data inserted into the structure at the beginning or are
insertions interspersed with other operations?
Can data be deleted?
Are the data processed in some well-defined order, or is random
access allowed?
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Logical vs. Physical Form
Data items have both a logical and a physical form.
Logical form: definition of the data item within an ADT.
Physical form: implementation of the data item within a data
structure.
For example, the concept (logical form) of a list could be
implemented (physical form) using an array or a linked list
Ref: https://www.cse.iitb.ac.in/~sri/cs213 /00-Intro.pdf (iitb.ac.in)
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
Memory allocation
https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”
• End of Chapter
NIT Rourkela Puneet Kumar Jain “Data Structure and Algorithms”