Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e68fad

Browse files
committed
Restructure repo and add code for "Representing Graphs in Python" article
1 parent 35b4513 commit 0e68fad

File tree

15 files changed

+280
-18
lines changed

15 files changed

+280
-18
lines changed
-727 Bytes
Binary file not shown.
-1.21 KB
Binary file not shown.
File renamed without changes.

‎Classes/node.py renamed to ‎base_classes/node.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ def __init__(self, name, id=-1):
44
self.m_name = str(name)
55

66
def __str__(self):
7-
return self.m_name
7+
return "node "+self.m_name
88

99
def __repr__(self):
10-
return self.m_name
10+
return "node "+self.m_name
1111

1212

1313
def set_id(self, id):
1414
self.m_id = id
1515

1616
def get_id(self):
1717
return self.m_id
18+
19+
def get_name(self):
20+
return self.m_name
1821

1922
def __eq__(self, other):
2023
return self.m_name == other.m_name
File renamed without changes.

‎Classes/graph_adj_list_dict.py renamed to ‎graph_implementations/graph_adj_list_dict.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from node import Node
2-
from graph import Graph
1+
from base_classes.node import Node
2+
from base_classes.graph import Graph
33
from queue import Queue
44

55
class AdjListGraph(Graph):
6+
67
###################################
78
# Constructor
89
###################################
@@ -23,15 +24,15 @@ def add_edge(self, node1_name, node2_name, weight=1):
2324
if (node1 not in self.m_nodes):
2425
node1_id = len(self.m_nodes)
2526
node1.set_id(node1_id)
26-
self.m_nodes.add(node1)
27+
self.m_nodes.append(node1)
2728
self.m_graph[node1_name] = set()
2829
else:
2930
node1 = self.get_node_by_name(node1_name)
3031

3132
if (node2 not in self.m_nodes):
3233
node2_id = len(self.m_nodes)
3334
node2.set_id(node2_id)
34-
self.m_nodes.add(node2)
35+
self.m_nodes.append(node2)
3536
self.m_graph[node2_name] = set()
3637
else:
3738
node2= self.get_node_by_name(node2_name)
@@ -105,14 +106,19 @@ def get_nodes(self):
105106
###################################
106107
# DFS Search
107108
###################################
108-
def dfs(self, start, target, path = [], visited = set()):
109-
path.append(start)
110-
visited.add(start)
111-
if start == target:
109+
def dfs(self, start_node_name, target_node_name, path = [], visited = set()):
110+
start_node = self.get_node_by_name(start_node_name)
111+
print("Start node:", start_node)
112+
target_node = self.get_node_by_name(target_node_name)
113+
print("Target node:", target_node)
114+
path.append(start_node)
115+
visited.add(start_node)
116+
if start_node == target_node:
112117
return path
113-
for (neighbour, weight) in self.m_graph[start]:
118+
for (neighbour, weight) in self.m_graph[start_node_name]:
119+
print(start_node, self.m_graph[start_node_name])
114120
if neighbour not in visited:
115-
result = self.dfs(neighbour, target, path, visited)
121+
result = self.dfs(neighbour.get_name(), target_node_name, path, visited)
116122
if result is not None:
117123
return result
118124
path.pop()
@@ -192,8 +198,18 @@ def bfs_traversal(self, start_node):
192198
[4, 3, 11]
193199
]
194200
# g.load_from_dict(adjacency_list)
195-
g.load_from_edge_list(edge_list)
196-
for node in g.get_nodes():
197-
print(node)
201+
# g.load_from_edge_list(edge_list)
202+
203+
# For dfs
204+
g.add_edge(0, 1)
205+
g.add_edge(0, 2)
206+
g.add_edge(1, 3)
207+
g.add_edge(2, 3)
208+
g.add_edge(3, 4)
198209
print(g)
210+
print(g.dfs(0, 3))
211+
212+
213+
214+
199215

‎Classes/graph_adj_matrix.py renamed to ‎graph_implementations/graph_adj_matrix.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
class Graph:
1+
from base_classes.graph import Graph
2+
3+
class AdjMatrixGraph(Graph):
4+
25
###################################
36
# Constructor
47
###################################

‎Classes/graph_edge_list.py renamed to ‎graph_implementations/graph_edge_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from node import Node
2-
from graph import Graph
1+
from base_classes.node import Node
2+
from base_classes.graph import Graph
33

44
class EdgeListGraph(Graph):
55
###################################
File renamed without changes.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /