Question

Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G...

Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G has a node for each course, and an edge from course v to course w if and only if v is a prerequisite for w. Find an algorithm that works directly with this graph representation, and computes the minimum number of semesters necessary to complete the curriculum (assume that a student can take any number of courses in one semester). The running time of your algorithm should be linear.

Please use topological sort for your answer! No pseudocode or code, just explain the algorithm. Thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let us consider some path P in course graph G. The courses in path P must be scheduled in all different semesters because the prerequisite courses need to be completed before starting with any course. Thus the number of semesters required to complete path P subjects is equal to the length of path P. Applying the same logic on longest path Q of graph G, we get minimum number of semesters required to complete the curriculum are equal to the length of Q.

Here the longest path is 3 -> 6 -> 7 -> 8 . Thus the minimum number of semesters required will be 4.

So, the algorithm will be iterative. In each iteration algorithm will find those courses which do not have any prerequisites, schedule them in a semester (by increasing count of some semester variable) and delete them from the graph indicating that courses are completed till that semester. In next iteration, there will be new courses whose prerequisites have already been completed till last semester (because they have no incoming edge or there indegree is zero). When graph is empty algorithm terminates. The semester count will give us the minimum number of semesters required to complete the curriculum.

After one iteration, first semester will have subjects 1,2,3 and 4 completed the resulting graph will be :

Here it is easily evident that in next semester courses 5 and 6 will be studied because 7 still need 6 to be studied.

And then in 7 in next and 8 in last semester.

NOTE: Indegree is the number of incoming edges on any vertex. If the indegree for some vertex is 0, then it has no incoming edge i.e. there are no prerequisite courses or they have been completed

To get linear ordering we can apply Topological Sorting. Now to apply topological sort, we first need to prove that graph is a DAG (Directed Acyclic Graph). For proving this, we know that the graph starts with at least some course that has no prerequisites otherwise how will curriculum start. Also, no course can be a prerequisite for its own prerequisite. So, the graph is acyclic. The graph needs to be directed to indicate which course is whose prerequisite. Thus, the graph is a DAG.

Complexity of Topological sort is O(E+V) where E: edges and V: variables.

Thus, in our algorithm complexity will be O(m+n). where m:number of courses and n: pairs of courses(v,w)

Add a comment
Know the answer?
Add Answer to:
Suppose a CS curriculum consists of n courses, all of them mandatory. The prerequisite graph G...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CS 3345: Data Structures and Algorithms -Homework 7 1. These three questions about graphs all have...

    CS 3345: Data Structures and Algorithms -Homework 7 1. These three questions about graphs all have the same subparts. Note that for parts (iii), (iv), and (v), your answer should be in terms of an arbitrary k, not assuming k-4 a) Suppose a directed graph has k nodes, where there are two lspecial" nodes. One has an edge from itself to every non-special node and the other has an edge from every non-special node to itself. There are no other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT