Docsity

data structure and algorithm notes, Lecture notes of Data Structures and Algorithms

Toggle sidebar
bg1
D
a
t
a
S
tru
c
tur
e
s
&
A
lg
or
i
t
h
ms
C
om
bi
n
ed
E
x
a
m
N
ot
e
s
๎‚‰
D
efi
n
i
t
i
ons
,
E
a
sy
M
e
a
n
i
n
g
s
&
A
lg
or
i
t
h
ms
D
S
A
La
b
๎‚702
C
O
0
C
084๎‚‚
ยท
S
e
m
III
ยท
B
.
T
ech
/
M
BA
T
ech
ยท
A
Y
2026๎‚ˆ27
Pa
rt
T
op
ic
s
0
H
ow
to
us
e
t
he
s
e
not
e
s
ยท
e
x
a
m
p
a
tt
e
rn
ยท
e
xp
e
r
i
m
e
nt
m
a
p
1
B
a
s
ic
s
:
d
a
t
a
stru
c
tur
e
,
a
lg
or
i
t
h
m
,
prop
e
rt
ie
s
,
c
omp
le
x
i
ty
,
a
symptot
ic
not
a
t
i
ons
2
A
rr
a
ys
:
a
dd
r
e
ss
c
a
lc
u
l
a
t
i
on
,
tr
a
v
e
rs
a
l
,
i
ns
e
rt
i
on
,
dele
t
i
on
๎‚
E
xp
1๎‚‚
3
S
e
a
r
chi
n
g
:
li
n
e
a
r
a
n
d
bi
n
a
ry
s
e
a
r
ch
๎‚
E
xp
2๎‚‚
4
I
t
e
r
a
t
i
on
a
n
d
r
ec
urs
i
on
:
f
a
c
tor
i
a
l
,
sum
o
f
a
rr
a
y
,
T
ow
e
r
o
f
H
a
no
i
,
GCD
,
F
ib
on
a
cci
๎‚
E
xp
3๎‚‚
5
S
t
a
ck
:
pus
h
/
pop
/
p
eek
,
li
n
ked
st
a
ck
,
mu
l
t
i
p
le
st
a
ck
s
,
r
e
v
e
rs
e
str
i
n
g
,
un
d
o
,
p
a
r
e
nt
he
s
e
s
,
P
o
li
s
h
not
a
t
i
on
,
c
onv
e
rs
i
on
a
n
d
e
v
a
l
u
a
t
i
on
๎‚
E
xp
4,
5๎‚‚
6
Q
u
e
u
e
:
li
n
e
a
r
,
ci
r
c
u
l
a
r
,
pr
i
or
i
ty
,
de
qu
e
,
a
pp
lic
a
t
i
ons
,
b
a
n
k
qu
e
u
e
,
h
osp
i
t
a
l
bed
s
๎‚
E
xp
6๎‚‚
7
L
i
n
ked
li
sts
:
s
i
n
gl
y
,
ci
r
c
u
l
a
r
,
d
ou
bl
y
,
ci
r
c
u
l
a
r
d
ou
bl
y
,
he
a
de
r
,
po
l
ynom
i
a
l
a
ddi
t
i
on
8
T
r
ee
s
:
t
e
rm
i
no
l
o
g
y
,
typ
e
s
,
r
e
pr
e
s
e
nt
a
t
i
on
,
tr
a
v
e
rs
a
l
s
,
c
onstru
c
t
i
on
,
B
ST
,
he
a
p
๎‚
E
xp
7,
8๎‚‚
9
G
r
a
p
h
s
:
typ
e
s
,
deg
r
ee
,
r
e
pr
e
s
e
nt
a
t
i
on
,
BF
S
,
DF
S
,
topo
l
o
gic
a
l
sort
,
P
r
i
m
,
K
rus
k
a
l
,
D
ijk
str
a
๎‚
E
xp
9๎‚‚
10
S
ort
i
n
g
:
i
ns
e
rt
i
on
,
s
elec
t
i
on
,
m
e
r
ge
,
c
omp
a
r
i
son
๎‚
E
xp
10๎‚‚
11
H
u
ff
m
a
n
c
o
di
n
g
12
La
st
-
m
i
nut
e
r
e
v
i
s
i
on
:
che
a
t
s
hee
t
,
v
i
v
a
qu
e
st
i
ons
,
h
ow
to
wr
i
t
e
a
f
u
ll
-
m
a
r
k
s
a
nsw
e
r
A
pp
.
S
ch
a
um
'
s
๎‚
L
i
ps
ch
ut
z
)
b
oo
k
-
f
orm
a
t
a
lg
or
i
t
h
ms
H
OW
EACH
TOP
IC
I
S
L
AID
OUT
D
efi
n
i
t
i
on
f
or
e
x
a
m
(
g
r
ee
n
)
โ†’
E
a
sy
m
e
a
n
i
n
g
(
bl
u
e
)
โ†’
e
x
a
mp
le
a
n
d
di
a
g
r
a
m
โ†’
a
d
v
a
nt
a
ge
s
/
di
s
a
d
v
a
nt
a
ge
s
a
n
d
diffe
r
e
n
ce
t
a
ble
s
โ†’
A
lg
or
i
t
h
m
(
or
a
n
ge
)
โ†’
D
ry
run
(
g
r
e
y
)
โ†’
c
omp
le
x
i
ty
.
Y
ell
ow
b
ox
e
s
a
r
e
e
x
a
m
po
i
nts
;
r
ed
b
ox
e
s
c
orr
ec
t
m
i
st
a
ke
s
f
oun
d
on
t
he
s
lide
s
.
A
lg
or
i
t
h
ms
on
l
y
โ€“
no
c
o
de
.
DSA โ€“ Combined Exam Notes Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download data structure and algorithm notes and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures & Algorithms

Combined Exam Notes ๎‚‰ Definitions, Easy Meanings &

Algorithms

DSA Lab ๎‚ 702 CO 0 C 084 ๎‚‚ ยท Sem III ยท B.Tech / MBA Tech ยท AY 2026 ๎‚ˆ 27

Part Topics

0 How to use these notes ยท exam pattern ยท experiment map

1 Basics: data structure, algorithm, properties, complexity, asymptotic notations

2 Arrays: address calculation, traversal, insertion, deletion ๎‚Exp 1 ๎‚‚

3 Searching: linear and binary search ๎‚Exp 2 ๎‚‚

4 Iteration and recursion: factorial, sum of array, Tower of Hanoi, GCD, Fibonacci ๎‚Exp 3 ๎‚‚

5 Stack: push/pop/peek, linked stack, multiple stacks, reverse string, undo, parentheses, Polish notation, conversion and evaluation ๎‚Exp 4, 5 ๎‚‚

6 Queue: linear, circular, priority, deque, applications, bank queue, hospital beds ๎‚Exp 6 ๎‚‚

7 Linked lists: singly, circular, doubly, circular doubly, header, polynomial addition

8 Trees: terminology, types, representation, traversals, construction, BST, heap ๎‚Exp 7, 8 ๎‚‚

9 Graphs: types, degree, representation, BFS, DFS, topological sort, Prim, Kruskal, Dijkstra ๎‚Exp 9 ๎‚‚

10 Sorting: insertion, selection, merge, comparison ๎‚Exp 10 ๎‚‚

11 Huffman coding

12 Last-minute revision: cheat sheet, viva questions, how to write a full-marks answer

App. Schaum's ๎‚Lipschutz) book-format algorithms

HOW EACH TOPIC IS LAID OUT

Definition for exam (green) โ†’ Easy meaning (blue) โ†’ example and diagram โ†’ advantages /

disadvantages and difference tables โ†’ Algorithm (orange) โ†’ Dry run (grey) โ†’ complexity.

Yellow boxes are exam points; red boxes correct mistakes found on the slides. Algorithms

only โ€“ no code.

0. How to Use These Notes

EXAM POINT

Your main exam ๎‚TEE, 50 marks) tests algorithms, not code. For every question:

๎ฒ๎‚” Write the definition.

๎ณ๎‚” Write the algorithm in numbered steps.

๎ด๎‚” Show a dry run (a trace on a small example).

๎ต๎‚” State the time and space complexity.

๎ถ๎‚” Be ready to explain it in the viva.

You need 20/50 to pass. All questions come from the 10 lab experiments.

Experiment Topic Part of these notes

Exp 1 Array insertion, deletion, traversal 2

Exp 2 Linear search and binary search 3

Exp 3 Sum of array by iteration and recursion 4

Exp 4 Stack: browser undo, reverse a string 5

Exp 5 Infixโ†’postfix, postfix evaluation, parenthesis matching 5

Exp 6 Simple and circular queue, bank queue, hospital beds (linked list) 6, 7

Exp 7 BST creation, traversals, count nodes, print leaves 8

Exp 8 BST insert, delete, search (student IDs) 8

Exp 9 Graph representation, BFS, DFS 9

Exp 10 Insertion, selection, merge sort + performance 10

Box guide:

Green = definition to write word for word.

Blue = easy meaning.

Orange = algorithm.

Grey = dry run.

Yellow = exam point.

Red = mistake on a slide and its correction.

Priority Topics

Revise if time Threaded trees ยท heaps ยท topological sort ยท Prim, Kruskal, Dijkstra ยท Huffman ยท polynomial addition

1. Basics of Data Structures and Algorithms

1.1 Data Structure

DEFINITION FOR EXAM

A data structure is a systematic way to organize, store and access data so that operations

can be performed efficiently.

EASY MEANING

A data structure is how you arrange data in memory , like arranging books on a shelf so you

can find them quickly.

Common data structures: array, linked list, stack, queue, tree, graph, hash table.

Linear data structure Non-linear data structure

Elements are arranged one after another Elements are arranged in levels or networks

Each element has one next element An element can connect to many elements

Examples: array, linked list, stack, queue Examples: tree, graph

1.1A Classification of Data Structures

DEFINITION FOR EXAM

Data structures are classified as primitive and non-primitive. Non-primitive data structures

are further divided into linear and non-linear , and into static and dynamic.

1.1C Abstract Data Type ๎‚ADT๎‚‚

DEFINITION FOR EXAM

An Abstract Data Type ๎‚ADT๎‚‚ is a data type defined only by its values and the operations

allowed on them , without saying how it is implemented.

EASY MEANING

An ADT tells you what a structure does, not how. A TV remote is an ADT๎‚’ you know the

buttons (operations) but not the circuit inside. A Stack ADT has PUSH, POP, PEEK, isEmpty

and isFull, whether it is built with an array or a linked list.

1.2 Algorithm

DEFINITION FOR EXAM

An algorithm is a finite, ordered sequence of unambiguous instructions that transforms the

input into the required output.

EASY MEANING

An algorithm is a recipe : clear steps that always finish and give an answer.

Five properties of an algorithm (very common viva question):

๎ฒ๎‚” Input โ€“ zero or more values are given.

๎ณ๎‚” Output โ€“ at least one result is produced.

๎ด๎‚” Definiteness โ€“ every step is clear and unambiguous.

๎ต๎‚” Finiteness โ€“ it stops after a finite number of steps.

๎ถ๎‚” Effectiveness โ€“ every step is basic enough to be carried out.

Pseudocode is a language-independent description of an algorithm using words like IF, ELSE,

FOR, WHILE, RETURN.

ALGORITHM MAX(A, B, C)

  1. max โ† A
  2. IF B > max THEN max โ† B
  3. IF C > max THEN max โ† C
  4. RETURN max

Example: A ๎‚› 12, B ๎‚› 25, C ๎‚› 18 ๎‡— max = 25.

1.2A Approaches to Designing an Algorithm

Approach Meaning

Top-down Start with the whole problem and break it into smaller sub-problems (modules) step by step

Bottom-up Design the small basic modules first, then combine them to solve the whole problem

Control structures used in algorithms:

๎ฒ๎‚” Sequence โ€“ steps are executed one after another.

๎ณ๎‚” Decision (selection) ๎‚‰ IF / ELSE chooses between paths.

๎ด๎‚” Repetition (iteration) ๎‚‰ WHILE / FOR repeats steps.

1.2B Timeโ€“Space Trade-off

DEFINITION FOR EXAM

The timeโ€“space trade-off means that an algorithm can often be made faster by using more

memory, or can use less memory at the cost of more time.

Example: storing extra pointers (a doubly linked list) uses more memory but makes deletion faster.

1.3 Time and Space Complexity

DEFINITION FOR EXAM

Time complexity describes how the running time of an algorithm grows as the input size n

increases. Space complexity describes how much extra (auxiliary) memory an algorithm

needs as n increases.

EASY MEANING

Time complexity counts how many steps ; space complexity counts how much extra

memory. We look at how these grow, not at actual seconds.

Best case โ€“ minimum work (e.g. the item is first in the list).

Average case โ€“ expected work.

Worst case โ€“ maximum work (e.g. the item is last, or not present).

Recursion also uses stack memory for its function calls.

Chapter 1 slide examples: student marks stored in an array ๎‚ƒ72, 85, 68, 91, 77 ๎‚„; largest of 3

numbers; linear search may inspect up to 1,000 elements when n ๎‚› 1,000; swapping two variables

with a temporary uses O๎‚ 1 ๎‚‚ space; for n ๎‚› 1,000, an O(nยฒ) algorithm does about 1,000,

operations.

1.5 Viva Questions with Answers ๎‚‰ Basics

Question Answer

What is a data structure? A way to organize, store and access data so that operations can be performed efficiently.

What is an algorithm? A finite, ordered sequence of unambiguous steps that turns input into output.

List the properties of an algorithm.

Input, output, definiteness, finiteness, effectiveness.

Linear vs non-linear? Linear: elements in a sequence (array, stack). Non-linear: hierarchical or network (tree, graph).

What is an ADT? A data type defined by its values and operations, independent of implementation.

What does Big-O tell us? The upper bound on growth, usually the worst-case running time.

Which is faster, O(n log n) or O(nยฒ๎‚‚?

O(n log n).

What is space complexity? The extra memory an algorithm needs as the input size grows.

2. Arrays ๎‚Exp 1 ๎‚‚

2.1 Array

DEFINITION FOR EXAM

An array is a collection of similar (same data type) elements stored in contiguous memory

locations , where each element can be accessed directly using its index.

EASY MEANING

An array is a row of numbered lockers next to each other. If you know the locker number, you

can open it straight away.

DRY RUN / EXAMPLE int marks[5] = {20, 30, 40, 50, 60};

index : 0 1 2 3 4 value : 20 30 40 50 60

Advantages:

Direct (random) access by index in O๎‚ 1 ๎‚‚.

Simple to use; good for a fixed amount of data.

Disadvantages:

Size is fixed in advance.

Inserting or deleting in the middle needs shifting (O(n)).

2.1A Array Terminology

Term Meaning

Element Each value stored in the array

Index (subscript) Position number of an element (starts at 0 in C๎‚‚

Base address Address of the first element

Lower bound ๎‚LB๎‚‚ Smallest index

Upper bound ๎‚UB๎‚‚ Largest index

Length / size Number of elements ๎‚› UB ๎‚ž LB ๎‚ 1

A. Traversal

DEFINITION FOR EXAM

Traversing means visiting every element of the array exactly once to process it.

ALGORITHM Step 1: [INITIALIZATION] SET I = lower_bound Step 2: Repeat Steps 3 to 4 while I <= upper_bound Step 3: Apply Process to A[I] Step 4: SET I = I + 1 [END OF LOOP] Step 5: EXIT

Time: O(n).

B. Insertion at a Given Position

EASY MEANING

To make space at position POS, move every element from the end one place to the right, then

put the new value in the gap. Start from the end so no value gets overwritten.

ALGORITHM INSERT(A, N, POS, VAL) Step 1: IF POS < 0 OR POS > N โ†’ Write "Invalid position", EXIT Step 2: SET I = N - 1 Step 3: Repeat Steps 4 and 5 while I >= POS Step 4: SET A[I + 1] = A[I] (shift right) Step 5: SET I = I - 1 [END OF LOOP] Step 6: SET A[POS] = VAL Step 7: SET N = N + 1 Step 8: EXIT

Dry run: A ๎‚› ๎‚ƒ10, 20, 30, 40 ๎‚„, insert 25 at POS ๎‚› 2.

DRY RUN / EXAMPLE start [10][20][30][40][ ] N = 4 I = 3: A[4]=A[3] [10][20][30][40][40] shift 40 I = 2: A[3]=A[2] [10][20][30][30][40] shift 30 A[2] = 25 [10][20][25][30][40] N = 5 Number of shifts = N โˆ’ POS = 4 โˆ’ 2 = 2

Insertion at the end (if space is free): N = N + 1; A[Nโˆ’1] = VAL โ†’ O๎‚ 1 ๎‚‚.

C. Deletion from a Given Position

EASY MEANING

Remove the element at POS, then move every element after it one place to the left to close

the gap.

ALGORITHM DELETE(A, N, POS) Step 1: IF POS < 0 OR POS >= N โ†’ Write "Invalid position", EXIT Step 2: SET ITEM = A[POS] Step 3: SET I = POS Step 4: Repeat Steps 5 and 6 while I <= N - 2 Step 5: SET A[I] = A[I + 1] (shift left) Step 6: SET I = I + 1 [END OF LOOP] Step 7: SET N = N - 1 Step 8: EXIT

Dry run: A ๎‚› ๎‚ƒ10, 20, 30, 40, 50 ๎‚„, delete POS ๎‚› 2.

DRY RUN / EXAMPLE start [10][20][30][40][50] ITEM = 30 I = 2: A[2]=A[3] [10][20][40][40][50] I = 3: A[3]=A[4] [10][20][40][50][50] N = 4 [10][20][40][50]

SLIDE / BOOK CORRECTION

The Unit-2 Arrays slide loops while I <= Nโˆ’1 , which reads one cell past the data. The

correct bound is I ๎‚™ N ๎‚ž 2 , as in the Chapter 1 slide.

Operation Best Worst

Access A[i] O๎‚ 1 ๎‚‚ O๎‚ 1 ๎‚‚

Traverse O(n) O(n)

Insert O๎‚ 1 ๎‚‚ at end O(n) at beginning

Delete O๎‚ 1 ๎‚‚ at end O(n) at beginning

Practice questions from the slides: position of the smallest of n numbers; mean of n numbers;

second largest of n numbers.

2.4 Viva Questions with Answers ๎‚‰ Arrays

Question Answer

Why can any array element be accessed in O๎‚ 1 ๎‚‚?

Elements are contiguous, so the address is Base + k ร— size.

Why shift from the end when inserting? Shifting from the front would overwrite values that have not been moved yet.

How many shifts to insert at position pos in an array of n elements?

n โˆ’ pos.

Main disadvantage of an array? Fixed size, and costly insertion and deletion in the middle.

Row-major vs column-major? Row-major stores the matrix row by row; column- major column by column.

3. Searching ๎‚Exp 2 ๎‚‚

DEFINITION FOR EXAM

Searching is the process of finding whether a given value is present in a collection and, if it

is, finding its location. If the value is found the search is successful , otherwise it is

unsuccessful.

3.1 Linear Search

DEFINITION FOR EXAM

Linear (sequential) search compares the value to be searched with every element of the

array one by one, from the first to the last, until a match is found.

EASY MEANING

Like looking for a friend in a line of people: check the first person, then the second, and so

on.

ALGORITHM LINEAR_SEARCH(A, N, VAL) Step 1: [INITIALIZE] SET POS = - Step 2: [INITIALIZE] SET I = 0 Step 3: Repeat Step 4 while I < N Step 4: IF A[I] = VAL SET POS = I PRINT POS Go to Step 6 [END OF IF] SET I = I + 1 [END OF LOOP] Step 5: IF POS = - PRINT "VALUE IS NOT PRESENT IN THE ARRAY" [END OF IF] Step 6: EXIT

Dry run: A ๎‚› ๎‚…10, 8, 2, 7, 3, 4, 9 ๎‚†, VAL ๎‚› 7.

DRY RUN / EXAMPLE I = 0: 10 โ‰  7 I = 1: 8 โ‰  7 I = 2: 2 โ‰  7 I = 3: 7 = 7 โ†’ found, POS = 3

Pass BEG END MID A๎‚ƒMID๎‚„ Action

1 0 5 2 5 5 ๎‚˜ 4 ๎‡— END ๎‚› 1

2 0 1 0 1 1 ๎‚— 4 ๎‡— BEG ๎‚› 1

3 1 1 1 3 3 ๎‚— 4 ๎‡— BEG ๎‚› 2

4 2 1 โ€“ โ€“ BEG ๎‚˜ END ๎‡— not present

Number of comparisons f(n) = logโ‚‚ n ๎‡— O(log n)

For 1,000,000 elements, binary search needs only about 20 comparisons.

Limitations:

The list must be sorted.

You need direct access to the middle element, so it does not work on a linked list.

3.2A Linear Search with a Sentinel ๎‚Schaum)

EASY MEANING

Put the value being searched for at the end of the array (the sentinel). The loop is then sure

to stop, so each step needs only one comparison instead of two.

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.

Worst case n ๎‚ 1 comparisons; average โ‰ˆ (n ๎‚ 1 ๎‚‚/2.

3.3 Difference Between Linear and Binary Search

Basis Linear search Binary search

Data required Sorted or unsorted Must be sorted

Method Checks one by one Halves the range each time

Best case O๎‚ 1 ๎‚‚ O๎‚ 1 ๎‚‚

Basis Linear search Binary search

Worst case O(n) O(log n)

Works on linked list Yes No

Best for Small or unsorted lists Large sorted lists

3.4 Applications of Searching

Finding a record (student, book, account) by its key.

Looking up a word in a dictionary or a name in a phone directory (binary search).

Checking whether an item exists before inserting it.

3.5 Viva Questions with Answers ๎‚‰ Searching

Question Answer When is linear search better? For small or unsorted lists, and for linked lists.

Condition for binary search? The list must be sorted and must allow direct access to the middle element.

How is MID calculated? MID ๎‚› ๎‚BEG ๎‚ END๎‚‚/2 (integer division).

When does binary search stop unsuccessfully?

When BEG ๎‚˜ END.

Comparisons for 1,000,000 elements? About 20 (logโ‚‚ 10 โถ ๎‚ข 20 ๎‚‚.

What is a sentinel? A copy of the search key placed at the end so the loop always stops.