Skip to content

Commit 2daf7c3

Browse files
committed
#9943 : Adding coverage test for basic_graphs.py
1 parent 52602ea commit 2daf7c3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

graphs/basic_graphs.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ def _input(message):
88
def initialize_unweighted_directed_graph(
99
node_count: int, edge_count: int
1010
) -> dict[int, list[int]]:
11+
"""
12+
Example:
13+
Edge 1: <node1> <node2> 1 2
14+
Edge 2: <node1> <node2> 3 2
15+
Edge 3: <node1> <node2> 2 4
16+
>>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input
17+
>>> initialize_unweighted_directed_graph(4, 3)
18+
Edge 1: <node1> <node2> Edge 2: <node1> <node2> Edge 3: <node1> <node2> {1: [2], 2: [4], 3: [2], 4: []}
19+
"""
1120
graph: dict[int, list[int]] = {}
1221
for i in range(node_count):
1322
graph[i + 1] = []
@@ -21,6 +30,15 @@ def initialize_unweighted_directed_graph(
2130
def initialize_unweighted_undirected_graph(
2231
node_count: int, edge_count: int
2332
) -> dict[int, list[int]]:
33+
"""
34+
Example:
35+
Edge 1: <node1> <node2> 1 2
36+
Edge 2: <node1> <node2> 3 2
37+
Edge 3: <node1> <node2> 2 4
38+
>>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2','3 2', '2 4'])) # input
39+
>>> initialize_unweighted_undirected_graph(4, 3)
40+
Edge 1: <node1> <node2> Edge 2: <node1> <node2> Edge 3: <node1> <node2> {1: [2], 2: [1, 3, 4], 3: [2], 4: [2]}
41+
"""
2442
graph: dict[int, list[int]] = {}
2543
for i in range(node_count):
2644
graph[i + 1] = []
@@ -35,6 +53,17 @@ def initialize_unweighted_undirected_graph(
3553
def initialize_weighted_undirected_graph(
3654
node_count: int, edge_count: int
3755
) -> dict[int, list[tuple[int, int]]]:
56+
"""
57+
Example:
58+
Edge 1: <node1> <node2> <weight> 1 2 1
59+
Edge 2: <node1> <node2> <weight> 3 2 6
60+
Edge 3: <node1> <node2> <weight> 2 4 10
61+
Edge 4: <node1> <node2> <weight> 4 1 7
62+
Edge 5: <node1> <node2> <weight> 4 3 12
63+
>>> import io, sys ; sys.stdin = io.StringIO(chr(10).join(['1 2 1','3 2 6', '2 4 10', '4 1 7', '4 3 12'])) # input
64+
>>> initialize_weighted_undirected_graph(4, 5)
65+
Edge 1: <node1> <node2> <weight> Edge 2: <node1> <node2> <weight> Edge 3: <node1> <node2> <weight> Edge 4: <node1> <node2> <weight> Edge 5: <node1> <node2> <weight> {1: [(2, 1), (4, 7)], 2: [(1, 1), (3, 6), (4, 10)], 3: [(2, 6), (4, 12)], 4: [(2, 10), (1, 7), (3, 12)]}
66+
"""
3867
graph: dict[int, list[tuple[int, int]]] = {}
3968
for i in range(node_count):
4069
graph[i + 1] = []
@@ -77,6 +106,14 @@ def initialize_weighted_undirected_graph(
77106

78107

79108
def dfs(g, s):
109+
"""
110+
>>> dfs({1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}, 1)
111+
1
112+
2
113+
4
114+
5
115+
3
116+
"""
80117
vis, _s = {s}, [s]
81118
print(s)
82119
while _s:
@@ -104,6 +141,17 @@ def dfs(g, s):
104141

105142

106143
def bfs(g, s):
144+
"""
145+
>>> bfs({1: [2, 3], 2: [4, 5], 3: [6, 7], 4: [], 5: [8], 6: [], 7: [], 8: []}, 1)
146+
1
147+
2
148+
3
149+
4
150+
5
151+
6
152+
7
153+
8
154+
"""
107155
vis, q = {s}, deque([s])
108156
print(s)
109157
while q:
@@ -128,6 +176,14 @@ def bfs(g, s):
128176

129177

130178
def dijk(g, s):
179+
"""
180+
dijk({1: [(2, 7), (3, 9), (6, 14)], 2: [(1, 7), (3, 10), (4, 15)], 3: [(1, 9), (2, 10), (4, 11), (6, 2)], 4: [(2, 15), (3, 11), (5, 6)], 5: [(4, 6), (6, 9)], 6: [(1, 14), (3, 2), (5, 9)]}, 1)
181+
7
182+
9
183+
11
184+
20
185+
20
186+
"""
131187
dist, known, path = {s: 0}, set(), {s: 0}
132188
while True:
133189
if len(known) == len(g) - 1:

0 commit comments

Comments
 (0)