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

DSA Notes Unit 1 To Unit 6

This document provides an introduction to the topic of linear arrays. It discusses basic terminology related to linear data structures and arrays. It then outlines the main topics that will be covered in the lecture, including memory representation of arrays, traversing arrays, insertion and deletion in arrays, sorting arrays using bubble sort, and searching arrays using linear and binary search. The document provides an overview of the key concepts and operations involved with linear arrays to introduce the main lecture.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views

DSA Notes Unit 1 To Unit 6

This document provides an introduction to the topic of linear arrays. It discusses basic terminology related to linear data structures and arrays. It then outlines the main topics that will be covered in the lecture, including memory representation of arrays, traversing arrays, insertion and deletion in arrays, sorting arrays using bubble sort, and searching arrays using linear and binary search. The document provides an overview of the key concepts and operations involved with linear arrays to introduce the main lecture.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 587

Data Structures

Lecture 1: Introduction to Data Structures

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Classification of Data Structures
• Data Structure Operations
• Review Questions
Basic Terminology
• Data: are values or set of values.

• Data Item: is a single unit of values.

• Data Items are divided into two categories:


• Group Items: Data items that are divided into sub-
items.
• Elementary Items: Data items that are not divided into
sub-items.
Data Structure
• Organization of data needed to solve the problem.

“ Logical or mathematical model of a particular


organization of data is called a Data Structure.”
Classification of Data Structures
Data Structures

Primitive Non-Primitive
Data Structures Data Structures
• Integer
Linear Non-Linear
• Real Data Structures Data Structures
• Character
• Boolean • Array • Tree
• Stack • Graph
• Queue
• Linked List
Data Structure Operations
Data Structures are processed by using certain operations.
1. Traversing: Accessing each record exactly once so that
certain items in the record may be processed.

2. Searching: Finding the location of the record with a given


key value, or finding the location of all the records that satisfy
one or more conditions.

3. Inserting: Adding a new record to the structure.

4. Deleting: Removing a record from the structure.


Special Data Structure-Operations
• Sorting: Arranging the records in some logical order
(Alphabetical or numerical order).

• Merging: Combining the records in two different sorted


files into a single sorted file.
Questions
Review Questions
• What is the difference between Data and Information?
Explain with example.

• What is the difference between Linear and Non-Linear


Data Structure?

• Differenciate between Sorting and Merging.

• How Searching is different from Traversing?


Brain Storming Questions
• Array is a user defined data type.

•A – Yes

•B - No
Brain Storming Questions
• Linked list is a linear data structure.

•A – True

•B - False
Data Structures
Lecture: Asymptotic Notations & Complexity
Analysis

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Complexity of Algorithm
• Asymptotic Notations
• Review Questions
Basic Terminology
• Algorithm: is a finite step by step list of well-defined
instructions for solving a particular problem.

• Complexity of Algorithm: is a function which gives running


time and/or space requirement in terms of the input size.

• Time and Space are two major measures of efficiency of an


algorithm.
Algorithm

Specification of Output
Specification of Input
(e.g. any sequence of natural Algorithm as a function of Input
numbers) (e.g. sequence of sorted
natural numbers)
Characteristics of Good Algorithm
• Efficient
• Running Time
• Space used

• Efficiency as a function of input size


• Size of Input
• Number of Data elements
Blank
Time-Space Tradeoff
• By increasing the amount of space for storing the
data, one may be able to reduce the time needed for
processing the data, or vice versa.
Blank
Complexity of Algorithm
• Time and Space used by the algorithm are two main
measures for efficiency of any algorithm M.

• Time is measured by counting the number of key


operations.

• Space is measured by counting the maximum of


memory needed by the algorithm.
• Complexity of Algorithm is a function f(n) which gives
running time and/or space requirement of algorithm M in terms
of the size n of the input data.

• Worst Case: The maximum value of f(n) for any possible input.

• Average Case: The expected or average value of f(n).

• Best Case: Minimum possible value of f(n).


Blank
Analysis of Insertion Sort Algorithm
cost times
for j←2 to n do c1 n
key ←A[j] c2 n-1
i ←j-1 c3 n-1
while i>0 and A[i] > key c4
do A[i+1] ←A[i] c5
i-- c6
A[i+1] ← key c7 n-1
Total Time = n(c1 + c2 + c3 + c7) +
- (c2 + c3 + c5 + c6 + c7)
Analysis of Insertion Sort
Total Time = n(c1 + c2 + c3 + c7) +
- (c2 + c3 + c5 + c6 + c7)
• Best Case: Elements are already sorted, tj=1
running time = f(n)
• Worst Case: Elements are sorted in reverse order,
tj=j
running time = f(n2)
• Average Case: tj= j/2
running time = f(n2)
Blank
Rate of Growth
• The rate of growth of some standard functions
g(n) is:

log2n < n < nlog2n < n2 < n3 < 2n


Blank
Asymptotic Notations
• Goal: to simplify analysis of running time .

• Useful to identify how the running time of an


algorithm increases with the size of the input in
the limit.

• Asymptotic is a line that approaches a curve but


never touches.
Asymptotic Notations
Special Classes of Algorithms
• Logarithmic: O(log n)
• Linear: O(n)
• Quadratic: O(n2)
• Polynomial: O(nk), k >= 1
• Exponential: O(an), a > 1
Big-Oh (O) Notation
• Asymptotic upper bound

• f(n) = O (g(n)), if there exists


constants c and n0 such that,

• f(n) <= c g(n) for n >= n0

• f(n) and g(n) are functions over non-


negative integers.
• Used for Worst-case analysis.
Big-Oh (O) Notation
• Simple Rule:
Drop lower order terms and constant factors.

Example:
• 50n log n is O(n log n)
• 8n2 log n + 5 n2 + n is O(n2 log n)
Blank
Big-Omega (Ω) Notation
• Asymptotic lower bound

• f(n) = Ω (g(n)), if there


exists constants c and n0
such that,
c g(n) <= f(n) for n >= n0

• Used to describe Best-case


running time.
Blank
Big-Theta (Ө)Notation
• Asymptotic tight bound

• f(n) = Ө (g(n)), if there exists


constants c1, c2 and n0 such that,

• c1 g(n) <= f(n) <= c2 g(n) for n


>= n0

• f(n) = Ө (g(n)), iff f(n) = O(g(n)) and


f(n) = Ω (g(n))
Blank
Little-Oh (o) Notation
• Non-tight analogue of Big-Oh.

• f(n) = o (g(n)), if for every c, there exists n0


such that,
f(n) < c g(n) for n >= n0

• Used for comparisons of running times.


Blank
Analysis of Algorithms
• // Here c is a constant
for (int i = 1; i <= c; i++)
{
// some O(1) expressions
}

• O(1): Time complexity of a function (or set of


statements) is considered as O(1) if it doesn’t
contain loop, recursion and call to any other
function.
Analysis of Algorithms
• for (int i = 1; i <= n; i += c)
{ // some O(1) expressions }

• for (int i = n; i > 0; i -= c)


{ // some O(1) expressions }

• O(n): Time Complexity of a loop is considered as


O(n) if the loop variables is incremented /
decremented by a constant amount.
Analysis of Algorithms
• for (int i = 1; i <=n; i += c)
{
for (int j = 1; j <=n; j += c)
{ // some O(1) expressions
}
}
• for (int i = n; i > 0; i -= c)
{
for (int j = i+1; j <=n; j += c)
{ // some O(1) expressions
} }
• O(n2): Time complexity of nested loops is equal to the
number of times the innermost statement is executed.
Analysis of Algorithms
• for (int i = 1; i <=n; i *= c)
{ // some O(1) expressions }

• for (int i = n; i > 0; i /= c)


{ // some O(1) expressions }

• O(Logn) Time Complexity of a loop is


considered as O(Logn) if the loop variables is
divided / multiplied by a constant amount.
Analysis of Algorithms
• for (int i = 2; i <=n; i = pow(i, c))
{ // some O(1) expressions }

• //Here fun is sqrt or cuberoot or any other


constant root
for (int i = n; i > 0; i = fun(i))
{ // some O(1) expressions }
• O(LogLogn) Time Complexity of a loop is
considered as O(LogLogn) if the loop variables is
reduced / increased exponentially by a constant
amount.
Analysis of Algorithms
• for (int i = 2; i*i <=n; i++))
{ // some O(1) expressions }

• O(√n) Time Complexity.


Questions
Review Questions
• When an algorithm is said to be better than the
other?

• Can an algorithm have different running times on


different machines?

• How the algorithm’ running time is dependent on


machines on which it is executed?
Review Questions
Find out the complexity:
function ()
{
if (condition)
{
for (i=0; i<n; i++) { // simple statements}
}
else
{
for (j=1; j<n; j++)
for (k=n; k>0; k--) {// simple statement}
}
}
Blank
Blank
Data Structures
Lecture : Linear Array

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Basic Terminology
• Linear Array
• Memory Representation of Linear Array
• Traversing Array
• Insertion and Deletion in Array
• Sorting (Bubble Sort)
• Searching (Linear Search and Binary Search)
• Review Questions
Basic Terminology
• Linear Data Structures: A data structure is said to be linear if its
elements form a sequence or a linear list.

• Linear Array: is a list of a finite number n of homogeneous data


elements such that:

(a) the elements of the array are referenced by an index set


consisting of n consecutive numbers.

(b) the elements of the array are stored respectively in


successive memory locations.
Key Terms
• Size / Length of Array
• Index of Array
• Upper bound and Lower bound of Array
Memory Representation of Arrays
19 5 42 18 199

1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
Traversing Linear Array
• Suppose we have to count the number of element is an array
or print all the elements of array.

• Algorithm 1: (Using While Loop)

1. [Initialize Counter.] Set K = LB.


2. Repeat Step 3 and 4 while K<= UB.
3. [Visit Element.] Apply PROCESS to A[K].
4. [Increase Counter.] Set K = K+1.
[End of Step 2 Loop.]
5. Exit.
Algorithm 2: (Using for loop)

1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.

Question.1: Find the Number of elements in an array which are


greater than 25.

Question 2: Find out the sum of all the two digit numbers in an
array.
Insertion and Deletion in an Array
• Two types of insertion are possible:
• Insertion at the end of array
• Insertion in the middle of the array
Insertion into a Linear Array
• Algorithm: (Insertion of an element ITEM into Kth position in
a Linear Array A)

1. [Initialize Counter.] Set J = N.


2. Repeat Steps 3and 4 while J >= K.
3. [Move Jth element downward] Set A[J+1] = A[J].
4. [Decrease Counter.] Set J = J-1.
[End of Step 2 loop]
5. [Insert element.] Set A[K] = ITEM.
6. [Reset N] N = N+1.
7. Exit
Deletion into a Linear Array
Algorithm: (Delete Kth element from Linear Array A)

1. Repeat for J = K to N-1.


2. [Move (J+1)th element upward] Set A[J] = A[J+1].
[End of loop.]
3. [Reset the number of elements N] Set N = N-1.
4. Exit
Merging Algorithm

• Suppose A is a sorted list with r elements and B is a sorted list


with s elements. The operation that combines the element of A
and B into a single sorted list C with n=r + s elements is called
merging.

11
Merging Algorithm
 Algorithm: Merging (A, R,B,S,C)
Here A and B be sorted arrays with R and S elements
respectively. This algorithm merges A and B into an array
C with N=R+ S elements
 Step 1: Set NA=0, NB=0 and NC=0
 Step 2: Repeat while NA < R and NB < S:
if A[NA] ≤ B[NB], then:
Set C[NC] = A[NA]
Set NA = NA +1
else
Set C[NC] = B[NB]
Set NB = NB +1
[End of if structure]
Set NC= NC +1
[End of Loop]
12
Merging Algorithm
 Step 3: If NA >=R, then:
Repeat while NB < S:
Set C[NC] = B[NB]
Set NB = NB+1
Set NC = NC +1
[End of Loop]
else
Repeat while NA < R:
Set C[NC] = A[NA]
Set NC = NC + 1
Set NA = NA +1
[End of loop]
[End of if structure]
 Step 4: Return C[NC]

13
Merging Algorithm
• Complexity of merging: The input consists of the total number
n=r+s elements in A and B. Each comparison assigns an
element to the array C, which eventually has n elements.
Accordingly, the number f(n) of comparisons cannot exceed n:
f(n) ≤ n = O(n)

14
Searching
1. Linear Search:
• Compares the item of interest with each element of Array
one by one.
• Traverses the Array sequentially to locate the desired item.
Linear Search Algorithm
• LINEAR (DATA, N, ITEM, LOC)

1. [Insert ITEM at the end of DATA] Set Data [N+1]= ITEM.


2. [Initialize Counter] Set LOC=1.
3. [Search for ITEM.]
Repeat while DATA [LOC] != ITEM
Set LOC = LOC +1.
[End of loop.]
4. [Successful?] if LOC = N + 1, then Set LOC = 0.
5. Exit.
Binary Search
• BINARY ( DATA, LB, UB, ITEM, LOC )
1. [Initialize Segment Variables]
Set BEG = LB, END = UB and MID = INT ((BEG+END)/2).
2. Repeat Steps 3 and 4 while BEG <= END and DATA [MID] != ITEM.
3. If ITEM < DATA[MID], then:
Set END = MID - 1.
Else:
Set BEG = MID + 1.
[End of if Structure.]
4. Set MID = INT ((BEG+END)/2).
[End of Step 2 Loop.]
5. If DATA [MID] = ITEM, then: LOC=MID
Else:
Set LOC = NULL.
[End of if structure.]
6. Exit.
Blank
Limitations of Binary Search
• Although the complexity of Binary Search is
O (log n), it has some limitations:
1. the list must be sorted
2. one must have direct access to the middle element in any
sublist.
Blank
Questions
Data Structures

Lecture: Multi-Dimensional Array

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Two-Dimensional Arrays
• Memory Representation of Two-Dimensional Arrays
• Multidimensional Array
Introduction
• Arrays where elements are referenced, respectively, by
two or more subscripts.

• Some programming languages allow up to 7


dimensional arrays.

• Normally we have Two-Dimensional and Three-


Dimensional Arrays.
Two-Dimensional Array
• A two-dimensional m×n array A is a collection of m*n data
elements such that each element is specified by a pair of
integers ( e.g. j, k), called subscripts, with the property that
1 <= j <= m
and 1 <= k <= n

• The element of A with first subscript j and second subscript k


will be denoted by Aj,k or A[j, k].

• Two-dimensional arrays are called Matrices in mathematics and


Tables in business applications.

• Two-dimensional arrays are some times known as Matrix


Arrays.
Memory Representation
• A Two-Dimensional array will be represented in memory by a
block of m*n sequential memory locations.

• Two-Dimensional array is stored in the memory is following


two orders:
1. Column-major Order: Column by column.
2. Row-major Order: Row by row.
Blank
Column-Major Order

(1, 1)
(2, 1) Column 1
(3, 1)
(1, 2)
(2, 2) Column 2
(3, 2)
(1, 3)
(2, 3) Column 3
(3, 3)
(1, 4)
(2, 4) Column 4
(3, 4)
Row-Major Order

(1, 1)
(1, 2)
Row 1
(1, 3)
(1, 4)
(2, 1)
(2, 2)
(2, 3) Row 2

(2, 4)
(3, 1)
(3, 2)
(3, 3) Row 3
(3, 4)
Column-Major Order:
LOC (A[j, k]) = Base (A) + w [M (k-1) + (j-1)]

Row-Major Order:
LOC (A[j, k]) = Base (A) + w [N (j-1) + (k-1)]
Blank
Review Questions
• Given, in a 2-D Array the lower bound and upper bound for first
index is 3 and 11 and that for second index is 5 and 9. Find out
the size of the array.

• In any 2-D Array, which are the elements that will be always
having the same memory address for both column-major order
& Row-major order?

• Consider a 20×5 matrix array Marks. Suppose Base (Marks) =


1002 and words per memory cell w=4. Using Column-major
order and row-major order, find out the marks of 3rd and 5th test
of student 11.
Data Structures
Lecture: Selection Sort
By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Min

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Min

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6
DONE!
Comparison

Data Movement

Sorted
Blank
Selection Sort
Selection_Sort (A, n)

1. Set j = 0.
2. Repeat While j < n – 1:
3. Set Min = j and i = j+1.
4. Repeat step 5 while i < n:
5. if(a[i] < a[Min]), then:
Min = i.
[End of step 4 loop.]
6. if (Min != j), then:
7. swap (a[j], a[Min]).
8. [End of step 2 loop.]
9. Return.
Comparison Table of Sorting
Best Average Worst
Case Case Case

Bubble Sort O(n) O(n2) O(n2)


Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Merge Sort O(n log n) O(n log n) O(n log n)
Quick Sort O(n log n) O(n log n) O(n2)
Heap Sort O(n log n) O(n log n) O(n log n)
Questions?
Data Structures
Lecture: Sorting Techniques

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
➢ Introduction
➢ Insertion Sort
➢ Merge Sort
➢ Bubble Sort
➢ Quick Sort
➢ Heap Sort
➢ Selection Sort
Sorting (Bubble Sort)
• Sorting refers to the operation of rearranging the elements of A
so they are in some particular order.

• Complexity of Bubble Sort Algorithm is: O(n2)

• Example of Bubble Sort


Bubble Sort Algorithm
Bubble (DATA, N)
1. Repeat Step 2 and 3 for K=1 to N-1.
2. [Initialize Pass Pointer P] Set P=1.
3. [Execute Pass] Repeat while P <= N-K.
(a) if DATA [P] > DATA [P+1], then:
Interchange DATA [P] and DATA[P+1]
[End of if Structure.]
(b) Set P = P+1.
[End of Inner Loop.]
[End of Step1 Outer Loop.]
4. Exit
Blank
Insertion Sort
Example of insertion sort
8 2 4 9 3 6
Example of insertion sort
8 2 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6
Example of insertion sort
8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done
Insertion Sort
INSERTION_SORT (A, N)
1. Set A[0] = -∞.
2. Repeat Step 3 to 5 for K = 2, 3, …, N:
3. Set TEMP = A[K] and PTR = K – 1.
4. Repeat while TEMP < A[PTR]:
(a) Set A[PTR+1] = A[PTR]
(b) Set PTR = PTR – 1.
[End of Loop.]
5. Set A[PTR+1] = TEMP.
6. Return.
Insertion Sort Complexity
➢ This Sorting algorithm is frequently used when n is very
small.

➢ Worst case occurs when array is in reverse order. The inner


loop must use K – 1 comparisons.

f(n) = 1 + 2 + 3 + ….+ (n – 1) = n(n – 1)/2


= O(n2)

➢ In average case, there will be approximately (K – 1)/2


comparisons in the inner loop.
f(n) = 1 + 2 + 3 + ….+ (n – 1)/2 = n(n – 1)/4
= O(n2)
Blank
Animation Link
• Insertion Sort
• http://courses.cs.vt.edu/~csonline/Algorithms/Lessons/Insertio
nCardSort/insertioncardsort.swf
Data Structures
Lecture : Linked List

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Why Linked List?
• Types of Linked List
• Memory Representation of Linked Lists
• Difference between Singly Linked List and Arrays
• Review Questions
Introduction
• A linked list (One-way list) is a linear collection of data
elements, called nodes, where the linear order is given by
means of pointers.

• Each node is divided into two parts.

• First part contains the information of the element.

• Second part contains the address of the next node in the list.
Linked List
• A singly linked list is a
concrete data structure
consisting of a sequence of next
nodes
• Each node stores
– element
– link to the next node node
element

A B C D
Blank
Key Points
❑ Linked list
• Linear collection of nodes
• Connected by pointer links
• Accessed via a pointer to the first node of the list
• Link pointer in the last node is set to null to mark the
list’s end
• Linked list contains a List Pointer Variable called
START or NAME, which contains the address of the
first node.
Why Linked List?
Arrays: pluses and minuses
+ Fast element access.
-- Impossible to resize.

Need of Linked List


• Many applications require resizing!
• Required size not always immediately available.
❑Use a linked list instead of an array when
• You have an unpredictable number of data elements
• You want to insert and delete quickly.
Memory Representation
• Linked lists are maintained in memory using linear arrays or
Parallel arrays.

INFO LINK
Start 2 1
2 A 6
3 E 9
4 C 7
5
6 B 4
7 D 3
8
9 F 0
10
Memory Representation (2)
• Multiple lists in memory

INFO LINK

Start1 2 1 S4 0
2 A 6
3 E 9
Start 2 10 4 C 7
5 S2 8
6 B 4
7 D 3
8 S3 1
9 F 0
10 S1 5
Blank
Memory Representation (3)
• INFO part of a node may be a record with multiple data items.
• Records in memory using Linked lists

Start Name Age Sex LINK


2
1
2 A 19 M 6
3 E 21 M 9
4 C 20 F 7
5
6 B 19 F 4
7 D 22 M 3
8
9 F 20 F 0
10
Types of Linked Lists
1. Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
2. Circular, singly linked
• Pointer in the last node points
back to the first node
3. Doubly linked list
• Two “start pointers” – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
4. Circular, doubly linked list
• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
Difference between Singly Linked List and
Arrays

Singly linked list Array

• Elements are stored in • Elements are stored in


linear order, accessible linear order, accessible
with links. with an index.

• Do not have a fixed size. • Have a fixed size.

• Cannot access the previous • Can access the previous


element directly. element easily.

• No binary search. • Binary search.


Review Questions
• Why we need Linked List?

• What are the two different Fields in a node of linked list.

• How Linked lists are different from Arrays?

• Why Binary search is not possible in Linked list?

• What is the difference between Circular Linked List and


Doubly Linked List?
Data Structures
Lecture: Linked List Operations

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Traversing a Linked List
• Searching a Linked List
• Memory Allocation & Garbage Collection
• Overflow and Underflow
• Review Questions
Introduction
• A linked list (One-way list) is a linear collection of data
elements, called nodes, where the linear order is given by
means of pointers.

• Each node is divided into two parts.

• First part contains the information of the element.

• Second part contains the address of the next node in the list.
Blank
Traversing a Linked List
• PTR is a pointer variable which points to the node currently
being processed.
• LINK [PTR] points to the next node to be processed.

❑ Algorithm (Traversing a linked list)

1. Set PTR = START. [Initialize pointer PTR]


2. Repeat step 3 and 4 while PTR ≠ NULL.
3. Apply PROCESS to INFO[PTR].
4. Set PTR = LINK [PTR]. [PTR points to next node]
[End of Step 2 Loop.]
5. EXIT
Problems
• Write an algorithm to modify each element of an integer
linked list such that
(a) each element is double of its original value.
(b) each element is sum of its original value and its previous element.

• Write an algorithm to find out the maximum and minimum


data element from an integer linked list.
Searching a Linked List
Blank
Searching a Linked List (1)
❑ List is Unsorted
SEARCH (INFO, LINK, START, ITEM, LOC)
1. Set PTR = START.
2. Repeat Step 3 While PTR ≠ NULL.
3. If ITEM = INFO [PTR], then:
Set LOC = PTR, and EXIT.
Else:
Set PTR = LINK [PTR]. [PTR points to next node]
[End of if Structure.]
[End of step 2 Loop.]
4. Set LOC = NULL. [Search is Unsuccessful.]
5. Exit . [Return LOC and Exit.]
Searching a Linked List (2)
❑ List is Sorted
SEARCH (INFO, LINK, START, ITEM, LOC)
1. Set PTR = START.
2. Repeat Step 3 While PTR ≠ NULL.
3. If ITEM > INFO [PTR], then:
PTR = LINK [PTR]. [PTR points to next node]
Else if ITEM = INFO [PTR], then:
Set LOC = PTR, and EXIT. [Search is successful.]
Else:
Set LOC = NULL, and EXIT. [ITEM exceeds INFO[PTR]…]
[End of if Structure.]
[End of step 2 Loop.]
4. Return LOC .
5. Exit.
Blank
Memory Allocation
• Together with the linked list, a special list is maintained
which consists of unused memory cells.

• This list has its own pointer.

• This list is called List of available space or Free-storage


list or Free pool.
Free Pool
• Linked list with free pool or list of Available space.

INFO LINK

START 2 1 0
2 A 6
3 E 9
AVAIL 10 4 C 7
5 8
6 B 4
7 D 3
8 1
9 F 0
10 5
Garbage Collection
• Garbage collection is a technique of collecting all the
deleted spaces or unused spaces in memory.

• The OS of a computer may periodically collect all the


deleted space onto the free-storage list.

• Garbage collection may take place when there is only some


minimum amount of space or no space is left in free
storage list.

• Garbage collection is invisible to the programmer.


Garbage Collection Process
• Garbage collection takes place in two steps.

1. The computer runs through all lists tagging those cells


which are currently in use.

2. Then computer runs through the memory, collecting all


the untagged spaces onto the free storage list.
Overflow and Underflow
❑Overflow: When a new data are to be inserted into a data
structure but there is no available space i.e. the free storage
list is empty.

• Overflow occurs when AVAIL = NULL, and we want insert


an element.

• Overflow can be handled by printing the ‘OVERFLOW’


message and/or by adding space to the underlying data
structure.
Overflow and Underflow
❑Underflow: When a data item is to be deleted from an
empty data structure.

• Underflow occurs when START = NULL, and we want to


delete an element.

• Underflow can be handled by printing the ‘UNDERFLOW’


message.
Review Questions
• Write an algorithm to find out the maximum and minimum
data element from an integer linked list.

• What is the condition of Overflow and underflow?

• What are the steps followed during garbage collection?


Data Structures
Lecture: Insertion in Linked List

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Insertion Algorithm
– Insertion at the beginning
– Insertion after a given node
– Insertion into a sorted list
• Review Questions
Insertion into a Linked List

❑ New node N (which is to be inserted) will come from AVAIL


list.
• First node in the AVAIL list will be used for the new node N.

❑ Types of insertion:
• Insertion at the beginning
• Insertion between two nodes
Checking the Available List

AVAIL

ITEM Ø

Free-Storage List
NEW
Insertion at the beginning of Linked List

START

ITEM
Blank
Insertion Algorithm (Beginning of the list)

INSFIRST (INFO, LINK, START, AVAIL, ITEM)

1. [OVERFLOW?] If AVAIL = NULL, then: Write: OVERFLOW, and


Exit.
2. [Remove first node from AVAIL list]
Set NEW= AVAIL and AVAIL= LINK [AVAIL].
3. Set INFO [NEW] = ITEM. [Copy the new data to the node].
4. Set LINK [NEW] = START. [New node points to the original first
node].
5. Set START = NEW. [START points to the new node.]
6. EXIT.
Blank
Insertion after a given node
START

LOC

ITEM
Insertion Algorithm (After a given node)
INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)
1. [OVERFLOW?] If AVAIL = NULL, then: Write: OVERFLOW, and
Exit.
2. [Remove first node from AVAIL list]
Set NEW= AVAIL and AVAIL= LINK [AVAIL].
3. Set INFO [NEW] = ITEM. [Copy the new data to the node].
4. If LOC = NULL, then: [Insert as a first node]
Set LINK [NEW] = START and START = NEW.
Else: [Insert after node with location LOC.]
Set LINK [NEW] = LINK [LOC] and LINK [LOC] = NEW.
[End of If structure.]
5. Exit.
Blank
Insertion into a sorted Linked List
• If ITEM is to be inserted into a sorted linked list. Then ITEM must be
inserted between nodes A and B such that:
INFO [A] < ITEM < INFO [B]
• First of all find the location of Node A
• Then insert the node after Node A.

INSERT (INFO, LINK, START, AVAIL, ITEM)

1. CALL FIND_A (INFO, LINK, START, AVAIL, ITEM)


[USE Algorithm FIND_A to find the location of node preceding
ITEM.]
2. CALL INSLOC (INFO, LINK, START, AVAIL, LOC, ITEM)
[Insert ITEM after a given node with location LOC.]
3. Exit.
FIND_A (INFO, LINK, START, ITEM, LOC)

1. [List Empty?] If START = NULL, then: Set LOC = NULL, and Return.
2. [Special Case?] If ITEM < INFO [START], then: Set LOC = NULL, and
Return.
3. Set SAVE = START and PTR = LINK [START]. [Initializes pointers]
4. Repeat step 5 and 6 while PTR ≠ NULL.
5. If ITEM < INFO [PTR] then:
Set LOC = SAVE, and Return.
[End of If Structure.]
6. Set SAVE = PTR and PTR = LINK [PTR]. [Update pointers]
[End of Step 4 Loop.]
7. Set LOC = SAVE.
8. Return.
Blank
Review Questions
• What is the condition for the list being empty?

• How will you insert a node in a linked list after a given


node?

• Which pointer fields are changed when:


– a node is inserted after a given node
– a node is inserted at the end of list
– a node is inserted at the beginning of the list.
Data Structures
Lecture: Deletion from Linked List

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Deletion from a Linked List

• Deletion Algorithm
– Deleting the Node following a given Node
– Deleting a Node with a given ITEM of Information

• Review Questions
Deletion from a Linked List
❑ A node N is to be deleted from the Linked List.
• Node N is between node A and node B.

❑ Deletion occurs as soon as the next pointer field of node A is


changed so that it points to node B.

❑ Types of Deletion:
• Deleting the node following a given node
• Deleting the Node with a given ITEM of Information.
Deletion from Linked List

START

ITEM Ø

Node A Node N Node B


Maintaining the AVAIL List
• After the deletion of a node from the list, memory space of node
N will be added to the beginning of AVAIL List.

• If LOC is the Location of deleted node N:

LINK [LOC] = AVAIL


AVAIL = LOC
Blank
Deleting the Node Following a given Node
DEL (INFO, LINK, START, AVAIL, LOC, LOCP)

1. If LOCP = NULL, then:


Set START = LINK [START]. [Delete First node.]
Else:
Set LINK [LOCP] = LINK [LOC]. [Delete node N.]
[End of If Structure.]

2. [Return Deleted node to the AVAIL list]


Set LINK [LOC] = AVAIL and AVAIL= LOC.

3. EXIT.
Blank
Deleting the Node with a given ITEM of
Information
DELETE (INFO, LINK, START, AVAIL, ITEM)
1. Call FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)
[Find the Location of node N and its preceding node]
2. If LOC = NULL, then: Write: ITEM not in LIST and EXIT.

3. [Delete node].
If LOCP = NULL, then:
Set START = LINK [START]. [Delete First node]
Else:
Set LINK [LOCP] = LINK [LOC].
[End of If Structure.]

4. [Return Deleted node to the AVAIL list]


Set LINK [LOC] = AVAIL and AVAIL= LOC.
5. EXIT.
FIND_B (INFO, LINK, START, ITEM, LOC, LOCP)

1. [List Empty?] If START = NULL, then:


Set LOC = NULL, LOCP = NULL and Return.
[End of If Structure.]

2. [ITEM in First node?] If INFO [START] = ITEM, then:


Set LOC = START, and LOCP = NULL, and Return.
[End of If Structure.]

3. Set SAVE = START and PTR = LINK [START]. [Initializes pointers]


4. Repeat step 5 and 6 while PTR ≠ NULL.
5. If INFO [PTR] = ITEM, then:
Set LOC = PTR and LOCP = SAVE, and Return.
[End of If Structure.]

6. Set SAVE = PTR and PTR = LINK [PTR]. [Update pointers]


[End of Step 4 Loop.]
7. Set LOC = NULL. [Search Unsuccessful.]
8. Return.
Blank
Review Questions
• What is the condition for the list being empty?

• Which pointer fields are changed when:


– a node is deleted after a given node
– a node is deleted which is at the end of list
– a node is deleted at the beginning of the list.
Blank
Data Structures
Lecture: Header Linked List

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Header Linked List
• Advantages of Header Linked List
• Types of Header Linked List
• Review Questions
Header Linked List
❑ A header linked list which always contains a special
node, called the header node, at the beginning of the
list.

START
Ø

HEADER NODE
Header Linked List

Name Salary LINK


2 0
START 1 5 59,000 6
2
E 10000 9
3
10 C 4000 7
AVAIL 4
8
5
6 B 20000 4
7 D 13000 3
8 1
9 F 12000 2
10 5
Advantages of Header Linked List
• Header linked list contains a special node at the top.

• This header node need not represent the same type of data that
succeeding nodes do.

• It can have data like, number of nodes, any other data...

• Header node can access the data of all the nodes in the linked
list.
Types of Header Linked List
❑ A Grounded header list is a header list where the last node
contains the null pointer.

❑ A Circular header list is a header list where the last node


points back to the header node.

Note:
• Unless otherwise stated or implied, header list will always be circular
list.
• Accordingly, in such a case, the header node also acts as a sentinel
indicating the end of the list.
START
Ø

HEADER NODE

Grounded Header List


START

HEADER NODE

Circular Header List


Blank
• If Link [START] = NULL,
then, Grounded Header List is Empty.

• If Link [START] = START,


then, Circular Header List is Empty.
Traversing a Circuar Header List
❑ Algorithm (Traversing a Circular Header list)

1. Set PTR = LINK [START]. [Initialize pointer PTR]


2. Repeat step 3 and 4 while PTR ≠ START.
3. Apply PROCESS to INFO[PTR].
4. Set PTR = LINK [PTR]. [PTR points to next node]
[End of Step 2 Loop.]
5. EXIT
Use of Header Linked List
• Header Linked lists are frequently used for maintaining
Polynomials in memory.
Blank
Review Questions
• What is Header Node?

• How a Linked List is different from Header linked list?

• What is Grounded Header list and circular header list?


Data Structures
Lecture: Two-Way List

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Two-Way List
• Two-Way Header List
• Operations on Two-Way List
• Traversing
• Searching
• Deleting
• Inserting
• Review Questions
Introduction
• If a list can be traversed in only one direction, it is called One-
way List.

• List Pointer variable START points to the first node or header


node.

• Next-pointer field LINK is used to point to the next node in the


list.

• Only next node can be accessed.

• Don’t have access to the preceding node.


Two-Way List
❑ A Two-Way list is a linear collection of data elements,
called nodes, where each node N is divided into three
parts:

1. An information field INFO which contains the data of N.

2. A pointer field FORW which contains the location of the


next node in the list.

3. A pointer field BACK which contains the location of the


preceding node in the list.
Two-Way List …
❑ The Two-Way list requires two list pointer variables:
1. FIRST: points to the first node in the list.
2. LAST: points to the last node in the list.
LAST

FIRST

BACK
X X

INFO FORW
Blank
Key Points
• If LOCA and LOCB are the locations of node A and node B
respectively, then

FORW [LOCA] = LOCB, iff


BACK [LOCB] = LOCA

• Two way lists are maintained in memory by means of linear


arrays as in one way lists.

• But now we require two pointer arrays BACK and FORW.


Two-Way Header List
• It is circular because the two end nodes point back to the
header node.
• Only one list pointer variable START is required.

START HEADER
NODE

INFO FORW
Blank
Insertion in a Two-way List
• INST_TWL (INFO, FORW, BACK, FIRST, LAST, AVAIL,
LOCA, LOCB, ITEM)

1. [OVERFLOW?] If AVAIL = NULL, then: Write: Overflow and Exit.

2. [Remove Node from AVAil List and copy the Item into it.]
Set NEW = AVAIL, AVAIL = FORW [AVAIL],
INFO [NEW] = ITEM.

3. [Insert Node into the list.]


Set FORW [LOCA] = NEW, FORW [NEW] = LOCB,
BACK [LOCB] = NEW, and BACK [NEW] = LOCA.

4. Exit.
Blank
Deletion in a Two-way List
• DEL_TWL (INFO, FORW, BACK, FIRST, LAST, AVAIL,
LOC)

1. [Delete Node.]
Set FORW [BACK [LOC] ] = FORW [LOC] and
BACK [FORW [LOC] ] = BACK [LOC].

2. [Return Node to AVAIL list.]


Set FORW [LOC] = AVAIL and AVAIL = LOC.

3. Exit.
Blank
Review Questions
• What is the advantage of Two-way List over Linked List?

• How Traversing is done in TWL?

• What is the difference between Deletion in Linked List and


TWL?

• How TWL is more efficient that Linked List in case of


Searching?
Blank
Data Structures
Lecture: Stacks

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Introduction
• Basic Operations
• Array Representation of Stacks
• Minimizing Overflow
• Linked Representation of Stacks
• Review Questions
Introduction
Stacks:
• A stack is a list of elements in which an element may
be inserted or deleted only at one end, called the TOP
of the Stack.

• Stack is a LIFO (Last In First Out) data structure.

• Elements are removed from a stack in the reverse order


of that in which they were inserted into the stack.
Examples
• Stack of Dishes
• Stack of Books
• A packet of Biscuits etc.

• Note: AVAIL List is also implemented using STACK.


STACK
PUSH
Anuj

TOP = 1
TOP = 0
Array Representation of Stacks

STACK

Rahul Anil Deepak Neha

1 2 3 4 5 6 7 8

4 8
TOP MAXSTK
Blank
Array Representation of Stacks
PUSH (STACK, TOP, MAXSTK, ITEM)

1. [Stack already filled?]


If TOP = MAXSTK, then: Print: OVERFLOW and Return.

2. Set TOP = TOP +1. [Increase TOP by 1.]

3. Set STACK [TOP] = ITEM. [Insert ITEM into the TOP.]

4. Return
Blank
Array Representation of Stacks
POP (STACK, TOP, ITEM)

1. [Stack has an ITEM to be removed?]


If TOP = 0, then: Print: UNDERFLOW and Return.

2. Set ITEM = STACK [TOP]. [ITEM is the TOP element.]

3. Set TOP = TOP - 1. [Decrease TOP. by 1.]

4. Return
Minimizing Overflow
• Stack involves a Time-space Trade-off.

• By reserving a great deal of space for stack, we can


minimize number of overflows.
Blank
Blank
Blank
Data Structures
Lecture: Stacks (Polish Notation)

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
• Arithmetic Expressions
• Polish Notation
• Evaluation of a Postfix Expression
• Transforming Infix expression to Postfix Expression
• Review Questions
Arithmetic Expressions
• Arithmetic Expressions involve constants and
operations.

• Binary operations have different levels of precedence.


– First : Exponentiation (^)
– Second: Multiplication (*) and Division (/)
– Third : Addition (+) and Subtraction (-)
Example
• Evaluate the following Arithmetic Expression:
5 ^ 2 + 3 * 5 – 6 * 2 / 3 + 24 / 3 + 3
• First:
25 + 3 * 5 – 6 * 2 / 3 + 24 / 3 + 3
• Second:
25 + 15 – 4 + 8 + 3
• Third:
47
Polish Notation
• Infix Notation: Operator symbol is placed between
the two operands.

Example:
(5 * 3) + 2 & 5 * (3 + 2)
Blank
Polish Notation
❑ Polish Notation: The Operator Symbol is placed before
its two operands.

Example: + A B, * C D, / P Q etc.

• Named after the Polish Mathematician Jan L..

• The order in which the operations are to be performed is


completely determined by the positions of operators and
operands in the expression.
Examples
• (A + B) * C =
* + ABC
• A + (B * C) =
+ A *BC
• (A + B) / (C - D) =
/ +AB –CD

• Also Known as Prefix Notation.


Blank
Reverse Polish Notation
❑ Reverse Polish Notation: The Operator Symbol is placed after
its two operands.

Example: A B+, C D*, P Q/ etc.

• Also known as Postfix Notation.


Blank
Evaluation of Postfix Expression
❑ P is an arithmetic expression in Postfix Notation.

1. Add a right parenthesis “)” at the end of P.

2. Scan P from left to right and Repeat Step 3 and 4 for each element of
P until the sentinel “)” is encountered.

3. If an operand is encountered, put it on STACK.


4. If an operator @ is encountered, then:
(A) Remove the two top elements of STACK, where A
is the top element and B is the next to top element.
(B) Evaluate B @ A.
(C) Place the result of (B) back on STACK.
[End of if structure.]
[End of step 2 Loop.]
5. Set VALUE equal to the top element on STACK.
6. Exit.
Blank
Infix to Postfix Transformation
POLISH (Q, P)
1. PUSH “(” on to STACK and add “)” to the end of Q.
2. Scan Q from left to right and Repeat steps 3 to 6 for each element of Q until
the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator @ is encountered, then:
(a) Repeatedly POP from STACK and add to P each operator (On
the TOP of STACK) which has the same precedence as or
higher precedence than @.
(b) Add @ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly POP from STACK and add to P each operator (On the TOP
of STACK.) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Don’t add the left parenthesis to P.]
[End of If Structure.]
[End of step 2 Loop.]
7. Exit.
Blank
Review Questions
• What is the need of Stacks?

• How Array representation of Stack is different from Linked


Representation?

• How will you handle Overflow and Underflow?


Blank
Data Structures
Lecture: Merge Sort

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Divide and Conquer
• Recursive in structure
– Divide the problem into sub-problems that are
similar to the original but smaller in size
– Conquer the sub-problems by solving them
recursively. If they are small enough, just solve
them in a straightforward manner.
– Combine the solutions to create a solution to
the original problem
An Example: Merge Sort
Sorting Problem: Sort a sequence of n elements into
non-decreasing order.

• Divide: Divide the n-element sequence to be


sorted into two subsequences of n/2 elements each
• Conquer: Sort the two subsequences recursively
using merge sort.
• Combine: Merge the two sorted subsequences to
produce the sorted answer.
Blank
Merge Sort – Example
Original Sequence Sorted Sequence
18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43

18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43

18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9

18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1
Blank
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r) // sort A[p..r] by divide & conquer


1 if p < r
2 then q  (p+r)/2
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)


Blank
Procedure Merge
Merge(A, p, q, r)
1 n1  q – p + 1
2 n2  r – q Input: Array containing
3 for i  1 to n1
4 do L[i]  A[p + i – 1] sorted subarrays A[p..q]
5 for j  1 to n2 and A[q+1..r].
6 do R[j]  A[q + j]
7 L[n1+1]   Output: Merged sorted
8 R[n2+1]  
9 i1 subarray in A[p..r].
10 j1
11 for k p to r
12 do if L[i]  R[j]
13 then A[k]  L[i]
14 ii+1 Sentinels, to avoid having to
15 else A[k]  R[j]
16 jj+1 check if either subarray is
fully copied at each step.
Blank
Questions?
Data Structures
Lecture: Stacks (Tower of Hanoi)

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Tower of Hanoi
• Tower of Hanoi is a mathematical puzzle invented by
a French Mathematician in 1883.
• The game starts by having few discs stacked in
increasing order of size. The number of discs can
vary, but there are only three pegs.
Tower of Hanoi
• The Objective is to transfer the entire tower to one of
the other pegs. However you can only move one disk
at a time and you can never stack a larger disk onto a
smaller disk. Try to solve it in fewest possible moves.
Tower of Hanoi
How to solve the 4 pegs
Tower of Hanoi
Recursive Solution for the Tower of Hanoi with algorithm

Let’s call the three peg Src(Source), Aux(Auxiliary) and


Dst(Destination).
1) Move the top N – 1 disks from the Source to Auxiliary tower
2) Move the Nth disk from Source to Destination tower
3) Move the N – 1 disks from Auxiliary tower to Destination
tower. Transferring the top N – 1 disks from Source to
Auxiliary tower can again be thought of as a fresh problem
and can be solved in the same manner.
Blank
Tower of Hanoi
TOWER(N, BEG, AUX, END)
1. If N = 1 then
a. Write BEG -> END
b. Return
2. [Move N-1 disks from peg BEG to peg AUX]
Call TOWER(N-1, BEG, END, AUX)
3. Write BEG -> END
4. [Move N-1 disks from peg AUX to peg END]
Call TOWER(N-1, AUX, BEG, END)
5. Return.
Blank
Tower of Hanoi
For N = 4 we get the following sequence

1. Move from Src to Aux


2. Move from Src to Dst
3. Move from Aux to Dst
4. Move from Src to Aux
5. Move from Dst to Src
6. Move from Dst to Aux
7. Move from Src to Aux
8. Move from Src to Dst
9. Move from Aux to Dst
10. Move from Aux to Src
11. Move from Dst to Src
12. Move from Aux to Dst
13. Move from Src to Aux
14. Move from Src to Dst
15. Move from Aux to Dst
Tower of Hanoi
How many moves will it take to transfer n disks from the left
post to the right post?

• for 1 disk it takes 1 move to transfer 1 disk from post A to


post C;
• for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3
• for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7
• for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15
• for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31
• for 6 disks... ?
Blank
Tower of Hanoi
• Explicit Pattern
• Number of Disks Number of Moves
1 1
2 3
3 7
4 15
5 31

• Powers of two help reveal the pattern:


• Number of Disks (n) Number of Moves
1 2^1 - 1 = 2 - 1 = 1
2 2^2 - 1 = 4 - 1 = 3
3 2^3 - 1 = 8 - 1 = 7
4 2^4 - 1 = 16 - 1 = 15
5 2^5 - 1 = 32 - 1 = 31
Blank
Tower of Hanoi Game link
• http://www.softschools.com/games/logic_ga
mes/tower_of_hanoi/
Blank
Data Structures
Lecture: Queue

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
➢ Introduction
➢ Examples of Queues
➢ Application of Queues
➢ Basic Operations on Queue
➢ Representation of Queues
➢ Array Representation
➢ Linked Representation
Introduction
➢ A Queue is a linear list of elements in which deletions can take
place only at one end, called the ‘FRONT’ and insertions can
take place only at the other end, called the ‘REAR’.

➢ Queues are also called as FIFO list.


Array
Representation
Insertion into a Queue
Q_INSERT (QUEUE, N, FRONT, REAR, ITEM)

1. If FRONT = 1 and REAR = N, or If FRONT = REAR+1, then:


Write: OVERFLOW and Return. [Overflow]
2. If FRONT = NULL, then [Queue initially empty]
Set FRONT = 1 and REAR = 1.
Else if REAR = N, then:
Set REAR = 1.
Else:
Set REAR = REAR + 1.
[End of if Structure.]
3. Set Queue[REAR] = ITEM.
4. Return.
Blank
Deletion in a Queue
Q_DELETE (QUEUE, N, FRONT, REAR, ITEM)

1. If FRONT = NULL, then:


Write: UNDERFLOW and Return. [Underflow]
2. Set ITEM = QUEUE [FRONT].

3. If FRONT = REAR, then [Queue has only one element]


Set FRONT = NULL and REAR = NULL.

Else if FRONT = N, then:


Set FRONT = 1.
Else:
Set FRONT = FRONT + 1.
[End of if Structure.]
4. Return.
Blank
LINKED
Representation
Insertion into a Queue
LINK_Q_INSERT (INFO, LINK, FRONT, REAR, AVAIL, ITEM)

1. [Available space?] If AVAIL = NULL, then:


Write “OVERFLOW” and Exit.
2. Set NEW = AVAIL, and AVAIL = LINK [AVAIL].

3. Set INFO [NEW] = ITEM and LINK [NEW] = NULL.

4. If FRONT = NULL, then: FRONT = REAR = NEW.


Else: Set LINK [REAR] = NEW and REAR = NEW

5. Exit
Blank
Deletion in a Queue
LINK_Q_DELETE (INFO, LINK, FRONT, REAR, AVAIL, ITEM)

1. [Queue Empty?] If FRONT = NULL, then:


Write “UNDERFLOW” and Exit.
2. Set TEMP = FRONT.

3. Set ITEM = INFO [TEMP].

4. If FRONT = REAR then


5. Set FRONT = REAR = NULL
6. Else
7. Set FRONT = LINK [FRONT]

8. Set LINK [TEMP] = AVAIL and AVAIL = TEMP.

9. Exit
Blank
Deques
➢ Deque (Double-ended queue) is a linear list in which elements
can be added or removed at either end but not in the middle.

➢ Two variations of Deque are:


1. Input-restricted deque
2. Output-restricted deque
Variations of Deque
Input-Restricted Deque:
allows insertion only at one end while deletion in both ends of
the list.

Output-Restricted Deque:
allows deletion only at one end while insertion at both the ends
of the list.
Blank
Priority Queue
➢ A Priority Queue is a collection of elements such that each
element has been assigned a priority and such that the order in
which elements are processed comes from the following rules:

o An element of higher priority is processed before any element of


lower priority.
o Two elements with the same priority are processed according to
the order in which they were added to the queue.
Blank
Example
➢ Time sharing systems: Programs of high priority are processed
first.
One-way list Representation
of
Priority queues
One-Way List Representation
➢ Each node in the list contains three types of information fields
(Information INFO, Priority Number PRN, and a link LINK).

➢ A node X precedes a node Y in the list when:


o X has higher priority than Y.
o Both X and Y have same priority but X was added before Y.
Deletion in a Priority Queue
1. Set ITEM = INFO [START].

2. Delete First node from the list.

3. Process ITEM.

4. Exit.
Blank
Insertion in a Priority Queue
1. Traverse the One-Way list until finding a node X whose priority
number exceeds N.

2. Insert ITEM in front of node X.

3. If no such node is found, insert the ITEM as the last element of the
list.

4. Exit.
Blank
Array Representation
of
Priority Queues
Array Representation of Priority Queues
➢ Use a separate queue for each level of priority (for each priority
number).

➢ Each such queue will appear in its own circular array and have its
own pointers FRONT and REAR.

FRONT REAR 1 2 3 4 5
2 4 [ X Y Z ]
3 3 [ P ]
0 0 [ ]
5 2 [ A B D ]
Blank
Deletion in a Priority Queue
➢ Delete and process the first element in a priority queue
maintained by a two-dimensional array QUEUE.

1. [Find the first non-empty queue.]


Find the smallest K such that FRONT [K] ≠ NULL.

2. Delete and process the front element in row K of QUEUE.

3. Exit.
Blank
Insertion in a Priority Queue
➢ Insert an ITEM with priority number M to a priority queue
maintained by a two-dimensional array QUEUE.

1. Insert ITEM as the REAR element in row K of QUEUE.

2. Exit.
Blank
Blank
Data Structures
Lecture: Tree

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Binary Tree
• Basic Terminology
• Complete Binary Tree
• Extended Binary Tree
• Traversing Binary Tree
•Pre-order
• In-order
• Post-order
Introduction
• Trees are non-linear data structures.

• Used to represent a hierarchical relationship


between the elements.
Binary Tree
• A Binary Tree T is defined as a finite set of elements,
called nodes, such that:

(a) T is empty (called the null tree or empty tree), or


(b) T contains a distinguished node R, called the root
of T, and the remaining nodes of T form an
ordered pair of disjoint binary trees T1 and T2.

• T1 and T2 are called left sub-tree and right subtrees of R.


Binary Tree
• T1 and T2 are called left sub-tree and right subtrees of R.

• If T1 is nonempty, then its root is called the left successor


of R; similarly, if T2 is nonempty, then its root is called the
right successor of R.
Blank
Basic Terminology
• Terminal Nodes: The node with no successor is called
terminal node or leaf.

• Similar Trees: Two binary trees T1 and T2 are said to be


similar if they have the same shape or structure.

• Copy of Trees: Two binary trees T1 and T2 are said to be


copies if they are similar and if they have the same contents at
corresponding nodes.
Blank
Basic Terminology
• Parent:
• Left Child:
• Right Child:
• Siblings:
• Level:

• A line drawn from the node N to its successor is called edge.


• The sequence of consecutive edges is called Path.
• Height or Depth of a Tree is the maximum number of nodes in a
branch of T.
Height = Largest Level + 1
Blank
Complete Binary Tree
• A Binary tree T is said to be complete if all its levels,
except possibly the last, have the maximum number of
possible nodes and all the nodes at the last level appear
as far left as possible.

• The depth of a complete Binary Tree T is:


D = floor value (log2n + 1)
Blank
Extended Binary Tree
• A Binary tree T is said to be extended binary tree or 2-
Tree or Full Binary Tree if each node N has either 0 or 2
children.

• Nodes with 0 child are called external nodes.

• Nodes with 2 children are called internal nodes.


Blank
Perfect Binary Tree
• A Binary tree is Perfect Binary Tree in which all
internal nodes have two children and all leaves are at
same level.
Blank
Questions
Review Questions
• What do you mean by Non-linear Data structures?
• What is the need of trees?

• How Complete Binary Tree is different from 2-Tree?


• What is the maximum number of nodes at any level n of a
Binary Tree?
• What is the Minimum number of nodes in the last level of
Complete Binary Tree?

• What can be the maximum height of a binary tree with n nodes?


Blank
Data Structures
Lecture: Tree Traversal

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Representing Binary Tree in Memory
• Linked Representation
• Sequential Representation
• Traversing Binary Tree
• Preorder
• Inorder
• Postorder
• Traversal algorithms using Stacks
• Review Questions
Introduction
• Trees are non-linear data structures.

• Trees can be represented in memory using linked list


(linked representation) and arrays (sequential
representation).

• One should have direct access to the root R of T and,


given a node N of T, one should have direct access to
the children of N.
Blank
Linked Representation of Binary Tree

• A pointer variable ROOT and three parallel arrays


(INFO, LEFT and RIGHT) are used.
• Each node N of Tree T corresponds to a location K
such that:
(1) INFO[K] contains the data at the node N.
(2) LEFT [K] contains the location of the left child of node
N.
(3) RIGHT[K] contains the location of the right child of
node N.
• If any subtree is empty then corresponding pointer
will contain the NULL value.

• If Tree is empty then Root will contain NULL.

• An entire record may be stored at the node N.


Blank
Linked Representation

INFO LEFT RIGHT


ROOT 2 0
A 6 4
E 0 0
AVAIL 10 C 0 9
8
B 7 3
D 0 0
1
F 0 0
5
Blank
Sequential Representation of Binary Tree

• Efficient for the tree T that is complete or nearly


complete.

• Use of only a single linear array TREE such that:


(a) The Root of T is stored in TREE [1].
(b) If a node N occupies Tree [K], then its left child is stored
in TREE [2*K] and right child is stored in TREE [2*K + 1].
Blank
Inorder Traversal of Binary Tree
INORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat while PTR != NULL:
(a) Set TOP = TOP+1. and STACK[TOP] = PTR.
(b) Set PTR = LEFT[PTR].
3. Set PTR = STACK [TOP] and TOP = TOP – 1.
4. Repeat Step 5 to 7 while PTR != NULL:
5. Apply PROCESS to INFO[PTR].
6. If RIGHT[PTR] != NULL, then:
(a) Set PTR = RIGHT[PTR].
(b) Go to Step 2.
7. Set PTR = STACK[TOP] and TOP = TOP-1.
8. Exit.
Blank
Preorder Traversal of Binary Tree
PREORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat step 3 to 5 while PTR != NULL:
3. Apply PROCESS to INFO[PTR].
4. If RIGHT[PTR] != NULL, then:
Set TOP = TOP+1. and
STACK[TOP] = RIGHT[PTR].
5. If LEFT[PTR] != NULL, then:
Set PTR = LEFT[PTR].
Else
Set PTR = STACK [TOP] and TOP = TOP – 1.
6. Exit.
Blank
Postorder Traversal of Binary Tree
POSTORDER (INFO, LEFT, RIGHT, ROOT )
1. Set TOP = 1, STACK[1] = NULL and PTR = ROOT.
2. Repeat step 3 to 5 while PTR != NULL:
3. Set TOP = TOP+1. and STACK[TOP] = PTR.
4. If RIGHT[PTR] != NULL, then:
Set TOP = TOP+1. and STACK[TOP] = -RIGHT[PTR].
5. Set PTR = LEFT[PTR].
6. Set PTR = STACK [TOP] and TOP = TOP – 1.
7. Repeat while PTR >0:
(a) Apply PROCESS to INFO[PTR].
(b) Set PTR = STACK[TOP] and TOP = TOP-1.
8. If PTR < 0, then:
(a) Set PTR = - PTR
(b) Go to Step 2.
9. Exit.
Blank
Questions
Blank
Data Structures
Lecture: Binary Search Tree

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
A Binary Search Tree is a binary tree with
the following properties:

•All items in the left subtree are less than the root.

•All items in the right subtree are greater or equal to the root.

•Each subtree is itself a binary search tree.


Blank
Basic Concepts
Binary search trees provide an excellent structure for
searching a list and at the same time for inserting and deleting
data into the list.
Blank
Blank
BST Operations
We discuss four basic BST operations: traversal, search, insert,
and delete; and develop algorithms for searches, insertion, and
deletion.

• Traversal
• Search
• Insertion
• Deletion
Blank
Searching and inserting in Binary Search Tree
FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
1.If ROOT = NULL, then Set LOC = NULL and PAR = NULL and return
2.If ITEM = INFO[ROOT], then Set LOC = ROOT and PAR = NULL and return
3.If ITEM < INFO[ROOT], then:
Set PTR = LEFT[ROOT] and SAVE = ROOT
Else
Set PTR = RIGHT[ROOT] and SAVE = ROOT
4.Repeat step 5 and 6 while PTR != NULL
5. If ITEM = INFO[PTR], then Set LOC = PTR and PAR = SAVE and return
6. If ITEM < INFO[PTR], then:
Set SAVE = PTR and PTR = LEFT[PTR]
Else
Set SAVE = PTR and PTR = RIGHT[PTR]
7.Set LOC = NULL and PAR = SAVE
8.Exit
Blank
INSBST (INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM, LOC)
1. Call FIND (INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR )
2. If LOC != NULL then Exit
3. [copy item into new node]
(a) If AVAIL = NULL then write OVERFLOW and Exit
(b) Set NEW = AVAIL, AVAIL = LEFT [AVAIL] and INFO[NEW] = ITEM
(c) LEFT[NEW] = NULL and ROGHT[NEW] = NULL
4. [Add item to tree]
If PAR = NULL then:
Set ROOT = NEW
Else if ITEM < INFO[PAR] then
Set LEFT[PAR] = NEW
Else
Set RIGHT[PAR] = NEW
7. Exit
Blank
Deletion in Binary Search Tree
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. If LEFT[LOC] = NULL and RIGHT[LOC] = NULL then:
Set CHILD = NULL
Else If LEFT[LOC] != NULL then:
Set CHILD = LEFT[LOC]
Else
Set CHILD = RIGHT[LOC]
4. If PAR != NULL then
5. If LOC =LEFT[PAR] then
Set LEFT[PAR] = CHILD
Else
Set RIGHT[PAR] = CHILD
Else
Set ROOT = CHILD
7. Return
Blank
CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR )
1. (a) Set PTR = RIGHT[LOC] and SAVE = LOC
(b) Repeat while LEFT[PTR] != NULL
Set SAVE = PTR and PTR = LEFT[PTR]
(c) Set SUC = PTR and PARSUC = SAVE
2. Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC, PARSUC)
4. (a) If PAR != NULL then
If LOC =LEFT[PAR] then
Set LEFT[PAR] = SUC
Else
Set RIGHT[PAR] = SUC
Else
Set ROOT = SUC
(b) Set LEFT[SUC] = LEFT[LOC] and
RIGHT[SUC] = RIGHT[LOC]
7. Return
Blank
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC = NULL then Write ITEM not in tree, and Exit
3. If RIGHT[LOC] != NULL and LEFT[LOC] != NULL then
Call CASEB (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
Else
Call CASEA (INFO, LEFT, RIGHT, ROOT, LOC, PAR)
4. Set LEFT[LOC] =AVAIL and AVAIL = LOC
5. Exit.
6.
Blank
Questions
4.https://www.cs.usfca.edu/~galles/visualization/BST.html
Blank
Data Structures
Lecture: AVL Tree

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Blank

2
Motivation
• When building a binary search tree, what type of trees
would we like? Example: 3, 5, 8, 20, 18, 13, 22

3
5 13
8
5 20
13
18 3 8 18 22
20
22
3
Blank

4
• Complete binary tree is hard to build when we allow dynamic insert
and remove.
– We want a tree that has the following properties
• Tree height = O(log(N))
• allows dynamic insert and remove with O(log(N)) time
complexity.
– The AVL tree is one of this kind of trees.

8
13
5 18
5 20
3 13 20
3 8 18 22 22
5
Blank

6
AVL (Adelson-Velskii and Landis) Trees

• An AVL Tree is a binary 4


44
search tree such that for
2 3
every internal node v of 17 78
T, the heights of the 1 2 1
children of v can differ 32 50 88
1 1
by at most 1. 48 62

An example of an AVL tree where


the heights are shown next to the
nodes:
7
Blank

8
• AVL tree is a binary search tree with balance
condition
– To ensure depth of the tree is O(log(N))
– And consequently, search/insert/remove
complexity bound O(log(N))
• Balance condition
– For every node in the tree, height of left and right
subtree can differ by at most 1

9
Blank

10
Which is an AVL Tree?

11
Blank

12
Balance Condition Violation
• If condition violated after a node insertion
– Which nodes do we need to rotate?
– Only nodes on path from insertion point to root may have their
balance altered
• Rebalance the tree through rotation at the deepest node with balance
violated
– The entire tree will be rebalanced

• Violation cases at node k (deepest node)


1. An insertion into left subtree of left child of k
2. An insertion into right subtree of left child of k
3. An insertion into left subtree of right child of k
4. An insertion into right subtree of right child of k

– Cases 1 and 4 equivalent


• Single rotation to rebalance
– Cases 2 and 3 equivalent
• Double rotation to rebalance
13
Blank

14
AVL Trees Complexity
• Overhead
– Extra space for maintaining height information at each
node
– Insertion and deletion become more complicated, but still
O(log N)
• Advantage
– Worst case O(log(N)) for insert, delete, and search

15
Single Rotation (Case 1)

• Replace node k2 by node k1


• Set node k2 to be right child of node k1
• Set subtree Y to be left child of node k2
• Case 4 is similar

16
Blank

17
Example

• After inserting 6
– Balance condition at node 8 is violated

18
Blank

19
Example
• Inserting 3, 2, 1, and then 4 to 7 sequentially into empty AVL tree

3
2
2
1 3
1

20
Blank

21
• Inserting 4 2

1 3

4
• Inserting 5

2 2

1 3 4
1
4 5
3
5
22
Blank

23
• Inserting 6
4
2
2 5
1 4
1 3 6
3 5
• Inserting 7
6
4
4
2 6
2 5
1 3 5 7
1 3 6

7
24
Blank

25
Example

• Continuing the previous example by inserting


– 16 down to 10, and then 8 and 9

• Inserting 16 and 15
4
4
2 6
2 6
1 3 5 15
1 3 5 7
7 16
16
15

26
Blank

27
• Inserting 14
4
4
2 7
2 6
1 3 6 15
1 3 5 15

14 16
7 16 5

14

28
Blank

29
Questions
Blank

• http://www.motleytech.net/balanced-binary-
tree-avl-tree-animation.html

• https://www.cs.usfca.edu/~galles/visualizatio
n/AVLtree.html

31
Data Structures
Lecture : Heap

By
Arvind Kumar
Asst. Professor,
Lovely Professional University, Punjab
Contents
• Introduction
• Heap Sort
• Heap Sort Complexity
• Review Questions
Blank
Introduction
• Heap (MaxHeap): A complete binary tree H with
n elements is called a Heap if each node N of H has the
following property:
“ The value at N is greater than or equal to the value
at each of the children of N.”

• If the value at N is less than or equal to the value at each of


the children of N, then it is called MinHeap.

• Heaps are maintained in memory by using linear array


TREE.
Blank
Insertion in Heap
INSHEAP(TREE, N, ITEM)
1. Set N = N +1 and PTR = N
2. Repeat step 3 to 6 while PTR > 1
3. Set PAR = floor(PTR/2)
4. If ITEM < = TREE[PAR] then
Set TREE[PTR] = ITEM and Return
5. Set TREE[PTR] = TREE[PAR]
6. Set PTR = PAR
7. Set TREE[1] = ITEM
8. Return
Blank
Deletion in Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM = TREE[1]
2. Set LAST = TREE[N] and N = N – 1
3. Set PTR = 1, LEFT = 2 and RIGHT = 3
4. Repeat step 5 to 7 while RIGHT <= N
5. If LAST >= TREE[LEFT] and LAST >= TREE[RIGHT] then
Set TREE[PTR] = LAST and Return
6. If TREE[RIGHT] <= TREE[ LEFT] then
Set TREE[PTR] = TREE[LEFT] and PTR = LEFT
Else
Set TREE[PTR] = TREE[RIGHT] and PTR = RIGHT
7. Set LEFT = 2*PTR and RIGHT = LEFT + 1
8. If LEFT = N and LAST < TREE[LEFT] then
Set TREE[PTR] = TREE[LEFT] and PTR = LEFT
9. Set TREE[PTR] = LAST
10. Return
Blank
Heap Sort
HEAPSORT(A, N)
1. Repeat for J = 1 to N-1
Call INSHEAP(A, J, A[J+1])
2. Repeat while N > 1
(a) Call DELHEAP(A, N, ITEM)
(b) Set A[N+1] = ITEM
3. Exit
Blank
Complexity of Heap Sort
Average and Worst case Complexity of Heap sort = O(nlogn).
Blank
Questions
• Min Heap
• https://www.cs.usfca.edu/~galles/visualizatio
n/Heap.html
• Max Heap
• https://visualgo.net/en/heap
• https://yongdanielliang.github.io/animation/
web/Heap.html
Blank
Data Structures
Lecture: Graph

By
Arvind Kumar
Asst. Professor
Lovely Professional University, Punjab
Contents
• Introduction
• Basic Terminology
• Sequential Representation of Graphs
• Adjacency Matrix
• Path Matrix
• Linked Representation of Graphs
• Warshall’s Algorithm: Shortest Path
• Review Questions
Introduction
• A Graph G is a collection of:
1. A set V of elements called Nodes or Vertices.
2. A set E of Edges such that each edge e in E is identified with
a unique pair [u, v] of nodes in V, denoted by e = [u, v].

G = (V, E)

• Nodes u and v are called end points of edge e and also known
as adjacent nodes or neighbors.
Blank
Basic Terminology
• Degree of a node: Degree of a node, deg(u), is the
number of edges containing u.

• If deg(u) = 0, then node u is called Isolated Node.

• A Path P of length n from node u to a node v is defined


as a sequence of n+1 nodes.
P = (v0, v1, v2,...,vn)
Blank
Graph
• Connected Graph: A graph G is connected iff there is
a simple path between any two nodes in G.

• Complete Graph: A graph G is said to be complete if


every node u in G is adjacent to every other node v in G.
i.e. each node u is directly connected to all other nodes v
in Graph G.

• A complete graph with n nodes will have n(n-1)/2


edges.
Blank
Labeled Graphs...
• A graph G is said to be labeled if its edges are
assigned data.

• Weighted Graph: Graph G is said to be weighted if


each edge e is assigned a non-negative numerical
value w(e) called the weight or length of edge.

• If no other information about weights are given in a


graph, then assume the weight w(e) = 1 for each edge.
Blank
Multi-Graph
• Multiple Edges: Distinct edges e and e’ are called
multiple edges if they connect the same endpoints,
i.e. if e = [u, v] and e’ = [u, v].
• Loops: An edge e is called a loop if it has identical
endpoints ,
i.e. if e = [u, u] .
• Note: Definition of a graph does not allow any loop or
multiple edge in Graph.

• A Graph with loops or multiple edges is called a Multi-


graph.
Blank
Directed-Graph
• A Directed-graph G, also called Digraph or Graph, is
same as the multigraph except that each edge e in G is
assigned a direction i.e. each edge e is identified by an
ordered pair (u, v ) of nodes in G, rather than an
unorderd pair [u, v].

Teminology:
• Edge e = (u, v) is called an Arc.
• e begins at u and ends at v.
• u is called origin and v is called destination.
• u is adjacent to v and v is not adjacent to u.
• u is predecessor of v and v is sucessor of u.
Blank
Degree of Graph
• Outdegree: Outdegree of a node u in G is the
number of edges beginning at u.

• Indegree: Indegree of a node u in G is the number


of edges ending at u.

• Source: A node u is called a source if it has a


positive outdegree but zero indegree.

• Sink: A node u is called a sink if it has a positive


indegree but zero outdegree.
Blank
Sequential Representation
• There are two ways of representing a graph in
memory:
• Sequential Representation
• Adjacency Matrix
• Path Matrix
• Linked Representation
Blank
Adjacency Matrix

Path Matrix
Blank
Warshall’s Algorithm
Path Matrix
1. Repeat for I = 1 to M
Repeat for J =1 to M
If A[I,J] = 0 then
Set P[I, J] = 0
Else
Set P[I, J] = 1
2. Repeat for K = 1 to M
Repeat for I = 1 to M
Repeat for J =1 to M
Set P[I, J] = P[I, J] V (P[I, K] ᴧ P[K, J])
3. Exit
Blank
Floyd Warshall Algorithm
Shortest Path Matrix
1. Repeat for I = 1 to M
Repeat for J =1 to M
If W[I,J] = 0 then
Set Q[I, J] = INFINITY
Else
Set Q[I, J] = W[I, J]
2. Repeat for K = 1 to M
Repeat for I = 1 to M
Repeat for J =1 to M
Set Q[I, J] = MIN (Q[I, J] , Q[I, K] + Q[K, J])
3. Exit
Blank
Questions
Blank
Links

• http://matrixmultiplication.xyz/
Data Structures
Lecture: Operations on Graph

By
Arvind Kumar
Asst. Professor
Lovely Professional University, Punjab
Contents
• Introduction
• Graph Traversal
• BFS
• DFS
• Searching in a Graph
• Insertion in a Graph
• Deletion in a Graphs
• Review Questions
Blank
Introduction
• There are two different ways of Traversing a Graph.
• Breadth First Search (BFS) : uses Queue
• Depth First Search (DFS) : uses Stack

• During Traversal, each node N of G will be in one of


the three states:
• STATUS = 1: Ready State (initial state)
• STATUS = 2: Waiting State (waiting in Queue/Stack)
• STATUS = 3: Processed State
Blank
Breadth First Search
1. Initialize all nodes to Ready State (STATUS = 1).
2. Put the statrting node A in Queue and change its status to
Waiting State (STATUS = 2).
3. Repeat step 4 and 5 until Queue is empty:
4. Remove the front node N of the Queue.
Process N and set the status of N to STATUS=3.
5. Add to the Rear of Queue all the neighbors of N that
are in STATUS = 1. and change their status to
STATUS = 2.
[End of step 3 Loop.]
6. Exit.
Blank
Depth First Search
1. Initialize all nodes to Ready State (STATUS = 1).
2. Push the statrting node A onto STACK and change its status
to Waiting State (STATUS = 2).
3. Repeat step 4 and 5 until STACK is empty:
4. POP the TOP node N from the STACK.
Process N and set the status of N to STATUS=3.
5. PUSH onto STACK all the neighbors of N that are in
STATUS = 1, and change their status to STATUS = 2.
[End of step 3 Loop.]
6. Exit.
Blank
Searching of node in a Graph
• Find the location LOC of a node N in graph G:
FIND (INFO, LINK, START, ITEM, LOC)
1. Set PTR = START.
2. Repeat while PTR != NULL.
3. If ITEM = INFO[PTR], then:
Set LOC = PTR and Return.
Else: Set PTR = LINK[PTR].
[End of Loop.]
4. Set LOC = NULL, and Return.
Blank
Searching of Edge in a Graph
• Find the loc of edge (A, B) in graph

FINDEDGE (NODE, NEXT, ADJ, START, DEST, LINK, A, B,


LOC)
1. Call FIND (NODE, NEXT, START, A, LOCA)
2. Call FIND (NODE, NEXT, START, B, LOCB)
3. If LOCA = NULL or LOCB = NULL then set LOC = NULL
Else call FIND (DEST, LINK, ADJ[LOCA], LOCB, LOC)
4. Return.
Blank
Insertion of node in a Graph
• Insert a node N in graph G (it will be an isolated node):
INSERT_NODE (NODE, LINK, ADJ, START, AVAIL, N,
FLAG)
1. [Overflow?] If AVAIL = NULL, then Set FLAG = FALSE, and
Return.
2. Set ADJ[AVAIL] = NULL.
3. Set NEW = AVAIL and AVAIL = LINK[AVAIL].
4. Set NODE[NEW] = N, LINK[NEW] = START and
START = NEW.
5. Set FLAG = TRUE, and Return.
Blank
Insertion of Edge in a Graph
• Insert edge (A, B) in graph

FINDEDGE (NODE, NEXT, ADJ, START, DEST, LINK,


AVAILE, A, B, FLAG)
1. Call FIND (NODE, NEXT, START, A, LOCA)
2. Call FIND (NODE, NEXT, START, B, LOCB)
3. If AVAILE = NULL then set FLAG = FALSE and return.
4. Set NEW = AVAILE and AVAILE = LINK[AVAILE]
5. Set DEST[NEW= LOCB, LINK[NEW] = ADJ[LOCA]
and ADJ[LOCA]= NEW
6. Set FLAG = TRUE and return
Blank
Deletion of node in a Graph
• Delete the first node in list containing ITEM or set FLAG = FALSE
DELETE (INFO, LINK, START,AVAIL, ITEM, FLAG)
1. If START = NULL then set FLAG = FALSE and return.
2. If INFO[START] = ITEM then
Set PTR = START, START= LINK[START]
LINK[PTR] = AVAIL, AVAIL = PTR
FLAG = TRUE and Return.
3. Set PTR =LINK [START] and SAVE = START
4. Repeat steps 5 and 6 while PTR != NULL
5. If INFO[PTR] = ITEM then
Set LINK[SAVE] = LINK[PTR], LINK[PTR] = AVAIL
AVAIL= PTR, FLAG = TRUE and return.
6. Set SAVE = PTR and PTR = LINK[PTR]
7. Set FLAG = FALSE and return.
Blank
Deletion of Edge in a Graph
• Delete edge (A, B) in graph

DELEDGE (NODE, NEXT, ADJ, START, DEST, LINK,


AVAILE, A, B, FLAG)
1. Call FIND (NODE, NEXT, START, A, LOCA)
2. Call FIND (NODE, NEXT, START, B, LOCB)
3. Call DELETE(DEST, LINK, ADJ[LOCA], AVAILE, LOCB,
FLAG).
6. Return
Blank
Deletion of node in a Graph
•Delete node N from graph
DELNODE(NODE, NEXT, ADJ, START, AVAILN, DEST, LINK, AVAILE, N, FLAG)
1. Call FIND(NODE, NEXT, START, N, LOC)
2. If LOC = NULL then set FLAG = FALSE and return.
3. (a) Set PTR = START
(b) Repeat while PTR != NULL
I. Call DELETE(DEST, LINK, ADJ[PTR], AVAILE, LOC, FLAG)
II. Set PTR = NEXT[PTR]
4. If ADJ[LOC] = NULL then Goto step 7
5. (a) set BEG = ADJ[LOC], END = ADJ[LOC] and PTR = LINK[END]
(b) Repeat while PTR != NULL
Set END = PTR and PTR = LINK[PTR]
6. Set LINK[END] = AVAILE and AVAILE = BEG
7. Call DELETE(NODE, NEXT, START, AVAILN, N, FLAG)
8. Return.
Blank
Questions
• https://visualgo.net/en/dfsbfs
Blank
Huffman Coding:
An Application of Binary Trees and Priority
Queues
Huffman Coding

• Proposed by Dr. David A. Huffman in 1952


– “A Method for the Construction of Minimum
Redundancy Codes”
• Applicable to many forms of data
transmission
– Our example: text files
The Basic Algorithm
• Huffman coding is a form of statistical coding
• Not all characters occur with the same
frequency!
• Yet all characters are allocated the same amount
of space
char = 1 byte, be it e or x
The Basic Algorithm

• Code word lengths vary and will be shorter for


the more frequently used characters.
The (Real) Basic Algorithm
1. Scan text to be compressed and tally
occurrence of all characters.
2. Sort or prioritize characters based on
number of occurrences in text.
3. Build Huffman code tree based on
prioritized list.
4. Perform a traversal of tree to determine
all code words.
5. Scan text again and create new file
using the Huffman codes.
Building a Tree
Scan the original text

• Consider the following short text:

Eerie eyes seen near lake.

• Count up the occurrences of all characters in the


text
Building a Tree
Scan the original text

Eerie eyes seen near lake.


• What characters are present?

E e r i space
y s n a l k .
Building a Tree
Scan the original text
Eerie eyes seen near lake.
• What is the frequency of each character in the text?

Char Freq. Char Freq. Char Freq.


E 1 y 1 k 1
e 8 s 2 . 1
r 2 n 2
i 1 a 2
space 4 l 1
Building a Tree
Prioritize characters

• Create binary tree nodes with character


and frequency of each character
• Place nodes in a priority queue
– The lower the occurrence, the higher the
priority in the queue
Building a Tree
• The queue after inserting all nodes

E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
• While priority queue contains two or more nodes
– Create new node
– Dequeue node and make it left subtree
– Dequeue next node and make it right subtree
– Frequency of new node equals sum of frequency of left
and right children
– Enqueue new node back into queue
Building a Tree

E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree

y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8

E i
1 1
Building a Tree

y l k . r s n a sp e
2
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree

k . r s n a sp e
2
1 1 2 2 2 2 4 8
E i
1 1

y l
1 1
Building a Tree

2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i 1 1
1 1
Building a Tree

r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i 1 1
1 1

k .
1 1
Building a Tree

r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
1 1 1 1 1 1
Building a Tree

n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1

r s
2 2
Building a Tree

n a 2 sp e
2 4
2
2 2 4 8

E i y l k . r s
1 1 1 1 1 1 2 2
Building a Tree

2 4 e
2 2 sp
8
4
y l k . r s
E i 1 1 1 1 2 2
1 1

n a
2 2
Building a Tree

2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i 1 1 1 1 2 2 2 2
1 1
Building a Tree

4 4 e
2 sp
8
4
k . r s n a
1 1 2 2 2 2

2 2

E i y l
1 1 1 1
Building a Tree

4 4 4
2 sp e
4 2 2 8
k . r s n a
1 1 2 2 2 2
E i y l
1 1 1 1
Building a Tree

4 4 4
e
2 2 8
r s n a
2 2 2 2
E i y l
1 1 1 1

2 sp
4
k .
1 1
Building a Tree

4 4 4 6 e
2 sp 8
r s n a 2 2
4
2 2 2 2 k .
E i y l 1 1
1 1 1 1
Building a Tree

4 6 e
2 2 2 8
sp
4
E i y l k .
1 1 1 1 1 1
8

4 4

r s n a
2 2 2 2
Building a Tree

4 6 e 8
2 2 2 8
sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree

8
e
8
4 4
10
r s n a
2 2 2 2 4
6
2 2 2 sp
4
E i y l k .
1 1 1 1 1 1
Building a Tree

8 10
e
8 4
4 4
6
2 2
r s n a 2 sp
2 2 2 2 4
E i y l k .
1 1 1 1 1 1
Building a Tree

10
16
4
6
2 2 e 8
2 sp 8
4
E i y l k . 4 4
1 1 1 1 1 1

r s n a
2 2 2 2
Building a Tree

10 16

4
6
e 8
2 2 8
2 sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree

26

16
10

4 e 8
6 8
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
•After
enqueueing
26 this node
there is only
16
10 one node left
4 e 8
in priority
6 8 queue.
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
Dequeue the single node
left in the queue.
26

16
This tree contains the 10
new code words for each
4 e 8
character. 6 8
2 2 2 sp 4 4
4
Frequency of root node E i y l k .
1 1 1 1 1 1 r s n a
should equal number of 2 2 2 2
characters in text.
Eerie eyes seen near lake. 26 characters
Encoding the File
Traverse Tree for Codes

• Perform a traversal of the


tree to obtain new code
words 26

• Going left is a 0 going right is 16


10
a1
• code word is only completed 4 e 8
6 8
when a leaf node is reached 2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Encoding the File
Traverse Tree for Codes

Char Code
E 0000
i 0001
y 0010 26
l 0011
k 0100 16
. 0101 10
space 011 4
e 10 e 8
6 8
r 1100 2 2 2 4 4
s 1101 sp
4
n 1110 E i y l k .
a 1111 1 1 1 1 1 1 r s n a
2 2 2 2
Encoding the File
• Rescan text and encode file
using new code words Char Code
E 0000
Eerie eyes seen near lake.
i 0001
y 0010
0000101100000110011 l 0011
k 0100
1000101011011010011 . 0101
1110101111110001100 space 011
1111110100100101 e 10
r 1100
• Why is there no need s 1101
for a separator n 1110
character? a 1111
.
Encoding the File
Results
• Have we made things any
better? 0000101100000110011
1000101011011010011
• 73 bits to encode the text
1110101111110001100
• ASCII would take 8 * 26 = 1111110100100101
208 bits
Decoding the File

• How does receiver know what the codes are?


Decoding the File
• Once receiver has tree it
scans incoming bit stream 26

• 0  go left 10
16

• 1  go right 4 e 8
6 8
2 2 2 sp 4 4
4
101000110111101111 E i y l k .
1 1 1 1 1 1 r s n a
01111110000110101 2 2 2 2
Summary
• Huffman coding is a technique used to compress files
for transmission
• Uses statistical coding
– more frequently used symbols have shorter code words
• Works well for text and fax transmissions
• An application that uses several data structures
Question

she saw seashells on the seashore


Blank
Blank
Blank
Blank
Blank
Blank
Blank
Link
• https://csfieldguide.org.nz/en/interactives/huf
fman-tree/

• https://people.ok.ubc.ca/ylucet/DS/Huffman.
html
Data Structures
Lecture: Hashing

By
Arvind Kumar
Asst. Professor

Lovely Professional University, Punjab


Outlines
➢ Introduction
➢ Hashing
➢ Hash Functions
▪ Division Method
▪ Midsquare Method
▪ Folding Method
➢ Collision Resolution
➢ Open Addressing : Linear Probing & Modifications
▪ Quadratic Probing
▪ Double Hashing
Blank
Introduction
➢ The search time of all the algorithms depends on the number n
of the elements in the collection of Data.

➢ A searching technique which is essentially independent of n is


called Hash Addressing or Hashing.

➢ F is a file with n records and a set K of Keys which uniquely


determine the records in F.

➢ F is maintained in memory by a table T of m memory locations


and L is a set of memory addresses of the locations in T.
Blank
Example
➢ Suppose a company with 68 employees assigns a 4 digit
Emp_No. to each employee which is used as primary key in
Employee file.

➢ Emp_No can be used as address of record in memory but we


will require 10000 memory locations.
Blank
Hashing
➢ Hashing is a searching technique which is independent of the
number of elements in file.

➢ The general idea of using the Key to determine the address of


records is an excellent idea. But it must be modified to prevent
the wastage of space.

➢ The Modification takes the form of a function H from the set K


of keys into the set L of memory addresses.

H: K → L
Blank
Hash Functions
➢ Hash function H is a mapping between set of Keys K and set of
memory locations L.
H: K → L

➢ Such a function H may not yield distinct values.

➢ It is possible that two different keys K1 and K2 will yield the


same hash address.

➢ This situation is called Collision.


Blank
Hash Functions…
➢ Two principle criteria used in selecting a hash function H are:
1. H should be very easy and quick to compute.
2. H should be uniformly distribute the hash address throughout
the set L. So that the number of collisions are minimized.

➢ Some popular hash Functions are:


➢ Division Method
➢ Midsquare Method
➢ Folding Method
Blank
Division Method
➢ Choose a number m larger than the number n of Keys(usually a
prime number) . Hash function is defined as:

H(K) = k (mod m)
or H(K) = k (mod m) + 1
(when we want hash address to range from 1 to m rather than 0 to m-1)
Blank
Midsquare Method
➢ The key is squared and some digits are deleted from both sides
to obtained l digits.
H(k) = l

where l is obtained by deleting digits from both ends of k2 .


Blank
Folding Method
➢ The key k is partitioned into a number of parts k1, k2, k3… kr,
where each part (except possibly the last) has the same number
of digits as the required address.

➢ Then the parts are added together, ignoring the last carry.

H(k) = k1 + k2 + … + kr
Blank
Hash Table
➢ A hash table (also hash map) is a data structure used to
implement an associative array, a structure that can map keys to
values.

➢ A hash table uses a hash function to compute an index into an


array of buckets or slots (or in memory), from which the correct
value can be found.

➢ Ideally, the hash function should assign each possible key to a


unique bucket, but this ideal situation is rarely achievable in
practice.
Blank
Collision Resolution
Collision Resolution
• Collision is a situation when two or more keys map to the same
memory location.

• Load Factor: The ratio of number n of keys in K to the number


m of hash addresses in L.
λ = n/m
• Efficiency of a hash function is measured by the average number
of probes needed to find the location of record with a given key
k.
Blank
Open Addressing or Close Hashing

➢ Linear Probing
• Resolves collisions by placing the data into the next
open slot in the table.

• We assume that the table T with m locations is circular, so that


T[1] comes after T[m].
Blank
Linear Probing
• Insert pairs whose keys are 6, 12, 34, 29, 28, 11,
23, 7, 0, 33, 30, 45

• divisor = b (number of buckets) = 17.


• H = key % 17.

0 4 8 12 16
34 0 45 6 23 7 28 12 29 11 30 33
Blank
Performance of Linear Probing

• Average number of probes for a successful search


S(λ) = 0.5 (1+ 1/(1- λ) )

• Average number of probes for an unsuccessful search


U(λ) = 0.5 (1+ 1/(1- λ)^2 )
Blank
Problems with Linear Probing

• Identifiers(keys) tend to cluster together


• Increase the search time

• Worst Case Complexity is (n). This happens when all keys


are in the same cluster.

• Two techniques are used to minimize clustering:


1. Quadratic Probing
2. Double Hashing
Blank
Quadratic Probing

• Quadratic probing uses a quadratic function of i as the


increment

• Examine buckets H(x), (H(x)+i2)%b


Blank
Double Hashing

• A second hash function H` is used for resolving the


collision.

• Let H(k) = h and H`(k) = h` ≠ m


Then we linearly search the locations with addresses:
h, h+h`, h+2h`, h+3h`, ...
Blank
Open Hashing or Separate Chaining
• Each bucket in the hash table is the head of a linked
list.
• All elements that hash to a particular bucket are
placed on that bucket’s linked list.
Blank
Open Hashing Data Organization

0 ...

1
...
2

D-1 ...
Blank
Let’s Do It
• Store the given record in table of size 7 by applying
following hash functions and calculate average no. of
probes for successful and unsuccessful search using
linear probing.
Records A B C D
Keys 1011 2012 3331 4114

• Hash Functions:
– Division method
– Mid square method
– Folding method
Blank
Merged by Skillanta.
https://t.me/skillanta

You might also like