Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. import string
  2. import random
  3.  
  4. class Graph:
  5.     def __init__(self, initial_data = None):
  6.         if initial_data is None:
  7.             initial_data = {}
  8.         self.graph = initial_data
  9.  
  10.     def add_node(self,point):
  11.         self.graph[point] = set()
  12.  
  13.     def add_edge(self,points):
  14.         if points[1] not in self.graph[points[0]] and points[1] in self.graph:
  15.             print(f"Adding edge from {points[0]} to {points[1]}")
  16.             self.graph[points[0]].add(points[1])
  17.         else:
  18.             print("Cannot add edge")
  19.  
  20.     def nodes(self):
  21.         return [x for x in self.graph]
  22.  
  23.     def edges(self):
  24.         return [(k, v if v != set() else None) for k,v in self.graph.items()]
  25.  
  26.     def generate_random(self, size):
  27.         possible_keys = string.ascii_lowercase
  28.         for x in range(size):
  29.             counter = 0
  30.             while counter<40:
  31.                 random_choice = random.choice(possible_keys)
  32.                 if random_choice not in self.graph:
  33.                     self.add_node(random_choice)
  34.                     break
  35.                 counter+=1
  36.             else:
  37.                 break
  38.  
  39.         for y in self.graph:
  40.             counter = 0
  41.             while counter<20:
  42.                 random_choice = random.choice(list(self.graph))
  43.                 if random_choice not in self.graph[y] and random_choice != y:
  44.                     self.add_edge([y, random_choice])
  45.                     if random.random() > 0.5: # I know what youre going to say, don't worry too much about it okay
  46.                         counter = 0           # you think I don't see how ugly this is?
  47.                         while counter < 20:   # it'll be okay, don't worry
  48.                             random_choice = random.choice(list(self.graph))
  49.                             if random_choice not in self.graph[y] and random_choice != y:
  50.                                 self.add_edge([y, random_choice])
  51.                                 break
  52.                     break
  53.                 counter+=1
  54.  
  55.     def explore(self):
  56.         traversed = []
  57.         def traverse(point):
  58.             if point not in traversed:
  59.                 print(f"going from {point} to {self.graph[point]}")
  60.                 traversed.append(point)
  61.                 for x in self.graph[point]:
  62.                     traverse(x)
  63.         while traversed != list(self.graph):
  64.             try:
  65.                 random_choice = random.choice([x for x in self.graph if x not in traversed])
  66.                 traverse(random_choice)
  67.             except IndexError:
  68.                 break
  69.  
  70.  
  71.  
  72.  
  73.  
  74. my_graph = Graph()
  75.  
  76.  
  77. my_graph.generate_random(12)
  78. print(my_graph.nodes())
  79. print(my_graph.edges())
  80. my_graph.explore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement