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

DS Final Note

Uploaded by

c233421
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)
6 views

DS Final Note

Uploaded by

c233421
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/ 32

Final Note

Data Structure
CSE-2321

Mohammed Nazmul Arefin


11-9-2023
Page 1 of 31

SyllabuS

Section-B (Final Exam: 50 Marks)


Group-A (20 Marks)
4. Queue – its representation; Insertion & deletion in Queue; Deques; Priority Queues. Recursion [Factorial
function, Fibonacci sequence, Ackermann function, Towers of Hanoi]
5. Linked list - Linked list & its representation in memory; Traversing, Searching, Insertion & Deletion
operation on Linked list; Header linked lists; two-way lists.
Group-B (30 Marks)
6. Complexity of algorithms, Rate of growth: Big O, Ω and Θ notations; Complexity of Linear Search, Binary
search & Bubble sort algorithm. Sorting - Insertion sort, selection sort, quick sort, merge sort; Searching &
data modification; Hashing: Hash function, collision resolution
7. Tree- Tree terminology; representation of binary trees in memory; Traversing binary tree; Binary search
tree; Insertion & deletion on binary search tree; Heap; Insertion & deletion on heap; Heapsort; B trees;
General tree; Balanced binary search tree (AVL tree, red-black tree)
8. Graph – graph terminology; representation of graphs – adjacency matrix, path matrix, adjacency list;
Traversing a graph – BFS & DFS

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 2 of 31

4. Queue

Recursion
1

[Au22]
Ans Given, N = 8, B = 3, 7, -2, 5, 6, -4, 2, 7
Y (5) = Y (5-1) + B (5) [K = 5 and 5 < 8]
= Y (4-1) + B (4) + B (5)
= Y (3-1) + B (3) + B (4) + B (5)
= Y (2-1) + B (2) + B (3) + B (4) + B (5)
= Y (1-1) + B (1) + B (2) + B (3) + B (4) + B (5)
= 0 + B (1) + B (2) + B (3) + B (4) + B (5) [K = 0]
= 3 + 7 + (-2) + 5 + 6
= 19
Given, N = 3, B = 2, 7, -4
Y (5) = Y (5-1) [K = 5 and 5 > 3]
= Y (4-1) [K =4 and 4 > 3]
= Y (3-1) + B (3)
= Y (2-1) + B (2) + B (3)
= Y (1-1) + B (1) + B (2) + B (3)
= 0 + B (1) + B (2) + B (3) [K = 0]
= 2 + 7 + (-4)
=5
2

[SP22]
Ans G (2) = 3 * 2 = 6
G (8) = 2 * G (8-5) + 0
= 2 * G (3) + 0
= 2 * 3 * 3 + 0 = 18
G (24) = 2 * G (24-8) + 0
= 2 * G (16) + 0
= 2 * 2 * G (16-5) + 0
= 2 * 2 * G (11)
= 2 * 2 * 2 * G (11-5) + 0
= 2 * 2 * 2 * 2 * G (6)
= 2 * 2 * 2 * 2 * 2 * G (6-5) + 0
= 2 * 2 * 2 * 2 * 2 * G (1)
=2*2*2*2*2*3*1
= 96

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 3 of 31

[SP19, SP18]
Ans GCD (6, 15) = GCD (15, 6) [As, A < B] GCD (20, 28) = GCD (28, 20) [As, A < B]
= GCD (6, MOD (15, 6)) = GCD (28, MOD (28, 20))
= GCD (6, 3) = GCD (28, 8)
= GCD (3, MOD (6,3)) = GCD (8, MOD (28, 8))
= GCD (3, 0) = GCD (8, 4)
=3 = GCD (4, MOD (8, 4))
= GCD (4, 0)
=4
4

[SP18]
Ans 1. F (4, 2) = 1
2. F (2, 4) = F (2-1, 4) + F (2-1, 4-1)
= F (1, 4) + F (1, 3)
= F (1-1, 4) + F (1-1, 4-1) + F (1-1, 3) + F (1-1, 3-1)
= F (0, 4) + F (0, 3) + F (0, 3) + F (0, 2)
=1+1+1+1=4

[AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 4 of 31

Circular queue:

Insert into the CQueue:

𝑖𝑓 𝒇𝒓𝒐𝒏𝒕 = 𝟎, 𝑡ℎ𝑒𝑛
𝑠𝑒𝑡 𝒓𝒆𝒂𝒓 = 𝒇𝒓𝒐𝒏𝒕 = 𝟏

𝑒𝑙𝑠𝑒 𝑖𝑓 𝒓𝒆𝒂𝒓 = 𝒏 & 𝒇𝒓𝒐𝒏𝒕 ! = 𝟏, 𝑡ℎ𝑒𝑛


𝑠𝑒𝑡 𝒓𝒆𝒂𝒓 = 𝟏

𝑒𝑙𝑠𝑒 𝒓𝒆𝒂𝒓 = 𝒓𝒆𝒂𝒓 + 𝟏

Is the CQueue full?


𝑖𝑓 (𝒇𝒓𝒐𝒏𝒕 = 𝟏 & 𝒓𝒆𝒂𝒓 = 𝒏)
𝑶𝑹,
(𝒓𝒆𝒂𝒓 + 𝟏)% 𝒏 = 𝒇𝒓𝒐𝒏𝒕

Delete from the CQueue:


𝑖𝑓 𝒇𝒓𝒐𝒏𝒕 = 𝒓𝒆𝒂𝒓, 𝑡ℎ𝑒𝑛
𝑓𝑟𝑜𝑛𝑡 = 𝑟𝑒𝑎𝑟 = 0

𝑒𝑙𝑠𝑒 𝑖𝑓 𝒇𝒓𝒐𝒏𝒕 = 𝒏, 𝑡ℎ𝑒𝑛


𝑓𝑟𝑜𝑛𝑡 = 1

𝑒𝑙𝑠𝑒 𝒇𝒓𝒐𝒏𝒕 = 𝒇𝒓𝒐𝒏𝒕 + 𝟏

How many elements in a CQueue?

(𝒓𝒆𝒂𝒓 − 𝒇𝒓𝒐𝒏𝒕 + 𝒏)%𝒏 + 𝟏

[Au’22]
Ans 1. Number of elements in the queue = (Rear - Front + n) % n +1
In this case, Front is 5, Rear is 10, and n is 12. Plug these values into the formula:
Number of elements in the queue = (10 - 5 + 12) % 12 +1
Now, calculate the numerator first:
(10 - 5 + 12) = 17
Now, calculate the remainder when you divide 17 by 12:
17 % 12 = 5 + 1 = 6
So, there are 6 elements in the circular array queue with Front at 5 and Rear at 10.

2. Number of elements in the queue = (Rear - Front + n) % n +1


Given, Front = 12, Rear = 3, n = 12 (number of memory cells)
Plug in the values:
Number of elements in the queue = (3 - 12 + 12) % 12 +1
Calculate the numerator first:
(3 - 12 + 12) = 3
Now, calculate the remainder when you divide 3 by 12:
3 % 12 = 3 +1 = 4
So, there are 4 elements in the circular array queue with Front at 12 and Rear at 3.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 5 of 31

2
[SP18]
Ans 1. Number of elements in the queue = (Rear - Front + n) % n +1
In this case, Front is 4, Rear is 8, and n is 12. Plug these values into the formula:
Number of elements in the queue = (8 - 4 + 12) % 12 +1
Now, calculate the numerator first:
(8 - 4 + 12) = 16
Now, calculate the remainder when you divide 16 by 12:
16 % 12 = 4 + 1 = 5

2. Number of elements in the queue = (Rear - Front + n) % n +1


Given, Front = 10, Rear = 3, n = 12 (number of memory cells)
Plug in the values:
Number of elements in the queue = (3 - 10 + 12) % 12 +1
Calculate the numerator first:
(3 - 10 + 12) = 5
Now, calculate the remainder when you divide 5 by 12:
5 % 12 = 5 +1 = 6
So, there are 6 elements in the circular array queue with Front at 10 and Rear at 3.

3 Consider the following circular queue where Queue is allocated 6 memory cells.
Front = 3, Rear = 5, Queue: _, _, U, C, S, _
Illustrate the Queue, including front and rear, as the following sequences take place:
i) E is added iii) U, V, A are added
ii) 2 items deleted iv) 3 items are deleted [SP22]
Ans:
i) E is added:
front = 3, rear = 5 + 1 = 6
Queue:
U C S E
1 2 3 4 5 6
↑Front ↑Rear
i) 2 items are deleted
a. front = 4, rear = 6
Queue:
C S E
1 2 3 4 5 6
↑Front ↑Rear
b. front = 5, rear = 6
Queue:
S E
1 2 3 4 5 6
↑Front ↑Rear
ii) U,V,A are added
a. front = 5, rear = 1
Queue:
U S E
1 2 3 4 5 6
↑Rear ↑Front
b. front = 5, rear = 2
Queue:
U V S E
1 2 3 4 5 6
↑Rear ↑Front

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 6 of 31

c. front = 5, rear = 3
Queue:
U V A S E
1 2 3 4 5 6
↑Rear ↑Front
iii) 3 items are deleted
a. front = 6, rear = 3
Queue:
U V A E
1 2 3 4 5 6
↑Rear ↑Front
b. front = 1, rear = 3
Queue:
U V A
1 2 3 4 5 6
↑Front ↑Rear
c. front = 2, rear = 3
Queue:
V A
1 2 3 4 5 6
↑Front ↑Rear
4

[SP19]
Ans
i) W is added:
front = 3, rear = 4 + 1 = 5
Queue:
X Y W
1 2 3 4 5
↑Front ↑Rear
ii) 2 items are deleted
i) front = 4, rear = 5
Queue:
Y W
1 2 3 4 5
↑Front ↑Rear
ii) front = 5, rear = 5
Queue:
W
1 2 3 4 5
↑Front
↑Rear
iii) U,V,A are added
i) front = 5, rear = 1
Queue:
U W
1 2 3 4 5
↑Rear ↑Front
ii) front = 5, rear = 2
Queue:
U V W
1 2 3 4 5
↑Rear ↑Front

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 7 of 31

iii) front = 5, rear = 3


Queue:
U V A W
1 2 3 4 5
↑Rear ↑Front
iv) 1 items is deleted
front = 1, rear = 3
Queue:
U V A
1 2 3 4 5
↑Front ↑Rear
5

[AU17]
6
[ SP22, SP19]
Ans Insertion of an element
1. START
2. Store the element to insert in linear data structure
3. Check if (front == 0 && rear == MAX-1) || (front == rear+1) then queue
Overflow else goto step 4
4. Check if (front == -1) then front = 0; rear= 0; else goto step 5
5. Check if (rear == MAX -1) then rear=0; else rear= rear+1; and goto step 6
6. Insert element cqueue_arr[rear] = item;
7. STOP
Deletion of an element
1. START
2. Store the element to delete.
3. If front == -1 then Queue Underflow.
4. Element deleted from queue is cqueue_arr[front].
5. if (front==rear) then front= -1; rear= -1; else goto 6.
6. if (front == MAX - 1) then front= 0 else goto 7.
7. front = front + 1.
8. STOP
7
[AU18]
8

[AU18]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 8 of 31

Factorial function, Fibonacci sequence, Ackermann function, Towers of Hanoi

1
[AU17]
Ans Ackerman function defined as
a. If m = 0, then A (m, n) = n + 1
b. If m ≠ 0 but n = 0, then A (m, n) = A (m-1, 1)
c. If m ≠ 0 and n ≠ 0, then A (m, n) = A (m-1, A (m, n-1))

A (1, 3)
= A (1-1, A (1, 3-1)) = A (0, A (0, A (0, 2)))
= A (0, A (1, 2)) = A (0, A (0, 2+1))
= A (0, A (1-1, A (1, 2-1))) = A (0, A (0, 3))
= A (0, A (0, A (1, 1))) = A (0, 3+1)
= A (0, A (0, A (1-1, A (1, 1-1)))) = A (0, 4)
= A (0, A (0, A (0, A (1, 0)))) = 4+1
= A (0, A (0, A (0, A (1-1, 1)))) =5
= A (0, A (0, A (0, A (0, 1))))
= A (0, A (0, A (0, 1+1)))

2 [Au’22]
Ans 7

♦ [SP19]
♦ [SP18]
♦ [AU17]

♦ [Au’22]

♦ [
SP22]

♦ [AU18,
AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 9 of 31

5. linked liSt

Linked list

Single linked list representation

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 10 of 31

Inserting node at the end

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 11 of 31

Inserting elements into an array at the end

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 12 of 31

Inserting node at the beginning

Inserting data at beginning in the array

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 13 of 31

Inserting node at any position

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 14 of 31

[SP19, AU18]

Data Structure Space Explanation


Complexity
Singly Linked List O(n) In a singly linked list, each element is stored in a node, which requires
O(n) additional memory for the nodes themselves. No additional data
structures are needed for the sorting.
Circular Linked O(n) Similar to the singly linked list, a circular linked list also requires O(n)
List space for the nodes. However, it might require a little extra space for a
pointer to indicate the start or end, which can be considered as a
constant factor and does not affect the overall space complexity.
Doubly Linked List O(n) A doubly linked list, which has both forward and backward pointers in
each node, requires O(n) additional memory for the nodes. Like the
singly linked list, no additional data structures are needed for sorting.
2

[AU22, SP22, SP18]

Aspect Linked List Linear Array


Dynamic Size - Dynamic size: Linked lists can easily - Fixed Size: Linear arrays have a
grow or shrink by adding or removing fixed size determined during
nodes. This is particularly useful when the initialization, making it less flexible
size of the data structure is unknown in when the size of the data structure
advance. needs to change.
- Time Complexity: O(1) for insertion and - Time Complexity: O(n) for insertion
deletion at the beginning (for singly linked or deletion at arbitrary positions since
lists). O(1) for insertion and deletion at it may require shifting elements.
both ends (for doubly linked lists).
Efficient - Efficient insertions and deletions at any - Efficient access and search: Linear
Insertions/Deletions position: Linked lists allow for O(1) arrays allow for O(1) time complexity
insertions and deletions at the beginning, for accessing elements using an index,
given a reference to the node. making them efficient for read
- Time Complexity: O(n) for search operations.
operations. - Time Complexity: O(1) for insertions
or deletions at the end, and O(n) for
insertions or deletions at arbitrary
positions.
Memory Efficiency - Memory-efficient: Linked lists only - Space overhead: Linear arrays may
allocate memory for the elements they have a fixed, unused space or be too
contain, which is advantageous when small for the data, leading to wasted
dealing with a variable number of items. memory.
- Time Complexity: O(n) for sequential - Time Complexity: O(1) for
access. sequential access.

[AU22, SP18]
4

[AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 15 of 31

[AU22, SP18]
6

[SP19]

9
10

11

12

(i) Avail = 10
so, after adding Jones following changes occure:
- Jones is added into the node taken from avail list which is BED[10]
- So, Avail list is updated by changing it’s link from Link[10] = 2
So, Avail = 2
- As this list is alphabetized, so, Jones will be added after Green.
So, Link[10] = 1, Link[8] =10
(ii) As Max is deleted, it’s node is added to the avail list.
We see that last node of the avail list is 6, Now we are adding current node to that ndoe.
- Link[6] = 4
- Link[7] = Link[4] = 12
- Link[4] = 0

Avail Start
↓ ↓
1 2 3 4 5 6 7 8 9 10 11 12
BED Kirk Dean Adam Lane Green Samu Jones Field Nelso
LINK 7 6 11 0 3 4 12 10 0 1 8 9

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 16 of 31

13

Start

1 2 3 4 5 6 7 8 9 10 11 12
INFO Mary June Barbar Paula Diana Audrey Karen Nancy Ruth Eileen Sandra Halen

LINK 8 7 5 9 10 3 1 4 11 12 0 2

14

15 Form a two-way list from the one-way list in previous question.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 17 of 31

Group B

6. Sort
♧ SORTING
Selection Sort

Insertion Sort

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 18 of 31

♦ [AU22]

♦ 4 [SP22]

♦ 4 [SP22]

♦ [AU17]

♦ [SP19]

♦ [AU22]

♦ [AU17, SP18]

♦ [SP18]

♦ [AU17]

♧ HASHING

♦ [
AU22]


[SP19]

♦ [AU18]

♧ COMPLEXITY

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 19 of 31

♦ [SP22]

♦ [AU18]

♦ [AU18,
SP18]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 20 of 31

7. tree
♧ BINARY TREE
A binary tree T is defined as a finite set of elements called nodes such that,
1. T is empty
2. T contains a distinguished node R, called the rot of T and the remaining nodes of T form an ordered
pair of disjoint binary trees T1 and T2
Sequential representation of binary tree:
Let T be a binary tree that is complete or nearly complete. Then T can be represented using a single linear
array TREE such that,
1. The Hoot R of T is stored in TREE [1]
2. If a node N occupies TREE [K], then its left child is stored in TREE [2K] and right child in TREE
[2K+1].

45

22 77

11 30 90

15 25 88

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
45 22 77 11 30 90 15 25 88
Sequential representation of binary tree

Binary tree traversal


1. In order: Left → Root → Right
2. Pre order: Root → Left→ Right
3. Post order: Left → Right → Root

Traversal of the tree above:


In order: 11, 15, 22, 25, 30, 45, 77, 88, 90
Pre order: 45, 22, 11, 15, 30, 25, 77, 90, 88
Post order: 15, 11, 25, 30, 22, 88, 90, 77, 45

[AU18]
We know that,
For Pre order: Root → Left→ Right
For in order: Left → Root → Right
So, we can get root for the tree from the pre order.

In the pre order list, first element is G. So, the root of the tree is G
Now, from the in-order list, we can divide the list into two subtrees by G.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 21 of 31

G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Left subtree Root Right subtree

Now, to find left child of the root G we search first occurrence of any of the elements of left subtree in
the pre-order list.
We can see that, B (from left subtree list) in the pre-order list. So, left node is B.
G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Q B K, C, F, A P E, D, H, R
Left subtree Root Right subtree
In the right side of B we have K, C, F, A. Now we get A appeared first in the pre order list. So, A is
the right node of B subtree.
G, B, Q, A, C, K, F, P, D, E, R, H
Q, B, K, C, F, A G P, E, D, H, R
Q B K, C, F, A P E, D, H, R
Q B K, C, F A P E D H, R
Q B K C F A P E D H R
Left subtree Root Right subtree
This is how, we get the tree:

[AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 22 of 31

[SP18]
4

3 [AU22, SP19, SP18]


*
/ \
+ ^
/ \ / \
3m n - 3
/ \
5x y

Pre-order: *, +, 3m, n, ^, -, 5x, y, 3


Post-order: 3m, n, +, 5x, y, -, 3, ^, *
5

5 [SP22]

♧ BINARY SEARCH TREE


A binary Search Tree is a binary tree that has the following
properties:

• The left subtree of a node contains only nodes with keys lesser
than the node’s key.
• The right subtree of a node contains only nodes with keys
greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
• There must be no duplicate nodes.

Difference between Binary Tree and Binary Search Tree:

# Basis Binary Tree Binary Search Tree


BINARY SEARCH TREE is a node
BINARY TREE is a nonlinear data
based binary tree that further has right
1. Definition structure where each node can have at
and left subtree that too are binary
most two child nodes.
search tree.
In BINARY SEARCH TREE the left
In BINARY TREE there is no ordering subtree has elements less than the nodes
3. Structure
in terms of how the nodes are arranged element and the right subtree has
elements greater than the nodes element.
Data Data Representation is carried out in a Data Representation is carried out in the
4.
Representation hierarchical format. ordered format.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 23 of 31

# Basis Binary Tree Binary Search Tree


Duplicate Binary Search Tree does not allow
5. Binary trees allow duplicate values.
Values duplicate values.
7. Complexity O(n). O(logn).
It is used for retrieval of fast and quick It works well at element deletion,
8. Application
information and data lookup. insertion, and searching.
It serves as the foundation for It is utilized in the implementation of
9. Usage implementing Full Binary Tree, BSTs, Balanced Binary Search Trees such as
Perfect Binary Tree, and others. AVL Trees, Red Black Trees, and so on.

Insertion of elements into Binary Search Tree:


During insertion, we must consider following conditions
a. Left < Root
b. Right > Root
1

[SP’18]

In order traversal: 22, 23, 34, 40, 45, 50, 60, 77


2

[AU22]
3

4 [SP19]
4

[AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 24 of 31

[SP18]
6

4 [SP22]
7

[AU18]

Deletion of elements from Binary search tree:


During deletion we can follow any two ways if the node has two children (left and right).
a. Predecessor: We can find the largest value from the left sub-tree and replace the value with the node
b. Successor: We can find the smallest value from the right sub-tree and replace the value with the node
If there is only one child, we can simply replace the value with the node we want to delete and remove the
node.

In order successor is needed only when the right child is not empty. In this particular case, the in-order successor can
be obtained by finding the minimum value in the right child of the node.

Delete node with single child:

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 25 of 31

♧ HEAP

Max Heap Min Heap


For every node i, the value of node is less than or For every node i, the value of node is greater than or
equal to its parent value. A[parent(i)] >= A[i] equal to its parent value. A[parent(i)] <= A[i]
→ Root > children’s values → Root < children’s values
→ Must be complete binary tree → Must be complete binary tree

Insertion in Max heap:


Build a max heap from the following list of numbers: 44, 30, 50, 22, 60, 55, 77, 55

Deletion from max heap:

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 26 of 31

Delete 77 from the max heap.

♦ [AU22]

♦ 4 [SP22]

♦ [AU17]

♦ [AU18]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 27 of 31

8. GraPH

1 Define: Graph, Adjacency matrix, adjacency list, path matrix


Graph: A graph consist of two things:
1. A set N of elements called nodes or vertices
2. A set E of edges such that each edge e in E is identified with a unique (unordered) pair [u, v] of
nodes in V, denoted by e = [u, v]
Adjacency matrix: In graph theory, an adjacency matrix is nothing but a square matrix utilized to
describe a finite graph.
Adjacency List: An adjacency list is a data structure used to represent a graph where each node in the
graph stores a list of its neighboring vertices.
Path matrix: A path matrix is a matrix representing a graph, where each value in mth row and nth column
project whether there is a path from node m to node n. The path may be direct or indirect. It may have
a single edge or multiple edge.

2 Give adjacency matrix, adjacency list of the following graph

Adjacency matrix:

Adjacency List:

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 28 of 31

3 Define 1) Simple graph 2) Complete graph


Simple graph Complete graph
A directed graph Gissaidto be simple if G has no A graph G is said to be complete if every node u in G
parallel edges. A simple graph G may haveloops but it is adjacent to every other node v in G.
can’t have more than one loop at a given node.

Figure: Complete graph


Figure: Simple graph
4

3 [sp22]

Figure: Graph G

3 [SP19, SP18]

[AU17]

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 29 of 31

[AU17]

8 BFS and DFS for the following graph consider A as the source:

The general idea behind Breadth First Search (BFS) beginning at a starting node A is as follows. First we examine
the starting node A. Then we examine all theh neighbors of A. Then we examine all the neighbors of the
neighbors of A and so on. Adjacency List Nodes Marker
Let us assume we want to traverse the graph using BFS starting at A. A B, C, D A √
We will use QUEUE to keep track of neighbor nodes. B C, G B √
First push A to BUEVE and mark A as visited and pop A, Then Push C D C √
neighbors of A to QUEDE. D E D √
QUEUE: B, C, D E C E √
Pop B, Push neighbors of B and mark B as visited. F C, E, J F √
QUEUE: C, D, 4
G C, F G √
Pop C, Push neighbores of c and mark Cas visited.
H F, G H √
QUEUE: D, G
J E, H J √
Pop D, Push neighbors of D and mark Das visited.
QUEUE: G, E
Pop 6,Push neighbors of G and mark & as visited.
QUEUE: E, F
Pop E, Push neighbors of E and mark E as visited.
QUEUE: F
Pop F, Push neighbores of F and mark F as visited.
QUEUE: J
Pop J, Push neighbors of J and mark J as visited.
QUEUE: H
Pop H, Push neighbors of H and mark H as visited.
QUEUE: (empty)
As we can see the queue is empty, that means we’ve visited all nodes in the graph.
We start from vertex A, the DFS algorithm starts by putting it in the Visited list and putting all its adjacent vertices
in the stack. So, put A in the visited list and B, C, D in the stack.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 30 of 31

A Visited
B C D Stack
Now, in the top of the stack we have B
So, Set B as visited and put G in the stack. [As C is already in the stack, we won’t put it again]
A B Visited
G C D Stack
Now, in the top of the stack we have G
So, Set G as visited and put F in the stack. [As C is already in the stack, we won’t put it again]
A B G Visited
F C D Stack
Now, in the top of the stack we have F
So, Set F as visited and put E, J in the stack. [As C is already in the stack, we won’t put it again]
A B G F Visited
E J C D Stack
Now, in the top of the stack we have E
So, Set E as visited. [As C is already in the stack, we won’t put it again]
A B G F E Visited
J C D Stack
Now, in the top of the stack we have J
So, Set J as visited and put H in the stack.
A B G F E J Visited
H C D Stack
Now, in the top of the stack we have H
So, Set H as visited.
A B G F E J H Visited
C D Stack
Now, in the top of the stack we have C
So, Set C as visited.
A B G F E J H C Visited
D Stack
Now, in the top of the stack we have D
So, Set D as visited.
A B G F E J H C D
Visited
Stack
After we visit the last element, stack doesn't have any unvisited adjacent nodes, so we have completed the
Depth First Traversal of the graph.
9

4 [sp22]

10 Consider the following graph below, give the linked list representation of the
graph.

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC
Page 31 of 31

Adjacency list of this graph:


A: B,C, D
B: C
C:
D: C, E
E: C
The linked list representation fo this graph will contain two list, Node list and Edge list
Each element in the Node list will be a record of the form:
NODE NEXT ADJ
Each element in the edge list will conrrespond to an edge of graph and will be a record of the form:
DEST LINK
The linked representation of the graph given below:

11

12

The maximum number of edges possible in a single graph with 'n' vertices is nC2 where, nC2 = n(n – 1)/2
BFS DFS
BFS starts traversal from root node and then DFS starts traversal from the root node and
explores the search in the level by level manner explore the search as far as possible from the
root node i.e. depth wise
BFS can be done using queue. DFS can be done using stack
It uses FIFO implementation It uses LIFO implementation
BFS is slower than DFS DFS is faster
13

14

15

CSE-2321 Data Structure Final Term Note M. Nazmul Arefin, Lecturer, CSE, IIUC

You might also like