Question

In: Computer Science

Give an example that shows the greedy algorithm for activity selection that picks the activity with...

Give an example that shows the greedy algorithm for activity selection that picks the activity with the smallest start time first (and continues in that fashion) does not solve the activity selection problems correctly.

Please show your work! I will rate quality answers. Thank you.

Solutions

Expert Solution

Consider the following 3 activities sorted by

by finish time.

     start[] = {10, 12, 20};

     finish[] = {20, 25, 30};

A person can perform at most two activities. The

maximum set of activities that can be executed

is {0, 2} [ These are indexes in start[] and

finish[] ]

import java.util.*;

import java.lang.*;

import java.io.*;

class ActivitySelection

{

    public static void printMaxActivities(int s[], int f[], int n)

    {

    int i, j;

     

    System.out.print("Following activities are selected : \n");

    i = 0;

    System.out.print(i+" ");

    for (j = 1; j < n; j++)

    {

         if (s[j] >= f[i])

         {

              System.out.print(j+" ");

              i = j;

          }

     }

    }

    public static void main(String[] args)

    {

    int s[] = {1, 3, 0, 5, 8, 5};

    int f[] = {2, 4, 6, 7, 9, 9};

    int n = s.length;

    printMaxActivities(s, f, n);

    }

     }


Related Solutions

Give a greedy algorithm to make a reasonable attempt at coloring a graph in ?(? +...
Give a greedy algorithm to make a reasonable attempt at coloring a graph in ?(? + ?) time. In pseudo code
Maximum-weight independent set. (a) Prove that the two greedy algorithms, optimal substructure and activity selection are...
Maximum-weight independent set. (a) Prove that the two greedy algorithms, optimal substructure and activity selection are equivalent. That is, they return the same output for a given input. (b) Prove that Greedy 2 returns an optimal solution to the maximum weight independent set problem by application of lemmata.
Describe the A* algorithm. Give one search example: give one graph as example with a heuristic...
Describe the A* algorithm. Give one search example: give one graph as example with a heuristic function for each node; give the start node and goal node; apply the A* algorithm on this problem. Show the steps and sequence of nodes visited in order that leads to the optimal solution.
Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes...
Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes and 5 resource types.
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call...
Recall our linear-time deterministic selection algorithm, SELECT. Give the pseudocode for a modified Quicksort algorithm, call it AWESOME-QUICKSORT , that has worst-case run time O(nlogn). State the cost of your algorithm using a recurrence T(n), and solve then solve this recurrence any way you like (e.g., master method).
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Is quicksort a stable sorting algorithm? If yes, prove it, if not, give an example
Give an example of adverse selection problem and an example of moral hazard problem. Explain how...
Give an example of adverse selection problem and an example of moral hazard problem. Explain how the financial intermediaries can help in avoiding these kind of problems and reducing the cost of asymmetric information.
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT