0% found this document useful (0 votes)
67 views

CAT1 (Design and Analysis of Algorithms)

The document describes an algorithm to solve the maximum score checkerboard problem in polynomial time. It uses a dynamic programming approach by storing the maximum score for each position in a 2D array. It iterates through each row from bottom to top, updating each position's maximum score based on the scores of the three positions directly below it. This results in an overall time complexity of O(m^2) where m is the size of the checkerboard, allowing it to run efficiently in polynomial time.

Uploaded by

kelciefay8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

CAT1 (Design and Analysis of Algorithms)

The document describes an algorithm to solve the maximum score checkerboard problem in polynomial time. It uses a dynamic programming approach by storing the maximum score for each position in a 2D array. It iterates through each row from bottom to top, updating each position's maximum score based on the scores of the three positions directly below it. This results in an overall time complexity of O(m^2) where m is the size of the checkerboard, allowing it to run efficiently in polynomial time.

Uploaded by

kelciefay8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

FAITH WAITHIRA WAHOME BIT/2021/84746

CAT 1(DESIGN AND ANALYSIS OF ALGORITHM)

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

// Iterate from the second element to the end of the array

for (let i = 1; i < arr.length; i++) {

let current = arr[i];

// Compare the current element with previous elements

let j = i - 1;

while (j >= 0 && arr[j] > current) {

arr[j + 1] = arr[j];

// Shift elements to the right

j--;

arr[j + 1] = current;

// Place the current element in its correct position

return arr;

// Test with the given array A

const A = [90, 4, 65, 100, 5];

const sortedArray = insertionSort(A);


console.log(sortedArray); // Output: [4, 5, 65, 90, 100]

2).Consider the following algorithm that is similar to quick


sort and quick selections. In particular, it also uses a pivot to partition the array into
two parts,
where the left part contains numbers smaller than the pivot and the right part
contains numbers
larger than the pivot. After recursing on both subarrays, the algorithm uses a MERGE
step on the
left and right part. See the description below:
ALG(A[])
If length(A) == 1 Then return
Pick a random pivot p in array A
Partition A such that A[i] = p, A[1..i-1] contains elements smaller than p
and A[i+1..n] contains elements larger than p
ALG(A[1..i-1])
ALG(A[i+1..n])
MERGE(A[1..i-1], A[i+1..n])
Here the MERGE step takes two arrays as input, and the amount of times it takes is
equal to
the product of the lengths of the two arrays. Show the expected running time of this
algorithm is
Cn2
for some constant C.

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.

Putting it all together, we can write the recurrence relation as:

T(n) = O(n) + T(i-1) + T(n-i) + O((i-1)(n-i))


To simplify the analysis, let's assume that the pivot divides the array into two equal-
sized subarrays on average, i.e., i = n/2. This assumption is reasonable when the pivot
selection is random.

Using this assumption, the recurrence relation becomes:

T(n) = O(n) + T(n/2-1) + T(n/2) + O((n/2-1)(n/2))

Expanding the recurrence relation further:

T(n) = O(n) + T(n/2-1) + T(n/2) + O((n^2/4 - n/2 - n/2 + 1))

Simplifying:

T(n) = T(n/2-1) + T(n/2) + O(n^2/4)

The running time using a recursion tree approach.

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:

T(n) = O(n^2/4) * log(n) = Cn^2

where C is a constant.

Expected Running Time=O(n^2) or Cn^2 for some constant C.

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

2. Sort the meeting rooms in non-decreasing order of their capacities (bj).

3. Initialize two pointers, `groupPointer` and `roomPointer`, both starting at 0.

4. Initialize a variable, `scheduledGroups`, to store the number of groups scheduled.

5. While `groupPointer` is less than k and `roomPointer` is less than m, do the


following:

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

 If the number of people in the group at `groupPointer` is greater than the


capacity of the room at `roomPointer`, move to the next room by incrementing
`roomPointer`.

6. Return `scheduledGroups` as the maximum number of groups scheduled.

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.

 Repeat this process for all positions in the current row.

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.

By utilizing dynamic programming, the algorithm avoids redundant calculations by


storing the maximum score at each position and reusing it when necessary. This
optimization contributes to the efficient polynomial time complexity of the algorithm.

You might also like