Data Structures Unit-I Lecture Notes
Data Structures Unit-I Lecture Notes
► Why ADT?
Data 2. Abstract Data Types (ADT) ► What is ADT?
Structures ► ADT implementation
► What is an Algorithm?
Unit-I 3. Algorithm Efficiency ► Pseudocode? ▪ Big-O notation
► Complexity Analysis ▪ Time Complexity
Unit-II ▪ Space Complexity
► Sparse matrix representation &
Unit-III implementation
4. Applications of Array ADT
► Polynomial Expressions representation &
Unit-IV implementation
What is a variable?
a
int a;
2 Bytes
b
float b;
4 Bytes
c
char c;
1 Byte
o What is an Array?
int a ……………….
a[10];
index 0 1 2 8 9
Array is a data structure used to store homogeneous elements at consecutive memory locations.
1. Is it posible to store element at the location of a[9] (at the end)? YES
3. Is it posible to store the elements into the array beyond the size of the array? NO
5
Data Structures--> Unit-I: Introduction to Data Structures Pandu Sowkuntla
10 Students data 10 Students data 10 Students data 10 Students data
► To store, process and retrieve the large amounts of complex data efficiently.
► Programmers can save time and memory space in storing, accessing and processing large
amounts of data.
✓ Efficiency
✓ Reusability
✓ Abstraction
7
Data Structures--> Unit-I: Introduction to Data Structures Pandu Sowkuntla
What is Data Structure?
► A data structure is an aggregation of atomic and composite data into a set with the
defined relationships.
OR
► A data structure is a particular way of organizing data in a computer so that it can be
used effectively.
8
Data Structures--> Unit-I: Introduction to Data Structures Pandu Sowkuntla
Abstract Data Types
o What is Data? ► Composite Data
The data that can be divided (not an atomic
Collections of facts from which data)
conclusions may be drawn is known as +91 9231093219 Date: dd/mm/yyyy
data.
Country code
Mobile no
o What is Information?
► Atomic Data
int a=2314;
(Data can not be divided further)
9
Data Structures--> Unit-I: Introduction to Data Structures Pandu Sowkuntla
Abstract Data Types
1. Can I retrieve the elements from the array in last in first out (LIFO) or first in last out
manner?
2. Can I retrieve the first stored element first (first in first out (FIFO)) manner?
➢ Can a programmer create his own data types (user defined data types) for solving above type
of problems?
10
Data Structures--> Unit-I: Abstract Data Types Pandu Sowkuntla
Abstract Data Types
An abstract data type is a data declaration packaged together with the operations that are
meaningful for the data type.
12
Data Structures--> Unit-I: Algorithm Efficiency Pandu Sowkuntla
Algorithm
► Why Algorithm?
• Provides a design to solve a problem.
al-Khwarizmi
• Improves the efficiency in problem solving.
9th-century’s Arab scholar
• To utilize the resources efficiently in problem solving.
► What is an Algorithm?
An algorithm is finite set of well-defined instructions,
if followed, accomplishes a particular task.
Input Algorithm Output
An Algorithm must satisfy the following criteria.
• Finiteness: Execution terminates after a finite number of steps for all possible inputs.
14
Data Structures--> Unit-I: Introduction to Algorithms Pandu Sowkuntla
Pseudocode
► We can define or describe an algorithm in two ways:
1. Pseudocode
2. Flowchart
► Pseudocode is an English like representation of the algorithm logic. It is part English,
and part structured code (syntax).
Conventions used in writing Pseudocode
• for, if, else, return and while (similar to C, Java, Python, etc.)
• ==, !=, <, >, <= and >= are used to compare values.
► Note: In this Data Structures course, we use the C programming language syntax in writing
pseudocodes.
15
Data Structures--> Unit-I: Pseudocode and Flowchart Pandu Sowkuntla
Pseudocode
Example
Algorithm name
Algorithm Max(a, n) Algorithm header
Flowchart
START
Pseudocode
Input
Step 1: Input M1,M2,M3,M4
M1,M2,M3,M
Step 2: GRADE = (M1+M2+M3+M4)/4 4
Step 3: if (GRADE < 50) then
Print “FAIL” GRADE(M1+M2+M3+M4)/4
else
Print “PASS” N
IS Y
end if
GRADE<50
Print Print
“PASS” “FAIL”
STOP
17
Data Structures--> Unit-I: Pseudocode and Flowchart Pandu Sowkuntla
Algorithm Efficiency
Big-O Notation
We don not need to determine the complete measure of the efficiency, only the factor that
determines the magnitude is considered.
The factor is denoted with big-O and read it as “in the order of.”
► Time complexity of an algorithm is the amount of computer time it needs to run to completion.
► Space complexity of an algorithm is the amount of computer memory it needs to run to completion.
19
Data Structures--> Unit-I: Algorithm Efficiency Pandu Sowkuntla
Algorithm Efficiency
Time Complexity
1. Linear Complexity
22
Data Structures--> Unit-I: Algorithm Efficiency Pandu Sowkuntla
Algorithm Efficiency
Time Complexity
23
Data Structures--> Unit-I: Algorithm Efficiency Pandu Sowkuntla
Algorithm Efficiency
T
i
m
e
Input size n
24
Data Structures--> Unit-I: Pandu Sowkuntla
Algorithm Efficiency
► Space Complexity
Types of complexities:
Space for the code Depends on variation in problem 1. Worst case (Big O)
2. Best case
3. Average
(will be discussed in future semesters)
25
Data Structures--> Unit-I: Algorithm Efficiency Pandu Sowkuntla
Array ADT
26
Data Structures--> Unit-I: Array ADT Pandu Sowkuntla
Sparse Matrices
• A sparse matrix is one in which the majority of the elements have 0 (zero) value.
Example
► Why would you want to use a new representation? 0 1 2 3
a[0][2]=9,
0 0 0 9 0
▪ To save the storage space. a[1][1]=1,
1 0 1 0 0
a[2][0]=5, 2 5 0 9 0
▪ To reduce the computing time. a[2][2]=9, 3 8 0 0 6
a[3][0]=8, Sparse Matrix
► Sparse Matrix can be represented in two ways: a[3][3]=6 A matrix contains zero values more
than half of its size.
▪ Array representation. (if (no.of zeros > Size(Array)/2))
27
Data Structures--> Unit-I: Array ADT Pandu Sowkuntla
Sparse Matrix using Array ADT
First row contains Sparse Matrix representation:b
no. of rows and cols 0 1 2 Total no. of
in the matrix
row col value non-Zero values
0 1 2 3 0 4 4 6
0 0 0 9 0 We store the non-zero elements 0 2 9
with 1
1 0 1 0 0
2 triple: <row, column, value>. 1 1 1
5 0 9 0 2
3 8 0 0 6
3 2 0 5
Sparse Matrix:a 4 2 2 9
5 3 0 8
6 3 3 6
Operations performed on Sparse matrices Row indices Col indices non-Zero values
1. Creation
2. Transpose
3. Addition
4. Multiplication
28
Data Structures--> Unit-I: Array ADT Pandu Sowkuntla
Algorithm for representing Sparse Matrix
31
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Stacks
• Stack is a linear data structure in which
items can be inserted and removed only at
one end, called the top.
➢ Operations on Stack
30 top=2
push(10) push(20) push(30)
(top++) (top++) (top++) 20
20
empty top=1
stack 10 10 10
top=0
initial pop()
top=-1 (top--) Stack
Underflow pop()
(is_empty() required) (top--)
50
top=3
40 40
top=2
Stack 20 20 push(40) 20
push(60) push(50)
Overflow (top++) top=1
(top++) (top++)
(is_full() 10 10 10
required)
Stack full
33
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Stack implementation
0 1 2 3 4 5 6 7 8 9
Stacks can be implemented in two ways:
stack: 17 23 97 44
1. Arrays.
top = 3
► Easy to implement, but the size of the array is fixed (static)
2. Linked lists.
► This implementation of a stack can grow and shrink according to the needs
at runtime.
34
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
MAX_SIZE = 100 Pseudocodes for Stack Operations
ARRAY stack[MAX_SIZE]
top = -1;
Algorithm: is_empty(top)
Algorithm: is_full(top) begin
begin if (top == -1) then
return True;
if (top == MAX_SIZE – 1) then else
return True; return False;
else end if
return False; end
end if
end
4. push(data) O(1)
5. pop() O(1)
36
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Applications of Stacks
• Infix expression to Prefix expression.
► Arithmetic Expression
Evaluation. • Infix expression to Postfix expression.
► Parenthesis Checking.
► String Reversal.
39
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Expression Representation
Infix x + y Operator between
operands.
Popular methods used for Prefix + x y Operator before
representation of an expression operands.
Postfix x y + Operator after
operands.
Precedence/Associativity table
40
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Converting Infix to Postfix
Example-1: 3*3/(4-1)+6*2
Algorithm for converting Infix to Postfix
Step 1: Consider the next element in the input. Expression Stack Output
Step 2: If it is operand, display it.
Step 3: If it is opening parenthesis, insert it on stack. 3 Empty 3
* * 3
Step 4: If it is an operator, then
3 * 33
• If stack is empty, insert operator on stack.
• If the top of stack is opening parenthesis, insert / / 33*
the operator on stack ( /( 33*
• If it has higher priority than the top of stack,
insert the operator on stack. 4 /( 33*4
• Else, delete the operator from the stack and - /(- 33*4
display it, repeat Step 4.
1 /(- 33*41
Step 5: If it is a closing parenthesis, delete the ) / 33*41-
operator from stack and display them until an opening + + 33*41-/
parenthesis is encountered. Delete and discard the opening
parenthesis. 6 + 33*41-/6
* +* 33*41-/6
Step 6: If there is more input, go to Step 1.
2 +* 33*41-/62
Step 7: If there is no more input, delete the remaining Empty 33*41-/62*+
operators to output.
Data Structures--> Unit-I: Stacks 41
Pandu Sowkuntla
Converting Infix to Postfix
Example-1: 3 * 3 / ( 4 - 1 ) + 6 * 2 Example-2: (a*b) + (d-c)
ab* + dc-
ab*dc-+
Example-3: a + b / c + d * (e-f) ^ g
a + b / c + d * ef- ^g
a + b / c + d * ef-g^
a + bc/ + d * ef-g^*
Example-4: K + L - M*N + (O^P) * W/U/V * T + Q
a + bc/ + def-g^*
abc/+ + def-g^*
abc/+def-g^*+
42
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Converting Infix to Prefix
Algorithm for Infix to Prefix Conversion:
Step 1: Insert “)” onto stack, and add “(” to end of the expression .
Step 2: Scan expression from right to left and repeat Step 3 to 6 for each element of A
until the stack is empty .
Step 7: Exit
43
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Converting Infix to Prefix
44
Data Structures--> Unit-I: Pandu Sowkuntla
Postfix expression evaluation Example infix expression: 2 * ( 4 + 3 ) – 5
45
Data Structures--> Unit-I: Pandu Sowkuntla
Postfix expression evaluation
46
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Prefix expression evaluation
Example infix expression: 2 * (4+3) – 5 Note: Prefix notation also known as Polish
Notation or Warsaw notation, and Postfix
Equivalent prefix: - 5 * 2 + 4 3 notation is also known as Reverse Polish
notation
Reverse of prefix: 3 4 + 2 * 5 -
Reverse of prefix: 3 4 + 2 * 5 –
47
Data Structures--> Unit-I: Stacks Pandu Sowkuntla
Queue Data Structure
48
Data Structures--> Unit-I: Queues Pandu Sowkuntla
Queues
• Queue is a linear data structure, where a
data item insertion is done at one end
(called rear) and deletion is performed at
the other end (called front).
➢ Operations on Queue
2. Linked lists.
► This implementation of a stack can grow and shrink according to the needs
at runtime.
50
Data Structures--> Unit-I: Queues Pandu Sowkuntla
Operations on Queues (Array implementation)
rear = -1
3 3 6 3 6 9 6 9 9
Queue full
front=0 front=0 front=0 front=1 front=2
queue: 17 23 97 44
• If the rear of the queue is at
0 1 2 3 4 5 6 7 the end of the array (the highest
index).
enqueue(333) 17 23 97 44 333
• Even if there are empty cells at
0 1 2 3 4 5 6 7
the beginning of the array, rear
can't go any further.
dequeue() 23 97 44 333
0 1 2 3 4 5 6 7 • Because all the elements are
After some front = 1 removed, the rear points to end
insertions rear = 4
of queue (the highest index).
and deletions
0 1 2 3 4 5 6 7
10 20 30 40 50
Queue:
front = 0 rear = 4
► When data is transferred asynchronously (data not necessarily received at same rate as
sent) between two processes.
Examples: People waiting in line at a bank, airplanes waiting to take off, stores and
reservation centres,. Etc.