Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WinstonCampeau/b714bcfb64206a6aa05fe12c4b2d36db to your computer and use it in GitHub Desktop.
Save WinstonCampeau/b714bcfb64206a6aa05fe12c4b2d36db to your computer and use it in GitHub Desktop.
SageMath - Add all potential edges in a bipartite graph, with weight
#Code produced with the help of Dr. Brett Stevens @ Carleton University
#Add all potential missing edges in a bigraph and with weight
#First copy some bigraph G and generate list of known edges
auxillary_graph = G.copy()
edge_list = []
for edge in auxillary_graph.edges():
edge_list.append((edge[0],edge[1]))
#This ensures pre-existing edges are not considered as potential edges
sample_edge = edge_list[0]
part0 = auxillary_graph.bipartition()[0]
part1 = auxillary_graph.bipartition()[1]
if sample_edge[0] not in part0:
temp_part = part0
part0 = part1
part1 = temp_part
#produces the disconnected 2-tuples between bipartitions and adds an edge of some weight (here, weight is 1000)
for potential_edge in itertools.product(part0,part1):
unicode_potential_edge = (unicode(potential_edge[0]),unicode(potential_edge[1]))
if unicode_potential_edge not in edge_list:
G.add_edge (unicode_potential_edge[0],unicode_potential_edge[1], 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment