
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































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
0. How to Use These Notes
EXAM POINT
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
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
EASY MEANING
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
1.1C Abstract Data Type ๎ADT๎
DEFINITION FOR EXAM
EASY MEANING
1.2 Algorithm
DEFINITION FOR EXAM
EASY MEANING
ALGORITHM MAX(A, B, C)
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
1.2B TimeโSpace Trade-off
DEFINITION FOR EXAM
1.3 Time and Space Complexity
DEFINITION FOR EXAM
EASY MEANING
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
EASY MEANING
DRY RUN / EXAMPLE int marks[5] = {20, 30, 40, 50, 60};
index : 0 1 2 3 4 value : 20 30 40 50 60
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
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
B. Insertion at a Given Position
EASY MEANING
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 / 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
C. Deletion from a Given Position
EASY MEANING
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 / 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
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
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
3.1 Linear Search
DEFINITION FOR EXAM
EASY MEANING
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 / 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)
3.2A Linear Search with a Sentinel ๎Schaum)
EASY MEANING
ALGORITHM LINEAR(DATA, N, ITEM, LOC)
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
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.