adaAssignment 5
adaAssignment 5
adaAssignment 5
For example, if P = {1, 2, 3} and F = {{1}, {2, 3}}, returning either {1, 2} or
{1, 3} would be an optimal solution. Returning {1, 2, 3} would not be optimal,
as this isn’t the smallest number of pieces that we could return. Returning {1}
is incorrect, as we don’t return any piece of the second set in F.
Construct a counterexample for the following algorithm for this problem: Sort
the pieces in non-increasing order based on the number of sets a piece occurs in.
Process the pieces one at a time and add a piece to the solution if it is part of a
set that doesn’t have any piece in the solution yet.
Remember to:
1
compX123 Assignment 5 s2 2024
to design an algorithm that returns (the locations of) the minimum number of
samples needed to sample all specimens.
Since this problem is a bit difficult to solve in 2D, we’ll look at the slightly
simplified 1D version instead: You’re given an array A of length n consisting of
1-dimensional line segments along the x-axis (i.e., tuples of left and right end-
points) and you can assume that each specimen A[i ] ∈ A is given as [li , ri ]. Your
task is to compute (the locations of) the minimum number of samples we need
to take to ensure that our samples cover all specimens.
Example:
A = [[2, 8], [1, 7], [2, 4], [1, 9]]
A[0]
A[1]
A[2]
A[3]
0 1 2 3 4 5 6 7 8 9 10
In this example, the smallest set of samples is {3}, as taking a single sample
there would ensure we cover all four specimens. The set {1, 4} would also cover
all specimens, but this isn’t the smallest such set. The set {1} doesn’t cover all
specimens, so it isn’t a valid solution.
Design a greedy algorithm that returns the locations of the minimum number
of samples needed to sample all specimens. For full marks, your algorithm
should run in O(n log n) time. Remember to:
2
compX123 Assignment 5 s2 2024
After scouting the Snowy Mountains, we’ve been provided with a string A
of length n encoding what each location can be used for. We’re also given a 3-
character string S, which encodes what the organisation is looking for. You can
treat the strings as arrays containing a single character at each position. We need
to determine the number of different ways to extract S (in order) from A to give
the organisation a good idea of the number of options they have to consider. The
characters of S do not have to be consecutive in A, as long as they occur in the
correct order.
Example:
A = acabdcb
S = acb
In this example, we can extract S in four ways: 1. A[0], A[1], A[3], 2. A[0], A[1], A[6],
3. A[0], A[5], A[6], and 4. A[2], A[5], A[6]. Thus, your algorithm should return 4.
Design a divide and conquer algorithm for the above problem. For full
marks, your algorithm should run in O(n) time. Remember to:
3
compX123 Assignment 5 s2 2024