Skip to content

Commit 4a41469

Browse files
committed
first test
1 parent b22fab0 commit 4a41469

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"githubPullRequests.ignoredPullRequestBranches": [
33
"master"
4-
]
4+
],
5+
"python.testing.pytestArgs": [
6+
"."
7+
],
8+
"python.testing.unittestEnabled": false,
9+
"python.testing.pytestEnabled": true
510
}

graphs/directed_and_undirected_weighted_graph.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,37 @@
88

99
class DirectedGraph:
1010
def __init__(self):
11+
"""
12+
Initialize an empty directed graph.
13+
14+
Example:
15+
>>> g = DirectedGraph()
16+
>>> g.graph
17+
{}
18+
"""
1119
self.graph = {}
1220

1321
# adding vertices and edges
1422
# adding the weight is optional
1523
# handles repetition
1624
def add_pair(self, u, v, w=1):
25+
"""
26+
Add a directed edge with an optional weight.
27+
28+
Args:
29+
u: Starting node.
30+
v: Destination node.
31+
w: Weight (default is 1).
32+
33+
Example:
34+
>>> g = DirectedGraph()
35+
>>> g.add_pair(1, 2)
36+
>>> g.graph
37+
{1: [[1, 2]], 2: []}
38+
>>> g.add_pair(2, 3, 5)
39+
>>> g.graph
40+
{1: [[1, 2]], 2: [[5, 3]], 3: []}
41+
"""
1742
if self.graph.get(u):
1843
if self.graph[u].count([w, v]) == 0:
1944
self.graph[u].append([w, v])
@@ -27,6 +52,21 @@ def all_nodes(self):
2752

2853
# handles if the input does not exist
2954
def remove_pair(self, u, v):
55+
"""
56+
Remove a directed edge from u to v.
57+
58+
Args:
59+
u: Starting node.
60+
v: Destination node.
61+
62+
Example:
63+
>>> g = DirectedGraph()
64+
>>> g.add_pair(1, 2)
65+
>>> g.add_pair(1, 3)
66+
>>> g.remove_pair(1, 2)
67+
>>> g.graph
68+
{1: [[1, 3]], 2: [], 3: []}
69+
"""
3070
if self.graph.get(u):
3171
for _ in self.graph[u]:
3272
if _[1] == v:
@@ -487,3 +527,8 @@ def bfs_time(self, s=-2):
487527
self.bfs(s)
488528
end = time()
489529
return end - begin
530+
531+
532+
if __name__ == "__main__":
533+
import doctest
534+
doctest.testmod()

0 commit comments

Comments
 (0)