
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for Activity Selection Problem
In this article, we will learn about the solution to the problem statement given below.
Problem statement− We are given n activities with their respective starting and finish times. We need to select the maximum number of activities that can be performed by a single person, provided he works on one activity at a time.
Variable notations
N - Total number of activities
S - An array that contains start time of all activities
F - An array that contains finish time of all activities
Now let’s observe the solution in the implementation below −
# Greedy approach
Example
# maximum number of activities that can be performed by a single person def Activities(s, f ): n = len(f) print ("The selected activities are:") # The first activity is always selected i = 0 print (i,end=" ") # For rest of the activities for j in range(n): # if start time is greator than or equal to that of previous activity if s[j] >= f[i]: print (j,end=" ") i = j # main s = [1, 2, 0, 3, 2, 4] f = [2, 5, 4, 6, 8, 8] Activities(s, f)
Output
The selected activities are: 0 1
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned about how we can make a Python Program for Activity Selection Problem
- Related Articles
- C Program for Activity Selection Problem
- Activity Selection Problem
- Activity Selection Problem (Greedy Algo-1) in C++?
- Python Program for Selection Sort
- Python Program for Subset Sum Problem
- Python Program for 0-1 Knapsack Problem
- Python Program for Number of stopping station problem
- 8086 program for selection sort
- C Program for Selection Sort?
- Selection Sort in Python Program
- Java Program to set Selection Mode for JList only for single selection
- C++ program for Sorting Dates using Selection Sort
- C Program for Number of stopping station problem
- Program to implement the fractional knapsack problem in Python
- C++ Program to Solve Travelling Salesman Problem for Unweighted Graph
