0% found this document useful (0 votes)
10 views3 pages

Document 1

The document discusses algorithms related to sorting, hashing, minimum spanning trees, tree traversal, and the Traveling Salesperson Problem. It provides examples of algorithms for Prim's algorithm, pre-order tree traversal, and a brute force TSP solution. It also analyzes the time complexity of these algorithms.

Uploaded by

mi5a.saad12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Document 1

The document discusses algorithms related to sorting, hashing, minimum spanning trees, tree traversal, and the Traveling Salesperson Problem. It provides examples of algorithms for Prim's algorithm, pre-order tree traversal, and a brute force TSP solution. It also analyzes the time complexity of these algorithms.

Uploaded by

mi5a.saad12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 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).

1. n log(n) + n^2 = O(n^2):


For n >= 1, n log(n) + n^2 <= 2n^2.
Thus, n log(n) + n^2 = O(n^2).

2. n log(n) + n^2 = Omega(n^2):


For n >= 1, n log(n) + n^2 >= n^2.
Thus, n log(n) + n^2 = Omega(n^2).

Therefore, n log(n) + n^2 = Theta(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:

1. Bucket B[0]: [0.1, 0.2]


2. Bucket B[1]: [0.7]

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:

- The algorithm traverses the tree recursively.


- At each node, it prints the value of the node.
- Then, it traverses the left subtree in pre-order.
- Finally, it traverses the right subtree in pre-order.

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.

You might also like