Sunday 11 April 2021

Course Schedule (LeetCode problem): C#, Python

Continuing our journey of understanding different languages, we will focus on another LeetCode problem - course schedule

Essentially the problem talks about n courses which have their prerequisites defined in a set of pairs and we need to find out if it is possible to take all courses in single semester. In other words, it is not possible to cover all courses if there is at least one circular dependency amongst the courses.

Few key points:

1. The dependency amongst the courses can be visualized as a directed graph where an edge defines a dependency path from course A to course B.

2. Topological sorting is a standard way of finding cycles in a graph.

A topological ordering is possible if and only if the graph has no directed cycles, that is, if it is a directed acyclic graph (DAG).

Let us look at the code.

C# (implementing using BFS)

public bool CanFinish(int n, int[][] p) {

       

    }

No comments:

Post a Comment