Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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.

Short Answer

Expert verified

The linear algorithm that computes the minimum number of semesters necessary to complete the curriculum in linear time is as follows:

Input: Graph G

Output: number of required semesters.

Add n vertices that has din=0to the first queue

n=din-1

if din-1=0

add nto the second queue

process second queue

if semester coincides with class schedule

return n

Step by step solution

01

Explain the given information

Consider the CS curriculum consists of n courses. The prerequisite graph G has node for each course and an edge from course v to course w, if and only if v is a prerequisite forw

02

Step 2: Give the linear time algorithm that computes the minimum number of semesters

The linear algorithm that computes the minimum number of semesters necessary to complete the curriculum in linear time is as follows:

Input: Graph G

Output: number of required semesters.

Add n vertices that has dn=0to the first queue

n=dn-1

if dn-1=0

add n to the second queue

process second queue

if semester coincides with class schedule

return n

The above algorithm runs in the linear-time.

Therefore, The linear algorithm that computes the minimum number of semesters necessary to complete the curriculum in linear time has been provided.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Give a linear-time algorithm to find an odd-length cycle in a directed graph. (Hint: First solve this problem under the assumption that the graph is strongly connected.)

Let S be a finite set. A binary relation on S is simply a collection R of ordered pairs(x,y)โˆˆSร—S. . For instance, S might be a set of people, and each such pair (x,y)โˆˆR might mean โ€œ x knows y โ€.

An equivalence relationis a binary relation which satisfies three properties:

  • Reflexivity: localid="1659006645990" (x,y)โˆˆR for all XโˆˆS
  • Symmetry: If (x,y)โˆˆR then (y,x)โˆˆR
  • Transitivity: if (x,y)โˆˆR and (y,z)โˆˆR then localid="1659006784500" (x,Z)โˆˆR

For instance, the binary relation โ€œhas the same birthday asโ€ is an equivalence relation, whereas โ€œis the father ofโ€ is not, since it violates all three properties.

Show that an equivalence relation partition set S into disjoint groups S1,S2,โ€ฆ,Sk (in other words, S=S1โˆชS2โˆชโ€ฆโˆชSkandSiโˆฉSj=ฯ•foralliโ‰ j ) such that:

  • Any two members of a group are related, that is, (x,y)โˆˆR for any localid="1659006702579" (x,y)โˆˆSi, for any i .
  • Members of different groups are not related, that is, for all iโ‰ j, for all localid="1659006762355" xโˆˆSi andyโˆˆSi, we have (x,Z)โˆˆR.

(Hint: Represent an equivalence relation by an undirected graph.)

The reverse of a directed graph G = (V,E) is another directed graphGR=(V,ER) on the same vertex set, but with all edges reversed that is,ER={(v,u):(u,v)โˆˆE} . Give a linear-time algorithm for computing the reverse of a graph in adjacency list format.

The police department in the city of Computopia has made all streets one-way. The mayor contends that there is still a way to drive legally from any intersection in the city to any other intersection, but the opposition is not convinced. A computer program is needed to determine whether the mayor is right. However, the city elections are coming up soon, and there is just enough time to run a linear-time algorithm.

a) Formulate this problem graph-theoretically, and explain why it can indeed be solved in linear time.

(b) Suppose it now turns out that the mayorโ€™s original claim is false. She next claims something weaker: if you start driving from town hall, navigating one-way streets, then no matter where you reach, there is always a way to drive legally back to the town hall. Formulate this weaker property as a graph-theoretic problem, and carefully show how it too can be checked in linear time.

The chapter suggests an alternative algorithm for linearization (topological sorting), which repeatedly removes source nodes from the graph (page 101). Show that this algorithm can be implemented in linear time.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free