Created
June 16, 2013 23:17
-
-
Save crustymonkey/5793809 to your computer and use it in GitHub Desktop.
breadth first traversal
This file contains hidden or 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/python | |
def getLinks(url): | |
pass | |
#### SOL 1 | |
def handleLinks(links , seen , out): | |
nextLayer = [] | |
for link in links: | |
if link in seen: | |
continue | |
print >> out , link | |
seen.add(link) | |
nextLayer.extend(getLinks(link)) | |
if len(nextLayer) > 0: | |
return handleLinks(nextLayer , seen , out) | |
start = 'http://example.com' | |
seen = set() | |
initLinks = getLinks(start) | |
outfile = '/var/tmp/out' | |
fh = open(outfile , 'w') | |
handleLinks(initLinks , seen , out) | |
fh.close() | |
#### SOL 2 | |
from collections import deque | |
start = 'http://example.com' | |
seen = set() | |
q = deque() | |
q.extend(getLinks(start)) | |
outfile = '/var/tmp/out' | |
fh = open(outfile , 'w') | |
while q: | |
link = q.popleft() | |
if link in seen: | |
continue | |
seen.add(link) | |
print >> fh , link | |
q.extend(getLinks(link)) | |
fh.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment