Self Studies

Algorithms Test 3

Result Self Studies

Algorithms Test 3
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0
    Which of following problems cannot be solved using greedy approach?
    Solution

    Greedy algorithm

    • It is a technique to solve the problem and goal is to make optimal solution.


    Example of greedy approach:

    • Minimum Spanning tree ( Prim’s and kruskal’s )
    • Single source shortest path problem ( Dijkastra’s algorithm )
    • Huffman code problem
    • Fractional knapsack problem
    • Job sequencing problem
    • Max flow problem and many more problems can be solved using Greedy method.


    Explanation:

    0-1 knapsack problem cannot be solved by the greedy method because it is enabled to fill the knapsack to full capacity so here greedy algorithm is not optimal. 0-1 knapsack problem is solved by Dynamic programming approach.

    Note:  

    Fractional knapsack problem is different from 0-1 knapsack problem.

  • Question 2
    1 / -0
    What is the efficient asymptotic running time to find the median of a two-sorted array after merging it?
    Solution

    Use the divide and conquer paradigm. Compute medians of both the arrays and compare them. Consider m1 be the median of the f 1st array a1[] and m2 be the median of the 2nd array a2[].  

    If m1 is greater than m2 then median may be present in subarray from the first element of a1[] to m1 or it may be present in subarray from m2 to last element of a2[].

    If m2 is greater than m1 then median may be present in subarray from the first element of a2[] to m2 or it may be present in subarray from m1 to last element of a1[].

    Repeat above process recursively. Hence, it will take O(log n) time.
  • Question 3
    1 / -0
    Recurrence relation for the time complexity of matrix multiplication using simple divide and conquer is:
    Solution

    \( \left[ {\begin{array}{*{20}{c}} A&B\\ C&D \end{array}} \right] \left[ {\begin{array}{*{20}{c}} E&F\\ G&H \end{array}} \right]= \left[ {\begin{array}{*{20}{c}} I&J\\ K&L \end{array}} \right] \)

    Therefore:

    I = AE + BG

    J = AF + BH

    K = CE + DG

    L = CF + DH

    Will need 8 multiplications of subproblems of size n/2 each.

    We also need to spend O(n2) time to add up these results. 

  • Question 4
    1 / -0

    We are given 9 – tasks T1, T2, T3, T4,...... T9 . The execution of each task requires one unit of time. Each task Ti has a profit Pi and a deadline Di. You will get profit Pi if the task Ti is completed before the end of the deadline Di.

    Task

    T1

    T2

    T3

    T4

    T5

    T6

    T7

    T8

    T9

    Profit

    15

    20

    30

    18

    18

    10

    23

    16

    25

    Deadline

    7

    2

    5

    3

    4

    5

    2

    7

    3


    If maximum profit is earned then

    Solution

    The Greedy algorithm for Job sequencing problem with deadline is as follows :

    1) Sort all jobs in decreasing order of profit.
    2) Initialize the result sequence as first job in sorted jobs.
    3) Do following for remaining n – 1 jobs
    a) If the current job can fit in the current result sequence
              without missing the deadline, add current job to the result.
              Else ignore the current job.

    Following the above algorithm :

    Task

    T3

    T9

    T7

    T2

    T4

    T5

    T8

    T1

    T6

    Profit

    30

    25

    23

    20

    18

    18

    16

    15

    10

    Deadline

    5

    3

    2

    2

    3

    4

    7

    7

    5


    Now we can add the task in the following order :

    {T3}

    {T9, T3}

    {T7, T9, T3}

    {T2, T7, T9, T3}

    {T2, T7, T9, T5, T3}

    {T2, T7, T9, T5, T3, T8}

    {T2, T7, T9, T5, T3, T8, T1}

    So task T4 and T6 are left out.
  • Question 5
    1 / -0

    Consider an array nums in which there is only one duplicate number findDuplicate() returns that duplicate number. Arrays.sort(nums) sort the array nums and nums.length gives the length of array.

    Consider the below given block of code which is used   find the duplicate one.

    int findDuplicate(int nums[ ]) {
            int length = nums.length-1;
            if(length == 0)
                return 0;
            Arrays.sort(nums);
            for(int i=0; i<length; i++)
            {
                if(nums[i] == nums[i+1])
                    return nums[i];
            }
            return 0;   
    }

    Which algorithm design technique is followed in the above case to get the specified result?

    Solution

    In the findDuplicate() function sorting is done to get the end result and hence it is the Greedy approach.

    Important Point:

    Since sorting algorithm is not mentioned it cannot be divide and conquer

    Nothing is stored previously to calculate the result and hence not Dynamic Programming

    Exhaustive search (Brute force)

    int findDuplicate(int[ ] nums) {
            int length = nums.length;
            if(length == 0)
                return 0;
            for(int i=0; i<length-1; i++)
            {

             for(int j=i+1; j < length; j++)

                  {
                if(nums[i] == nums[j])
                    return nums[i];

                 }
            }
            return 0;   
        }
    }

  • Question 6
    1 / -0

    Let a one-dimensional array declare and initialize as int Array =  {1, 5,-6, 11, -2, -3, 6, -1, -2, 1, 4, 5, -3, 4, -2}

    The subsequence sum \(X\left( {m,n} \right) = \mathop \sum \nolimits_{x = m}^n Array\left[ x \right]\). Determine the maximum of X(m, n) where 0 ≤ m ≤ n < 15?

    NOTE: Use divide and Conquer Technique

    Solution

    Largest Sum Contiguous subarray is from index 3 to 13

    Maximum subsequence sum = {11, -2, -3, 6, -1, -2, 1, 4, 5, -3, 4} = 20

    Code in C++ to find the maximum contiguous sum of array

    #include <iostream>

    using namespace std;

    int kadane(int arr[], int n)

    {

    int max_so_far = 0;

    int max_ending = 0;

    for (int i = 0; i < n; i++)

    {

    max_ending = max_ending + arr[i];

    max_ending = max(max_ending, 0);

    max_so_far = max(max_so_far, max_ending);

    }

    return max_so_far;

    }

    int main()

    {

    int arr[] = { −5, −10, 6, 3, −1, −2, 13, 4, −9, −1, 4, 12, −3, 0};

    int n = sizeof(arr)/sizeof(arr[0]);

    cout << "largest sum of contiguous sub-array is " << kadane(arr, n);

    return 0;

    }

    Important Point

    There is no significance of Divide and Conquer note.

  • Question 7
    1 / -0
    In a merge sort algorithm, the worst case takes 2048 seconds for an input size of 65536. What is the maximum input size of a problem that can be solved in 16 minutes (chose the close approximate answer)?
    Solution

    Data:

    T1 = 2048 sec = 211 sec

    n1 = 65536 = 216

    T2 = 16 min = 16 × 60 sec

    Formula:

    Time complexity of merge sort is O(nlog2n),

    T = c × n × log2n

    where c is constant

    Calculation:

    T1 = c × n1 × log2 n1

    211 = c × 216 × log2 216

    1 = c × 25 × 16

    \(\therefore c = \frac{1}{{{2^9}}}\)

    T2 = c × n × log2n

    \(16 \times 60 = \frac{1}{{{2^9}}}\;{n_2}{\log _2}{n_2}\)

    nLog2n = 29 × 24 × 22 × 15

    nLog2n = 15 × 215

    ∴ n = 215 = 32768
  • Question 8
    1 / -0

    Consider the table given below consisting of 5 items with their profit and weight associated with it.

    Items

    1

    2

    3

    4

    5

    Profit

    90

    130

    60

    110

    42

    Weight

    20

    60

    20

    50

    15

     

    The weight of the knapsack is 90. Find the maximum profit gain by applying fractional knapsack?

    Solution

    Concept:

    Fractional knapsack uses Greedy approach.

    An item with maximum profit/weight is selected first

    Explanation:

    Items

    1

    2

    3

    4

    5

    Profit

    90

    130

    60

    110

    42

    Weight

    20

    60

    20

    50

    15

    Profit/Weight

    4.5

    2.16

    3

    2.2

    2.8

     

    Profit = Item 1 + Item 3 + Item 4 + fraction part of 4

    Profit = 90 + 60 + 42 + \(35 \times\frac{110}{50}\) = 269

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Selfstudy
Selfstudy
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now