Skip to content

Commit

Permalink
*added docstring and doctest for find_isolated_nodes (TheAlgorithms#1…
Browse files Browse the repository at this point in the history
…0684)

*added docstring and doctest for edglist

*added docstring and doctest for adjm

Co-authored-by: Ketan <ketanbmahajan@gmail.com>
  • Loading branch information
ketan96-m and ketan96-m authored Oct 19, 2023
1 parent bd3072b commit 6987614
Showing 1 changed file with 75 additions and 6 deletions.
81 changes: 75 additions & 6 deletions graphs/basic_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,29 @@ def topo(g, ind=None, q=None):


def adjm():
n = input().strip()
r"""
Reading an Adjacency matrix
Parameters:
None
Returns:
tuple: A tuple containing a list of edges and number of edges
Example:
>>> # Simulate user input for 3 nodes
>>> input_data = "4\n0 1 0 1\n1 0 1 0\n0 1 0 1\n1 0 1 0\n"
>>> import sys,io
>>> original_input = sys.stdin
>>> sys.stdin = io.StringIO(input_data) # Redirect stdin for testing
>>> adjm()
([(0, 1, 0, 1), (1, 0, 1, 0), (0, 1, 0, 1), (1, 0, 1, 0)], 4)
>>> sys.stdin = original_input # Restore original stdin
"""
n = int(input().strip())
a = []
for _ in range(n):
a.append(map(int, input().strip().split()))
a.append(tuple(map(int, input().strip().split())))
return a, n


Expand Down Expand Up @@ -260,10 +279,29 @@ def prim(g, s):


def edglist():
n, m = map(int, input().split(" "))
r"""
Get the edges and number of edges from the user
Parameters:
None
Returns:
tuple: A tuple containing a list of edges and number of edges
Example:
>>> # Simulate user input for 3 edges and 4 vertices: (1, 2), (2, 3), (3, 4)
>>> input_data = "4 3\n1 2\n2 3\n3 4\n"
>>> import sys,io
>>> original_input = sys.stdin
>>> sys.stdin = io.StringIO(input_data) # Redirect stdin for testing
>>> edglist()
([(1, 2), (2, 3), (3, 4)], 4)
>>> sys.stdin = original_input # Restore original stdin
"""
n, m = tuple(map(int, input().split(" ")))
edges = []
for _ in range(m):
edges.append(map(int, input().split(" ")))
edges.append(tuple(map(int, input().split(" "))))
return edges, n


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


def krusk(e_and_n):
# Sort edges on the basis of distance
"""
Sort edges on the basis of distance
"""
(e, n) = e_and_n
e.sort(reverse=True, key=lambda x: x[2])
s = [{i} for i in range(1, n + 1)]
Expand All @@ -299,8 +339,37 @@ def krusk(e_and_n):
break


# find the isolated node in the graph
def find_isolated_nodes(graph):
"""
Find the isolated node in the graph
Parameters:
graph (dict): A dictionary representing a graph.
Returns:
list: A list of isolated nodes.
Examples:
>>> graph1 = {1: [2, 3], 2: [1, 3], 3: [1, 2], 4: []}
>>> find_isolated_nodes(graph1)
[4]
>>> graph2 = {'A': ['B', 'C'], 'B': ['A'], 'C': ['A'], 'D': []}
>>> find_isolated_nodes(graph2)
['D']
>>> graph3 = {'X': [], 'Y': [], 'Z': []}
>>> find_isolated_nodes(graph3)
['X', 'Y', 'Z']
>>> graph4 = {1: [2, 3], 2: [1, 3], 3: [1, 2]}
>>> find_isolated_nodes(graph4)
[]
>>> graph5 = {}
>>> find_isolated_nodes(graph5)
[]
"""
isolated = []
for node in graph:
if not graph[node]:
Expand Down

0 comments on commit 6987614

Please sign in to comment.