Advertisement
Guest User

Untitled

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