You are on page 1of 5

Problem 2:

Solution:

A. Algorithm Description “Let the given set of activities be S = {1, 2, 3, …n} and
activities are sorted by finish time. The greedy choice is to always pick activity 1. How
come activity 1 always provides one of the optimal solutions. We can prove it by
showing that if there is another solution B with the first activity other than 1, then there
is also a solution A of the same size with activity 1 as the first activity. Let the first
activity selected by B be k, then there always exist A = {B – {k}} U {1}.
Note that the activities in B are independent and k has the smallest finishing time
among all. Since k is not 1, finish(k) >= finish(1)”
Pseudocode GreedyAlgorithm(int s[], int f[], int n)

int i, j;
      
    print("Following activities are selected : n");
    i = 0;
    System.out.print(i+" ");
    for (j = 1; j < n; j++)
    {
         // If this activity has start time greater than or
         // equal to the finish time of previously selected
         // activity, then select it
         if (s[j] >= f[i])
         {
             print(j+" ");
              i = j;
          }
     }
“Complexity Analysis It takes O(n log n) time if input activities may not be sorted. It takes
O(n) time when it is given that input activities are always sorted.”
Output :
0 3 are seelected
B. Algorithm “Dynamic(int t, int s[], int f[], int n)
 int dp[t + 1];
    memset(dp, 0, sizeof(dp));
 
    for (int i = 1; i < n + 1; i++) {
        for (int t = t; t >= 0; t--) {
 
            if (wt[i - 1] <= w)
                // finding the maximum value
                dp[t] = max(dp[t],
                            dp[t - s[i - 1]] + f[i - 1]);
        }
    }
    return dp[t];
}
Time Complexity: O(N*b). As redundant calculations of states are avoided.”
Output:
TRANSACTION 1 2 3 4 5
1 0 0 0 1 1
2 0 0 0 1 1
3 0 0 0 1 1
4 0 0 0 1 1
5 0 0 0 1 1

Problem 4:
Solution:
A. Algorithm Description “A Simple Solution is to generate all subsets of a given set of
jobs and check individual subsets for the feasibility of jobs in that subset. Keep track of
maximum profit among all feasible subsets. The time complexity of this solution is
exponential.”
Pseudocode “Efficient( Job arr[], int n)
    sort(arr, arr+n, comparison);
  
    int result[n];
    bool slot[n];   
    for (int i=0; i<n; i++)
        slot[i] = false;
    for (int i=0; i<n; i++)
    {
       for (int j=min(n, arr[i].dead)-1; j>=0; j--)
       {
                  if (slot[j]==false)
          {
             result[j] = i;
             slot[j] = true;
             break;
          }
       }
    }
    for (int i=0; i<n; i++)
       if (slot[i])
         cout << arr[result[i]].id << " ";
}”
Time complexity : “The Time Complexity of the above solution is O(n 2). It can be
optimized using Priority Queue(max heap).”
Output: following is maximum profit sequence 1,3,2,4,5
B. Algorithm Description “Use two nested loops. Taking one element at a time, consider
outer loop value as buying  date index and compare it with the every element in the inner
loop which will be considered as selling index date. Keep track of the maximum. This
will be the maximum profit.
Algorithm
for (int i = 0; i <prices.length ; i++) {
for (int j = i; j <prices.length ; j++) {
if(prices[j]>prices[i] && (prices[j]–prices[i]>profit)) {
profit = prices[j] – prices[i];
buyDateIndex = i;
sellDateIndex = j;
.println("Maximum Profit: " + profit + ", buy date index: " + buyDateIndex + ", sell date
index: " + sellDateIndex);”
Output: Maximum Profit: 9, Size: 1, fee: 13

Problem 5:
Solution:
A. Algorithm “GENERATE-BSP-TREE (Node, PolygonSet)
if (IS-CONVEX-SET (PolygonSet))
Tree f BSPTreeNode (PolygonSet)
Divider f CHOOSE-DIVIDING-POLYGON (PolygonSet)
PositiveSet f {}
NegativeSet f {}
for each polygon P1 in PolygonSet
Value f CALCULATE-SIDE (Divider, P1)
if(Value = INFRONT)
PositiveSet f PositiveSet U P1
else if (Value = BEHIND)
NegativeSet f NegativeSet U P1
else if (Value = SPANNING)
Split_Polygon10 (P1, Divider, Front, Back)
PositiveSet f PositiveSet U Front
NegativeSet f NegativeSet U Back
GENERATE-BSP-TREE (Tree.RightChild, PositiveSet)
GENERATE-BSP-TREE (Tree.LeftChild, NegativeSet)
Complexity analysis: O(n2 lg n)
B. Yes it is applicable to index 1 for h = 14 in less than 10 hours.
Most operations on a BST take time proportional to the height of the tree, so it is
desirable to keep the height small. The height must always be at most the ceiling of log2n.
Balanced BSTs are not always so precisely balanced, since it can be expensive to keep a
tree at minimum height at all times; instead, most algorithms keep the height within a
constant factor of this lower bound.
C. The call to CHOOSE-DIVIDING-POLYGON is of order O(n 2 lg n), which dominates the
rest of the function except for the recursive calls. If we assume that the division of the
polygon set is fairly even we can formulate the following function to calculate the bounds
of GENERATE-BSP-TREE: T(n) = 2T(n/2) + O(n2 lg n)”
T(n) ≤ cn lg n for some c ≥ 0 where c = 2
T(n 0 ) ≤ cn’ lg n’ for all n 0 < n
So T(n) = O(n2 lg n)

You might also like