Question

In: Computer Science

Q1- Given a set S = {1, 2, . . . , n} of players, with...

Q1-

Given a set S = {1, 2, . . . , n} of players, with skill levels

a1, a2, . . . , an,

we need to partition the set of players S into two teams S1 and S2 of equal total skill. The teams do not need to be of equal size. However, every player must be in exactly one team. In other words,

S1S2 = S                                        (1)

S1 ∩   S2 = ∅                                                     (2)

Σ ak= Σ ak                                  (3)

kS1   kS2

  1. Give a brute-force algorithm to solve this problem.

Hint: You must be able to generate subsets of S. To do that, you can use a list of binary digits b = b1b2. . . bnwhere setting b = 100010 means that only players 1 and 5 are in the subset represented by b.

  1. Give time complexity analysis for the worst case.

Hint: How many subsets of S contain a specific element i?

Q2-

Intervals (1, 8), (6, 9), (10, 14), (7, 11), (3, 12). Maximum activity is in the interval (7, 8), where four jobs are active.

Given a set S of n open intervals on R defined by

(a1, b1), (a2, b2), . . . , (an, bn)

Each airepresents the beginning time of a process, and birepresents the termination time of the process. There is an open interval (x, y) where the greatest number of processes is active. What are the x and y of such an interval. Consider the example given in Figure 1.

  1. Give a brute-force algorithm to solve this problem, with a worst-case running time of Θ(n3).
  2. Provide the time complexity analysis.

Solutions

Expert Solution

ANSWER:

Q1-

Let isSubsetSum(arr, n, sum/2) be the function that returns true if
there is a subset of S[1..n] with sum equal to sum/2


The isSubsetSum problem can be divided into two subproblems
a) isSubsetSum() without considering last element
(reducing n to n-1)
b) isSubsetSum considering the last element
(reducing sum/2 by S[n] and n to n-1)
If any of the above the above subproblems return true, then return true.
isSubsetSum (arr, n, sum/2) = isSubsetSum (arr, n-1, sum/2) ||
isSubsetSum (arr, n-1, sum/2 - arr[n-1])

Algorithm->

bool isSubsetSum (S[1..n], sum) //auxiliary algo

{

    // Base Cases

    if sum = 0 then return true;

    if n = 0 and sum != 0 then return false;

    // If last element is greater than sum, then

    // ignore it

    if S[n] > sum then   return isSubsetSum (S, sum);

else   return isSubsetSum (S, sum) or isSubsetSum (S,sum-S[n]);

}

bool findPartition (S[1..n]) //main algo

{

    int sum = 0;

    for i=1 to n do

    sum += S[i];

end

    // If sum is odd, there cannot be two subsets

    // with equal sum

    if sum%2 != 0 then return false;

    return isSubsetSum (S, n, sum/2);

}

Time complexity = O(2^n)

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

using substitution or recursion tree, we get T(n) = O(2^n)

Q2)

Screenshot of code:

output:

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
ll s,e;
};
bool cmp(node x,node y)
{
return x.s<y.s;
}
int main()
{
ll n,i,j;
cout<<"Enter no of intervals: ";
cin>>n;
node a[n];
cout<<"Enter interval ranges: ";
for(i=0;i<n;i++)
scanf("%lld%lld",&a[i].s,&a[i].e);
sort(a,a+n,cmp);
ll gc=0;
ll gst,ge;
for(i=0;i<n;i++)
{
ll st=a[i].s;
ll ed=a[i].e;
ll cnt=1;
for(j=i+1;j<n;j++)
{
if(a[j].s<=a[i].e)
{
cnt++;
st=max(st,a[j].s);
ed=min(ed,a[j].e);
}
else
break;
}
if(cnt>gc)
{
gc=cnt;
gst=st;
ge=ed;
}
}
cout<<"Maximum no of activity is: "<<gc<<endl;
cout<<"Interval is: ("<<gst<<" "<<ge<<")"<<endl;
}

//please upvote//


