Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I want this Lab's C code There are my false example code at the end of my question Lab 9 : Course Registration Description A
I want this Lab's C code
There are my false example code at the end of my question
Lab : Course Registration
Description
A newly admitted student must register courses in a way that adheres to prerequisite requirements. A prerequisite is a requirement that one course must be completed before another course can be taken. Each course can have multiple prerequisites, and the prerequisite structure forms a directed acyclic graph. Each course is identified by a unique number from to N The task is to determine if all courses can be successfully registered by the student.
Specific rules include:
prerequisitesiai bi means that course bi must be taken before course ai
An algorithm should be designed to determine if it's possible to complete all courses, returning True if possible and False otherwise.
Input
The input provides the total number of courses N and a list of prerequisite pairs. Each pair a b specifies a prerequisite relationship where course b must be completed before course a
The prerequisite pairs are provided as a ba bak bk
ai and bi represent courses identified by their numbers. There are no spaces or tabs in the input.
Output
The output should be either True or False, indicating whether all courses can be registered according to the prerequisites.
Constraints
N
prerequisites.length
prerequisitesilength
ai bi N
This problem essentially involves checking for cycles in a directed graph to determine if a topological ordering of the courses is possible, which allows all courses to be taken in compliance with the prerequisites.
sample input
True
Explanation: Course denotes that you must complete course before taking course
sample input
False
Explanation: There is a cycle because course must be completed before course and course must be completed before course
sample input
False
Explanation: A course cannot be a prerequisite to itself as it forms an immediate cycle.
#include
#include
#include
using namespace std;
bool canFinishint numCourses, vector &prerequisites
Graph to hold the list of prerequisites for each course
vector graphnumCourses;
Array to track the number of prerequisites indegrees for each course
vector inDegreenumCourses;
Build the graph and count the indegrees
for auto &pre : prerequisites
graphprepushbackpre; Note the reversal, pre is prerequisite for pre
inDegreepre; Increment the indegree of the course that needs pre
Queue for the courses with no prerequisites
queue zeroInDegree;
for int i ; i numCourses; i
if inDegreei
zeroInDegree.pushi; If no prerequisites, it's ready to be taken
int visited ; Counter for the number of courses that can be completed
while zeroInDegree.empty
int course zeroInDegree.front;
zeroInDegree.pop;
visited; A course can be taken, mark it visited
Check the courses that depend on the current course
for int next : graphcourse
if inDegreenext If no more prerequisites for this course
zeroInDegree.pushnext; It's ready to be taken
If we were able to visit all courses, then it's possible to finish
return visited numCourses;
int main
int N; Total number of courses
cin N;
vector prerequisites;
int a b;
char comma; To handle the comma in the input
Read pairs of courses in the format a b
while cin a comma b
prerequisites.pushbacka b;
cout canFinishN prerequisites "True" : "False" endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started