Created
December 27, 2015 23:33
-
-
Save s-leroux/b1630402fb0e39f7cfa5 to your computer and use it in GitHub Desktop.
Build random graphs to collect statistics about search in adjacency lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from random import randint | |
from collections import defaultdict | |
for i in range(10, 10100, 100): | |
nEdges = 50 | |
# Build i random pair of nodes | |
edges = set() # ensure uniqueness of each edge in the graph | |
while len(edges) < nEdges: | |
edge = (randint(1,i), randint(1,i)) | |
edges.add(edge) | |
# Build adjacency list | |
adjlist = defaultdict(list) | |
for start, end in edges: | |
adjlist[start].append(end) | |
# find all nodes to "1" -- counting iterations | |
# print(adjlist) | |
count = 0 | |
for k, l in adjlist.items(): | |
for n in l: | |
count += 1 | |
if n == 1: | |
break | |
print(i, count, sep="\t") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment