-
Notifications
You must be signed in to change notification settings - Fork 2
/
testGraphObject.py
66 lines (58 loc) · 3.79 KB
/
testGraphObject.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from classes import *
def testGraphObject():
possibleCases = [True, False]
counter = 1
for isMultiGraph in possibleCases:
for isDirected in possibleCases:
for isWeighted in possibleCases:
for n in range(1,21): #testing graphs with between 1 and 20 nodes
numNodes = n
if isDirected:
numConnections = n*(n-1)
else: #undirected
numConnections = floor(n*(n-1)/2)
g = Graph(seed=2, numNodes=numNodes, numConnections=numConnections, name=f"Graph{counter}", date="", description="", weightsRange=[1,5], isMultiGraph=isMultiGraph, isDirected=isDirected, isWeighted=isWeighted)
# test that for every key in the adjacencyLists we have an index and a column in the adjacencyMatrix, otherwise print "False" and return
adjListNodes = sorted(list(g.adjacencyLists.keys()))
adjMatrSources = sorted(g.adjacencyMatrix.index)
adjMatrDestinations = sorted(g.adjacencyMatrix.columns)
if len(adjListNodes) != len(adjMatrSources) or len(adjListNodes) != len(adjMatrDestinations) or len(adjMatrSources) != len(adjMatrDestinations):
print("False")
return
for i,j,k in zip(adjListNodes,adjMatrSources,adjMatrDestinations):
if i != j or i != k or j != k:
print("False")
return
# test that all weights are the same for every source->destination pair
for source in adjListNodes:
destinations = g.adjacencyLists.get(source)
for destinationTuple in destinations:
destination = destinationTuple[0]
adjListWeights = sorted(destinationTuple[1:])
adjMatrWeights = sorted(g.adjacencyMatrix.at[source,destination])
if adjListWeights != adjMatrWeights:
print("False")
return
#checks for unweighted and non-multigraph issues
for row in adjMatrSources:
for col in adjMatrDestinations:
if not g.isWeighted and isinstance(g.adjacencyMatrix.at[row,col], type(np.array([]))):
for w in g.adjacencyMatrix.at[row,col]:
if w != 1: #if it's unweighted, all weights should be 1
print("w!=1")
print("False")
return
if not g.isMultiGraph: #if it's not a multigraph
# then the leading diagonal should be all NaNs, otherwise print "False"
if row == col:
if not np.isnan(g.adjacencyMatrix.at[row,col]):
print(f"value at {row},{col} when it should be NaN")
print("False")
return
# and the number of weights between each source and destination should be 1
if isinstance(g.adjacencyMatrix.at[row,col], type(np.array([]))) and len(g.adjacencyMatrix.at[row,col]) > 1:
print(f"There should only be 1 value at {row},{col} but instead there are {g.adjacencyMatrix.at[row,col]}")
print("False")
return
counter += 1
print("True")