-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
128 lines (104 loc) · 3.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from Graph import Graph
'''
problems:
BT/BST aren't working (problem with undirected graphs, roots other than 0)
important notes:
nodes must start at 0 and increment by 1
problems if not true
if weights for same edge differ, last edge's weight will be used
'''
# default to some Graph, will be global graph used for all operations
g = Graph(3)
g.addEdge(0, 1, 0)
g.addEdge(0, 2, 4)
def create():
global g
warning = input("Warning: Creating a new graph will replace current graph. Is this okay? y/n ")
if warning == "y":
nodes = int(input("How many nodes? Must ensure this is correct. "))
g = Graph(nodes)
creating = "y"
print("\nGraph is created by creating edges. Will enter start node, end node, and the weight between them\n")
while creating == "y":
start = int(input("Enter starting node: "))
end = int(input("Enter ending node: "))
weight = int(input("Enter edge weight: "))
g.addEdge(start, end, weight)
creating = input("Add another edge? y/n ")
# directed or undirected, unweighted graph
# takes in start node
def runFloyd():
global g
if g != None:
start = int(input("Which node is the starting node? "))
g.cycleDetection(start)
# directed or undirected, unweighted graph
# takes in root node
def runBFT():
global g
if g != None:
root = int(input("Which node is the root? "))
g.bft(root)
# undirected or undirected, unweighted graph
# takes in root node
def runDFT():
global g
if g != None:
root = int(input("Which node is the root? "))
g.dft(root)
# directed (no two-way edges), a-cyclic, unweighted graph
# bst takes in root node
def runBST():
global g
if g != None:
root = int(input("Which node is the root? "))
g.bst(root)
# undirected, weighted graph
# prims takes in start node
def runPrim():
global g
if g != None:
start = int(input("Which node is the start node? "))
g.prims(start)
# draws graph
def runDraw():
global g
if g != None:
g.draw()
# rules of program
def printHowTo():
print("\nRules: Nodes and weights must be represented as integers. Nodes must start at 0 and increment by 1. It is important to have number of nodes correct in 'Create'. \nGraph will be undirected if you create an edge each way.\n")
def run():
while True:
option = input(
"""Select an algorithm:
1. Create Graph
2. Draw Current Graph
3. Floyd's Cycle Determining/Locating Algorithm
4. Breadth First Traversal
5. Depth First Traversal
6. Is Binary Tree?
7. Prim's Minimum Spanning Tree
8. How To
x. Quit
: """)
if option == "1":
create()
elif option == "2":
runDraw()
elif option == "3":
runFloyd()
elif option == "4":
runBFT()
elif option == "5":
runDFT()
elif option == "6":
runBST()
elif option == "7":
runPrim()
elif option == "8":
printHowTo()
elif option == "x":
return
if __name__ == "__main__":
run()