def setUp(self):
        graph = UndirectedGraph(7)
        graph.addEdge(0, 1)
        graph.addEdge(1, 2)
        graph.addEdge(1, 3)
        graph.addEdge(1, 4)
        graph.addEdge(4, 5)
        graph.addEdge(4, 6)
        graph.addEdge(0, 4)

        self.solver = DFSSolver(graph)
Exemple #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("file", help="input file with graph adjacency matrix")
    parser.add_argument("-d", "--debug", help="print additional messages", action="store_true")
    args = parser.parse_args()
    file = args.file
    graph = readGraph(file)
    solver = DFSSolver(graph, args.debug)
    solution, price = solver.findBestSolution()
    if solution == None:
        print("No spanning tree found.")
    else:
        print("Minimum spanning tree, degree = {0}:".format(price))
        print(solution)
    def test_findBestSolutionFor10(self):
        graph = UndirectedGraph(10)
        graph.addEdge(0, 4)
        graph.addEdge(0, 5)
        graph.addEdge(0, 6)
        graph.addEdge(0, 8)
        graph.addEdge(0, 9)

        graph.addEdge(1, 2)
        graph.addEdge(1, 3)
        graph.addEdge(1, 5)
        graph.addEdge(1, 6)
        graph.addEdge(1, 7)
        graph.addEdge(1, 8)

        graph.addEdge(2, 6)
        graph.addEdge(2, 7)
        graph.addEdge(2, 9)

        graph.addEdge(3, 5)
        graph.addEdge(3, 6)
        graph.addEdge(3, 7)

        graph.addEdge(4, 5)
        graph.addEdge(4, 6)
        graph.addEdge(4, 8)
        graph.addEdge(4, 9)

        graph.addEdge(5, 9)

        graph.addEdge(6, 7)
        graph.addEdge(6, 9)

        graph.addEdge(7, 8)

        solver = DFSSolver(graph)

        # self.solver.DEBUG = True
        solution, price = solver.findBestSolution()
        self.assertEqual(2, price)
        expected = [Edge(0, 9), Edge(1, 2), Edge(1, 3), Edge(3, 5), Edge(4, 5), Edge(4, 8), Edge(6, 7), Edge(6, 9), Edge(7, 8)]
        self.assertEqual(set(expected), set(solution.edgeList()))
Exemple #4
0
def main():
    comm = MPI.COMM_WORLD
    my_rank = comm.Get_rank()
    if (my_rank == 0):
        parser = argparse.ArgumentParser()
        parser.add_argument("file", help="input file with graph adjacency matrix")
        parser.add_argument("-d", "--debug", help="print additional messages", action="store_true")
        args = parser.parse_args()
        file = args.file
        debug = args.debug
        graph = readGraph(file)
    else:
        graph = None
        debug = None
    debug = comm.bcast(debug)
    received = comm.bcast(graph)
    solver = DFSSolver(received, comm, debug)

    comm.Barrier()

    solution, price = solver.findBestSolution()
    sys.stdout.flush()

    comm.Barrier()

    # print("? {1}'s price is {0}").format(price, my_rank)

    min_price = comm.reduce(sendobj=price, op=MPI.MIN, root=0)
    if my_rank == 0:
        print("*** Minimum price is {0}.").format(min_price)
    if price == min_price:
        print("-------------------------------------")
        print("{0}: Minimum spanning tree, degree = {1}:").format(my_rank, price)
        print(solution)
        print("*************************************")

    MPI.Finalize()