filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

if graph.nodes:
    to_traverse = graph.get_to_traverse()
else:
    root_pagename = "Kevin_Bacon"
    to_traverse = {root_pagename}
count = len(graph.nodes)

# traverse whole graph
with open(filename, "a+", encoding="utf-8") as file:
    while to_traverse:
        pagename = to_traverse.pop()
        graph.add_node(pagename)
        count += 1

        print(f"Traversing {pagename}")
        print(count)

        for link in get_links(pagename):
            graph.add_link(pagename, link)
            if link in graph.nodes: continue
            to_traverse.add(link)  # BFS

        graph.write_node_to_file(file, pagename)
Esempio n. 2
0
# Create vertices for interstation connections
###

# interstation cost (mins)
isc = 5
interrows = []
for stopA in fullStops:
    for stopB in fullStops:
        a, b = spsl(stopA)
        c, d = spsl(stopB)
        if (a == c and not (b == d)):
            interrows.append((stopA, stopB, isc))

GMaster = DiGraph('awesome')
for stop in fullStops:
    GMaster.add_node(stop)

for row in rows:
    GMaster.add_edge(row[0], row[1], row[2])

for irow in interrows:
    GMaster.add_edge(irow[0], irow[1], irow[2])

print "Calculate Routes..."

# Holds all routes in memory to aid filtering
fromToDict = {}

for stopFrom in fullStops:
    print "Currently working on:", stopFrom
    stA, t = spsl(stopFrom)