Skip to content

Commit e8b99a2

Browse files
singh-shreya6rishabhgarg25699
authored andcommitted
Fixes in Activity Selection in C, C++, Go (jainaman224#871)
1 parent c771736 commit e8b99a2

File tree

3 files changed

+110
-53
lines changed

3 files changed

+110
-53
lines changed
Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1+
/*
2+
The problem statement for Activity Selection is that "Given a set of n activities with their start and finish times, we need to select maximum number of non-conflicting activities that can be performed by a single person, given that the person can handle only one activity
3+
at a time." The Activity Selection problem follows a Greedy approach.
4+
*/
5+
16
#include <stdio.h>
27

3-
void print_activities(int start[], int finish[], int n)
8+
// Function to compute the activities to be chosen
9+
void ActivitySelection(int start[], int finish[], int n)
410
{
5-
printf("Following activities are selected");
11+
printf("Following activities are selected: ");
12+
// Select the first activity
613
int i = 0;
714
printf("\n%d ", i);
815
int j;
9-
for (j = 1; j < n; j++)
16+
// if start time of current activity j is greater than or equal to previous activity chosen, select activity j
17+
for (j = 1; j < n; j++)
1018
{
11-
if (start[j] >= finish[i])
19+
if (start[j] >= finish[i])
1220
{
1321
printf("%d ", j);
1422
i = j;
1523
}
1624
}
1725
}
1826

19-
int main()
27+
// Driver function
28+
int main()
2029
{
21-
int start[] = {1, 3, 1, 5, 8, 6};
22-
int finish[] = {2, 6, 6, 7, 10, 8};
30+
// starting time of activities
31+
int start[] = {1, 3, 1, 5, 6, 8};
32+
// sorted finish time of activities
33+
int finish[] = {2, 6, 6, 7, 8, 10};
34+
// computing size of array
2335
int n = sizeof(start) / sizeof(start[0]);
24-
print_activities(start, finish, n);
36+
// call to Activity Selection function
37+
ActivitySelection(start, finish, n);
2538
return 0;
2639
}
2740

28-
// Output
29-
// Following activities are selected
30-
// 0 1 4
41+
/*
42+
Input:
43+
Start
44+
1 3 1 5 6 8
45+
Finish
46+
2 6 6 7 8 10
47+
48+
Output:
49+
Following activities are selected
50+
0 1 4 5
51+
*/
Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
1-
#include <iostream>
1+
/*
2+
The problem statement for Activity Selection is that "Given a set of n
3+
activities with their start and finish times, we need to select maximum number
4+
of non-conflicting activities that can be performed by a single person, given
5+
that the person can handle only one activity at a time." The Activity Selection
6+
problem follows a Greedy approach.
7+
*/
28

9+
#include <iostream>
310
using namespace std;
411

5-
void print_activities(int start[], int finish[], int n)
12+
// Function to compute the activities to be chosen
13+
void Activity_Selection(int start[], int finish[], int n)
614
{
7-
cout << "Following activities are selected";
15+
cout << "Following activities are selected" << endl;
16+
// Select the first activity
817
int i = 0;
9-
cout << "\n" << i << "\t";
10-
for (int j = 1; j < n; j++)
18+
cout<<i<<" ";
19+
// if start time of current activity j is greater than or equal to previous
20+
// activity chosen, select activity j
21+
for (int j = 1; j < n; j++)
1122
{
12-
if (start[j] >= finish[i])
13-
{
14-
cout << j << "\t";
15-
i = j;
16-
}
23+
if (start[j] >= finish[i])
24+
{
25+
cout << j << " ";
26+
i = j;
27+
}
1728
}
1829
}
1930

20-
int main()
31+
// Driver Function
32+
int main()
2133
{
22-
int start[] = {1, 3, 1, 5, 8, 6};
23-
int finish[] = {2, 6, 6, 7, 10, 8};
34+
// The array of n elements where start[i] denotes starting time of ith activity
35+
int start[] = {1, 3, 1, 5, 6, 8};
36+
// The array of n elements where finish[i] denotes finishing time of ith activity
37+
int finish[] = {2, 6, 6, 7, 8, 10};
2438
int n = sizeof(start) / sizeof(start[0]);
25-
print_activities(start, finish, n);
39+
// Call to activities function
40+
Activity_Selection(start, finish, n);
2641
return 0;
2742
}
2843

29-
// Output
30-
// Following activities are selected
31-
// 0 1 4
44+
/*
45+
Input:
46+
Start
47+
1 3 1 5 6 8
48+
Finish
49+
2 6 6 7 8 10
50+
Output:
51+
Following activities are selected
52+
0 1 4 5
53+
*/
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
1+
/*
2+
The problem statement for Activity Selection is that "Given a set of n activities with their start and finish times, we need to select maximum number of non-conflicting activities that can be performed by a single person, given that the person can handle only one activity
3+
at a time." The Activity Selection problem follows a Greedy approach.
4+
*/
5+
16
package main
27

38
import (
4-
"fmt"
5-
"strconv"
9+
"fmt"
610
)
711

8-
// this algorithm is from wikipage(https://en.wikipedia.org/wiki/Activity_selection_problem)
9-
func activitySelection(startTime []int, finishTime []int) {
10-
// second step is to print first activity from sorted activity
11-
fmt.Println("activity 1")
12-
// do following in sorted activities:
13-
// If the start time of this activity is greater than the finish time of previously
14-
// selected activity then select this activity and print it
15-
preSelected := 0
16-
for i := 1; i < len(finishTime); i++ {
17-
if startTime[i] > finishTime[preSelected] {
18-
fmt.Println("activity", strconv.Itoa(i + 1))
19-
preSelected = i
20-
}
21-
}
12+
// Function to compute the activities to be chosen
13+
func activitySelection(start []int, finish []int, size int) {
14+
// second step is to print first activity from sorted activity
15+
i := 0
16+
fmt.Println("Following activities are selected: ")
17+
fmt.Printf("%d ", i)
18+
// do following in sorted activities:
19+
// If the start time of this activity is greater than the finish time of previously
20+
// selected activity then select this activity and print it
21+
for j := 1; j < size; j++ {
22+
if start[j] >= finish[i] {
23+
fmt.Printf("%d ", j)
24+
i = j
25+
}
26+
}
2227
}
2328

29+
// Driver function
2430
func main() {
25-
// first step is to sort activities by finish time, in order to save time,
26-
// we use an sorted finishTime array
27-
startTime := []int{1, 3, 1, 5, 8, 6}
28-
finishTime := []int{2, 6, 6, 7, 10, 8}
29-
activitySelection(startTime, finishTime)
31+
// The array of n elements where start[i] denotes starting time of ith activity
32+
start := []int{1, 3, 1, 5, 6, 8}
33+
// The array of n elements where finish[i] denotes finish time of ith activity
34+
finish := []int{2, 6, 6, 7, 8, 10}
35+
size := len(finish)
36+
activitySelection(start, finish, size)
3037
}
3138

32-
// Output
33-
// activity 1
34-
// activity 2
35-
// activity 5
39+
/*
40+
Input:
41+
Start
42+
1 3 1 5 6 8
43+
Finish
44+
2 6 6 7 8 10
45+
46+
Output:
47+
Following activities are selected
48+
0 1 4 5
49+
*/

0 commit comments

Comments
 (0)