From 1a5df6bc466c05be4a271e93e8bf4687ab9e62e6 Mon Sep 17 00:00:00 2001 From: Shivam Arora Date: Wed, 31 Oct 2018 16:14:30 +0530 Subject: [PATCH] Added the method to find the isolated nodes in the graph --- graphs/basic_graphs.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/graphs/basic_graphs.py b/graphs/basic_graphs.py index c9a269f1a..3b3abeb17 100644 --- a/graphs/basic_graphs.py +++ b/graphs/basic_graphs.py @@ -1,14 +1,14 @@ from __future__ import print_function try: - raw_input # Python 2 + raw_input # Python 2 except NameError: raw_input = input # Python 3 try: - xrange # Python 2 + xrange # Python 2 except NameError: - xrange = range # Python 3 + xrange = range # Python 3 # Accept No. of Nodes and edges n, m = map(int, raw_input().split(" ")) @@ -141,7 +141,7 @@ from collections import deque def topo(G, ind=None, Q=[1]): if ind is None: - ind = [0] * (len(G) + 1) # SInce oth Index is ignored + ind = [0] * (len(G) + 1) # SInce oth Index is ignored for u in G: for v in G[u]: ind[v] += 1 @@ -279,3 +279,12 @@ def krusk(E_and_n): s[j].update(s[i]) s.pop(i) break + + +# find the isolated node in the graph +def find_isolated_nodes(graph): + isolated = [] + for node in graph: + if not graph[node]: + isolated.append(node) + return isolated