USE PYTHON LANGUAGE. READ INPUT FROM FILE (NAME THE FILE "input.txt') The problem needs to be solved using Greedy Algorithm Strategy. Problem: Suppose, you have N number of assignments with their time intervals- i.e. starting and ending times. As you are a student, you need to find out how you can finish the maximum number of assignments. Now, Implement a greedy algorithm to find the maximum number of assignments that can be completed by you. The following conditions must be met when writing the code: A student can only work on a single assignment at a time. The input will contain N assignments, and then N lines with the starting time and ending time in the format given below: N S₁ E₁ S₂ E₂ ……. Sn En You have to read input from a file. The output will contain the maximum number of assignments that can be completed followed by the intervals of the selected assignment. Sample input and output is given below "input.txt”. Make sure to try out different input examples to ensure that your code is working for different cases. Include the input file in your zipped submission folder. Input 1: 7 0 4 3 4 1 5 9 10 6 9 2 3 1 2 Input 2: 6 1 5 1 2 2 4 6 8 5 7 8 9 Output 1: 5 1 2 2 3 3 4 6 9 9 10 Output 2: 4 1 2 2 4 5 7 8 9 A greedy algorithm for the above problem is discussed below. //arr[ ][ ] is a 2D array-each index contains the start and finish time of each assignment Assignment_Selection (arr[ ][ ], n) Add the 1st assignment from the sorted array in a queue/list “selected” initialize a variable count = 1. Current finish time f = arr[0][1] //(finish time of first assignment) For each remaining assignment in index c: If the starting time of this assignment is greater or equal to the ending time of previously selected assignment, f then count++ f=arr[c][1] Add the start and finish time of the assignment in index c to “selected” Print count. Print the queue/list selected
USE PYTHON LANGUAGE.
READ INPUT FROM FILE (NAME THE FILE "input.txt')
The problem needs to be solved using Greedy
Problem:
Suppose, you have N number of assignments with their time intervals- i.e. starting and ending times. As you are a student, you need to find out how you can finish the maximum number of assignments. Now, Implement a greedy algorithm to find the maximum number of assignments that can be completed by you. The following conditions must be met when writing the code: A student can only work on a single assignment at a time.
The input will contain N assignments, and then N lines with the starting time and ending time in the format given below:
N
S₁ E₁
S₂ E₂
…….
Sn En
You have to read input from a file. The output will contain the maximum number of assignments that can be completed followed by the intervals of the selected assignment. Sample input and output is given below "input.txt”. Make sure to try out different input examples to ensure that your code is working for different cases. Include the input file in your zipped submission folder.
Input 1: 7 0 4 3 4 1 5 9 10 6 9 2 3 1 2 |
Input 2: 6 1 5 1 2 2 4 6 8 5 7 8 9 |
Output 1: 5 1 2 2 3 3 4 6 9 9 10 |
Output 2: 4 1 2 2 4 5 7 8 9 |
A greedy algorithm for the above problem is discussed below.
//arr[ ][ ] is a 2D array-each index contains the start and finish time of each assignment
Assignment_Selection (arr[ ][ ], n)
- Add the 1st assignment from the sorted array in a queue/list “selected”
- initialize a variable count = 1.
- Current finish time f = arr[0][1] //(finish time of first assignment)
- For each remaining assignment in index c:
- If the starting time of this assignment is greater or equal to the ending time of previously selected assignment, f
- then count++
- f=arr[c][1]
- Add the start and finish time of the assignment in index c to “selected”
- Print count.
- Print the queue/list selected
Step by step
Solved in 4 steps with 2 images