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

solution of week -3

The document provides solutions to an assignment on fundamental algorithms, focusing on hash tables, binary search trees, and dynamic programming. It includes questions about hash table insertion orders, characteristics of dynamic programming, time complexities, and traversal methods for binary search trees. Key answers highlight the importance of balanced structures in maintaining efficiency in sorting and searching operations.
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

solution of week -3

The document provides solutions to an assignment on fundamental algorithms, focusing on hash tables, binary search trees, and dynamic programming. It includes questions about hash table insertion orders, characteristics of dynamic programming, time complexities, and traversal methods for binary search trees. Key answers highlight the importance of balanced structures in maintaining efficiency in sorting and searching operations.
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/ 8

Fundamental Algorithms: Design and Analysis

Solution of Assignment: 3

1. A hash table of length 10 uses open addressing with hash function


h(k) = k mod 10, and linear probing. After inserting 6 values into an
empty hash table, the table is as shown below.

0
1
2 42
3 23
4 34
5 52
6 46
7 33
8
9

Which one of the following choices gives a possible order in which the
key values could have been inserted in the table?

(a) 46, 42, 34, 52, 23, 33


(b) 34, 42, 23, 52, 33, 46
(c) 46, 34, 42, 23, 52, 33
(d) 42, 46, 33, 23, 34, 52

Answer - (c)
Explanation :
- The sequence (A) doesn’t create the hash table as the element 52
appears before 23 in this sequence.
- The sequence (B) doesn’t create the hash table as the element 33
appears before 46 in this sequence.
-The sequence (C) creates the hash table as 42, 23 and 34 appear before
52 and 33, and 46 appears before 33.
- The sequence (D) doesn’t create the hash table as the element 33
appears before 23 in this sequence.

2. Consider we insert the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash ta-
ble with collisions resolved by chaining. Let the table have 9 slots, and
let the hash function be h(k) = k mod 9. Let the slots be numbered
as 0, 1, ..., 8. Then which of the following is FALSE ?

1
(a) 0th slot has no keys
(b) 1st slot has 3 keys
(c) 5th slot has 2 keys
(d) 6th slot has 2 keys

Answer - (c)
Explanation: 5th slot has 1 keys

3. The running time of the . . . . . . . . . . . . . . . . . . . . . . . . search algorithm


depends on the order in which the nodes are inserted.

(a) AVL Tree


(b) Red-Black Tree
(c) Binary Search Tree
(d) Binary Heap Tree

Answer - c) Binary Search Tree ’s


Explanation: The running time of the Binary Search Tree (BST)
search algorithm depends on the order in which nodes are inserted
because this determines the shape of the tree. If the nodes are inserted
in sorted order, the tree becomes skewed (essentially a linked list), and
the search time degrades to O(n). On the other hand, a balanced BST
ensures O(log n) search time.

4. Which of the following is not a characteristic of dynamic programming?

(a) It solves problems by breaking them into smaller subproblems


(b) It stores the results of subproblems to avoid recomputation.
(c) It uses the greedy method for optimization.
(d) It is used to solve problems with overlapping subproblems.

The correct answer is:


c) It uses the greedy method for optimization.
Explanation:

2
Dynamic programming (DP) and the greedy method are different ap-
proaches to solving optimization problems.
- Dynamic Programming Characteristics:
1. Breaks problems into smaller subproblems: DP divides a problem
into smaller, overlapping subproblems.
2. Stores results to avoid recomputation: Known as memoization or
using a table to store intermediate results.
3. Used for problems with overlapping subproblems: DP is particularly
effective when subproblems are reused multiple times.
- Greedy Method: This approach makes the best possible choice at
each step, aiming for a local optimum in the hope that it leads to a
global optimum. Unlike DP, the greedy method doesn’t involve solving
overlapping subproblems or storing intermediate results.
Thus, ”It uses the greedy method for optimization” is not a character-
istic of dynamic programming.

5. Which of the following is the primary characteristic of dynamic pro-


gramming problems?

(a) Optimal substructure.


(b) Greedy decision-making.
(c) Iterative approach.
(d) Divide and conquer.

The correct answer is:


a) Optimal substructure.
Explanation: The primary characteristic of dynamic programming
problems is the presence of optimal substructure. This means that
the solution to a problem can be constructed from the solutions to its
subproblems.
Key Characteristics: 1. Optimal Substructure: If the optimal solution
of a problem can be obtained by combining the optimal solutions of its
subproblems, the problem exhibits optimal substructure. .

3
2. Overlapping Subproblems: DP problems also have overlapping sub-
problems, which are solved multiple times in naive approaches. DP
avoids recomputation by storing these results.
Incorrect Options:
- Greedy decision-making: This is a characteristic of greedy algorithms,
not DP.
- Iterative approach: While DP often uses an iterative method to fill
tables, this is not the defining characteristic.
- Divide and conquer: Though DP involves breaking a problem into
subproblems, the solutions are reused (overlapping subproblems), un-
like divide-and-conquer, where subproblems are independent.
Thus, optimal substructure is the primary characteristic of dynamic
programming problems.

6. Given a hash table T with 92 slots that stores 2024 elements, the load
factor α for T is .

(a) 11
(b) 14
(c) 22
(d) 24

Explanation:
The load factor (α) of a hash table is calculated using the formula:

Number of elements stored


α=
Number of slots in the hash table
Given:
- Number of elements stored (n) = 2024
- Number of slots (m) = 92

2024
α= = 22
92
Correct Answer:

4
c) 22

7. The time complexity of BST sort in the worst case occurs when the
tree becomes:

(a) Balanced.
(b) A full binary tree.
(c) A complete binary tree.
(d) A skewed binary tree.

The correct answer is:


d) A skewed binary tree.
Explanation: The time complexity of BST sort depends on the shape
of the binary search tree (BST).
1. Balanced Tree: If the BST is balanced, the height of the tree is
O(log n), and the sorting algorithm will run in O(n log n).
2. Full Binary Tree: This is a special case of a balanced tree, where all
levels except possibly the last are completely filled. The sorting time
is still O(n log n).
3. Complete Binary Tree: This is also balanced, and sorting time
remains O(n log n).
4. Skewed Binary Tree: When the BST becomes skewed (e.g., all ele-
ments are inserted in sorted order), it degenerates into a linked list. The
height of the tree becomes O(n), and the worst-case time complexity
for sorting is O(n2 ).
Worst-Case Scenario:
The skewed binary tree leads to the worst-case time complexity for
BST sort, making (d) the correct choice.

8. Which of the following traversal methods is used to get a sorted order


of elements in a binary search tree?

(a) Pre-order traversal.


(b) In-order traversal.

5
(c) Post-order traversal.
(d) Level-order traversal.

The correct answer is:


b) In-order traversal.
Explanation: In a binary search tree (BST), the property is that for
any node:

Left Subtree Nodes < Current Node < Right Subtree Nodes.

When performing in-order traversal (Left, Root, Right), the nodes are
visited in ascending order of their values. This makes in-order traversal
the standard method to retrieve elements of a BST in sorted order.
Other Traversal Methods:
1. Pre-order traversal (Root, Left, Right): Produces a sequence based
on root-first exploration, not sorted.
2. Post-order traversal (Left, Right, Root): Produces a sequence based
on root-last exploration, not sorted.
3. Level-order traversal: Visits nodes level by level (breadth-first),
which is not sorted for a BST.
Thus, in-order traversal is used to get elements in sorted order.

9. What data structure can be used to ensure that BST Sort does not
degrade to O(n2 )?

(a) Max-Heap.
(b) Linked List.
(c) AVL Tree.
(d) Binary Search Tree.

The correct answer is:


c) AVL Tree.
Explanation: In a regular Binary Search Tree (BST), if elements
are inserted in a sorted order (ascending or descending), the tree can

6
become skewed, leading to a worst-case time complexity of O(n2 ) for
operations like search, insert, and traversal.
To avoid this degradation, a self-balancing binary search tree like an
AVL Tree can be used.
Characteristics of an AVL Tree:
1. Self-balancing Property: After every insertion or deletion, the tree
ensures that the height difference (balance factor) between the left and
right subtrees of any node is at most 1.
2. Guaranteed Height: The height of an AVL tree is always O(log n),
ensuring O(log n) for insert, delete, and search operations.
3. BST Sort: When used for sorting, the traversal time remains
O(n log n) even in the worst case.
Why Other Options Are Incorrect:
1. Max-Heap: A heap does not maintain a sorted structure; it is de-
signed for priority-based operations.
2. Linked List: A linked list is not suitable for efficient sorting due to
sequential traversal, leading to O(n2 ) in the worst case.
3. Binary Search Tree: A regular BST can degrade to O(n2 ) if not
balanced.
Thus, an AVL Tree ensures that BST sort does not degrade to O(n2 ).

10. What is the advantage of using a self-balancing binary search tree for
sorting?

(a) Reduced space complexity.


(b) Guaranteed O(log n) height.
(c) Simple implementation.
(d) Randomized data structure.

The correct answer is:


b) Guaranteed O(log n) height.
Explanation: A self-balancing binary search tree (e.g., AVL Tree, Red-
Black Tree) maintains a balanced structure by ensuring that the height

7
of the tree remains O(log n), regardless of the order of insertion or
deletion of elements.
Advantages for Sorting:
1. Guaranteed O(log n) Height: - Ensures that operations like insert,
delete, and search are performed in O(log n) time.
- When used for sorting, the in-order traversal retrieves the sorted
elements in O(n), making the overall complexity O(n log n).
2. Avoids Worst-Case Scenarios:
- Unlike an unbalanced binary search tree, a self-balancing tree prevents
degradation to O(n2 ) in cases of skewed structures.
Why Other Options Are Incorrect:
1. Reduced space complexity: Self-balancing trees have similar space
complexity (O(n)) as other binary search trees. This is not a unique
advantage.
2. Simple implementation: Self-balancing trees (e.g., AVL, Red-Black)
are more complex to implement compared to regular BSTs.
3. Randomized data structure: Self-balancing trees are deterministic,
not randomized.
Thus, the key advantage of using a self-balancing binary search tree for
sorting is the guaranteed O(log n) height.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy