Ejemplo n.º 1
0
    def criticalPathAnalysis(g):
        """
        (Digraph) -> DigraphAsLists
        Computes the critical path in an event-node graph.
        """
        n = g.numberOfVertices

        earliestTime = Array(n)
        earliestTime[0] = 0
        g.topologicalOrderTraversal(
            Algorithms.EarliestTimeVisitor(earliestTime))

        latestTime = Array(n)
        latestTime[n - 1] = earliestTime[n - 1]
        g.depthFirstTraversal(PostOrder(
            Algorithms.LatestTimeVisitor(latestTime)), 0)

        slackGraph = DigraphAsLists(n)
        for v in xrange(n):
            slackGraph.addVertex(v)
        for e in g.edges:
            slack = latestTime[e.v1.number] - \
                earliestTime[e.v0.number] - e.weight
            slackGraph.addEdge(
                e.v0.number, e.v1.number, slack)
        return Algorithms.DijkstrasAlgorithm(slackGraph, 0)
Ejemplo n.º 2
0
    def DijkstrasAlgorithm(g, s):
        """
        (Digraph, int) -> DigraphAsLists
        Dijkstra's algorithm to solve the single-source, shortest path problem
        for the given edge-weighted, directed graph.
        """
        n = g.numberOfVertices
        table = Array(n)
        for v in xrange(n):
            table[v] = Algorithms.Entry()
        table[s].distance = 0
        queue = BinaryHeap(g.numberOfEdges)
        queue.enqueue(Association(0, g[s]))
        while not queue.isEmpty:
            assoc = queue.dequeueMin()
            v0 = assoc.value
            if not table[v0.number].known:
                table[v0.number].known = True
                for e in v0.emanatingEdges:
                    v1 = e.mateOf(v0)
                    d = table[v0.number].distance + e.weight
                    if table[v1.number].distance > d:

                        table[v1.number].distance = d
                        table[v1.number].predecessor = v0.number
                        queue.enqueue(Association(d, v1))
        result = DigraphAsLists(n)
        for v in xrange(n):
            result.addVertex(v, table[v].distance)
        for v in xrange(n):
            if v != s:
                result.addEdge(v, table[v].predecessor)
        return result
Ejemplo n.º 3
0
 def main(*argv):
     "Demostration program number 10."
     print Demo10.main.__doc__
     GraphAsMatrix.main([])
     DigraphAsMatrix.main([])
     GraphAsLists.main([])
     DigraphAsLists.main([])
     return 0
Ejemplo n.º 4
0
    def criticalPathAnalysis(g):
        """
        (Digraph) -> DigraphAsLists
        Computes the critical path in an event-node graph.
        """
        n = g.numberOfVertices

        earliestTime = Array(n)
        earliestTime[0] = 0
        g.topologicalOrderTraversal(
            Algorithms.EarliestTimeVisitor(earliestTime))

        latestTime = Array(n)
        latestTime[n - 1] = earliestTime[n - 1]
        g.depthFirstTraversal(
            PostOrder(Algorithms.LatestTimeVisitor(latestTime)), 0)

        slackGraph = DigraphAsLists(n)
        for v in range(n):
            slackGraph.addVertex(v)
        for e in g.edges:
            slack = latestTime[e.v1.number] - \
                earliestTime[e.v0.number] - e.weight
            slackGraph.addEdge(e.v0.number, e.v1.number, slack)
        return Algorithms.DijkstrasAlgorithm(slackGraph, 0)
Ejemplo n.º 5
0
    def DijkstrasAlgorithm(g, s):
        """
        (Digraph, int) -> DigraphAsLists
        Dijkstra's algorithm to solve the single-source, shortest path problem
        for the given edge-weighted, directed graph.
        """
        n = g.numberOfVertices
        table = Array(n)
        for v in range(n):
            table[v] = Algorithms.Entry()
        table[s].distance = 0
        queue = BinaryHeap(g.numberOfEdges)
        queue.enqueue(Association(0, g[s]))
        while not queue.isEmpty:
            assoc = queue.dequeueMin()
            v0 = assoc.value
            if not table[v0.number].known:
                table[v0.number].known = True
                for e in v0.emanatingEdges:
                    v1 = e.mateOf(v0)
                    d = table[v0.number].distance + e.weight
                    if table[v1.number].distance > d:

                        table[v1.number].distance = d
                        table[v1.number].predecessor = v0.number
                        queue.enqueue(Association(d, v1))
        result = DigraphAsLists(n)
        for v in range(n):
            result.addVertex(v, table[v].distance)
        for v in range(n):
            if v != s:
                result.addEdge(v, table[v].predecessor)
        return result