def createLinkedList(n): g = WeightedGraph() for i in range(1, n + 1): g.addNode(createLabel(i)) nodes = g.getAllNodes() for i in range(len(nodes) - 1): nodes[i].neighbors[nodes[i + 1]] = 1 return g
def createRandomCompleteWeightedGraph(n): g = WeightedGraph() for i in range(1, n + 1): g.addNode(createLabel(i)) nodes = g.getAllNodes() for i in nodes: x = nodes.index(i) suggested = nodes[:x] + nodes[x + 1:] for j in suggested: randomWeight = randint(1, 15) g.addDirectedEdge(i, j, randomWeight) return g
def createRandomCompleteWeightedGraph(n): g = WeightedGraph() for i in range(1,n+1): g.addNode(createLabel(i)) nodes = g.getAllNodes() for i in nodes: x = nodes.index(i) # Make a list including all values but the current suggestedList = nodes[:x]+nodes[x+1:] for j in suggestedList: randomWeight = randint(1, 15) g.addDirectedEdge(i, j, randomWeight) return g
def createRandomDAGIter(n): # Create new DirectedGraph and add n amount of nodes to it g = DirectedGraph() for i in range(1, n + 1): g.addNode(createLabel(i)) # Copy the list of the nodes from the graph # so we can pop from list nodes = g.getAllNodes().copy() # Shuffle the nodes so the graph doesn't # start with "A" every time shuffle(nodes) # While there are nodes in the list while len(nodes) > 0: cur = nodes.pop(0) if len(nodes) <= 1: break # XXX Choose a random amount of children XXX # Make nodes have 2 children num = 2 # randrange(1,len(nodes)) # Add a random sample of num length # the neighbor of the cur for i in sample(nodes, num): g.addDirectedEdge(cur, i) # For every neighbor of cur do the same for n in cur.neighbors: nodes.pop(nodes.index(n)) if len(nodes) <= 1: break num = 2 # randrange(1,len(nodes)) for i in sample(nodes, num): g.addDirectedEdge(n, i) return g
def createRandomGridGraph(n): g = GridGraph() cols = n # floor(sqrt(n)) # Pick how long each row is rowcount = colcount = 0 randomNums = list(range(1, n**2 + 1)) # randomize range list 1..n^2 shuffle(randomNums) for idx in randomNums: if colcount == cols: colcount = 0 rowcount += 1 x = rowcount y = colcount g.addGridNode(x, y, createLabel(idx)) # Insert a node at (x,y) nodes = g.getAllNodes() # Get the updated node grid if colcount > 0: # If its not the leftmost node in grid if randint(0, 2): # 2/3 chance of creating a node, 50% was too little g.addUndirectedEdge(nodes[x][y - 1], nodes[x][y]) if idx > cols: # If its not the first row if randint(0, 2): # 2/3 chance of creating a node, 50% was too little g.addUndirectedEdge(nodes[x - 1][y], nodes[x][y]) colcount += 1 return g