Advertisement
RokasC

Untitled

Mar 12th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. #!/user/bin/env python3
  2.  
  3. import random
  4. import sys
  5. import networkx as nx
  6. import matplotlib.pyplot as plt
  7.  
  8. class Vertex(object):
  9. id = 0
  10. adj = []
  11.  
  12. def __init__(self, id):
  13. self.id = id
  14. self.adj = []
  15.  
  16. def __repr__(self):
  17. return "{0} : {1} : {2}".format(self.id, len(self.adj), self.adj)
  18.  
  19. def _add_edge(self, current, next):
  20. current.adj.append(next.id)
  21. next.adj.append(current.id)
  22.  
  23. def add_egde(self, a, b, graph):
  24. for i in graph:
  25. if i.id == a:
  26. i.adj.append(int(b))
  27. elif i.id == b:
  28. i.adj.append(int(a))
  29. else:
  30. print("Doesnt exists", i.id, a, type(i.id), type(a))
  31.  
  32.  
  33. def check_if_int(value):
  34. try:
  35. int(value)
  36. return True
  37. except:
  38. print("Enter intger type")
  39. return False
  40.  
  41. def visualize_graph(graph):
  42. G = nx.Graph()
  43.  
  44. for current in graph:
  45. G.add_node(current.id)
  46.  
  47. temp = 0
  48. temp_current = 0
  49. for current in graph:
  50. for i in current.adj:
  51. if temp_current == current.id:
  52. G.add_edge(current.id, i)
  53. temp = temp + 1
  54. else:
  55. temp_current = temp_current + 1
  56. temp = 0
  57. nx.draw(G)
  58. plt.show()
  59.  
  60.  
  61. if len(sys.argv) == 4:
  62. v_count = int(sys.argv[1])
  63. aMin = int(sys.argv[2])
  64. aMax = int(sys.argv[3])
  65.  
  66. graph = []
  67.  
  68. for i in range(0, v_count):
  69. graph.append(Vertex(i))
  70.  
  71. density = 0.3
  72. density_value = random.random()
  73.  
  74. for current in graph:
  75. for next in graph:
  76. if current.id != next.id:
  77. if len(next.adj) < aMax and len(current.adj) < aMax:
  78. if density > density_value:
  79. if current.id not in next.adj:
  80. current.adj.append(next.id)
  81. next.adj.append(current.id)
  82. density_value = random.random()
  83.  
  84. for vertex in graph:
  85. print(vertex)
  86.  
  87. visualize_graph(graph)
  88. else:
  89. pass
  90. # print("Komandos: \n\
  91. # *** iveskit addV (virsunes index'a) pvz.: addV 1 \n\
  92. # *** iveskit addE virsuneA virsuneB pvz.: addE 1 2 \n\
  93. # *** iveskite quit, jeigu norite iseiti is programos ")
  94. # grap = []
  95. # command = input("Iveskite normima komanda:")
  96. # while command != "quit":
  97. # if command.lower() == "addv":
  98. # id = input("Enter vertex id: ")
  99. # if check_if_int(id):
  100. # v = Vertex(id)
  101. # grap.append(v)
  102. # print("Vertex is added")
  103. # elif command.lower() == "adde":
  104. # vertices = input("Enter vertices to add edge between 1,2: ")
  105. # temp = vertices.split(',')
  106. # if len(temp) == 2:
  107. # a, b = temp
  108. # v.add_egde(a, b, grap)
  109. # # v.add_egde(int(a), int(b), grap)
  110. # print("Edge is added")
  111. # for i in grap:
  112. # print(i)
  113. # visualize_graph(grap)
  114. # command = input("Input: ")
  115. # else:
  116. # print("Thanks for stopping by!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement