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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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) 

  1. Add the 1st assignment from the sorted array in a queue/list “selected”
  2. initialize a variable count = 1. 
  3. Current finish time f = arr[0][1] //(finish time of first assignment)
  4. For each remaining assignment in index c:
    1. If the starting time of this assignment is greater or equal to the ending time of previously selected assignment, f
      1. then count++
      2. f=arr[c][1]
      3. Add the start and finish time of the assignment in index c to “selected”
  5. Print count.
  6. Print the queue/list selected 

 

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY