Data Structures and Algorithms Lecture Notes

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/50

flashcard set

Earn XP

Description and Tags

Comprehensive practice flashcards covering Graphs, Trees, AVL & Red-Black Trees, Heaps, Hashing, and String Pattern Matching algorithms from the lecture notes.

51 Terms

1
New cards

How do linear and non-linear data structures differ in terms of arrangement and traversal paths?

In linear data structures, elements are arranged sequentially in a single level and traversal is performed sequentially. In non-linear data structures, elements are arranged hierarchically or interconnected across multiple levels and traversal can follow multiple paths.

2
New cards

How is a graph formally defined mathematically?

A graph is defined as an ordered set G(V,E)G(V, E), where V(G)V(G) represents the set of vertices (or nodes) and E(G)E(G) represents the set of edges (or arcs) used to connect these vertices.

3
New cards

What are the three main types of graphs based on edge properties?

  1. Directed Graph: Every edge has a specific direction shown by arrows, representing a one-way relationship.
  2. Undirected Graph: Edges do not have direction; connections work in both directions.
  3. Weighted Graph: Each edge is assigned a numerical cost or weight representing distance, time, or expense.
4
New cards

What are the primary differences in time and space complexity between an Adjacency Matrix and an Adjacency List for representing graphs?

An Adjacency Matrix requires O(V2)O(V^2) memory, allows edge existence checking in O(1)O(1) time, and finding adjacent vertices takes O(V)O(V) time. An Adjacency List requires less memory for sparse graphs, checking edge existence takes O(degree of vertex)O(\text{degree of vertex}) time, and it is more efficient for traversals.

5
New cards
<p>What graph representation and corresponding graph are shown in the image?</p>

What graph representation and corresponding graph are shown in the image?

An Adjacency Matrix representation for an undirected 4-vertex cycle graph where vertices A,B,C,DA, B, C, D have edges between adjacent pairs.

6
New cards
<p>What traversal method is demonstrated in the diagram, and what is its traversal sequence?</p>

What traversal method is demonstrated in the diagram, and what is its traversal sequence?

Depth-First Search (DFS), producing the traversal sequence ADFCEBA D F C E B.

7
New cards
<p>What traversal method is shown in the diagram, and what is its traversal sequence?</p>

What traversal method is shown in the diagram, and what is its traversal sequence?

Breadth-First Search (BFS), producing the level-by-level traversal sequence ABCDEFA B C D E F.

8
New cards

What underlying data structure does Breadth-First Search (BFS) use, and what does Depth-First Search (DFS) use?

BFS uses a Queue (FIFO list), whereas DFS uses a Stack (LIFO list) or recursion.

9
New cards
<p>What concept is shown in the graph with the highlighted blue edges?</p>

What concept is shown in the graph with the highlighted blue edges?

A Minimum Spanning Tree (MST), connecting all vertices with minimum total edge weight and no cycles.

10
New cards

What are the key properties of a Minimum Spanning Tree (MST)?

  1. It connects all vertices of a connected, undirected, weighted graph.
  2. It contains exactly V−1V - 1 edges (where VV is the number of vertices).
  3. It contains no cycles.
  4. It has the minimum possible total edge weight.
11
New cards

How do Kruskal's and Prim's algorithms differ in edge selection and graph growth?

Kruskal's algorithm sorts all edges and selects edges in increasing order of weight from anywhere in the graph using Disjoint Set (Union-Find) for cycle detection (running in O(Elog⁡E)O(E \log E)). Prim's algorithm starts from a single vertex and greedily grows a single connected tree by selecting the minimum-weight adjacent edge connecting a visited vertex to an unvisited vertex (running in O(V2)O(V^2) with an adjacency matrix).

12
New cards

What is the edge relaxation formula used in Dijkstra's Shortest Path Algorithm, and what weight constraint does it impose?

The relaxation formula is d[v]=min⁡(d[v],d[u]+w(u,v))d[v] = \min(d[v], d[u] + w(u, v)). Dijkstra's algorithm requires all edge weights to be non-negative.

13
New cards

What is the ordering property of a Binary Search Tree (BST)?

For any node, the left subtree contains values smaller than the root node, the right subtree contains values greater than the root node, and both left and right subtrees must also be BSTs.

14
New cards
<p>What data structure is illustrated in the diagram?</p>

What data structure is illustrated in the diagram?

A Binary Search Tree (BST) constructed with root node 5050, left child 3030, right child 7070, and leaf nodes 2020, 4040, 6060, and 8080.

15
New cards

What are the average-case and worst-case time complexities for searching, insertion, and deletion in a Binary Search Tree?

Average case: O(log⁡n)O(\log n). Worst case (when the tree becomes skewed): O(n)O(n).

16
New cards
<p>Which BST deletion case is illustrated in the diagram, and how is it executed?</p>

Which BST deletion case is illustrated in the diagram, and how is it executed?

Case 1: Deletion of a leaf node (node 2020). Its parent's corresponding pointer is set to NULL, and the node is deleted directly.

17
New cards
<p>Which BST deletion case is illustrated in the diagram, and how is it executed?</p>

Which BST deletion case is illustrated in the diagram, and how is it executed?

Case 2: Deletion of a node with a single child (node 7070). The node's parent is connected directly to its child (node 8080), and the node is deleted.

18
New cards
<p>Which BST deletion case is illustrated in the diagram, and how is it executed?</p>

Which BST deletion case is illustrated in the diagram, and how is it executed?

Case 3: Deletion of a node with both children (root node 5050). The node's value is replaced with its in-order successor (the smallest node in the right subtree, 6060), and the in-order successor is deleted from its original position.

19
New cards

What is the difference between a Binary Tree and a Binary Search Tree regarding inorder traversal?

In a Binary Tree, inorder traversal does not necessarily produce sorted data. In a Binary Search Tree, inorder traversal always yields the elements in sorted order.

20
New cards
<p>What multiway search tree index structure is shown in the diagram?</p>

What multiway search tree index structure is shown in the diagram?

A B-Tree Index, featuring a root node (4343), internal nodes (2525, 6161), and leaf nodes (1616, 3434, 5252, 7070).

21
New cards
<p>What tree structure is illustrated in the diagram, and what feature enables its efficient sequential range queries?</p>

What tree structure is illustrated in the diagram, and what feature enables its efficient sequential range queries?

A B+ Tree; all actual values are held in leaf nodes that are linked sequentially, allowing range queries without jumping back up the tree.

22
New cards

What are three major structural differences between a B-Tree and a B+ Tree?

  1. In a B-Tree, keys and records can be stored in internal and leaf nodes; in a B+ Tree, records are stored only at leaf nodes while internal nodes store only keys and child pointers.
  2. In a B-Tree, leaf nodes are not linked; in a B+ Tree, leaf nodes are linked together.
  3. In a B-Tree, search keys appear only once; in a B+ Tree, search keys may be duplicated in internal nodes and leaf nodes.
23
New cards

Why is a B+ Tree preferred over a B-Tree for database indexing?

Because internal nodes store only keys and child pointers, giving them higher fan-out and reducing overall tree height (fewer disk I/O operations). Furthermore, linked leaf nodes make sequential access and range-based queries much faster.

24
New cards

How is the Balance Factor (BF) defined in an AVL tree, and which values represent a balanced state?

Balance Factor is defined as: BF=Height(Left)−Height(Right)\text{BF} = \text{Height(Left)} - \text{Height(Right)} Balanced states are BF=0\text{BF} = 0, BF=+1\text{BF} = +1 (left-heavy), and BF=−1\text{BF} = -1 (right-heavy). Values of +2+2 or −2-2 indicate an unbalanced node.

25
New cards

What single rotations correct Left-Left (LL) and Right-Right (RR) imbalances in an AVL tree?

LL Case is corrected using a single right rotation on the unbalanced node. RR Case is corrected using a single left rotation on the unbalanced node.

26
New cards

What double rotations correct Left-Right (LR) and Right-Left (RL) imbalances in an AVL tree?

LR Case is corrected using: (1) a left rotation on the left child, followed by (2) a right rotation on the unbalanced node. RL Case is corrected using: (1) a right rotation on the right child, followed by (2) a left rotation on the unbalanced node.

27
New cards
<p>What data structure is illustrated in the diagram?</p>

What data structure is illustrated in the diagram?

A Red-Black Tree, displaying black root (1313), red internal nodes (88, 1717, 66, 2222, 2727), and black NIL leaf nodes satisfying the equal black-height property.

28
New cards

What are the primary color and path rules of a Red-Black Tree?

  1. Every node is either Red or Black.
  2. The root node must be Black.
  3. All NIL leaves are Black.
  4. Red Property: A Red node cannot have a Red parent or Red child (no consecutive red nodes).
  5. Black Height Property: Every path from a node to its descendant NIL leaves must contain the same number of Black nodes.
29
New cards
<p>What operations are depicted in the splaying cases diagram?</p>

What operations are depicted in the splaying cases diagram?

Splaying cases in a Splay Tree: the zig-zig rotation (same direction double rotation), the zig-zag rotation (opposite direction double rotation), and the zig rotation (single rotation).

30
New cards

What is the defining mechanism and amortized time complexity of a Splay Tree?

A Splay Tree adjusts its structure by moving the most recently accessed node to the root via rotations (a process called Splaying). It requires no balance factors or colors, and has an amortized time complexity of O(log⁡n)O(\log n) per operation.

31
New cards
<p>What practical application of tree data structures is represented in this diagram?</p>

What practical application of tree data structures is represented in this diagram?

A hierarchical File System, where the topmost node is the Root directory, directories act as parent nodes, and files/subdirectories act as child nodes.

32
New cards

How do Binomial Heaps and Fibonacci Heaps compare in terms of insertion and decrease-key time complexities?

In a Binomial Heap, insertion takes O(log⁡n)O(\log n) and decrease-key takes O(log⁡n)O(\log n). In a Fibonacci Heap, insertion takes O(1)O(1) amortized time and decrease-key takes O(1)O(1) amortized time.

33
New cards

Why is a Fibonacci Heap preferred over a Binary Heap in Dijkstra's shortest path algorithm?

Because its decrease-key operation takes O(1)O(1) amortized time compared to O(log⁡V)O(\log V) in a Binary Heap, improving the overall running time of Dijkstra's algorithm from O((V+E)log⁡V)O((V + E) \log V) to O(Vlog⁡V+E)O(V \log V + E).

34
New cards
<p>What specific data structure is depicted in the diagram, and what is its defining property?</p>

What specific data structure is depicted in the diagram, and what is its defining property?

A Max Heap; every parent node is greater than or equal to its children, meaning root element 5050 has the highest priority.

35
New cards

How do a Heap-Based Priority Queue and an Unsorted Array-Based Priority Queue compare for insertion and deletion of the highest-priority element?

Heap-based: insertion takes O(log⁡n)O(\log n) and deletion takes O(log⁡n)O(\log n). Unsorted array-based: insertion takes O(1)O(1) and deletion takes O(n)O(n).

36
New cards
<p>What algorithm and tree structure are illustrated in the diagram, showing an expected encoding cost of $$2.24$$?</p>

What algorithm and tree structure are illustrated in the diagram, showing an expected encoding cost of 2.242.24?

Huffman Coding / Huffman Tree, which assigns variable-length prefix-free binary codes to characters based on frequency.

37
New cards

What heap is used in Huffman Coding, and what are its total construction time and space complexities?

A Min Heap (Minimum Priority Queue) is used. The total construction time complexity is O(nlog⁡n)O(n \log n) and space complexity is O(n)O(n), where nn is the number of distinct characters.

38
New cards

What are the time and auxiliary space complexities of Heap Sort?

Time complexity is O(nlog⁡n)O(n \log n) in all cases (Best, Average, and Worst); building the heap takes O(n)O(n); auxiliary space complexity is O(1)O(1) (an in-place, unstable sort).

