Example #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 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
Example #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 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
Example #3
0
 def main(*argv):
     "Demostration program number 7."
     print Demo7.main.__doc__
     SetAsArray.main(*argv)
     SetAsBitVector.main(*argv)
     MultisetAsArray.main(*argv)
     MultisetAsLinkedList.main(*argv)
     PartitionAsForest.main(*argv)
     PartitionAsForestV2.main(*argv)
     PartitionAsForestV3.main(*argv)
     return 0
Example #4
0
 def equivalenceClasses(input, output):
     """
     (File, File) -> None
     Computes equivalence classes using a partition.
     First reads an integer from the input stream that
     specifies the size of the universal set.
     Then reads pairs of integers from the input stream
     that denote equivalent items in the universal set.
     Prints the partition on end-of-file.
     """
     line = input.readline()
     p = PartitionAsForest(int(line))
     for line in input.readlines():
         words = line.split()
         i = int(words[0])
         j = int(words[1])
         s = p.find(i)
         t = p.find(j)
         if s is not t:
             p.join(s, t)
         else:
             output.write("redundant pair: %d, %d\n" % (i, j))
     output.write(str(p) + "\n")
Example #5
0
 def equivalenceClasses(input, output):
     """
     (File, File) -> None
     Computes equivalence classes using a partition.
     First reads an integer from the input stream that
     specifies the size of the universal set.
     Then reads pairs of integers from the input stream
     that denote equivalent items in the universal set.
     Prints the partition on end-of-file.
     """
     line = input.readline()
     p = PartitionAsForest(int(line))
     for line in input.readlines():
         words = line.split()
         i = int(words[0])
         j = int(words[1])
         s = p.find(i)
         t = p.find(j)
         if s is not t:
             p.join(s, t)
         else:
             output.write("redundant pair: %d, %d\n" % (i, j))
     output.write(str(p) + "\n")