示例#1
0
 def KruskalsAlgorithm(g):
     """
     (Graph) -> GraphAsLists
     Kruskal's algorithm to find a minimum-cost spanning tree
     for the given edge-weighted, undirected graph.
     """
     n = g.numberOfVertices
     result = GraphAsLists(n)
     for v in xrange(n):
         result.addVertex(v)
     queue = BinaryHeap(g.numberOfEdges)
     for e in g.edges:
         weight = e.weight
         queue.enqueue(Association(weight, e))
     partition = PartitionAsForest(n)
     while not queue.isEmpty and partition.count > 1:
         assoc = queue.dequeueMin()
         e = assoc.value
         n0 = e.v0.number
         n1 = e.v1.number
         s = partition.find(n0)
         t = partition.find(n1)
         if s != t:
             partition.join(s, t)
             result.addEdge(n0, n1)
     return result
示例#2
0
 def KruskalsAlgorithm(g):
     """
     (Graph) -> GraphAsLists
     Kruskal's algorithm to find a minimum-cost spanning tree
     for the given edge-weighted, undirected graph.
     """
     n = g.numberOfVertices
     result = GraphAsLists(n)
     for v in range(n):
         result.addVertex(v)
     queue = BinaryHeap(g.numberOfEdges)
     for e in g.edges:
         weight = e.weight
         queue.enqueue(Association(weight, e))
     partition = PartitionAsForest(n)
     while not queue.isEmpty and partition.count > 1:
         assoc = queue.dequeueMin()
         e = assoc.value
         n0 = e.v0.number
         n1 = e.v1.number
         s = partition.find(n0)
         t = partition.find(n1)
         if s != t:
             partition.join(s, t)
             result.addEdge(n0, n1)
     return result
示例#3
0
文件: demo6.py 项目: RDWang/python
 def main(*argv):
     "Demostration program number 6."
     print Demo6.main.__doc__
     BinaryHeap.main(*argv)
     LeftistHeap.main(*argv)
     BinomialQueue.main(*argv)
     Deap.main(*argv)
     return 0
示例#4
0
 def PrimsAlgorithm(g, s):
     """
     (Graph, int) -> GraphAsLists
     Prim's algorithm to find a minimum-cost spanning tree
     for the given edge-weighted, undirected 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 = e.weight
                 if not table[v1.number].known and \
                         table[v1.number].distance > d:
                     table[v1.number].distance = d
                     table[v1.number].predecessor = v0.number
                     queue.enqueue(Association(d, v1))
     result = GraphAsLists(n)
     for v in range(n):
         result.addVertex(v)
     for v in range(n):
         if v != s:
             result.addEdge(v, table[v].predecessor)
     return result
示例#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
示例#6
0
 def PrimsAlgorithm(g, s):
     """
     (Graph, int) -> GraphAsLists
     Prim's algorithm to find a minimum-cost spanning tree
     for the given edge-weighted, undirected 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 = e.weight
                 if not table[v1.number].known and \
                         table[v1.number].distance > d:
                     table[v1.number].distance = d
                     table[v1.number].predecessor = v0.number
                     queue.enqueue(Association(d, v1))
     result = GraphAsLists(n)
     for v in xrange(n):
         result.addVertex(v)
     for v in xrange(n):
         if v != s:
             result.addEdge(v, table[v].predecessor)
     return result
示例#7
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