39
New cards

What is a hash collision, and what formula defines a standard modulo division hash function?

A collision occurs when two or more distinct keys produce the same hash table index. The standard division hash function formula is: h(key)=key mod table sizeh(\text{key}) = \text{key} \bmod \text{table size}

40
New cards
<p>Which collision resolution technique is shown in the example table where keys mapping to the same index form a list?</p>

Which collision resolution technique is shown in the example table where keys mapping to the same index form a list?

Separate Chaining, where each hash table location maintains a linked list of elements mapped to that index.

41
New cards
<p>Which open addressing probing method is illustrated in the diagram resolving a collision at index 6?</p>

Which open addressing probing method is illustrated in the diagram resolving a collision at index 6?

Linear Probing, where the next consecutive index is checked using (6+1) mod 7=0(6 + 1) \bmod 7 = 0 to place the element at index 0.

42
New cards

What is the probing formula for Quadratic Probing, and what problem of linear probing does it reduce?

The formula is: hi(k)=(h(k)+i2) mod mh_i(k) = (h(k) + i^2) \bmod m It reduces primary clustering, though secondary clustering can still occur.

43
New cards

What is the probing formula for Double Hashing?

The formula is: hi(k)=(h1(k)+i×h2(k)) mod mh_i(k) = (h_1(k) + i \times h_2(k)) \bmod m where h1(k)h_1(k) is the primary hash function and h2(k)h_2(k) is the secondary hash function providing variable intervals.

44
New cards
<p>What process is illustrated in the diagram when the load factor becomes $$5/7$$ and the table size increases to 17?</p>

What process is illustrated in the diagram when the load factor becomes 5/75/7 and the table size increases to 17?

Rehashing, where existing elements are re-inserted into a new, larger hash table of size 17 using h(x)=x mod 17h(x) = x \bmod 17 to reduce collisions.

45
New cards

How is the load factor (\alpha) of a hash table defined?

The load factor is defined as: α=Number of elementsSize of Hash Table\alpha = \frac{\text{Number of elements}}{\text{Size of Hash Table}}

46
New cards

What are the common hash function design methods mentioned in the notes?

  1. Division Method: h(k)=k mod mh(k) = k \bmod m
  2. Mid-Square Method: Square the key and extract middle digits.
  3. Folding Method: Divide the key into equal parts and sum them.
  4. Digit Extraction Method: Extract specific digit positions.
  5. Multiplication Method: Multiply the key by a constant between 0 and 1, extract fractional part, and multiply by table size.
47
New cards

What is a Dictionary ADT, and how does its access method differ from an Array?

A Dictionary ADT is an abstract data type that stores unique key-value pairs, accessing data by key lookup (often O(1)O(1) using hashing). An Array stores elements in contiguous memory and accesses data using an integer index.

48
New cards

What are the best-case, average-case, and worst-case time complexities of the Brute-Force pattern matching algorithm?

Let nn be text length and mm be pattern length: Best case: O(n)O(n) Average/Worst case: O(nm)O(nm) Space complexity: O(1)O(1).

49
New cards

How does the Boyer-Moore pattern matching algorithm compare characters, and which two heuristics does it use to determine shift distance?

It compares characters from right to left (starting from the end of the pattern). It uses the Bad Character Heuristic and the Good Suffix Heuristic, shifting by the larger valid shift.

50
New cards

What is an LPS array in the Knuth-Morris-Pratt (KMP) algorithm, and what is its value for the pattern 'ABABAC'?

LPS stands for 'Longest Proper Prefix which is also Suffix'. For pattern 'ABABAC', the LPS array is: [0,0,1,2,3,0][0, 0, 1, 2, 3, 0]

51
New cards

What are the total time and space complexities of the Knuth-Morris-Pratt (KMP) algorithm?

Let nn be text length and mm be pattern length: LPS array construction takes O(m)O(m); pattern searching takes O(n)O(n); total time complexity is O(n+m)O(n + m) with O(m)O(m) auxiliary space.