-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
41 lines (32 loc) · 899 Bytes
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
from graph import Graph, Edge, Vertex
class GraphGenerator:
# Generate a new graph with the provided configuration.
#
# Uses a new pseudo-random algorithm w/ seed as starting point to ensure repeatibility
@staticmethod
def generate(seed, vertices, edges):
random.seed(seed)
g = Graph()
# Vertices
for i in range(vertices):
success = False
while not success:
v = Vertex(random.randint(1, 20), random.randint(1, 20))
if not g.isOccupiedRadius(v.x, v.y, 1):
g.addVertex(v)
success = True
# Edges
for i in range(edges):
success = False
while not success:
max = len(g.vertices) - 1
a = g.vertices[random.randint(0, max)]
b = g.vertices[random.randint(0, max)]
while b == a:
b = g.vertices[random.randint(0, max)]
if not g.edgeExists(a, b):
e = Edge(a, b)
g.addEdge(e)
success = True
return g