ADS
ADS
1. What is Recursion?
Recursion is a programming technique where a function calls itself in order
to solve a problem. It breaks down a problem into smaller, more
manageable sub-problems of the same type. Recursive functions typically
have a base case that stops the recursion and prevents infinite loops.
2. What is base condition in recursion?
The base condition (or base case) in recursion is a condition that stops the
recursive calls. It is a critical part of a recursive function because it defines
the simplest instance of the problem that can be solved directly, without
further recursion. Without a base condition, the function would call itself
indefinitely, leading to a stack overflow error.
3. Why Stack Overflow error occurs in recursion?
A stack overflow error occurs in recursion when the recursive function calls
itself too many times without reaching a base condition. Each function call
consumes a certain amount of stack space, and if the recursion is too
deep (i.e., too many nested calls), it can exceed the stack size limit,
causing a stack overflow.
4. What is the importance of the stopping case in recursive functions?
The stopping case, or base case, is crucial in recursive functions because
it prevents infinite recursion and ensures that the function eventually
terminates. It provides a condition under which the function can return a
result without making further recursive calls, thus allowing the recursion to
unwind and produce a final result.
5. Recursive function for the mathematical function:
//—------------------------------------------------------------------------------------------------------------------------------------------------------------
Page 74:
Question 1: Hash Code for Vehicle Identification Numbers
To generate a good hash code for a vehicle identification number (VIN) such as “9XXX9X9XX99999”:
● Assign values to letters and digits, e.g., ′0′→0,′1′→1,…,′X′→10'0' \rightarrow 0, '1' \rightarrow 1,
\dots, 'X' \rightarrow 10′0′→0,′1′→1,…,′X′→10.
● Sum the weighted values (weight can be positional, like multiplying each character's value by its
index).
● Return the result modulo the table size mmm.
Using the function h(i)=(3i+5)mod 11h(i) = (3i + 5) \mod 11h(i)=(3i+5)mod11, hash the keys: 12, 44, 13,
88, 23, 94, 11, 39, 20, 16, 5.
Steps:
12 (3×12 + 5) % 11 = 10 10 → [12]
44 (3×44 + 5) % 11 = 7 7 → [44]
13 (3×13 + 5) % 11 = 5 5 → [13]
88 (3×88 + 5) % 11 = 3 3 → [88]
11 (3×11 + 5) % 11 = 9 9 → [11]
5 (3×5 + 5) % 11 = 9 9 → [11, 5]
Steps:
1. Calculate h(i)h(i)h(i) and place the key. If occupied, move sequentially until an empty slot is found.
Result:
12 10 10
44 7 7
13 5 5
88 3 3
23 5→6 6
94 10 → 0 0
11 9 9
39 3→4 4
20 7→8 8
16 10 → 1 1
5 9→2 2
Final Hash Table (Linear Probing):
makefile
Copy code
0: 94
1: 16
2: 5
3: 88
4: 39
5: 13
6: 23
7: 44
8: 20
9: 11
10: 12
Given keys = [67, 815, 45, 39, 2, 901, 34] and table size m=11m = 11m=11:
Question 9 & 10
For rehashing with new hash functions, follow the same insertion logic after computing the new
h(i)h(i)h(i).
//—------------------------------------------------------------------------------
Page 105:
To maximize edges:
Thus:
3. Draw an adjacency matrix representation of the undirected graph shown in Figure 14.1.
Since Figure 14.1 is missing, I cannot draw the exact matrix. However, to represent an
undirected graph:
● Use a square matrix AAA where A[i][j]=1A[i][j] = 1A[i][j]=1 if there is an edge between
vertex iii and vertex jjj, otherwise A[i][j]=0A[i][j] = 0A[i][j]=0.
● AAA is symmetric for undirected graphs.
4. Draw an adjacency list representation of the undirected graph shown in Figure 14.1.
Adjacency list:
● Use a list of vertices where each vertex points to its neighbors (connected vertices).
Example:
1 → [2, 3]
2 → [1, 4]
3 → [1]
4 → [2]
Pseudocode in Java:
java
Copy code
void insertEdge(int[][] matrix, int u, int v, int x) {
matrix[u][v] = x; // Add weight x at (u, v)
matrix[v][u] = x; // For undirected graph, mirror the edge
}
Explanation:
a. The graph has 10,000 vertices and 20,000 edges, and it is important to use as little
space as possible.
c. You need to answer getEdge(u, v) as fast as possible, no matter how much space
you use.
//—------------------------------------------------------------------------------
Page 131:
● /user/rt/courses/
● cs016/
● cs252/
● homeworks/
● programs/
● projects/
● papers/
● grades
● homeworks/
○ hw1, hw2, hw3
● programs/
○ pr1, pr2, pr3
d. How many ancestors does node cs016/ have?
Node cs016/ has 1 ancestor: /user/rt/courses/.
● grades
● programs/
● papers/
○ buylow, sellhigh
● demos/
○ market
● From the root /user/rt/courses/ to the deepest leaf node (buylow, sellhigh,
market) → there are 4 levels.