Skip to content

Conversation

Siddhram
Copy link

This PR introduces a clean and well-documented implementation of the Course Schedule problem in R using Kahn’s Algorithm (BFS-based topological sort).

Overview

Courses are modeled as nodes and prerequisites as directed edges (prereq → course). The problem reduces to cycle detection: if the graph is acyclic, all courses can be completed, and a valid topological ordering exists. This implementation provides both a boolean feasibility check and an optional function to return one valid course order.

Features

  • BFS-based Kahn’s Algorithm for topological sorting and cycle detection
  • Robust input normalization:
    • Accepts prerequisites as a matrix, data.frame, or flat numeric vector
    • Supports both 0-based and 1-based indices (auto-normalized)
  • Two public APIs:
    • can_finish(num_courses, prerequisites): returns TRUE/FALSE
    • find_course_order(num_courses, prerequisites): returns a valid order or integer(0) if impossible
  • Clear comments and consistent structure with other graph_algorithms scripts
  • Embedded examples for quick validation (no external dependencies)

Complexity

  • Time Complexity: O(V + E)
  • Space Complexity: O(V + E)

Directory

  • Updated DIRECTORY.md to include “Course Schedule (Kahn)” under Graph Algorithms

Demonstration

Run the script to execute built-in examples:

From command line / PowerShell:

Rscript "R/graph_algorithms/course_schedule_can_finish.r"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant