Calculating the critical path of a graph

后端 未结 1 626
余生分开走
余生分开走 2020-12-18 15:20

for homework of graph theory, I asked to calculate the (s) Critical (s) Routes (s) and timing slack of a project under the following format:

Entry: The first line o

相关标签:
1条回答
  • 2020-12-18 15:35

    First of all, I'm assuming we have a directed acyclic graph (DAG).

    1 - We need to find, for each vertex, the minimum possible starting time of each activity. This is like finding the longest path for each vertex in the graph. For general graphs this is NP-hard, but since the graph is a DAG, we may use a topological sort to do this in polynomial time.

    2 - Compute the indegree of each vertex (that is, count the number of edges entering them). Since the graph is acyclic, there is at least one vertex that has indegree zero. Put all such vertices on a queue. Also, initialize an array of distances as 0.

    Pseudo-code

    # Compute the indegree
    for each vertex v from the graph:
        for each neighbor u of v:
            indegree[u] += 1
    
    queue Q = empty queue
    distance = array filled with zeroes
    for each vertex v:
        if indegree[v] = 0:
            insert v on Q
    

    3 - Select the first vertex v from the queue. For each neighbor u of v, update distance[u] as distance[u] = max(distance[u], distances[v] + time(v, u)), where time(v, u) is the time necessary to perform task (u, v). Remove v from the graph. This can be done my decreasing the indegree of each of its neighbors. Enqueue any new vertex that now have 0 indegree. Repeat this procedure until all vertex are processed.

    Pseudo-code

    while Q is not empty:
        v = get front element from Q
        for each neighbor u of v:
            distance[u] = max(distance[u], distance[v] + time(v, u))
            indegree[u] -= 1
            if indegree[u] = 0:
                insert u on Q
    

    4 - Now, select the vertex x with the largest distance. This is the minimum total project duration.

    5 - We need to re-construct the critical path. A task (u, v) is on the critical path if has tight times, that is, distance[u] + time(u, v) = distance[v]. So, start with vertex x and search for a path to a starting vertex, with the following constraint: if you're in a vertex a, you can only go to vertex b, with there's an edge (b, a) such that distance[a] = distance[b] + time(b, a).

    6 - For the edges that were not on the path, you need to find the total slack and the free slack. The free slack is easy: to not delay the following task, you need to compute the amount of time between the start of the next task and the end time of the current one. This can be found by the equation: distance[v] - (distance[u] + time(u, v)) for each (u, v).

    7 - To find the total slack you'll need a new piece of information, that is the latest time a task may begin without delaying the whole project. This can be done by reverting the direction of the edges of your graph. Then, starting with vertex x, initialize an array late with the total project duration.

    8 - Again, using the topological order, whenever you dequeue a vertex v, for all its neighbors u, you do late[u] = min(late[u], late[v] - time(v, u)). After reverting the directions bacj, it's easy to see that the total slack is given by late[v] - (late[u] + time(u, v)) for each edge (u, v).

    9 - Finally, as far as I understood, you have to mark with an R all edges that have total slack > free slack.

    0 讨论(0)
提交回复
热议问题