Skip to content

Instantly share code, notes, and snippets.

@negedng
Created August 27, 2019 18:17
Show Gist options
  • Save negedng/03330c14bf191a94d7e0ce73934f8c50 to your computer and use it in GitHub Desktop.
Save negedng/03330c14bf191a94d7e0ce73934f8c50 to your computer and use it in GitHub Desktop.
Functions to generate random weighted trees and complete graphs
def random_tree(V):
heads = [i for i in range(V-1)]
tails = [i+1 for i in range(V-1)]
weights = list(np.random.rand(V-1))
G = [[heads[i],tails[i],weights[i]] for i in range(len(heads))]
return V, G
def random_complete_graph(V):
heads, tails, weights = [], [], []
for i in range(V-1):
for j in range(i+1, V):
heads.append(i)
tails.append(j)
weights = list(np.random.rand(len(heads)))
G = [[heads[i],tails[i],weights[i]] for i in range(len(heads))]
return V, G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment