def driver():
    print("Which method of edge generation would you like to use?")
    print("1: Weight based on actual distance between nodes")
    print("2: Weight based on user-defined maximum edge weight")
    edgeMethod = int(raw_input('> '))

    print("How many nodes are in the graph (any number greater than 0)?")
    totalNodes = int(raw_input('> '))
    print("How big is the graph (any number greater than 0)?")
    graphSize = int(raw_input('> '))
    print(
        "What is the k-value that should be used (any number greater than 0)?")
    kval = int(raw_input('> '))
    maxWeight = 1
    if edgeMethod == 2:
        print(
            "What is the maximum weight of an edge (any number greater than 0)?"
        )
        maxWeight = int(raw_input('> '))
    pTot = list()
    kTot = list()
    sTot = list()
    for i in xrange(1000):
        print(i)
        trees = GenGraph.GenerateGraph(totalNodes, graphSize, maxWeight, kval,
                                       edgeMethod)
        start = time.clock()
        Prims.runPrims(trees)
        pTot.append(time.clock() - start)

        start = time.clock()
        Kruskals.runKruskals(trees)
        kTot.append(time.clock() - start)

        start = time.clock()
        Sollins.runSollins(trees)
        sTot.append(time.clock() - start)
    print("Prim's Algorithm Avg. Run Time = " +
          str(sum(pTot) / float(len(pTot))))
    print("Kruskal's Algorithm Avg. Run Time = " +
          str(sum(kTot) / float(len(kTot))))
    print("Sollin's Algorithm Avg. Run Time = " +
          str(sum(sTot) / float(len(sTot))))
Exemple #2
0
def driver():
	print "Hello! This is a program which randomly generates nodes and edges to connect those nodes.",
	print "After the graph is generated, you can then choose from finding an MST of each tree created",
	print "by using either Prim's, Kruskal's, or Sollin's algorithm. Addtionally, there are two methods",
	print "of generating edges. The first method is to randomly assign a weight between 1 and the user-defined",
	print "maximum edge weight, the second is to assign the weight based on the actual distance between",
	print "the two nodes. Finally, the amount of nodes in the graph, the size of the graph, and the k-value,",
	print "which determines the likelihood any two nodes will have an edge between them."

	print "Which method of edge generation would you like to use?"
	print "1: Weight based on actual distance between nodes"
	print "2: Weight based on user-defined maximum edge weight"
	edgeMethod = int(raw_input('> '))

	print "How many nodes are in the graph (any number greater than 0)?"
	totalNodes = int(raw_input('> '))
	print "How big is the graph (any number greater than 0)?"
	graphSize = int(raw_input('> '))
	print "What is the k-value that should be used (any number greater than 0)?"
	kval = int(raw_input('> '))
	maxWeight = 0
	if edgeMethod == 2:
		print "What is the maximum weight of an edge (any number greater than 0)?"
		maxWeight = int(raw_input('> '))
	trees = GenGraph.GenerateGraph(totalNodes, graphSize, maxWeight, kval, edgeMethod)
	print "Here are the adjacency lists of the trees randomly generated using your inputs (format = ((x,y,) weight)):"
	printTrees(trees, "Tree")
	print "Here are the MSTs for each of the generated trees."
	pTrees = copy.deepcopy(trees)
	start= time.clock()
	MSTs = Prims.runPrims(pTrees)
	pTot = (time.clock()-start)
	print "Result of Prim's"
	printTrees(MSTs,"MST")
	kTrees = copy.deepcopy(trees)
	start= time.clock()
	MSTs = Kruskals.runKruskals(kTrees)
	kTot = (time.clock()-start)
	print "Result of Kruskal's"
	printTrees(MSTs,"MST")
	sTrees = copy.deepcopy(trees)
	start= time.clock()
	MSTs = Sollins.runSollins(sTrees)
	sTot = (time.clock()-start)
	print "Result of Sollin's"
	printTrees(MSTs,"MST")
	print "Prim's Algorithm Run Time = "+str(pTot)
	print "Kruskal's Algorithm Run Time = "+str(kTot)
	print "Sollin's Algorithm Run Time = "+str(sTot)
Exemple #3
0
def driver():
    print "Which method of edge generation would you like to use?"
    print "1: Weight based on actual distance between nodes"
    print "2: Weight based on user-defined maximum edge weight"
    edgeMethod = int(raw_input("> "))

    print "How many nodes are in the graph (any number greater than 0)?"
    totalNodes = int(raw_input("> "))
    print "How big is the graph (any number greater than 0)?"
    graphSize = int(raw_input("> "))
    print "What is the k-value that should be used (any number greater than 0)?"
    kval = int(raw_input("> "))
    maxWeight = 1
    if edgeMethod == 2:
        print "What is the maximum weight of an edge (any number greater than 0)?"
        maxWeight = int(raw_input("> "))
    pTot = list()
    kTot = list()
    sTot = list()
    for i in xrange(1000):
        print i
        trees = GenGraph.GenerateGraph(totalNodes, graphSize, maxWeight, kval, edgeMethod)
        start = time.clock()
        Prims.runPrims(trees)
        pTot.append(time.clock() - start)

        start = time.clock()
        Kruskals.runKruskals(trees)
        kTot.append(time.clock() - start)

        start = time.clock()
        Sollins.runSollins(trees)
        sTot.append(time.clock() - start)
    print "Prim's Algorithm Avg. Run Time = " + str(sum(pTot) / float(len(pTot)))
    print "Kruskal's Algorithm Avg. Run Time = " + str(sum(kTot) / float(len(kTot)))
    print "Sollin's Algorithm Avg. Run Time = " + str(sum(sTot) / float(len(sTot)))
Exemple #4
0
def driver():
    print(
        "Hello! This is a program which randomly generates nodes and edges to connect those nodes.",
    )
    print(
        "After the graph is generated, you can then choose from finding an MST of each tree created",
    )
    print(
        "by using either Prim's, Kruskal's, or Sollin's algorithm. Addtionally, there are two methods",
    )
    print(
        "of generating edges. The first method is to randomly assign a weight between 1 and the user-defined",
    )
    print(
        "maximum edge weight, the second is to assign the weight based on the actual distance between",
    )
    print(
        "the two nodes. Finally, the amount of nodes in the graph, the size of the graph, and the k-value,",
    )
    print(
        "which determines the likelihood any two nodes will have an edge between them."
    )

    print("Which method of edge generation would you like to use?")
    print("1: Weight based on actual distance between nodes")
    print("2: Weight based on user-defined maximum edge weight")
    edgeMethod = int(raw_input('> '))

    print("How many nodes are in the graph (any number greater than 0)?")
    totalNodes = int(raw_input('> '))
    print("How big is the graph (any number greater than 0)?")
    graphSize = int(raw_input('> '))
    print(
        "What is the k-value that should be used (any number greater than 0)?")
    kval = int(raw_input('> '))
    maxWeight = 0
    if edgeMethod == 2:
        print(
            "What is the maximum weight of an edge (any number greater than 0)?"
        )
        maxWeight = int(raw_input('> '))
    trees = GenGraph.GenerateGraph(totalNodes, graphSize, maxWeight, kval,
                                   edgeMethod)
    print(
        "Here are the adjacency lists of the trees randomly generated using your inputs (format = ((x,y,) weight)):"
    )
    printTrees(trees, "Tree")
    print("Here are the MSTs for each of the generated trees.")
    pTrees = copy.deepcopy(trees)
    start = time.clock()
    MSTs = Prims.runPrims(pTrees)
    pTot = (time.clock() - start)
    print("Result of Prim's")
    printTrees(MSTs, "MST")
    kTrees = copy.deepcopy(trees)
    start = time.clock()
    MSTs = Kruskals.runKruskals(kTrees)
    kTot = (time.clock() - start)
    print("Result of Kruskal's")
    printTrees(MSTs, "MST")
    sTrees = copy.deepcopy(trees)
    start = time.clock()
    MSTs = Sollins.runSollins(sTrees)
    sTot = (time.clock() - start)
    print("Result of Sollin's")
    printTrees(MSTs, "MST")
    print("Prim's Algorithm Run Time = " + str(pTot))
    print("Kruskal's Algorithm Run Time = " + str(kTot))
    print("Sollin's Algorithm Run Time = " + str(sTot))