Reputation: 31
Some students wants to minimize their lecture attendance by sending minimum number of students to each of n
lectures.
i
begins at time a[i]
and ends at time b[i]
r[i][j]
time to commute from lecture i
to lecture j
Is there any algorithm to calculate the minimum number of students needed to attend all lectures?
Upvotes: 3
Views: 372
Reputation: 36476
Consider the classes as nodes in a graph. Construct edges between pairs of nodes (i,j)
in the graph if it is possible to commute from lecture i
to lecture j
in time. The "root" nodes of your graph are the first classes of the day (classes that start before any other classes end in time to get to that class).
The first key observation is that the graph is directed and acyclic (you can't go back in time).
Your goal is to find the minimum number of paths through the graph such that every node is visited at least once. This immediately leads to the second key observation, which is that this can be accomplished greedily.
So the algorithm goes as follows:
minNumStudents
Finding the longest path in a DAG is simple using dynamic programming:
ordering
of the graphdist
with V
elements (the number of nodes in the graph), initialized to 0
prev
with V
elements, storing the previous node in the pathThen
for each vertex `V` in `ordering`
for each edge (V,W)
dist[W] = min(dist[W],dist[V]+1)
prev[W] = V;
Your longest path ends at the W
such that dist[W]
is maximum. And the longest path can be computed by backtracking from W
using the prev
array.
Upvotes: 1