@@ -185,10 +185,29 @@ def topo(g, ind=None, q=None):
185
185
186
186
187
187
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 ())
189
208
a = []
190
209
for _ in range (n ):
191
- a .append (map (int , input ().strip ().split ()))
210
+ a .append (tuple ( map (int , input ().strip ().split () )))
192
211
return a , n
193
212
194
213
@@ -260,10 +279,29 @@ def prim(g, s):
260
279
261
280
262
281
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 (" " )))
264
302
edges = []
265
303
for _ in range (m ):
266
- edges .append (map (int , input ().split (" " )))
304
+ edges .append (tuple ( map (int , input ().split (" " ) )))
267
305
return edges , n
268
306
269
307
@@ -278,7 +316,9 @@ def edglist():
278
316
279
317
280
318
def krusk (e_and_n ):
281
- # Sort edges on the basis of distance
319
+ """
320
+ Sort edges on the basis of distance
321
+ """
282
322
(e , n ) = e_and_n
283
323
e .sort (reverse = True , key = lambda x : x [2 ])
284
324
s = [{i } for i in range (1 , n + 1 )]
@@ -299,8 +339,37 @@ def krusk(e_and_n):
299
339
break
300
340
301
341
302
- # find the isolated node in the graph
303
342
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
+ """
304
373
isolated = []
305
374
for node in graph :
306
375
if not graph [node ]:
0 commit comments