0% found this document useful (0 votes)
19 views18 pages

Algorithm U2 Answer Key

ada notes

Uploaded by

Vikram Nairy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
19 views18 pages

Algorithm U2 Answer Key

ada notes

Uploaded by

Vikram Nairy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 18

Unit 2

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.

2. List any four importance of Brute force.


Ans: Four importance of Brute force:
Simplicity: The brute force approach is easy to understand and implement.
Guaranteed solution: The brute force approach always finds a solution if one exists.
Benchmarking: The brute force algorithm can be used as a benchmark to compare
the performance of other algorithms.
Educational value: The brute force approach can help students understand the
concept of algorithm efficiency.

2. Write the number of operations and time complexity of selection sort.


Ans: Number of operations and time complexity of selection sort:
o Number of operations: (n^2 - 3n) / 2
o Time complexity: O(n^2)

4. Write the number of operations and time complexity of bubble sort.


Ans: Number of operations and time complexity of bubble sort:
o Number of operations: n^2
o Time complexity: O(n^2)

5. What do you mean by Sequential Search? Write its time complexity.


Ans: Sequential search, also known as linear search, is a simple search algorithm that
searches for a target element in a list by examining each element in the list
sequentially until the target element is found or the end of the list is reached.
6. Write the worst case and average case complexity of Brute force String
Matching.
Ans: Worst case and average case complexity of Brute force String Matching:
• Worst case complexity: O(mn)
• Average case complexity: O(nm/2)

7. Write the number of operations and time complexity of Closest-Pair Problem.


Ans: Number of operations and time complexity of Closest-Pair Problem:
• Number of operations: n(n-1)/2
• Time complexity: O(n^2)

8. Define Convex and Convex hull.


Ans: Convex: A set of points in space is convex if for any two points in the set, the
line segment connecting them lies entirely within the set.
Convex hull: The convex hull of a set of points is the smallest convex set that
contains all the points in the set.

9. List any two example algorithms of the brute force approach.


Ans: Two example algorithms of the brute force approach:
• Brute force string matching
• Exhaustive search

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)

11.Define Exhaustive search.mechansim. Give example.Exhaustive search


Ans: Exhaustive search, also known as brute-force search or backtracking, is a
problem-solving strategy that involves generating and evaluating all possible
solutions to a problem.
Example: Finding the maximum or minimum value in an unsorted list by checking
each element. Or Finding the shortest path in a maze by checking all possible paths.
12.What do you mean by Knapsack Problem? Write its time complexity.
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.
Time complexity:
• Pseudo-polynomial: O(nd)
• Dynamic programming: O(nW)

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

2. Write an algorithm to sort N numbers by applying Bubble sort. Derive the


number of operations and time complexity.
Ans: Bubble sort algorithm:
Python code:
def bubble_sort(numbers):
for i in range(len(numbers) - 1):
for j in range(len(numbers) - 1 - i):
if numbers[j] > numbers[j + 1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
Number of operations:
The bubble sort algorithm makes n^2 comparisons and n(n-1)/2 swaps.
Time complexity:
The time complexity of the bubble sort algorithm is O(n^2).

3. Write and describe the Sequential search algorithm.


Ans: Sequential search, also known as linear search, is a simple method to find a
specific value in a list. It works by starting at the beginning of the list and comparing
each element with the value we’re searching for, until the desired element is found
or the end of the list is reached.
Here is a basic algorithm for sequential search:
1. Start from the leftmost element of the list.
2. Compare the target value with each element in the list.
3. If the target value matches an element, stop and return the index of the
element.
4. If the target value doesn’t match any element, return -1 or a similar value to
indicate that the target value was not found.
Python code:
def sequential_search(list, target):
for i in range(len(list)):
if list[i] == target:
return i # Element found, return its index
return -1 # Element not found

# 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.

4. Write and describe Brute force String Matching Algorithm.


Ans: Brute force string matching is a simple string matching algorithm that searches
for a pattern string in a text string by comparing the pattern string to each substring
of the text string of the same length.
Algorithm:
Python code:
def brute_force_string_matching(text, pattern):
for i in range(len(text) - len(pattern) + 1):
if text[i:i + len(pattern)] == pattern:
return i
return -1

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

# Divide the points into two subsets based on their x-coordinate


mid = len(points) // 2
left_points = points[:mid]
right_points = points[mid:]

# Recursively find the closest pair in each subset


closest_left_pair = closest_pair(left_points)
closest_right_pair = closest_pair(right_points)

# Find the closest pair that crosses the median


closest_cross_pair = closest_cross_pair(points, left_points, right_points, mid)

# Determine the overall closest pair


if closest_left_pair is None and closest_right_pair is None:
return closest_cross_pair

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.

6. Write and explain the algorithm for Convex Hull problem.


Ans: The Convex Hull is the smallest convex polygon that contains all the points of a
given set in the plane. The problem is to compute the Convex Hull of a set of points.
Here is a simple version of the algorithm, known as the Gift Wrapping algorithm or
Jarvis march:
1. Initialize: Start from the leftmost point (or point with minimum x
coordinate value), and let it be P.
2. Search: Do the following while we don’t come back to P. a) The next
point Q is the point such that the triplet PQ and the next point is a
counterclockwise turn. b) Q becomes the new P. c) Add Q to the result.
3. Return: Return the result as the Convex Hull.
Here is the pseudocode for the algorithm:
# Let points[] be the array of points
n = len(points)
if n < 3:
return
# Initialize Result
Result = []

# Start from leftmost point


l=0
for i in range(1, n):
if points[i].x < points[l].x:
l=i
p=l
q=0
# Iterate until we come back to the first point
do
# Add current point to result
Result.append(points[p])
# Search for a point 'q' such that orientation(p, x, q) is
counterclockwise for all points 'x'
q = (p + 1) % n
for i in range(n):
if orientation(points[p], points[i], points[q]) == 2:
q=i
# Now q is the most counterclockwise with respect to p, set p as q for
next iteration
p=q

while p != l # While we don't come to first point

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.

7. Explain Travelling Salesman Problem by exhaustive search with an example.


Ans: The traveling salesman problem (TSP) is a classic optimization problem in
computer science. The problem is to find the shortest path that visits a set of cities
and returns to the starting city. Exhaustive search can be used to solve the TSP, but
this approach is impractical for large problems due to its exponential time
complexity.
How Exhaustive Search Works for TSP: 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 TSP, this would involve listing out all possible paths between
the cities and then selecting the shortest one.
Example:
Consider a simple TSP with three cities: A, B, and C. The exhaustive search algorithm
would list out all possible paths between these cities, as shown below:
1. A-B-C-A
2. A-C-B-A
3. B-A-C-B
4. B-C-A-B
5. C-A-B-C
6. C-B-A-C
Once all possible paths have been listed, the shortest path can be selected. In this
case, the shortest path is A-B-C-A, with a total distance of 6.
The number of possible paths grows very quickly as the number of cities increases.
This is why exhaustive search is impractical for large problems.
Limitations of Exhaustive Search:
While exhaustive search is guaranteed to find the correct solution to the TSP, it is
impractical for large problems due to its exponential time complexity. For example, a
problem with only 10 cities would have 362,880 possible paths, and a problem with
20 cities would have over 2 × 10^18 possible paths.

Conclusion: The traveling salesman problem is a challenging optimization problem


that has been studied extensively by computer scientists. While exhaustive search is
a simple and intuitive approach to solving the TSP, it is impractical for large
problems. More efficient algorithms are needed to solve real-world TSP instances.

8. Write a note on Knapsack Problem with an example.


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.
Given a set of items, each with a weight and a value, and a knapsack with a
maximum weight capacity, the goal is to find the subset of items that can be placed
in the knapsack without exceeding the weight capacity and that maximizes the total
value of the items in the knapsack.
Example: Suppose you have a knapsack with a maximum weight capacity of 10
pounds. You have five items to choose from, with the following weights and values:
Item Weight Value
A 2 6
B 3 7
C 5 8
D 4 4
E 1 3
The goal is to find the subset of these items that can be placed in the knapsack
without exceeding the weight capacity and that maximizes the total value of the
items in the knapsack.
Solution Approaches:
There are several different algorithms that can be used to solve the knapsack
problem. Some of the most common algorithms include:
• Dynamic programming:
• Greedy algorithms:
• Branch and bound:
The knapsack problem has a wide range of applications, including:
• Resource allocation:
• Packing problems:
• Selection problems:

9. Find the optimal solution for the Traveling Salesman problem using exhaustive
search method by considering ‘A’ as the starting city.

Ans: The Traveling Salesman Problem (TSP) is a classic algorithmic problem in


the field of computer science and operations research. It focuses on
optimization. In this problem, a salesman is given a list of cities, and must
determine the shortest route that allows him to visit each city once and return
to his original location.

Given the following distances between cities:

City A City B City C City D

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.

Ans: The Traveling Salesman Problem (TSP) is a classic algorithmic problem in


the field of computer science and operations research. It focuses on
optimization. In this problem, a salesman is given a list of cities, and must
determine the shortest route that allows him to visit each city once and return
to his original location.

Given the following distances between cities:

City A City B City C City D

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.

Ans: The Traveling Salesman Problem (TSP) is a classic algorithmic problem in


the field of computer science and operations research. It focuses on
optimization. In this problem, a salesman is given a list of cities, and must
determine the shortest route that allows him to visit each city once and return
to his original location.

Given the following distances between cities:

City A City B City C City D

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:

1. D -> A -> B -> C -> D with total distance = 20 + 35 + 10 + 25 = 90


2. D -> A -> C -> B -> D with total distance = 20 + 15 + 10 + 30 = 75
3. D -> B -> A -> C -> D with total distance = 30 + 35 + 15 + 25 = 105
4. D -> B -> C -> A -> D with total distance = 30 + 10 + 15 + 20 = 75
5. D -> C -> A -> B -> D with total distance = 25 + 15 + 35 + 30 = 105
6. D -> C -> B -> A -> D with total distance = 25 + 10 + 35 + 20 = 90

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:

Subset Weight (kg) 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.

Given the following items:


| Item | Weight (kg) | Value |
| ---- | ----------- | ----- |
| Item1| 8 | 40 |
| Item2| 10 | 45 |
| Item3| 7 | 65 |
| Item4| 4 | 30 |

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.

You might also like