Skip to content

Commit 3080bd0

Browse files
ketan96-msedatguzelsemme
authored andcommitted
*added docstring and doctest for find_isolated_nodes (TheAlgorithms#10684)
*added docstring and doctest for edglist *added docstring and doctest for adjm Co-authored-by: Ketan <ketanbmahajan@gmail.com>
1 parent 51109f6 commit 3080bd0

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

graphs/basic_graphs.py

+75-6
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,29 @@ def topo(g, ind=None, q=None):
185185

186186

187187
def adjm():
188-
n = input().strip()
188+
r"""
189+
Reading an Adjacency matrix
190+
191+
Parameters:
192+
None
193+
194+
Returns:
195+
tuple: A tuple containing a list of edges and number of edges
196+
197+
Example:
198+
>>> # Simulate user input for 3 nodes
199+
>>> input_data = "4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 0 1 0\n"
200+
>>> import sys,io
201+
>>> original_input = sys.stdin
202+
>>> sys.stdin = io.StringIO(input_data) # Redirect stdin for testing
203+
>>> adjm()
204+
([(0, 1, 0, 1), (1, 0, 1, 0), (0, 1, 0, 1), (1, 0, 1, 0)], 4)
205+
>>> sys.stdin = original_input # Restore original stdin
206+
"""
207+
n = int(input().strip())
189208
a = []
190209
for _ in range(n):
191-
a.append(map(int, input().strip().split()))
210+
a.append(tuple(map(int, input().strip().split())))
192211
return a, n
193212

194213

@@ -260,10 +279,29 @@ def prim(g, s):
260279

261280

262281
def edglist():
263-
n, m = map(int, input().split(" "))
282+
r"""
283+
Get the edges and number of edges from the user
284+
285+
Parameters:
286+
None
287+
288+
Returns:
289+
tuple: A tuple containing a list of edges and number of edges
290+
291+
Example:
292+
>>> # Simulate user input for 3 edges and 4 vertices: (1, 2), (2, 3), (3, 4)
293+
>>> input_data = "4 3\n1 2\n2 3\n3 4\n"
294+
>>> import sys,io
295+
>>> original_input = sys.stdin
296+
>>> sys.stdin = io.StringIO(input_data) # Redirect stdin for testing
297+
>>> edglist()
298+
([(1, 2), (2, 3), (3, 4)], 4)
299+
>>> sys.stdin = original_input # Restore original stdin
300+
"""
301+
n, m = tuple(map(int, input().split(" ")))
264302
edges = []
265303
for _ in range(m):
266-
edges.append(map(int, input().split(" ")))
304+
edges.append(tuple(map(int, input().split(" "))))
267305
return edges, n
268306

269307

@@ -278,7 +316,9 @@ def edglist():
278316

279317

280318
def krusk(e_and_n):
281-
# Sort edges on the basis of distance
319+
"""
320+
Sort edges on the basis of distance
321+
"""
282322
(e, n) = e_and_n
283323
e.sort(reverse=True, key=lambda x: x[2])
284324
s = [{i} for i in range(1, n + 1)]
@@ -299,8 +339,37 @@ def krusk(e_and_n):
299339
break
300340

301341

302-
# find the isolated node in the graph
303342
def find_isolated_nodes(graph):
343+
"""
344+
Find the isolated node in the graph
345+
346+
Parameters:
347+
graph (dict): A dictionary representing a graph.
348+
349+
Returns:
350+
list: A list of isolated nodes.
351+
352+
Examples:
353+
>>> graph1 = {1: [2, 3], 2: [1, 3], 3: [1, 2], 4: []}
354+
>>> find_isolated_nodes(graph1)
355+
[4]
356+
357+
>>> graph2 = {'A': ['B', 'C'], 'B': ['A'], 'C': ['A'], 'D': []}
358+
>>> find_isolated_nodes(graph2)
359+
['D']
360+
361+
>>> graph3 = {'X': [], 'Y': [], 'Z': []}
362+
>>> find_isolated_nodes(graph3)
363+
['X', 'Y', 'Z']
364+
365+
>>> graph4 = {1: [2, 3], 2: [1, 3], 3: [1, 2]}
366+
>>> find_isolated_nodes(graph4)
367+
[]
368+
369+
>>> graph5 = {}
370+
>>> find_isolated_nodes(graph5)
371+
[]
372+
"""
304373
isolated = []
305374
for node in graph:
306375
if not graph[node]:

0 commit comments

Comments
 (0)