Algorithm U2 Answer Key
Algorithm U2 Answer Key
2 Marks
1. What is Brute force approach of problem solving? Give a example.
Ans: Brute force approach of problem solving: The brute force approach is a
problem-solving strategy that involves trying every possible solution until the correct
one is found. It is a straightforward and intuitive method, but it can be inefficient for
large problems due to its exponential time complexity.
Example: Finding the shortest path between two cities in a road network. or
To find the largest number in a list of numbers.
10.What do you mean by convex hull problem? Write its time complexity.
Ans: Convex hull problem: The convex hull problem is to find the convex hull of a set
of points.
Time complexity:
Worst case: O(n^2)
Average case: O(n log n)
13.What do you mean by Travelling Salesman Problem? Write its time complexity.
Ans: The travelling salesman problem (TSP) is an optimization problem in which a
salesman visits a set of cities and returns to the starting city while minimizing the
total distance travelled.
Time complexity:
• Brute force: O(n!)
• Held-Karp: O(2^n)
14.What do you mean by Job assignment problem? Write its time complexity.
Ans: The job assignment problem is a classic optimization problem in computer
science. The problem is to assign a set of n jobs to n machines in such a way that the
total completion time is minimized.
Time complexity:
• Brute force: O(n!)
• Hungarian algorithm: O(n^3)
Long Answer Questions (THREE, FOUR OR FIVE Marks Questions)
1. Write an algorithm to sort N numbers using Selection sort. Derive the number of
operations and time complexity.
Ans: Selection sort algorithm:
Python code:
def selection_sort(numbers):
for i in range(len(numbers) - 1):
min_index = i
for j in range(i + 1, len(numbers)):
if numbers[j] < numbers[min_index]:
min_index = j
numbers[i], numbers[min_index] = numbers[min_index], numbers[i]
Number of operations:
The selection sort algorithm makes n(n-1)/2 comparisons and n-1 swaps.
Time complexity:
The time complexity of the selection sort algorithm is O(n^2).
# Example usage:
numbers = [1, 2, 3, 4, 5]
print(sequential_search(numbers, 3)) # Output: 2
print(sequential_search(numbers, 6)) # Output: -1
The time complexity of sequential search is O(n), where n is the number of elements
in the list. This is because in the worst-case scenario, we have to check every
element in the list. Therefore, while sequential search is easy to implement, it can be
inefficient for large lists. More efficient search algorithms, such as binary search or
hash tables, are often used for larger datasets. However, these require the data to be
sorted or structured in a specific way, unlike sequential search which works on any
list.
Explanation:
1. The algorithm iterates over the text string, starting at the first character.
2. For each position in the text string, the algorithm compares the substring
of the text string that starts at that position to the pattern string.
3. If the substring of the text string matches the pattern string, then the
algorithm returns the position of the substring in the text string.
4. If the algorithm reaches the end of the text string without finding a
match, then the algorithm returns -1.
Limitations:
The brute force string matching algorithm is a simple and intuitive algorithm, but it is
also inefficient for large problems. This is because the time complexity of the
algorithm is O(mn), which means that the algorithm can take a long time to run for
long text strings and pattern strings.
5. Write and explain the algorithm for Closest-Pair Problem. Derive its complexity.
Ans: The closest-pair problem is to find the pair of points in a set of points that are
closest to each other. The Euclidean distance between two points is used to
determine closeness.
algorithm to find the closest pair of points:
Python code:
def closest_pair(points):
if len(points) <= 1:
return None
if closest_left_pair is None:
closest_pair = closest_right_pair
elif closest_right_pair is None:
closest_pair = closest_left_pair
else:
closest_pair = min(closest_left_pair, closest_right_pair, closest_cross_pair,
key=lambda pair: distance(pair[0], pair[1]))
return closest_pair
Explanation:
1. Divide the points into two subsets: The first step is to divide the points
into two subsets based on their x-coordinate. This is done by finding the
midpoint of the sorted list of points and then dividing the list into two
halves.
2. Recursively find the closest pair in each subset: The next step is to
recursively find the closest pair of points in each subset. This is done by
calling the closest_pair function on each subset.
3. Find the closest pair that crosses the median: The third step is to find the
closest pair of points that crosses the median. This is done by iterating
over the points in each subset and finding the smallest distance between
a point in one subset and a point in the other subset.
4. Determine the overall closest pair: The final step is to determine the
overall closest pair of points. This is done by comparing the three closest
pairs found in the previous steps and selecting the pair with the smallest
distance.
Time Complexity:
The time complexity of the closest-pair problem algorithm is O(n^2). This is because
the algorithm iterates over all pairs of points in the worst case. However, in practice,
the algorithm is often much faster than this due to the use of recursion and the early
termination condition when a closer pair is found.
The time complexity of the Gift Wrapping algorithm is O(m * n), where n is the
number of input points and m is the number of output or hull points (m ≤ n). In the
worst case, the time complexity is O(n^2). The worst case occurs when all the points
are part of the Convex Hull. This algorithm is not commonly used due to its high time
complexity. A more efficient algorithm for this problem is the Graham’s scan
algorithm, which has a time complexity of O(n log n).
Please note that the above pseudocode is a simplified version of the algorithm and
may not cover all edge cases. It’s always recommended to refer to a comprehensive
algorithm guide or textbook for full details and implementation guidance. Also, the
actual implementation of the algorithm may vary based on the programming
language used.
9. Find the optimal solution for the Traveling Salesman problem using exhaustive
search method by considering ‘A’ as the starting city.
City A - 20 34 30
City B 20 - 12 35
City C 34 12 - 42
City D 30 35 42 -
We can solve the problem using exhaustive search by enumerating all possible
routes starting and ending at City A:
1. A -> B -> C -> D -> A with total distance = 20 + 12 + 42 + 30 = 104
2. A -> B -> D -> C -> A with total distance = 20 + 35 + 42 + 34 = 131
3. A -> C -> B -> D -> A with total distance = 34 + 12 + 35 + 30 = 111
4. A -> C -> D -> B -> A with total distance = 34 + 42 + 35 + 20 = 131
5. A -> D -> B -> C -> A with total distance = 30 + 35 + 12 + 34 = 111
6. A -> D -> C -> B -> A with total distance = 30 + 42 + 12 + 20 = 104
From the above routes, the route with the shortest total distance is A -> B -> C
-> D -> A and A -> D -> C -> B -> A both with a total distance of 104. Therefore,
the optimal solution to this problem is either of these routes.
10. Find the optimal solution for the Traveling Salesman problem using exhaustive
search method by considering ‘C’ as the starting city.
City A - 10 50 25
City B 10 - 25 45
City C 50 25 - 40
City D 25 45 40 -
We can solve the problem using exhaustive search by enumerating all possible
routes starting and ending at City C:
1. C -> A -> B -> D -> C with total distance = 50 + 10 + 45 + 40 = 145
2. C -> A -> D -> B -> C with total distance = 50 + 25 + 45 + 25 = 145
3. C -> B -> A -> D -> C with total distance = 25 + 10 + 25 + 40 = 100
4. C -> B -> D -> A -> C with total distance = 25 + 45 + 25 + 50 = 145
5. C -> D -> A -> B -> C with total distance = 40 + 25 + 10 + 25 = 100
6. C -> D -> B -> A -> C with total distance = 40 + 45 + 10 + 50 = 145
From the above routes, the route with the shortest total distance is C -> B -> A
-> D -> C and C -> D -> A -> B -> C both with a total distance of 100. Therefore,
the optimal solution to this problem is either of these routes.
11. Find the optimal solution for the Traveling Salesman problem using exhaustive
search method by considering ‘City D’ as the starting city.
City A - 35 15 20
City B 35 - 10 30
City C 15 10 - 25
City D 20 30 25 -
We can solve the problem using exhaustive search by enumerating all possible
routes starting and ending at City D:
From the above routes, the route with the shortest total distance is D -> A -> C
-> B -> D and D -> B -> C -> A -> D both with a total distance of 75. Therefore,
the optimal solution to this problem is either of these routes.
12.Consider the Knapsack problem with the following inputs. Solve the problem
using exhaustive search. Enumerate all possibilities and indicate unfeasible
solutions and Optimal solution. Knapsack total capacity W=15kg
Items A B C D
Weight(kg) 3 5 4 6
Value 36 25 41 34
Ans: The knapsack problem is a classic optimization problem in computer science.
The problem is to find the subset of a set of items that maximizes the total value of
the items subject to a weight constraint.
Input:
Knapsack total capacity W = 15kg
Items
Item Weight (kg) Value
A 3 36
B 5 25
C 4 41
D 6 34
Exhaustive search: Exhaustive search, also known as brute force search, involves
evaluating all possible solutions to a problem until the correct one is found. In the
case of the knapsack problem, this would involve listing out all possible subsets of
the items and then selecting the subset with the highest total value that does not
exceed the weight constraint.
Enumeration of all possibilities: There are a total of 2^4 = 16 possible subsets of the
items, since there are four items and each item can be either included or excluded
from the subset. These subsets are listed below, along with their total weight and
total value:
A 3 36
B 5 25
C 4 41
D 6 34
A+B 8 61
A+C 7 77
A+D 9 70
B+C 9 66
B+D 11 59
C+D 10 75
A+B+C 12 102
A+B+D 14 95
A+C+D 13 111
B+C+D 15 100
A+B+C+D 18 136
Unfeasible solutions: Any subset with a total weight greater than the knapsack
capacity (15kg) is an unfeasible solution. These subsets are highlighted in the table
above.
Optimal solution: The optimal solution is the subset with the highest total value that
does not exceed the knapsack capacity. In this case, the optimal solution is A + C + D,
with a total weight of 13kg and a total value of 111.
Conclusion: Exhaustive search is a simple and intuitive approach to solving the
knapsack problem, but it is impractical for large problems due to its exponential time
complexity. However, for small problems, exhaustive search can be an effective way
to find the optimal solution.
13.Consider the Knapsack problem with the following inputs. Solve the problem
using exhaustive search. Enumerate all possibilities and indicate unfeasible
solutions and optimal solution. Knapsack total capacity W=20kg
Items Item1 Item2 Item3 Item4
Weight 8 10 7 4
Value 40 45 65 30
Ans: The Knapsack problem is a combinatorial optimization problem. It involves
selecting items with given weights and values, and the goal is to maximize the total
value while keeping the total weight under or equal to a given limit.
And a knapsack with a total capacity of 20 kg, we can solve the problem using
exhaustive search by enumerating all possible combinations of items:
1. {} (total weight = 0 kg, total value = 0)
2. {Item1} (total weight = 8 kg, total value = 40)
3. {Item2} (total weight = 10 kg, total value = 45)
4. {Item3} (total weight = 7 kg, total value = 65)
5. {Item4} (total weight = 4 kg, total value = 30)
6. {Item1, Item2} (total weight = 18 kg, total value = 85)
7. {Item1, Item3} (total weight = 15 kg, total value = 105)
8. {Item1, Item4} (total weight = 12 kg, total value = 70)
9. {Item2, Item3} (total weight = 17 kg, total value = 110)
10. {Item2, Item4} (total weight = 14 kg, total value = 75)
11. {Item3, Item4} (total weight = 11 kg, total value = 95)
12. {Item1, Item2, Item3} (total weight = 25 kg, total value = 150) - Unfeasible
13. {Item1, Item2, Item4} (total weight = 22 kg, total value = 115) - Unfeasible
14. {Item1, Item3, Item4} (total weight = 19 kg, total value = 135)
15. {Item2, Item3, Item4} (total weight = 21 kg, total value = 140) - Unfeasible
16. {Item1, Item2, Item3, Item4} (total weight = 29 kg, total value = 180)-Unfeasible
From the above combinations, the combination with the highest total value that
doesn't exceed the total capacity of the knapsack is {Item1, Item3, Item4} with a
total weight of 19 kg and a total value of 135. Therefore, the optimal solution to this
problem is to take items Item1, Item3, and Item4.
14.Consider the Job Assignment problem with the following inputs. Solve the
problem using exhaustive search.Calcualte Minimal total cost to complete to all
jobs one job by each person
JOB1 JOB2 JOB3 JOB4
Person-1 9 2 7 8
Person-2 6 4 3 7
Person-3 5 8 1 8
Person-4 7 6 9 4
Ans: The Job Assignment problem is a classic optimization problem where the goal is
to assign n jobs to n people in a way that minimizes the total cost. Each person can
perform each job with a different cost, and each person must be assigned exactly
one job.
Given the following costs:
JOB1 JOB2 JOB3 JOB4
Person-1 9 2 7 8
Person-2 6 4 3 7
Person-3 5 8 1 8
Person-4 7 6 9 4
We can solve the problem using exhaustive search by enumerating all possible
assignments of jobs to people and calculating the total cost for each assignment.
Here are all the possible assignments and their total costs:
1. {Person-1: JOB1, Person-2: JOB2, Person-3: JOB3, Person-4: JOB4} Total
Cost = 9 + 4 + 1 + 4 = 18
2. {Person-1: JOB1, Person-2: JOB2, Person-3: JOB4, Person-4: JOB3} Total
Cost = 9 + 4 + 8 + 9 = 30
3. {Person-1: JOB1, Person-2: JOB3, Person-3: JOB2, Person-4: JOB4} Total
Cost = 9 + 3 + 8 + 4 = 24
4. {Person-1: JOB1, Person-2: JOB3, Person-3: JOB4, Person-4: JOB2} Total
Cost = 9 + 3 + 8 + 6 = 26
5. {Person-1: JOB1, Person-2: JOB4, Person-3: JOB2, Person-4: JOB3} Total
Cost = 9 + 7 + 8 + 9 = 33
6. {Person-1: JOB1, Person-2: JOB4, Person-3: JOB3, Person-4: JOB2} Total
Cost = 9 + 7 + 1 + 6 = 23
7. {Person-1: JOB2, Person-2: JOB1, Person-3: JOB3, Person-4: JOB4} Total
Cost = 2 + 6 + 1 + 4 = 13
8. {Person-1: JOB2, Person-2: JOB1, Person-3: JOB4, Person-4: JOB3} Total
Cost = 2 + 6 + 8 + 9 = 25
9. {Person-1: JOB2, Person-2: JOB3, Person-3: JOB1, Person-4: JOB4} Total
Cost = 2 + 3 + 5 + 4 = 14
10.{Person-1: JOB2, Person-2: JOB3, Person-3: JOB4, Person-4: JOB1} Total
Cost = 2 + 3 + 8 + 7 = 20
11.{Person-1: JOB2, Person-2: JOB4, Person-3: JOB1, Person-4: JOB3} Total
Cost = 2 + 7 + 5 + 9 = 23
12.{Person-1: JOB2, Person-2: JOB4, Person-3: JOB3, Person-4: JOB1} Total
Cost = 2 + 7 + 1 + 7 = 17
13.{Person-1: JOB3, Person-2: JOB1, Person-3: JOB2, Person-4: JOB4} Total
Cost = 7 + 6 + 8 + 4 = 25
14.{Person-1: JOB3, Person-2: JOB1, Person-3: JOB4, Person-4: JOB2} Total
Cost = 7 + 6 + 8 + 6 = 27
15.{Person-1: JOB3, Person-2: JOB2, Person-3: JOB1, Person-4: JOB4} Total
Cost = 7 + 4 + 5 + 4 = 20
16.{Person-1: JOB3, Person-2: JOB2, Person-3: JOB4, Person-4: JOB1} Total
Cost = 7 + 4 + 8 + 7 = 26
17.{Person-1: JOB3, Person-2: JOB4, Person-3: JOB1, Person-4: JOB2} Total
Cost = 7 + 7 + 5 + 6 = 25
18.{Person-1: JOB3, Person-2: JOB4, Person-3: JOB2, Person-4: JOB1} Total
Cost = 7 + 7 + 8 + 7 = 29
19.{Person-1: JOB4, Person-2: JOB1, Person-3: JOB2, Person-4: JOB3} Total
Cost = 8 + 6 + 8 + 9 = 31
20.{Person-1: JOB4, Person-2: JOB1, Person-3: JOB3, Person-4: JOB2} Total
Cost = 8 + 6 + 1 + 6 = 21
21.{Person-1: JOB4, Person-2: JOB2, Person-3: JOB1, Person-4: JOB3} Total
Cost = 8 + 4 + 5 + 9 = 26
22.{Person-1: JOB4, Person-2: JOB2, Person-3: JOB3, Person-4: JOB1} Total
Cost = 8 + 4 + 1 + 7 = 20
23.{Person-1: JOB4, Person-2: JOB3, Person-3: JOB1, Person-4: JOB2} Total
Cost = 8 + 3 + 5 + 6 = 22
24.{Person-1: JOB4, Person-2: JOB3, Person-3: JOB2, Person-4: JOB1} Total
Cost = 8 + 3 + 8 + 7 = 26
From the above combinations, the combination with the lowest total cost is {Person-
1: JOB2, Person-2: JOB1, Person-3: JOB3, Person-4: JOB4} with a total cost of 13.
Therefore, the optimal solution to this problem is to assign JOB2 to Person-1, JOB1 to
Person-2, JOB3 to Person-3, and JOB4 to Person-4.
15.Consider the Job Assignment problem with the following inputs. Solve the
problem using exhaustive search.Calcualte Minimal total cost to complete to all
jobs one job by each person
JOB1 JOB2 JOB3
Person-1 9 2 7
Person-2 6 4 3
Person-3 5 8 1
Ans: The Job Assignment problem is a classic optimization problem where the goal is
to assign n jobs to n people in a way that minimizes the total cost. Each person can
perform each job with a different cost, and each person must be assigned exactly
one job.
Given the following costs:
JOB1 JOB2 JOB3
Person-1 9 2 7
Person-2 6 4 3
Person-3 5 8 1
We can solve the problem using exhaustive search by enumerating all possible
assignments of jobs to people and calculating the total cost for each assignment.
Here are all the possible assignments and their total costs:
1. {Person-1: JOB1, Person-2: JOB2, Person-3: JOB3} Total Cost = 9 + 4 + 1 = 14
2. {Person-1: JOB1, Person-2: JOB3, Person-3: JOB2} Total Cost = 9 + 3 + 8 = 20
3. {Person-1: JOB2, Person-2: JOB1, Person-3: JOB3} Total Cost = 2 + 6 + 1 = 9
4. {Person-1: JOB2, Person-2: JOB3, Person-3: JOB1} Total Cost = 2 + 3 + 5 = 10
5. {Person-1: JOB3, Person-2: JOB1, Person-3: JOB2} Total Cost = 7 + 6 + 8 = 21
6. {Person-1: JOB3, Person-2: JOB2, Person-3: JOB1} Total Cost = 7 + 4 + 5 = 16
From the above combinations, the combination with the lowest total cost is {Person-
1: JOB2, Person-2: JOB1, Person-3: JOB3} with a total cost of 9. Therefore, the
optimal solution to this problem is to assign JOB2 to Person-1, JOB1 to Person-2, and
JOB3 to Person-3.
Please note that this is a simplified version of the problem and the actual Job
Assignment problem can be much more complex and may require more
sophisticated algorithms such as the Hungarian algorithm or other optimization
techniques. Also, the exhaustive search method used here has a time complexity of
O(n!), which makes it impractical for large inputs. For larger problems, more efficient
algorithms are needed.