/
main.py
executable file
·218 lines (172 loc) · 5.76 KB
/
main.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
208
209
210
211
212
213
214
215
216
217
218
import networkx as nx
import matplotlib.pyplot as plt
from algorithms.helpers.file_handler import File
from algorithms.helpers.gui import Gui
def printLine():
print("-----------------------------------")
def importAlgo(algoChoose):
algoList = ["Prims", "Kruskal", "Dijkstra", "Bellman Ford", "Floyd"]
algoObj = None
printLine()
print("Algo Used: ", algoList[algoChoose])
if algoChoose == 0:
from algorithms import prims as algo
algoObj = algo.Prims()
elif algoChoose == 1:
from algorithms import kruskal as algo
algoObj = algo.Kruskal()
elif algoChoose == 2:
from algorithms import dijkstra as algo
algoObj = algo.Dijkstra()
elif algoChoose == 3:
from algorithms import bellman_ford as algo
algoObj = algo.BellmanFord()
elif algoChoose == 4:
from algorithms import floyd as algo
algoObj = algo.Floyd()
else:
print("invalid Module index passed")
return False
return algoObj
def operateHex(color, val):
color = (hex(int(color, base=16) + val))[2:]
color = ("0" if len(color) <= 1 else "") + color
color = ("0" if len(color) <= 1 else "") + color
return color
def addEdges(graph, fileObj, selectedPath, showAllEdges):
edge_width = []
default_width = 0.4
redColor = "#ff0000"
color = ["00", "ff"]
changeStep = int(255 / ((fileObj.nodesCount**2) / 2))
if not changeStep:
changeStep = 1
for i in range(len(fileObj.graph)):
for j in range(i + 1, len(fileObj.graph)):
if [i, j] in selectedPath or [j, i] in selectedPath:
edge_width.append(default_width * 1.5)
graph.add_edge(i, j, weight=fileObj.graph[i][j], color=redColor)
elif fileObj.graph[i][j] != 0 and showAllEdges:
tempColor = "#%s%s%s%s" % (color[0], color[0], color[0], color[1])
edge_width.append(default_width)
graph.add_edge(i, j, weight=fileObj.graph[i][j], color=tempColor)
if not (color[0] == "80" or color[1] == "80"):
color[0] = operateHex(color[0], changeStep)
color[1] = operateHex(color[1], -changeStep)
return edge_width
def addNodes(G, fileObj):
node_edge_color = []
node_edge_width = []
for i in range(fileObj.nodesCount):
if i == fileObj.startNode:
node_edge_color.append("#000044")
node_edge_width.append(1.2)
else:
node_edge_color.append("#5555ff")
node_edge_width.append(0.8)
G.add_node(i, pos=(fileObj.nodesCordinate[i][0], fileObj.nodesCordinate[i][1]))
return node_edge_color, node_edge_width
def clusterCoefficient(fileObj):
lccFinal = 0.0
for i in range(fileObj.nodesCount):
degree = 0
link = 0
visited = []
for j in range(fileObj.nodesCount):
if fileObj.graph[i][j] != 0:
degree += 1
visited.append(j)
for j in visited:
for k in range(fileObj.nodesCount):
if fileObj.graph[j][k] != 0 and k in visited:
link += 1
visited.remove(j)
maxLink = (degree * (degree - 1)) / 2
if maxLink == 0:
maxLink = 1
lcc = link / maxLink
lccFinal += lcc
lccFinal /= fileObj.nodesCount
return lccFinal
def calculateCost(edgeList, graph, V):
totalCost = 0
for edge in edgeList:
totalCost += graph[edge[0]][edge[1]]
return totalCost
def main():
# graphing variables
fileName = "files/input100.txt"
algoIndex = 4
showAllEdges = False
showWeights = True
# create user input gui
gui = Gui()
gui.guiCreate()
fileName = "files/" + gui.guiArray[0]
algoIndex = gui.guiArray[1]
showAllEdges = gui.guiArray[2]
showWeights = gui.guiArray[3]
# read input file
fileObj = File(fileName)
fileObj.readFile()
# apply algo
algoObj = importAlgo(algoIndex)
selectedPath = algoObj.runAlgorithm(
fileObj.graph, fileObj.nodesCount, fileObj.startNode
)
# print selected path
printLine()
print("Selected Edges:")
for pathI, pathJ in selectedPath:
print("(", pathI, "->", pathJ, ") :\t", fileObj.graph[pathI][pathJ])
# print(selectedPath)
printLine()
print(
"Total Edge Cost: %0.2f"
% calculateCost(selectedPath, fileObj.graph, fileObj.nodesCount)
)
# display local clustering coefficient
localClusterVal = clusterCoefficient(fileObj)
printLine()
print("Clustering Coefficient: %0.2f" % localClusterVal)
# plot axis
fig, ax = plt.subplots()
fig.add_subplot(111)
ax.axis((0, fileObj.maxXY[0], 0, fileObj.maxXY[1]))
# add nodes and edges
G = nx.Graph()
node_edge_color, node_edge_width = addNodes(G, fileObj)
edge_width = addEdges(G, fileObj, selectedPath, showAllEdges)
# set position and edge variables
pos = nx.get_node_attributes(G, "pos")
edges = G.edges()
edge_color = [G[u][v]["color"] for u, v in edges]
# show edge weights if variable true
if showWeights:
labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(
G,
pos,
edge_labels=labels,
font_size=6,
alpha=0.4,
font_weight=0.1,
label_pos=0.5,
)
# draw the graph with desired attributes
nx.draw(
G,
pos,
with_labels=True,
node_size=140,
font_size=7,
node_color="#ccccff",
linewidths=node_edge_width,
edgecolors=node_edge_color,
width=edge_width,
edge_color=edge_color,
)
# display graph screen
plt.show()
if __name__ == "__main__":
main()