Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. __author__ = 'kaare_000'
  2. import matplotlib.pyplot as plt
  3. import networkx as nx
  4. import random as r
  5.  
  6. def bfs(dict, start, finish):
  7.     print(dict)
  8.     current= start
  9.     nodes = [current]
  10.     j=1
  11.     while(finish != current):
  12.         current = nodes[0]
  13.         for i in range(len(dict[current])):
  14.             nodes.append(dict[current][i])
  15.         current = nodes[j]
  16.         j += 1
  17.         nodes.pop(0)
  18.     print('Ilma mäluta = ',j)
  19.  
  20.  
  21.  
  22. def bfs2(dict, start, finish):
  23.     current= start
  24.     nodes = []
  25.     nodes.append(current)
  26.     j=1
  27.     visited = []
  28.     while(finish not in set(visited)):
  29.         current = nodes[0]
  30.         visited = list(visited)
  31.         visited.append(nodes[0])
  32.         visited = set(visited)
  33.         for i in range(len(dict[current])):
  34.             if dict[current][i] not in visited:
  35.                 nodes.append(dict[current][i])
  36.         j += 1
  37.         nodes.pop(0)
  38.  
  39.     print('Mäluga, külastatud tipud = ', j)
  40.  
  41. def bfs3(dict, start, finish):
  42.     current= start
  43.     nodes = [current]
  44.     j=1
  45.     seen = [current]
  46.     for i in range(len(dict[current])):
  47.         seen.append(dict[current][i])
  48.     while(finish not in seen):
  49.         for i in range(len(dict[current])):
  50.             if dict[current][i] not in set(seen):
  51.                 seen.append(dict[current][i])
  52.             else:
  53.                 nodes.append(dict[current][i])
  54.         nodes.pop(0)
  55.         current = nodes[j]
  56.         j += 1
  57.  
  58.     print('Mäluga, nähtud tipud = ', j)
  59.  
  60. def main(n, probability, seed):
  61.     start = r.randint(0,n)
  62.     finish = r.randint(0,n)
  63.     G = nx.gnp_random_graph(n,probability,seed=seed)
  64.     dict = nx.to_dict_of_lists(G)
  65.  
  66.     bfs(dict, start, finish)
  67.     bfs2(dict, start, finish)
  68.     bfs3(dict, start, finish)
  69.  
  70. #main(512, r.choice([0.2,0.3,0.4,0.5]), '142845IAP')
  71.  
  72. def printing():
  73.     for i in range(4,10):
  74.         n = 2**i
  75.         prob = r.choice([0.3])
  76.         print('N = ', n,'\nProbability = ', prob)
  77.         main(n, prob, '142845iapb')
  78.         print()
  79.  
  80. printing()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement