# Golang program to find the minimum number of resources needed to complete all tasks simultaneously

In this Golang article, w will understand how to use interval scheduling Algorithm find the minimum number of resources needed to complete all the tasks simultaneously. Interval scheduling Algorithm is a type of Algorithm used to solve scheduling problems where a set of tasks with start and end times must be scheduled on a limited resource.

## Algorithm

• Step 1 − First, we need to import the fmt and Sort packages. Then initialize the required structs along with the functions.

• Step 2 − The task struct is used to track the start and end time of the task whereas the Len() function calculates the length of the task struct.

• Step 3 − Similarly Swap and Less functions are used to swap the given values and find the smaller value between those provided.

• Step 4 − Initialize the number of resources needed to 1 and the end time of the first task to its end time.

• Step 5 − For each task starting from the second task. If its start time is after the current end time, increment the number of resources needed and update the end time to its end time.

• Step 6 − Return the number of resources needed.

• Step 7 − Now, create the main() function. Inside the main() initialize the array of integers and store values to it. Further call the function created above and pass the array of integers as argument to it.

• Step 8 − Now, store the result returned by the function in a variable and print it on the screen.

## Example

In this program, we first sort the tasks by their end time using the ByEnd type and the sort.Sort function. We then initialize the number of resources needed to 1 and the end time of the first task to its end time. We iterate over the rest of the tasks, and for each task, we check if its start time is after the current end time. If it is, we increment the number of resources needed and update the end time to its end time.

In the main function, we create an Example list of tasks and print the minimum number of resources needed using the minResources function.

package main

import (
"fmt"
"sort"
)

start int
end   int
}

func (t ByEnd) Len() int {
return len(t)
}

func (t ByEnd) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

func (t ByEnd) Less(i, j int) bool {
return t[i].end < t[j].end
}

resources := 1
for i := 1; i < len(tasks); i++ {
resources++
}
}
return resources
}

func main() {
tasks := []Task{{10, 30}, {20, 50}, {40, 17}, {16, 19}, {80, 57}, {75, 76}}
fmt.Println("Minimum number of resources needed:", res)
}


## Output

The given array is: [{10 30} {20 50} {40 17} {16 19} {80 57} {75 76}]
Minimum number of resources needed: 4


## Conclusion

In conclusion, we have seen how to use the Interval Scheduling Algorithm to determine the minimum number of resources needed to complete a set of tasks simultaneously. By sorting the tasks by their end time and then iteratively assigning resources to each task, we can minimize the total number of resources needed while ensuring that each task is completed on time. This Algorithm has a time complexity of O(n log n) due to the sorting operation, where n is the number of tasks.

Updated on: 05-Apr-2023

83 Views 