Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/env python3
- import random
- import sys
- import networkx as nx
- import matplotlib.pyplot as plt
- class Vertex(object):
- id = 0
- adj = []
- def __init__(self, id):
- self.id = id
- self.adj = []
- def __repr__(self):
- return "{0} : {1} : {2}".format(self.id, len(self.adj), self.adj)
- def _add_edge(self, current, next):
- current.adj.append(next.id)
- next.adj.append(current.id)
- def add_egde(self, a, b, graph):
- for i in graph:
- if i.id == a:
- i.adj.append(int(b))
- elif i.id == b:
- i.adj.append(int(a))
- else:
- print("Doesnt exists", i.id, a, type(i.id), type(a))
- def check_if_int(value):
- try:
- int(value)
- return True
- except:
- print("Enter intger type")
- return False
- def visualize_graph(graph):
- G = nx.Graph()
- for current in graph:
- G.add_node(current.id)
- temp = 0
- temp_current = 0
- for current in graph:
- for i in current.adj:
- if temp_current == current.id:
- G.add_edge(current.id, i)
- temp = temp + 1
- else:
- temp_current = temp_current + 1
- temp = 0
- nx.draw(G)
- plt.show()
- if len(sys.argv) == 4:
- v_count = int(sys.argv[1])
- aMin = int(sys.argv[2])
- aMax = int(sys.argv[3])
- graph = []
- for i in range(0, v_count):
- graph.append(Vertex(i))
- density = 0.3
- density_value = random.random()
- for current in graph:
- for next in graph:
- if current.id != next.id:
- if len(next.adj) < aMax and len(current.adj) < aMax:
- if density > density_value:
- if current.id not in next.adj:
- current.adj.append(next.id)
- next.adj.append(current.id)
- density_value = random.random()
- for vertex in graph:
- print(vertex)
- visualize_graph(graph)
- else:
- pass
- # print("Komandos: \n\
- # *** iveskit addV (virsunes index'a) pvz.: addV 1 \n\
- # *** iveskit addE virsuneA virsuneB pvz.: addE 1 2 \n\
- # *** iveskite quit, jeigu norite iseiti is programos ")
- # grap = []
- # command = input("Iveskite normima komanda:")
- # while command != "quit":
- # if command.lower() == "addv":
- # id = input("Enter vertex id: ")
- # if check_if_int(id):
- # v = Vertex(id)
- # grap.append(v)
- # print("Vertex is added")
- # elif command.lower() == "adde":
- # vertices = input("Enter vertices to add edge between 1,2: ")
- # temp = vertices.split(',')
- # if len(temp) == 2:
- # a, b = temp
- # v.add_egde(a, b, grap)
- # # v.add_egde(int(a), int(b), grap)
- # print("Edge is added")
- # for i in grap:
- # print(i)
- # visualize_graph(grap)
- # command = input("Input: ")
- # else:
- # print("Thanks for stopping by!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement