Algorithm U3 Answer Key
Algorithm U3 Answer Key
2 marks Questions
1. Define decrease and conquer technique and list any two of its variations.
Ans: Decrease and conquer is a problem-solving technique where a problem is solved
by reducing it to a simpler or smaller instance of the same problem and then solving
the simpler instance.
Variations:
1. Decrease by a constant: In each step, the problem size is reduced by a fixed amount.
2. Decrease by a constant factor: In each step, the problem size is reduced by a fixed
fraction.
11.Define following
a. Tree Edge
b. Cross Edge
c. Back Edge
Ans: --Tree Edge: A tree edge is an edge that connects two nodes in a tree. Tree edges
are directed from a parent node to a child node. In a depth-first search (DFS) tree, a
tree edge is an edge that connects a vertex to an ancestor in the tree.
--Cross Edge: A cross edge is an edge that connects two nodes in a tree that are not
parent-child relationships. Cross edges are not directed. A cross edge is an edge in a
DFS traversal that connects two vertices in different subtrees of the DFS tree and
doesn't create a cycle.
--Back Edge: A back edge is an edge that connects a node to one of its ancestors in a
tree. Back edges are directed. A back edge is an edge in a DFS traversal that connects a
vertex to an ancestor in the DFS tree and creates a cycle.
19.Write the time complexity for worst, best and average case of Binary Search.
Ans: The time complexity for binary search is:
Worst case: O(log n)
Best case: O(1)
Average case: O(log n)
while stack:
node = stack.pop()
if node not
in visited:
visited.add(node)
for neighbor in graph[node]:
stack.append(neighbor)
while queue:
node = queue.pop(0)
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
queue.append(neighbor)
Note that the traversal order can vary depending on the starting node and the order
in which the neighbors are visited. The above orders are based on starting from
node ‘a’ and visiting the neighbors in the order they were given.
Ans: BFS:
f
b
c
d
a
e
DFS:
f
e
b
d
c
a
Explanation:
BFS: We start at the root node (f) and add all of its neighbors to a queue (b, c). We then
process the queue, removing the first node (b) and adding all of its neighbors to the queue
(d). We repeat this process until the queue is empty.
DFS: We start at the root node (f) and recursively explore all of its descendants. We mark
each node as visited so that we don't revisit it. We continue exploring all of the descendants
of the current node until we reach a node that has no unvisited neighbours. We then
backtrack to the parent node and explore any unvisited neighbors.
In this case, we start at the root node (f) and explore its child node (e). We then mark
e as visited and recursively explore its child node (b). We mark b as visited and recursively
explore its child node (d). We continue this process until we reach a node that has no
unvisited neighbors. We then backtrack to the parent node (b) and explore its other child
node (c). We continue this process until we have explored all of the nodes in the graph.
7. Give any five comparisons among Depth First Search and Breadth First Search.
Ans: Five comparisons between Depth First Search (DFS) and Breadth First Search
(BFS):
o Data Structure: BFS uses a queue to keep track of the next vertex to visit, whereas
DFS uses a stack or can be implemented with recursion.
o Traversal Order: BFS visits all the vertices of a level before going to the next level,
while DFS visits a vertex and then iteratively explores its adjacent vertices before
backtracking.
o Memory Space: DFS uses less memory than BFS because it’s not necessary to store
all the child pointers at each level.
o Cycle Detection: DFS is more suitable for cycle detection in a graph because it
explores vertices as far as possible before backtracking, while BFS is not suitable for
cycle detection.
o Path Finding: BFS is generally better for path finding problems where the shortest
path is required, as it explores vertices in order of their distance from the source,
while DFS is not suitable for such problems.
A topological sorting of this graph is: 5, 4, 1, 2, 0, 3. Note that for any directed edge u -
> v, u comes before v in the ordering.
The topological sorting for a graph is not unique. For the above graph, another
topological sorting could be: 4, 5, 1, 2, 0, 3. This is also a valid topological sorting.
9. Explain DFS-based algorithm to solve the topological sorting problem and also
write topological order for below graph.
Ans: C1 -> C3
C1 -> C5
C3 -> C4
C5 -> C4
C3 -> C2
C4 -> C2
DFS-based algorithm to solve the topological sorting problem:
1. Create a list of nodes that have zero indegree. This is a list of nodes that have no
incoming edges.
2. Iteratively remove the first node from the list and add it to the topological order.
3. For each neighbor of the removed node, decrease its indegree by one.
4. If a neighbor's indegree becomes zero, add it to the list.
5. Repeat steps 2-4 until the list is empty.
Topological order for the graph in the image:
C1
C3
C5
C4
C2
We start by creating a list of nodes that have zero indegree. In this case, the list
contains the node C1. We then remove C1 from the list and add it to the topological
order. Next, we look at the neighbors of C1. C1 has two neighbors, C3 and C5. We
decrease the indegree of C3 and C5 by one. Since C3 and C5 now have an indegree of
zero, we add them to the list.
We then repeat the process of removing nodes from the list and adding them to the
topological order, decreasing the indegree of neighbors along the way.
1 C3, C5 C1
2 C5 C1, C3
3 C5, C4 C1, C3
4 C4 C1, C3, C5
5 C4, C2 C1, C3, C5
Once the list is empty, we have completed the topological sort and the
topological order is the following:
C1
C3
C5
C4
C2
10.Apply the DFS-based algorithm to solve the topological sorting problem for
the following digraphs
Ans: To apply the DFS-based algorithm to solve the topological sorting problem for
the following digraphs in the image:
Digraph A:
```
a -> b
b -> c
c -> d
```
Digraph B:
```
a -> b
b -> c
c -> d
a -> d
We can follow these steps:
1. Create a list of nodes that have zero indegree.
2. Iteratively remove the first node from the list and add it to the topological order.
3. For each neighbor of the removed node, decrease its indegree by one.
4. If a neighbor's indegree becomes zero, add it to the list.
5. Repeat steps 2-4 until the list is empty.
Digraph A:
1. The list of nodes with zero indegree is `a`.
2. Remove `a` from the list and add it to the topological order.
3. `a` has one neighbor, `b`. Decrease the indegree of `b` by one.
4. `b` now has an indegree of zero, so add it to the list.
5. Remove `b` from the list and add it to the topological order.
6. `b` has one neighbor, `c`. Decrease the indegree of `c` by one.
7. `c` now has an indegree of zero, so add it to the list.
8. Remove `c` from the list and add it to the topological order.
9. `c` has one neighbor, `d`. Decrease the indegree of `d` by one.
10. `d` now has an indegree of zero, so add it to the list.
11. Remove `d` from the list and add it to the topological order.
12. The list is now empty, so we have completed the topological sort.
Digraph B:
1. The list of nodes with zero indegree is `a`.
2. Remove `a` from the list and add it to the topological order.
3. `a` has two neighbors, `b` and `d`. Decrease the indegree of `b` and `d` by one.
4. `b` and `d` now have an indegree of zero, so add them to the list.
5. Remove `b` from the list and add it to the topological order.
6. `b` has one neighbor, `c`. Decrease the indegree of `c` by one.
7. `c` now has an indegree of zero, so add it to the list.
8. Remove `c` from the list and add it to the topological order.
9. `c` has one neighbor, `d`. Decrease the indegree of `d` by one.
10. `d` now has an indegree of zero, so add it to the list.
11. Remove `d` from the list and add it to the topological order.
12. The list is now empty, so we have completed the topological sort.
Ans: The source-removal algorithm is a simple and efficient algorithm for solving the
topological sorting problem. It works by repeatedly removing the nodes with zero
indegree from the graph and adding them to the topological order. The algorithm
terminates when the graph is empty or when there are no nodes with zero indegree
left in the graph.
Algorithm:
1. Create a list of nodes with zero indegree.
2. While the list is not empty:
o Remove the first node from the list and add it to the topological order.
o For each neighbor of the removed node, decrease its indegree by one.
o If a neighbor's indegree becomes zero, add it to the list.
3. If the graph is not empty and there are no nodes with zero indegree left in the
graph, then the graph is cyclic and there is no topological order.
Topological order for the graph in the image:
C1
C3
C5
C4
C2
Explanation:
1. The list of nodes with zero indegree is C1.
2. Remove C1 from the list and add it to the topological order.
3. C1 has two neighbors, C3 and C5. Decrease the indegree of C3 and C5 by one.
4. C3 and C5 now have an indegree of zero, so add them to the list.
5. Remove C3 from the list and add it to the topological order.
6. C3 has one neighbor, C4. Decrease the indegree of C4 by one.
7. C4 now has an indegree of zero, so add it to the list.
8. Remove C4 from the list and add it to the topological order.
9. C4 has one neighbor, C2. Decrease the indegree of C2 by one.
10. C2 now has an indegree of zero, so add it to the list.
11. Remove C2 from the list and add it to the topological order.
12. The list is now empty, so we have completed the topological sort.
Therefore, the topological order for the graph in the image is C1, C3, C5, C4, C2.
13.Explain divide and conquer technique of solving problem with diagram.
Ans: The divide and conquer technique is a problem-solving strategy that involves
recursively breaking down a problem into smaller subproblems until they can be
solved directly, and then combining the solutions to the subproblems to solve the
original problem. This technique is often used to solve problems that are too difficult
or time-consuming to solve directly.
Steps in the Divide and Conquer Technique:
1. Divide: Break the problem into smaller subproblems.
2. Conquer: Solve the subproblems recursively.
3. Combine: Combine the solutions to the subproblems to solve the original
problem.
Diagram :
Problem
/ \
/ \
Subproblem 1 Subproblem 2
/ \ / \
/ \ / \
... ... ... ...
Solution 1 Solution 2
\ /
\ /
\ /
\/
Combined Solution
Since f(n) is O(n), which is the same as the worst-case time complexity of the
subproblems, we have Case 2 of the Master theorem. This means that the time
complexity of the recurrence is T(n) = O(n log n).
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
```
Time complexity:
The time complexity of merge sort is O(n log n).
n log n
10 3.32
100 6.64
1,000 9.97
10,000 13.29
100,000 16.61
As you can see, the time complexity of binary search grows very slowly as the
number of elements in the array increases. This makes binary search a very efficient
algorithm for searching large arrays.
Ans: Inorder Traversal: To traverse the binary tree in inorder, we follow these steps:
1. Visit the left subtree.
2. Visit the root node.
3. Visit the right subtree.
Preorder traversal: To traverse the binary tree in preorder, we follow these steps:
1. Visit the root node.
2. Visit the left subtree.
3. Visit the right subtree.
Postorder Traversal: To traverse the binary tree in postorder, we follow these steps:
1. Visit the left subtree.
2. Visit the right subtree.
3. Visit the root node.
Inorder traversal: gdebacf
Preorder traversal: abdgecf
Postorder traversal: gdebfca
21.Explain the Strassen’s algorithm of matrix multiplication and derive the time
complexity.
Ans: Strassen's algorithm is a method for matrix multiplication that is more efficient
than the naive algorithm. It was invented by Volker Strassen in 1969 and is based on
the principle of divide and conquer.
1. Divide the input matrices A and B, and the output matrix C into n/2 x n/2
submatrices:
```
A = | A11 A12 | B = | B11 B12 | C = | C11 C12 |
| | | | | |
| A21 A22 | | B21 B22 | | C21 C22 |
23.Compute 1234 x 2526 using divide and conquer approach for the multiplication of
two large numbers.
Ans: To multiply two large integers using the divide and conquer approach, we can
follow these steps:
1. Divide the integers into smaller subproblems. In this case, we can divide the
numbers 1234 and 2526 into their digit representations.
1234 = 1000 + 200 + 30 + 4
2526 = 2000 + 500 + 20 + 6
2. Recursively solve the subproblems. In this case, we can recursively multiply
each of the digit representations of the two numbers:
1000 * 2000 = 2000000
200 * 500 = 100000
30 * 20 = 600
4 * 6 = 24
3. Combine the solutions to the subproblems. In this case, we can add up the
products of the digit representations.
2000000 + 100000 + 600 + 24 = 2100624
Therefore, 1234 x 2526 = 2100624.
24.Explain the Multiplication two larger integer using divide and conquer approach
and derive the time complexity.
Ans: The divide and conquer approach for multiplication of two large integers is a
recursive algorithm that breaks down the problem into smaller subproblems until they
can be solved directly, and then combines the solutions to the subproblems to solve
the original problem. The time complexity of the algorithm is O(n^1.585), where n is
the number of digits in the two integers.
The divide-and-conquer approach for multiplying two large integers is a recursive
algorithm that works by breaking the problem down into smaller subproblems until
they can be solved directly, and then combining the solutions to the subproblems to
solve the original problem. This approach is also known as Karatsuba multiplication,
named after the Soviet mathematician Anatoliy Karatsuba, who first published the
algorithm in 1960.
Algorithm:
1. Divide the integers into smaller subproblems: Divide the two integers into two
halves of approximately equal size. For example, if we want to multiply the
integers 1234 and 2526, we can divide them into the following halves:
``1234 = 1200 + 34 2526 = 2500 + 26
2. Recursively solve the subproblems: Multiply the two halves of each integer
recursively. In our example, this would involve the following multiplications:
``1200 * 2500 = 3000000 34 * 26 = 884
3. Combine the solutions to the subproblems: Combine the results of the recursive
multiplications to obtain the final product. In our example, this involves the
following steps:
• Compute the product of the first halves of the two integers and the product of
the second halves of the two integers. In our example, these products are 3000000
and 884, respectively.
• Compute the product of the sum of the first halves and the sum of the second
halves. In our example, this product is (1200 + 34) * (2500 + 26) = 3100624.
• Subtract the product of the first and second halves from the product obtained in
the previous step to obtain the final product. In our example, this calculation is
3100624 - 884 = 3100000.
25.Compute 34 x 26 using divide and conquer approach for the multiplication of two
large numbers.
Ans: Using the same steps as in question 23, we can compute 34 x 26 as follows:
1. Divide the integers into smaller subproblems.
34 = 30 + 4
26 = 20 + 6
2. Recursively solve the subproblems.
30 * 20 = 600
4 * 6 = 24
3. Combine the solutions to the subproblems.
600 + 24 = 624
Therefore, 34 x 26 = 624.
Matrix B:
[0 1 0 1]
[2 1 0 4]
[2 0 1 1]
[1 3 5 0]
Matrix B:
B11 = [0 1] B12 = [0 1]
[2 1] [0 4]
B21 = [2 0] B22 = [1 1]
[1 3] [5 0]
P6 = (A12 - A22) * (B21 + B22) = ([2 1] - [3 0]) * ([2 0] + [1 1]) = [-1 1] * [3 1] = [-3 1]
P7 = (A11 - A21) * (B11 + B12) = ([1 0] - [0 1]) * ([0 1] + [0 1]) = [1 -1] * [0 2] = [0 -2]