-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
184 lines (136 loc) · 5.76 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import networkx as nx
from kivy.app import App
from kivy.clock import Clock
from random import random
import time
from functools import partial
# UIX
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.button import ButtonBehavior
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.dropdown import DropDown
from kivy.graphics import Color, Ellipse, Line,Rectangle
from kivy.properties import StringProperty
from kivy.properties import ListProperty
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from edge import Edge
from node import Node
from graph_editor import GraphEditor
def enumerate2(xs, start=0, step=1):
for x in xs:
yield (start, x)
start += step
# MAIN
class MainApp(App):
def build(self):
# Graph Editor
self.editor = GraphEditor()
# Dropdown button
self.dropdown = DropDown()
self.dropdown.add_widget(Button(text = "DFS",size_hint_y=None, height=20,on_release = self.window_traversal))
self.dropdown.add_widget(Button(text = "BFS",size_hint_y=None, height=20,on_release = self.window_traversal))
self.dropdown.add_widget(Button(text = "Dijkstra's algorithm (shortest Path)",size_hint_y=None, height=20,on_release = self.window_traversal))
Layout = BoxLayout(orientation = 'vertical')
# Top bar
top_bar = BoxLayout(orientation = 'horizontal',size_hint = (1,.1))
top_bar.add_widget(Button(text = "Add vertex",height = 10,on_press = self.button_pressed))
top_bar.add_widget(Button(text = "Add Edge",height = 10,on_press = self.button_pressed))
mainbutton = Button(text='Graph Algo')
mainbutton.bind(on_release=self.dropdown.open)
top_bar.add_widget(mainbutton)
top_bar.add_widget(Button(text = "Delete",on_press=self.editor.clear_canvas))
# Status bar
status_bar = BoxLayout(orientation = 'horizontal',size_hint = (1,.05))
self.status = Label(text = "Status")
status_bar.add_widget(self.status)
# Adding top bar and editor
Layout.add_widget(status_bar)
Layout.add_widget(top_bar)
Layout.add_widget(self.editor)
# Output
return Layout
def button_pressed(self,value):
if value.text == "Add vertex":
self.editor.activate_vertex_addition(value)
self.status.text = "Add vertex"
elif value.text== "Add Edge":
self.editor.activate_edge_addition(value)
self.status.text = "Add edge"
def window_traversal(self,value):
if value.text == "DFS":
press = self.dfs
elif value.text == "BFS":
press = self.bfs
elif value.text == "Dijkstra's algorithm (shortest Path)":
window = FloatLayout()
window.add_widget(Label(text = "Enter source: ",size_hint = (.2,None),height = 30,pos_hint = {'x':.20,'y':.4}))
self.input_source = TextInput(multiline = False,size_hint = (.2,None),height = 30,pos_hint = {'x':.38,'y':.4})
window.add_widget(Label(text = "Enter target: ",size_hint = (.2,None),height = 30,pos_hint = {'x':.20,'y':.3}))
self.input_target = TextInput(multiline = False,size_hint = (.2,None),height = 30,pos_hint = {'x':.38,'y':.3})
window.add_widget(Button(text = "submit",size_hint = (.2,None),height = 30,pos_hint = {'x':.38,'y':.2},on_press = self.shortest_path))
window.add_widget(self.input_source)
window.add_widget(self.input_target)
self.popup = Popup(title = "Test",content = window)
self.popup.open()
self.dropdown.dismiss()
return
self.dropdown.dismiss()
window = FloatLayout()
window.add_widget(Label(text = "Enter source: "))
self.input = TextInput(multiline = False,size_hint = (.2,None),height = 30,pos_hint = {'x':.38,'y':.4})
window.add_widget(self.input)
window.add_widget(Button(text = "submit",size_hint = (.2,None),height = 30,pos_hint = {'x':.38,'y':.2},on_press = press))
self.popup = Popup(title = "Test",content = window)
self.popup.open()
def dfs(self,value):
# Should add my own implementations
node_order = list(nx.dfs_edges(self.editor.G,source = self.input.text))
self.traversal(node_order)
def bfs(self,value):
# Should add my own implementations
node_order = list(nx.bfs_edges(self.editor.G,source = self.input.text))
self.traversal(node_order)
def traversal(self,node_order):
self.editor.activate_vertex_addition(None)
self.editor.activate_edge_addition(None)
for count,edge in enumerate2(node_order,0,0.5):
node_a = self.return_node(edge[0])
node_b = self.return_node(edge[1])
edge = self.return_edge(edge[0],edge[1])
node_a.selected = True
node_b.selected = True
edge.selected = True
Clock.schedule_once(partial(node_a.draw_node,node_a.pos),count+.5)
Clock.schedule_once(partial(edge.draw_edge,node_a,node_b),count+1)
Clock.schedule_once(partial(node_b.draw_node,node_b.pos),count+1)
self.editor.add_edge = False
self.popup.dismiss()
def path(self,node_order):
self.editor.activate_vertex_addition(None)
self.editor.activate_edge_addition(None)
for count,edge in enumerate2(node_order,0,.5): # edge = (Node_1, Node_2)
node_a = self.return_node(edge[0])
node_a.selected = True
Clock.schedule_once(partial(node_a.draw_node,node_a.pos),count)
self.editor.add_edge = False
self.popup.dismiss()
def shortest_path(self,value):
self.dropdown.dismiss()
# Should add my own implementations
node_order = list(nx.shortest_path(self.editor.G,source = self.input_source.text,target = self.input_target.text,weight = 'weight',method='dijkstra')) # returns list
self.path(node_order)
def return_node(self,label):
for n in self.editor.nodes:
if n.label == label:
return n
def return_edge(self,node_a,node_b):
node = (self.return_node(node_a),self.return_node(node_b))
for edge in self.editor.edges:
if node[0] in edge.nodes and node[1] in edge.nodes:
return edge
if __name__ == '__main__':
MainApp().run()