Graph Theory37 sections · 1633 units
Open in Course

Parallel Courses III - Algorithm

DP on topological order

1.1. Build the adjacency list and in-degree array from relations. For each [prev,next][prev, next], add edge prevnextprev \to next and increment indegree[next].

2.2. Initialize finishTime[i] = time[i] for every course. Courses with no prerequisites finish in time[i] months.

3.3. Push all courses with indegree=0indegree = 0 into the queue and run Kahn's algorithm.

4.4. When processing course uu, for each neighbor vv: set finishTime[v] = max(finishTime[v], finishTime[u] + time[v]). This propagates the earliest start.

5.5. The answer is max(finishTime[i]) over all courses. This runs in O(V+E)O(V + E) time and uses O(V+E)O(V + E) space.