Advertisement
Guest User

Untitled

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