forked from DaniloDamico/EX2IA19
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphGenerator.py
More file actions
25 lines (20 loc) · 851 Bytes
/
graphGenerator.py
File metadata and controls
25 lines (20 loc) · 851 Bytes
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
import random
def randomLetter():
return random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
def randomWeight():
return 1 + random.random() * 1000
def graphGenerator(G): # genera un grafo connesso con n-1 archi
"""
Il generatore crea un albero di un singolo nodo e, ogni volta
che crea un nuovo vertice, lo connette all'albero mediante un
arco. Il risultato finale è un grafo non orientato, connesso e
pesato sui vertici.
:param G: un grafo vuoto
"""
firstNode = G.addWeightedNode(randomLetter(), randomWeight()) # inserisco il primo nodo
listNode = [firstNode]
while True:
yield
newNode = G.addWeightedNode(randomLetter(), randomWeight())
G.insertEdge(newNode.id, random.choice(listNode).id)
listNode.append(newNode)