Instructor:
Aqsa Zahid pre Requisite : C & C++
Overview:
2
Store
Organize
Access
Manipulate
11/19/2024 07:36 AM
3
Another Example: Library
11/19/2024 07:36 AM
4
Introduction to Data
Structure
A data structure is a particular way of
storing and organizing data in a
computer so that it can be used
efficiently
Physically it doesn’t exist , it is a logical
entity.
Set of rules that data is stored in a
particular way for efficient access
11/19/2024 07:36 AMand
Why do we need Data Structures?
5
Allows data to be stored in a specific manner.
Efficient data search and retrieval.
Specific data structures are decided to work for
specific problems.
11/19/2024 07:36 AM
Google Map
6
11/19/2024 07:36 AM
Excel Sheet
7
11/19/2024 07:36 AM
Family Tree
8
11/19/2024 07:36 AM
Basic DS Example: Arrays
An array is a linear data structure that
collects elements of the same data type
and stores them in contiguous and
adjacent memory locations.
Arrays work on an index system
starting from 0 to (n-1), where n is the
size of the array.
11/19/2024 07:36 AM
Arrays (cont.)
10
Create an integer array that has 12
elements. The index of the array starts
with 0, so the array having 12 elements
has indexes from 0 to 11.
Write memory addresses of the array
starting from 200 till 11th memory
location.
11/19/2024 07:36 AM
Discussion : variable vs
11
Array
Your are suppose to store marks of 100
students.
Think of as many ways as possible.
To solve this particular problem, you will
create100 variables of integer type or create an
array of integer type with the size 100.
This is just an example how a data structure
help us to efficiently manage memory and
efficiently access different data from memory.
11/19/2024 07:36 AM
Classification of Data
12
Structure
11/19/2024 07:36 AM
Classification of Data Structure
13
A data structure is a particular way of
organizing data in computer memory so
that it can be used efficiently.
Classification of Data Structure
Data structure can be classified into two
main categories: Primitive & Non-Primitive
1. Primitive Data Structure
Basic data types are known as primitive
data structures. Also called simple Data
Structure e.g. integer, real, Boolean,
character etc. 11/19/2024 07:36 AM
Classification of Data Structure
14
2. Non-Primitive Data Structure
The data types which are derived from
primary data types are known as non-
primitive data structure. These are used
to store group of values e.g. array,
structures, link list, stacks, queues etc.
Non-Primitive data structure are further
divided into two categories:
Linear
Non-Linear
11/19/2024 07:36 AM
Non-Primitive data structure
15
Linear Data Structure
A data structure is said to be linear if its
elements form a sequence i.e. elements are
attached with one another e.g. array, link
list, stacks, queues etc.
Non-Linear Data Structure
In non-linear data structure elements are
not organized in a sequential fashion. A
data item can be attached to several other
data elements e.g. tree, graph etc.
11/19/2024 07:36 AM
Linear Vs. Non-Linear data Structure:
16
11/19/2024 07:36 AM
Comparison:
17
11/19/2024 07:36 AM
18 Introduction to Algorithms
A precise rule (or set of rules) specifying how
to solve some problem.
An algorithm is a clearly defined set of
instructions to be followed to solve a
problem. Algorithms are generally created
independent of underlying programming
languages.
11/19/2024 07:36 AM
Algorithms
19
From the data structure point of view,
following are some important categories of
algorithms.
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
11/19/2024 07:36 AM
Characteristics of an Algorithm
20
Not all procedures can be called an algorithm. An
algorithm should have the following characteristics:
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.
Independent − An algorithm should have step-by-step
directions, which should be independent of any
programming code. 11/19/2024 07:36 AM
Activity:
21
Design an algorithm to add two numbers
and display the result.
Write in terms of Steps
Step1:
Step2:
…
11/19/2024 07:36 AM
Example # 1
22
Step 1 – START
Step 2 – declare three integers a, b & c
Step 3 – define values of a & b
Step 4 – add values of a & b
Step 5 – store output of Step 4 to c
Step 6 – print c
Step 7 – STOP
11/19/2024 07:36 AM
Example # 2
23
Step 1 – START ADD
Step 2 – get values of a & b
Step 3 – ca+b
Step 4 – display c
Step 5 – STOP
Second method is preferable in Data
Structure and Algorithms
11/19/2024 07:36 AM
Which is better Algorithms?
24
Algorithms solve problems
Good choice:
more efficient programs
Bad choice:
poor programs performance
11/19/2024 07:36 AM
Example:
25
Example:
Problem: Find the largest element ‘k’ out of ‘N’
integers
Easy algorithms: sort all integers, then list the first
or last element
Betteralgorithm: take first element then read
through the list
Different algorithms perform better on
different inputs
Input size also affect the performance.
11/19/2024 07:36 AM
Notion of Algorithm and
26
Problem
Proble
m
Algorithm
Input program Outpu
t
11/19/2024 07:36 AM
Role of Data Structure
27
Proble
m
Data Structure Algorithm
program
11/19/2024 07:36 AM
Representation of an
28
Algorithms
An algorithm may be represented in
different forms:
A description using English/other languages
A real computer program, e.g. C++ or java
A pseudo-code, C-like program, program-
language-like program.
Program = algorithms + data structures
11/19/2024 07:36 AM
Algorithm Efficiency
29
There are often many algorithms for a
given problem. How do we choose the
best?
Goals of program design:
Algorithm is to be easy to understand, code,
debug
Algorithm makes efficient use of computer’s
resources
11/19/2024 07:36 AM
How to measure the efficiency?
30
Empirical comparison (run the program)
Asymptotic algorithm analysis (without running
the program)
Factors affecting running time (size of the
input)
11/19/2024 07:36 AM
Best, Worst and Average
31
Cases
Not all inputs of a given size take the
same time.
Each algorithm has three cases:
Best case:
Worst Case:
Average Case:
11/19/2024 07:36 AM
Example: Best, Worst and Average Cases
32
Sequential search for ‘k’ in an array of ‘n’
integers:
Best case: ‘k’ is the first element of the
array.
Worst case: the search must visit every
element once. This happens when the
value being searched for is either the last
element in the list, or is not in the list
Average case: on average, assuming the
value searched for is in the list and each list
element is equally likely to be the value
searched for, the search visits11/19/2024
only n/207:36 AM
Asymptotic Notations
33
Following are the commonly used
asymptotic notations to calculate the
running time complexity of an
algorithm.
Ο (Big-O) Notation (Worst Time) (Upper
Bound)
Ω (Omega) Notation (Best Time)
(Lower Bound)
θ (Theta) Notation (Average Time)
(Exact)
Abstract Data Type (ADT)
34
Abstract data type are like user defined
data type on which we can perform
functions without knowing what is there
inside the datatype and how the
operations are performed on them.
An abstract data type (or ADT) is a class
that has a defined set of operations and
values.
11/19/2024 07:36 AM
Abstract Data Type (ADT)
35
In other words, you can create the starter motor
as an entire abstract data type, protecting all of
the inner code from the user. When the user wants
to start the car, they can just execute the start()
function.
Objects such as lists, stacks and queues along
with their operations, can be viewed as ADTs, just
as Integers, Reals and Booleans are data types.
Integers, Reals and Booleans have operations
associated with them, and so they are also called
ADTs.
11/19/2024 07:36 AM
Types of ADTs:
36
11/19/2024 07:36 AM
Terms in Data Structures
37
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
Implementation provides the internal
representation of a data structure. It also
provides the definition of algorithms used in
the operation of data structure.
11/19/2024 07:36 AM
Characteristics of Data Structure
38
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.
Space Complexity
Memory usage of a data structure
operation should be as little as possible.
11/19/2024 07:36 AM
Selecting a Data Structure
39
Select a data structure as follows:
1. Analyze the problem to determine the
resource constraints a solution must meet.
2. Determine the basic operations that must be
supported.
3. Select the data structure that best meets
these requirements.
11/19/2024 07:36 AM
Fundamental Data
40
Structures
Basic Data
Structures
Linear Data Non-Linear Data
Structures Structures
Array Linked Stac Queu Graph Tree Hash
s Lists ks es s s Tables
11/19/2024 07:36 AM
41
array
Linked list
queue
tree stack
11/19/2024 07:36 AM
Linear Data Structures
42
A data structure is said to be linear if its
elements form a sequence or a linear
list.
Examples:
Arrays
Linked Lists
Stacks
Queues
11/19/2024 07:36 AM
Non-Linear Data Structures
43
A data structure is said to be non-linear
if its elements does not form a sequence
or a linear list.
Examples:
Trees
Graphs
Hash Tables
Each element may be connected with
two or more other nodes or items in a
non-linear arrangement.
11/19/2024 07:36 AM
Arrays
44
A sequence of n items of the same data type that
are stored contiguously in computer memory and
made accessible by specifying a value of the array’s
index.
Properties:
fixed length (need preliminary reservation of
memory)
contiguous memory locations
direct access
Insert/delete
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
a[8] a[9]
1 2 3 4 5 6 7 8
9 10
Array a with 10 integer elements
11/19/2024 07:36 AM
Linked List
45
A sequence of zero or more nodes each containing
two kinds of information: some data and one or
more links called pointers to other nodes of the
linked list.
Properties
dynamic length
arbitrary memory locations
access by following links
Insert/delete
Types of Linked List
Singly linked list (next pointer)
Doubly linked list (next + previous pointers)
11/19/2024 07:36 AM
Stacks
46
A stack is a data structure that uses last-
in, first-out (LIFO) ordering and allows
reading and writing on the top element
only.
Properties
insertion/deletion can be done only at the top
LIFO
Two operations
Push (insertion)
Pop (removal)
11/19/2024 07:36 AM
Queues
47
Collection with access only to the item
that has been present the longest
Properties
Insertion/enqueue from the rear (back) and
deletion/ dequeue from the front.
FIFO
Two operations
Enqueue
Dequeue
Front 20 30 10 60 57 Back
11/19/2024 07:36 AM
29
48
Non-Linear Data
Structures
Graphs
Trees
Hash Tables
11/19/2024 07:36 AM
Graphs
49
Formal definition: A graph G = <V, E> is
defined by a pair of two sets: a finite set V of
items called vertices and a set E of vertex pairs
called edges.
Undirected and directed graphs
(digraphs).
11/19/2024 07:36 AM
Undirected Graph Directed Graph
Trees
50
A Tree is a way of
representing the hierarchical
nature of a structure in a
graphical form.
Properties of trees
Root Node
Child Node Unordered Tree
Parent Node
Leaf Node
Types
Unordered Tree
Binary Tree is an ordered tree
data structure in which each node
has at most two children.
Binary07:36
11/19/2024 TreeAM
Examples:
51
11/19/2024 07:36 AM
Activity:
52
On what basis we could compare the
time complexity of data structure?
On the basis of number of Operations
performed
11/19/2024 07:36 AM
Summary
53
A data structure is a particular way of storing and
organizing data in a computer so that it can be
used efficiently.
Linear Data Structures
Arrays
Linked List
Stacks
Queues
Non Linear Data Structures
Graphs
Trees
Hash Tables
11/19/2024 07:36 AM