Document 1
Document 1
a)
To show that n log(n) + n^2 = Theta(n^2) , we need to prove both n log(n) + n^2 = O(n^2) and n
log(n) + n^2 = Omega(n^2).
b)
The solution to the recurrence relation T(N) = 2T(N/2) + N is T(N) = Theta(N log(N)). This is
determined using the master theorem where a = 2, b = 2, and f(N) = N. Since f(N) = Theta(N) =
Theta(N^log_b(a)), we apply the second case of the master theorem.
c)
Following the execution of line 5, array A has the same contents. This is due to the fact that line
5 sorts the array's second half while leaving the first half's items unaffected.
d)
Before line 5, the contents of each bucket are:
e)
After inserting the numbers 1, 18, 11, 4, and 14 into the hash table using the hash function h(k) =
k%6, the contents of the hash table are:
0: [18]
1: [1]
2: [14]
3: []
4: [4]
5: [11]
f)
After inserting 3, 6, 1, 12, and 7 into the hash table using linear probing with h(k) = k%6, the
contents of the hash table are:
0: [6, 12]
1: [1, 7]
2: []
3: [3]
4: []
5: []
Question 3):
a)
The following is how Prim's algorithm accomplishes its goal:
1. It starts with a single vertex in the least spanning tree (Line 2).
2. Up until all vertices are present, edges are added to the tree repeatedly (Lines 3-7).
- Line 5 specifies the minimum-weight edge that joins a vertex inside the tree to a vertex outside
of it.
- Line 6 adds the specified edge's destination vertex to the tree.
- Line 7 adds the chosen edge to the tree.
b)
Here's an algorithm to print the nodes of a tree in pre-order traversal:
PREORDER(root):
1. if root is not null:
2. print root value
3. PREORDER(root->left)
4. PREORDER(root->right)
Explanation:
c)
The Traveling Salesperson Problem (TSP) has exponential time complexity, usually O(n!), where
n is the number of graph vertices. This is due to the fact that brute-force methods check every
conceivable permutation of the vertices, which becomes more and more difficult as n increases.
d)
Here's a shorter version of the brute-force algorithm for the TSP:
BRUTE_FORCE_TSP(graph G):
1. Let min_cost = INFINITY
2. Let best_path = []
3. Generate all permutations of vertices
4. For each permutation p:
5. Calculate the total cost of the tour
6. If cost < min_cost:
7. Update min_cost and best_path
8. Return min_cost and best_path
Justification:
-The algorithm determines the overall cost of each tour by repeatedly going through all
conceivable combinations of the vertices.
-If a tour with a lower cost is discovered, it changes the minimal cost and optimal route.
-Ultimately, it yields the optimal approach and the lowest cost.
e)
f)
The Traveling Salesperson Problem (TSP) decision variant is in NP because, given a certificate
(a tour with a total length less than k), its validity may be checked in polynomial time. It is NP-
complete, nevertheless, since it cannot be known to be in P unless P = NP.