'How to ensure the training set is connected during train-test split?

I am doing research in link prediction on social network. I divided my data set into train and test set for each experiment using python and networkx as shown below:

for experiment in range(experiments):
    test_edge_list_split = random.sample(G.edges(), int(0.1 * G.number_of_edges()))
    test_edge_list = list(test_edge_list_split)
# Remove some edges
    training_graph = G.copy()
    training_graph.remove_edges_from(test_edge_list_split)

training_graph.to_undirected()
print(nx.is_connected(training_graph))

but i want to make sure that, in each experiment before i remove test data the remaining training graph is connected.How can i do it? Any help will be much appreciated .Than you alot



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source