This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.py
207 lines (167 loc) · 6.85 KB
/
Graph.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from graphUtils import *
from copy import deepcopy
class Graph:
"""
Class of a graph, defined by:
- vertice number
- edge number
- list of all the edges with there values as
[in, out, value]
- adjency matrix
- value matrix
"""
def __init__(self, verticeNumber, edgeNumber, edgeList):
"""Constructor of Graph class"""
self.verticeNumber = verticeNumber
self.edgeNumber = edgeNumber
self.edgeList = edgeList
self.verticeList = self._setVerticeList()
self.adjencyMatrix = self._setAdjencyMatrix()
self.valueMatrix = self._setValueMatrix()
@classmethod
def fromFile(cls, filename:str):
"""Function to read graph from specified file"""
with open(filename, 'r') as myfile:
lines = myfile.readlines()
verticeNumber = int(lines[0])
edgeNumber = int(lines[1])
edgeList = []
for i in range(2, edgeNumber + 2):
line = lines[i].replace('\n', '').split(" ")
inEdge = int(line[0])
outEdge = int(line[1])
value = int(line[2])
edgeList.append([inEdge, outEdge, value])
return cls(verticeNumber, edgeNumber, edgeList)
# ======== Getters =========
def getAdjencyMatrix(self) -> list:
return deepcopy(self.adjencyMatrix)
def getValueMatrix(self) -> list:
return deepcopy(self.valueMatrix)
def getVerticeList(self) -> list:
return deepcopy(self.verticeList)
# ======== Setters =========
def _setAdjencyMatrix(self) -> list:
"""Function that compute the adjency matrix"""
adjencyMatrix = []
# Fill a empty matrix with False
adjencyMatrix = [False] * self.verticeNumber
for i in range(self.verticeNumber):
adjencyMatrix[i] = [False] * self.verticeNumber
# Assign value to the matrix
for edge in self.edgeList:
adjencyMatrix[edge[0]][edge[1]] = True
return adjencyMatrix
def _setValueMatrix(self) -> list:
"""Function that compute the values matrix"""
valueMatrix = []
# Fill a empty matrix with None value
valueMatrix = [None] * self.verticeNumber
for i in range(self.verticeNumber):
valueMatrix[i] = [None] * self.verticeNumber
# Assign value to the matrix
for edge in self.edgeList:
valueMatrix[edge[0]][edge[1]] = edge[2]
return valueMatrix
def _setVerticeList(self) -> list:
return list(range(self.verticeNumber))
# ========= Methods =========
def hasCicle(self) -> bool:
"""Function that check if the graph has a cicle"""
print("Using the method of elimination of entry points")
adjencyMatrix = self.getAdjencyMatrix()
verticeList = self.getVerticeList()
finish = False
while (len(verticeList) != 0 and not finish):
entryPointList = []
print("List of vertice before removing entry point ", verticeList)
# Finding entry points
for index in range(len(verticeList)):
if (not hasPredecessor(index, adjencyMatrix)):
entryPointList.append(index)
# If no entry point found we stop the search
if(len(entryPointList) == 0):
print("No more removable entry point")
finish = True
else:
# Removing entry points
adjencyMatrix, verticeList = removeEntryPointFromMatrix(
entryPointList, adjencyMatrix, verticeList)
print("Remaining vertices: ", verticeList)
if (len(verticeList) == 0):
return False
else:
return True
def getRanks(self) -> list:
print("Using the method of elimination of entry points")
verticeList = self.getVerticeList()
currentRank = 0
ranks = [0] * self.verticeNumber
adjencyMatrix = self.getAdjencyMatrix()
while (len(verticeList) != 0):
print("current rank: ", currentRank)
print("List of vertice before removing entry point ", verticeList)
entryPointList = []
# Finding entry points
for index in range(len(verticeList)):
if (not hasPredecessor(index, adjencyMatrix)):
entryPointList.append(index)
for index in entryPointList:
ranks[verticeList[index]] = currentRank
# Removing entry points
adjencyMatrix, verticeList = removeEntryPointFromMatrix(
entryPointList, adjencyMatrix, verticeList)
currentRank += 1
return ranks
def isScheduling(self, ranks: list) -> bool:
"""Check if a graph is a scheduling graph"""
valueMatrix = self.getValueMatrix()
if (getEntryPointNumber(ranks) > 1):
print("There is more than one entry point")
return False
else:
print("There is one entry point")
if (getEndPointNumber(ranks) > 1):
print("There is more than one exit point")
return False
else:
print("There is one exit point")
if (hasNegativeEdge(valueMatrix)):
print("There is a negative edge")
return False
else:
print("There is no negative edge")
if (not hasSameValueEdges(0, ranks.index(0), valueMatrix)):
print("There is a non negative outgoing edge from an entry point")
return False
else:
print("There is no non negative outgoing edge from an entry point")
if (not hasSameWeight(valueMatrix)):
print("Some vertices have some outgoing edges with different weight")
return False
else:
print("Every vertices have outgoing edges whith the same weight")
return True
def getCalendars(self, ranks: list):
# Computing earliest time
earliestDate = getEarliestDate(self.getValueMatrix(), ranks)
print("Earlyest time: ", end="")
printArray(earliestDate)
# Comuting latest time
latestDate = getLatestDate(
self.getValueMatrix(), ranks, max(earliestDate))
margins = getMargins(earliestDate, latestDate)
print("Latest time: ", end="")
printArray(latestDate)
# Computing margins
print("margins: ", end="")
printArray(margins)
def __str__(self) -> str:
"""Function that display a graph"""
verticeNumberStr = str(self.verticeNumber) + " vertices\n"
edgeNumberStr = str(self.edgeNumber) + " edges\n"
edgeListStr = ""
for edge in self.edgeList:
edgeListStr += str(edge[0]) + " -> " + \
str(edge[1]) + " = " + str(edge[2]) + "\n"
return verticeNumberStr + edgeNumberStr + edgeListStr