예제 #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 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
예제 #3
0
파일: demo10.py 프로젝트: RDWang/python
 def main(*argv):
     "Demostration program number 10."
     print Demo10.main.__doc__
     GraphAsMatrix.main([])
     DigraphAsMatrix.main([])
     GraphAsLists.main([])
     DigraphAsLists.main([])
     return 0
예제 #4
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
예제 #5
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