Related Solutions

Show that {t_(1,s) : 2 ≤ s ≤ n} is a minimal generating set for S_n....
Show that {t_(1,s) : 2 ≤ s ≤ n} is a minimal generating set for S_n. You may use the fact that {t_(r,s) : 1 ≤ r < s ≤ n}, as defined in the outline, generates S_n.
Given the following information: n 1 =4 n1=4 , s 2 1 =0.59 s12=0.59 , n...
Given the following information: n 1 =4 n1=4 , s 2 1 =0.59 s12=0.59 , n 2 =31 n2=31 , s 2 2 =0.93 s22=0.93 , H a : σ 2 1 ≠σ 2 2 Ha: σ12≠σ22 , α=0.1 α=0.1 Step 1 of 2: Determine the critical value(s) of the test statistic. If the test is two-tailed, separate the values with a comma. Round your answer(s) to four decimal places.
The subset-sum problem is defined as follows. Given a set of n positive integers, S =...
The subset-sum problem is defined as follows. Given a set of n positive integers, S = {a1, a2, a3, ..., an} and positive integer W, is there a subset of S whose elements sum to W? Design a dynamic program to solve the problem. (Hint: uses a 2-dimensional Boolean array X, with n rows and W+1 columns, i.e., X[i, j] = 1,1 <= i <= n, 0 <= j <= W, if and only if there is a subset of...
You are given a set S of n real numbers where for each element x ∈...
You are given a set S of n real numbers where for each element x ∈ S, 1 < | x | < 10. You are also given a black box that returns true if there is some combination (x, y, z) ∈ S such that x · y = z. Say the black box returns only z. Write an O(n log n) algorithm to find x and y. In other words, given a set S with n real numbers...
Briefly compare and contrast the S N 1 and S N 2 mechanisms in terms of...
Briefly compare and contrast the S N 1 and S N 2 mechanisms in terms of kinetics, steric effect, nature of the nucleophile, and the nature of the leaving group.
1. prove s(n, k) = s(n − 1, k − 1) − (n − 1)s(n −...
1. prove s(n, k) = s(n − 1, k − 1) − (n − 1)s(n − 1, k). 2. What is ∑n k=0 s(n, k)?
Use mathematical induction to prove that for every integer n >=2, if a set S has...
Use mathematical induction to prove that for every integer n >=2, if a set S has n elements, then the number of subsets of S with an even number of elements equals the number of subsets of S with an odd number of elements. pleases send all detail solution.
Given a set S = {1, -1, i, -i} where i2 = -1 and with multiplication...
Given a set S = {1, -1, i, -i} where i2 = -1 and with multiplication * on this set, complete the following Cayley table: * 1 i -1 -i 1 1 -1 -i i i -1 -1 -i (b) Determine whether, or not, the operation * is a binary operation on S. (c) Is * commutative on S? (d) Investigate the following properties of binary operations for this operation on S: i. Closed ii. Identity iii. Inverse iv. Associativity
Q1. Let {Xn|n ≥ 0} is a Markov chain with state space S = {0, 1,...
Q1. Let {Xn|n ≥ 0} is a Markov chain with state space S = {0, 1, 2, 3} and transition probability matrix (pij ). Let τi = min{n ≥ 1 : Xn = i}, i = 0, 1, 2, 3. Define Bij = {Xτj = i}. Is Bij ∈ σ(X0, · · · , Xτj ) ? Q2. Let {Xn|n ≥ 0} is a Markov chain with state space S = {0, 1, 2, 3}, X0 = 0, and transition...
H0:σ^2 (1) ≤σ^2 (2), HA:σ^2 (1)>σ^2 (2) n(1) = 15, s^2 (1) = 1000; n(2) =...
H0:σ^2 (1) ≤σ^2 (2), HA:σ^2 (1)>σ^2 (2) n(1) = 15, s^2 (1) = 1000; n(2) = 27, s^2 (2) = 689 α=0.05 Reject or do not reject H0?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT