I have a Graph of this structure:
G = {
'1':['100', '134', '1435'],
'145':['4', '2345', '253'],
'3773':['12'], '773':['1211', '629']}
The graph is actually so large, with 6378 nodes and 39932 edges. My problem is that the graph is disconnected, and i want the graph to be fully connected with no disconnected component.
Can someone help me with the python code please? I have been cracking my head since sunday.Thank you
def add_disconnected_nodes(Graph, begin):
gkey = []
cap_vertices = []
for vertex in Graph.keys():
gkey.append(vertex)
if begin in gkey:
begin = gkey[0]
for vertices in Graph.keys():
if vertices != begin and vertices not in Graph[begin]:
cap_vertices.append(vertices)
#Graph[begin] = [Graph[begin], cap_vertices]
Graph[begin] + cap_vertices
Graph.update()
return Graph
I wrote this code, but it ran without an error. Still, it would not get the job done. I know i am not doing something right
EDITED: So i rewrote the code this way, now it is taking forever to execute. I chose a start vertex as key, and every other node node in the value of this key, i tried to add to the value. Maybe i am doing something wrong; Someone please help me!
def add_disconnected_nodes(Graph, begin):
if begin not in Graph:
return False
beg = {}
bbg = []
for vet in Graph.keys():
bbg.append(vet)
bba = []
while len(bbg) != 0:
for ls in bbg:
if ls != begin and ls not in Graph[begin]:
bba.append(ls)
bbg.remove(ls)
if len(bbg) == 0:
break
beg[begin] = Graph[begin] + bba
Graph.update(beg)
return Graph