-1

Let's say we have a directed graph that represents school courses. Some courses are a prerequisite of others (e.g. [2,1] means that course 1 needs to be taken before course 2). We can take as many courses in parallel as we want, as long as the prerequisite is completed

I think this would naturally fit into a directed graph. To get the order of courses that can be taken, we would use topological sorting. If instead, we want to find the "bottleneck" course, what kind of algorithm makes sense?

A bottleneck looks like this:

  • Lets say we have the following prerequisites: [2,1],[3,2],[6,3],[5,4],[6,5]
  • That means we can take {1,4} in parallel, then {2,5}, then we have 3 as a "bottleneck" for course 6 because it's the last course that is left before we can take 6

Visualization attached below in case it helps

enter image description here

I'm not sure where to start with this problem

2 Answers 2

2

You seem to be assuming that

  1. Every course MUST be taken
  2. Every course takes the same amount of time

Algorithm:

  • LOOP R over nodes that have an in-degree of zero ( no dependencies )
    • DO a breadth first search from R
      • WHEN search visits V
        • IF V never before visited, mark with distance from R
        • IF V previously visited, mark with larger of distance from R and previous mark
  • LOOP over nodes with out-degree zero, keep track of node with largest mark
  • LOOP over parents of node with largest mark
    • BOTTLENECK is parent with largest mark.
1
  • Thanks! My assumption was that all edges are equal weight (all courses take same time). It seems like your solution is generic and would work with weighted edges.
    – jmtt
    Commented Mar 18, 2023 at 19:15
0
  1. Start from the root node.
  2. Find the child node having the largest height.

This can be done if you store the children in an array of pointers( in a binary tree we will have pointers to the left child and right child. In general, if we store the children in an array of pointers(for e.g. child[0] refers to the leftmost child.....child[n-1] for the nth child from left.)

  1. The child node having the largest height will automatically be the bottleneck.
4
  • "Start from the root node." There is no one root node. Commented Mar 18, 2023 at 13:32
  • Make a root node that points to all nodes having indegree zero.
    – Dave
    Commented Mar 18, 2023 at 17:27
  • @Dave that would work, but is an unneeded complication. The code simply loops over nodes with in-degree zero, without having to modify the input graph. Commented Mar 18, 2023 at 18:29
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 24, 2023 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.