CAT1 (Design and Analysis of Algorithms)
CAT1 (Design and Analysis of Algorithms)
1.Using a language of your choice write a pseudo code for insertion sort consisting of
array A with n item and use the array A [90, 4.65.100,5 ]
function insertionSort(arr) {
let j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = current;
return arr;
Let T(n) be the expected running time of the algorithm on an input of size n.
In the algorithm, we first pick a random pivot and partition the array into two parts.
The partition step takes O(n) time since we need to compare each element with the
pivot.
After partitioning, we recursively call the algorithm on the left and right subarrays,
which have sizes i-1 and n-i, respectively. Therefore, the expected running time for the
recursive calls is T(i-1) and T(n-i).
Finally, we have the merge step, which takes O((i-1)(n-i)) time, as stated in the problem.
Simplifying:
At each level of the recursion tree, we have two recursive calls, each operating on a
subarray of size n/2. The total work done at each level is O(n^2/4) due to the merge
step.
The height of the recursion tree is log(n), and at each level, the total work done is
O(n^2/4). Therefore, the overall expected running time can be approximated as:
where C is a constant.
3.(a)There are n groups of people trying to schedule meetings at the same time, and
there are m meeting rooms available. Each group i (i = 1, 2, ..., n)
has ai people, and each room j (j = 1, 2, ..., m) has a capacity of bj . Group i can be
scheduled to
use room j if and only if ai ≤ bj (i.e., the number of people is no larger than the
capacity of the
room).
Of these n groups, the first k (1 ≤ k ≤ n) groups have high priority and they need to be
scheduled (there will always be at least one way to schedule them). Design an
algorithm that
schedules all the high priority group, and schedules as many other groups as
possible.
algorithm:
1. Sort the high priority groups (first k groups) in non-decreasing order of their number
of people (ai).
If the number of people in the group at `groupPointer` is less than or equal to the
capacity of the room at `roomPointer`, schedule this group in this room.
Increment both `groupPointer` and `roomPointer`. Increment `scheduledGroups`
by 1.
3.(b) Describe your algorithm and analyse its running time. Your algorithm should
run in O(n log n + m log m) time.
This algorithm works by iterating through the sorted high priority groups and meeting
rooms simultaneously. It assigns a group to a room if the number of people in the group
is less than or equal to the room's capacity. By sorting both groups and rooms in non-
decreasing order, we ensure that each group is scheduled in the best possible room
based on its size.
The time complexity of this algorithm is O(k log k + m log m), where k is the number of
high priority groups and m is the number of meeting rooms. The sorting steps
contribute to the time complexity, and the subsequent iteration over the groups and
rooms takes linear time.
Analysis:
The algorithm sorts the groups and meeting rooms initially, which takes O(n log n) and
O(m log m) time, respectively, using efficient sorting algorithms like Merge Sort or
Quick Sort.
The subsequent iteration over the groups and rooms takes linear time, as each group
and room is examined at most once. Therefore, the overall time complexity is
dominated by the sorting steps, resulting in O(n log n + m log m) time complexity.
The algorithm efficiently schedules the high priority groups and maximizes the number
of other groups scheduled by utilizing the sorted order of groups and rooms. By
assigning groups to rooms in a way that ensures the room capacity is not exceeded, it
optimally utilizes the available resources.
4).Suppose you are given an m × m checkerboard and a checker. You are allowed to
move the
checker from any square x to a square y on the board as long as square y is directly
above
x, or y is directly to the left of the square directly above x, or y is directly to the right
of
the square directly above x. Every time you make a move from square x to square y,
you
earn p(x, y) points, where p(x, y) is an integer (possibly negative) that is part of the
input.
Give an algorithm that on input the values p(x, y), outputs the maximum score that
can be
achieved by placing the checker on some square on the bottom row and moving the
checker
all the way to some square of the top row. The algorithm should run in polynomial
time
Algorithm:
1. Create a 2D array, `dp`, of size m × m to store the maximum score at each position on
the board. Initialize all elements to negative infinity except for the bottom row, which is
initialized with the respective scores p(x, y).
2. For each row, starting from the second last row up to the top row, do the following:
For each position in the current row, consider the three possible positions in the
row below (directly below, to the left, and to the right).
Update the maximum score at the current position (dp[row][col]) as the sum of
the current score (p(x, y)) plus the maximum score of the three possible positions
in the row below.
3. Once the above steps are completed, the maximum score in the top row represents
the maximum achievable score. Return the maximum score.
Analysis:
The algorithm iterates through each position on the checkerboard, updating the
maximum score based on the three possible positions below. This process is performed
for each row from the second last row up to the top row. As a result, the algorithm has a
time complexity of O(m^2), which is polynomial in the input size.