Created
May 16, 2012 09:47
-
-
Save melpomene/2709136 to your computer and use it in GitHub Desktop.
Test difference in speed and memory usage of DFS and BFS.
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
import time,sys | |
from random import randint | |
class node(): | |
def __init__(self, child1, child2, data): | |
self.left = child1 | |
self.right = child2 | |
self.data = data | |
def __str__(self): | |
return str(self.data) | |
def print_tree(self): | |
import networkx as nx | |
G=nx.Graph() | |
queue = [self] | |
while len(queue) != 0: | |
current = queue.pop(0) | |
G.add_node(current) | |
if current.left is not None: | |
G.add_edge(current, current.left) | |
queue.append(current.left) | |
if current.right is not None: | |
G.add_edge(current, current.right) | |
queue.append(current.right) | |
import matplotlib.pyplot as plt | |
nx.draw_spectral(G) | |
plt.show() | |
def buildTree(nr=10): | |
if nr == 0: | |
return None | |
return node(buildTree(nr-1), buildTree(nr-1), randint(0, 99)) | |
def BFS(value, root): | |
queue = [root] | |
maxlen = 1 | |
iterations = 0 | |
while len(queue) != 0: | |
iterations += 1 | |
current = queue.pop(0) | |
if current.data == value: | |
break | |
else: | |
if current.left is not None: | |
queue.append(current.left) | |
if current.right is not None: | |
queue.append(current.right) | |
if len(queue) > maxlen: | |
maxlen = len(queue) | |
print "Bredth first search" | |
print "-------------------" | |
print "Max length of data: " + str(maxlen) | |
print "Found data in " + str(iterations) + " iterations" | |
def DFS(value, root): | |
stack = [root] | |
maxlen = 1 | |
iterations = 0 | |
while len(stack) != 0: | |
iterations += 1 | |
current = stack.pop() | |
if current.data == value: | |
break | |
else: | |
if current.left is not None: | |
stack.append(current.left) | |
if current.right is not None: | |
stack.append(current.right) | |
if len(stack) > maxlen: | |
maxlen = len(stack) | |
print "Depth first search" | |
print "------------------" | |
print "Max length of data: " + str(maxlen) | |
print "Found data in " + str(iterations) + " iterations" | |
if __name__ == "__main__": | |
if len(sys.argv[1]) == 2 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): | |
print "python tree.py <depth-of-tree> <search-for-this-nr> [-v]" | |
print "-v \tVisualize graph." | |
print "Data is between 0 and 99" | |
exit() | |
if len(sys.argv) < 3: | |
size = 20 | |
search_for = 19 | |
else: | |
size = int(sys.argv[1]) | |
search_for = int(sys.argv[2]) | |
print "Tree size " + str(size) | |
print "Searching for " + str(search_for) | |
print "Constructing tree..." | |
root = buildTree(size) | |
print "Done" | |
print "" | |
t = time.time() | |
BFS(search_for, root) | |
print "It took: " +str(t - time.time()) | |
print "" | |
t = time.time() | |
DFS(search_for, root) | |
print "It took: " +str(t - time.time()) | |
if "-v" in sys.argv: | |
root.print_tree() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that the trees are binary!
Here is some sample output