Skip to content

Conversation

@AnjaliPai16
Copy link
Contributor

🎯 Problem Information

Problem Name: Course Schedule
Category: Graph Algorithms
CSES Link: https://cses.fi/problemset/task/1679
Difficulty: [Medium]

📝 Description

This PR adds a complete C++ solution for the Course Schedule problem from CSES, which requires finding a valid order in which all courses can be completed given prerequisite constraints.

Each course represents a node in a directed graph, and each prerequisite relation represents a directed edge. The problem essentially asks for a topological ordering of this graph.
If such an order doesn’t exist (i.e., the graph contains a cycle), the program outputs "IMPOSSIBLE".

🧩 Solution Approach

  • Algorithm Used: Kahn’s Algorithm (BFS-based Topological Sort)
  • Time Complexity: O(n+m),where n = number of courses, m = number of dependencies
  • Space Complexity: O(n+m)
  • Key Insights: 1)Nodes with no incoming edges (indegree = 0) can be taken first.
    2)Each time we “take” a course, we remove it and reduce the indegree of its dependents.
    3)If all courses are processed → valid order found.
    4)If not → cycle detected → impossible to complete all courses.

✅ Checklist

Code Quality

  • Solution follows the required template format
  • Code is clean and well-commented
  • Variable names are descriptive
  • Proper error handling for edge cases

Testing

  • Solution passes all CSES test cases
  • Tested with custom edge cases
  • Handles large input constraints efficiently
  • No runtime errors or timeouts

Documentation

  • Added solution to appropriate category folder
  • File name follows naming convention (snake_case)
  • Added brief comment explaining the approach
  • Updated any relevant documentation if needed

Style Guide

  • Uses required headers (#include <bits/stdc++.h>)
  • Includes fast I/O optimization
  • Uses appropriate data types (long long for large numbers)
  • Follows competitive programming best practices

🏷️ Type of Change

  • 🐛 Bug fix (fixes an existing solution)
  • ✨ New problem solution
  • 📚 Documentation improvement
  • 🔧 Code optimization/refactoring
  • 🎃 Hacktoberfest contribution

🧪 Testing Details

Describe how you tested your solution:
To ensure the correctness, performance, and robustness of the solution, I performed several layers of testing:

  1. CSES Judge Verification
    Submitted the solution on the official CSES Course Schedule problem
    page.
    The solution passed all hidden test cases with 0.00s runtime and no memory issues.
    Verified the output format matches the expected specification (space-separated order or “IMPOSSIBLE”).

  2. Custom Test Cases
    Manually constructed small and edge-case inputs to validate correctness and edge handling

  3. Performance & Efficiency Checks
    Ran the solution with maximum input size to verify it runs within CSES time limits.
    Checked that the memory footprint remains O(n + m) — suitable for 10⁵ nodes and 2×10⁵ edges.
    No stack overflows (since BFS-based), making it safer than DFS for large graphs.

  4. Edge Case Validations
    Verified handling of isolated nodes (courses with no prerequisites and not required by others).
    Confirmed that cycle detection works by comparing order.size() != n.
    Checked behavior when all courses depend on a single root course.

Test Cases Used:

Input:
5 3
1 2
3 1
4 5

Expected Output:
3 4 1 5 2

Actual Output:
3 4 1 5 2

📸 Screenshots

Screenshot 2025-10-20 at 1 50 10 AM

For Maintainers:

  • Code review completed
  • Solution verified on CSES judge
  • Documentation updated if needed
  • Labels applied appropriately

@ks-iitjmu ks-iitjmu linked an issue Oct 21, 2025 that may be closed by this pull request
9 tasks
@ks-iitjmu ks-iitjmu added enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest-accepted Approved PRs for Hacktoberfest hacktoberfest_2025 Hacktoberfest 2025 specific contributions medium Medium difficulty problems category: graph Graph algorithm problems labels Oct 21, 2025
@ks-iitjmu ks-iitjmu merged commit a213172 into ks-iitjmu:main Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: graph Graph algorithm problems enhancement New feature or request good first issue Good for newcomers hacktoberfest Issues/PRs for Hacktoberfest participation hacktoberfest_2025 Hacktoberfest 2025 specific contributions hacktoberfest-accepted Approved PRs for Hacktoberfest medium Medium difficulty problems

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NEW] Add solution for Course Schedule Graph Algorithm

2 participants