Skip to content

Commit 2919322

Browse files
committed
Added graph examples
1 parent 9546390 commit 2919322

14 files changed

+26
-9
lines changed

Algorithms/BellmamFord.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def Bellman_Ford(G: Graph, s: Node, w=weight_function):
2323
Initialize_single_source(G, s)
2424
for i in range(len(G.nodes) - 1):
2525
for u, v in G.get_all_lines():
26-
Relax(u, v, w(u, v))
26+
Relax(u, v, w)
2727
for u, v in G.get_all_lines():
2828
if v.d > u.d + w(u, v):
2929
return False

Algorithms/Dijkstra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def Dijkstra(G: Graph, s: Node, w=weight_function):
2727
u = Q.extract_min()
2828
T.add(u)
2929
for v in u.adjacent_nodes.keys():
30-
Relax(u, v, w(u, v))
30+
Relax(u, v, w)

Builder/GraphBuilder.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ def add_line(self, event):
6363
self.canvas.create_text(mid_x, mid_y, text=str(weight), fill="black", font=("Helvetica", 15, "bold"))
6464
else:
6565
self.graph.add_line(self.selected_node, label)
66-
self.canvas.create_line(start_x, start_y, end_x, end_y,
67-
fill="black", width=3, arrow=tk.LAST, arrowshape=(15, 20, 8))
66+
if self.directed:
67+
self.canvas.create_line(start_x, start_y, end_x, end_y,
68+
fill="black", width=3, arrow=tk.LAST, arrowshape=(15, 20, 8))
69+
else:
70+
self.canvas.create_line(start_x, start_y, end_x, end_y, fill="black", width=3)
6871
else:
6972
messagebox.showwarning("Line exists", "Line already exists in the graph.")
7073
self.selected_node = None

Builder/GraphCreator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def __init__(self, label):
2828
def __repr__(self):
2929
return f"{circle_value(self.label)}"
3030

31+
def __str__(self):
32+
return f"{self.label}"
33+
3134
def add_adj(self, node, weight=None):
3235
self.adjacent_nodes[node] = weight
3336
self.adjacent_nodes = OrderedDict(sorted(self.adjacent_nodes.items(), key=lambda item: item[0].label))
@@ -60,7 +63,7 @@ def add_node(self, node: str):
6063
:param node: Node name
6164
"""
6265
self.nodes.append(self.NodeClass(node))
63-
self.nodes.sort(key=lambda x: x.label)
66+
self.nodes.sort(key=lambda x: (x.label.lower() != 's', x.label.lower()))
6467

6568
def add_line(self, node1: str, node2: str, weight: int = None):
6669
"""
@@ -182,7 +185,7 @@ def save(self, filename: str):
182185
183186
:param filename: a name of the file without extension
184187
"""
185-
with open(filename, 'wb') as file:
188+
with open('Graphs/' + filename, 'wb') as file:
186189
pickle.dump(self, file)
187190

188191
@classmethod
@@ -192,7 +195,7 @@ def load(cls, filename: str):
192195
193196
:param filename: a name of the file without extension
194197
"""
195-
with open(filename, 'rb') as file:
198+
with open('Graphs/' + filename, 'rb') as file:
196199
return pickle.load(file)
197200

198201
def has_cycle(self):

Builder/GraphCreatorApp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def create_section1(self):
5252
self.next_button.pack()
5353

5454
def load_graph(self):
55-
self.loaded_filename = basename(filedialog.askopenfilename(title="Select Graph File", initialdir=dirname(dirname(realpath(__file__)))))
55+
self.loaded_filename = basename(filedialog.askopenfilename(title="Select Graph File", initialdir=dirname(dirname(realpath(__file__))) + '/Graphs'))
5656
if self.loaded_filename:
5757
messagebox.showinfo("Info", f"Graph loaded from file: {self.loaded_filename}")
5858
self.submit_data()
@@ -178,6 +178,5 @@ def submit_data(self):
178178
self.root.quit()
179179
self.root.destroy()
180180

181-
182181
def return_result(self):
183182
return self.result

Graphs/BFSVideoExample

529 Bytes
Binary file not shown.

Graphs/BellmanFordVideoExample

390 Bytes
Binary file not shown.

Graphs/DFSVideoExample

564 Bytes
Binary file not shown.

Graphs/DijkstraVideoExample

355 Bytes
Binary file not shown.

Graphs/FloydWarshallVideoExample

314 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)