-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphingSuite.py
176 lines (164 loc) · 5.86 KB
/
GraphingSuite.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
import networkx as nx
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peak(self):
return self.items[-1]
def size(self):
return len(self.items)
class Queue:
def __init__(self):
self.items = deque([])
def enqueue(self, item):
self.items.append(item)
def dequeue(self):
return self.items.popleft()
def peak(self):
return self.items[0]
def size(self):
return len(self.items)
def GraphGenerator(file):
path = open(file)
n = int(path.readline())
connections = path.read().split("\n")
connections = [i.split(" ") for i in connections]
while([""] in connections):
connections.remove([""])
graph = nx.Graph()
for j in range(0, len(connections)):
graph.add_edge(connections[j][0], connections[j][1], weight=float(connections[j][2]))
return graph
def DepthFirstSearch(graph):
vertices = sorted(graph.nodes())
cycle = False
gray = []
black = []
for vertex in vertices:
if (vertex not in gray) & (vertex not in black):
S = Stack()
S.push(vertex)
gray.append(vertex)
while(S.size() > 0):
x = S.peak()
y = [i for i in sorted(graph.neighbors(x)) if (i not in gray) & (i not in black)]
if y != []:
y = y[0]
cyc = [j for j in graph.neighbors(y) if (j in gray) & (j != x)]
if cyc != []:
cycle = True
S.push(y)
gray.append(y)
else:
S.pop()
black.append(x)
output = gray
return output, cycle
def BreadthFirstSearch(graph):
vertices = sorted(graph.nodes())
gray = []
black = []
output = []
for vertex in vertices:
if (vertex not in gray) & (vertex not in black):
Q = Queue()
Q.enqueue(vertices[0])
gray.append(vertices[0])
while(Q.size() > 0):
x = Q.peak()
weighted = sorted(graph[x].items(), key=lambda lowest: lowest[1]['weight'])
for y in weighted:
if (y[0] not in gray) & (y[0] not in black):
Q.enqueue(y[0])
gray.append(y[0])
z = Q.dequeue()
output.append(z)
black.append(z)
return output
def MinimumSpanningTree(graph):
weighted = sorted(graph.edges(data='weight'), key=lambda x: x[2])
newGraph = nx.Graph()
for edge in weighted:
newGraph.add_edge(edge[0], edge[1], weight=edge[2])
out, cyc = DepthFirstSearch(newGraph)
if cyc != False:
newGraph.remove_edge(edge[0], edge[1])
newWeight = sorted(newGraph.edges(data='weight'), key=lambda x: x[2])
sumWeight = 0
for edge in newWeight:
sumWeight += edge[2]
sumWeight = round(sumWeight, 5)
print('Minimum Spanning Tree:')
print('V = ' + ', '.join(sorted(newGraph.nodes())))
print('E = ' + ', '.join(str(tup) for tup in newWeight))
print('Total Weight: ' + str(sumWeight))
def ShortestPath(graph):
nodes = sorted(graph.nodes())
D = np.zeros((len(nodes), len(nodes)))
S = np.ones((len(nodes), len(nodes)))
S.fill(None)
wtTotal = 1
for edge in graph.edges(data='weight'):
wtTotal += edge[2]
for i in range(0, len(D)):
for j in range(0, len(D)):
if graph.has_edge(nodes[i], nodes[j]):
D[i][j] = graph.get_edge_data(nodes[i], nodes[j])['weight']
S[i][j] = j
elif i == j:
D[i][j] = 0
else:
D[i][j] = wtTotal
for k in range(0, len(D)):
for i in range(0, len(D)):
for j in range(0, len(D)):
orig = D[i][j]
D[i][j] = min(D[i][j], D[i][k] + D[k][j])
if orig != D[i][j]:
S[i][j] = S[i][k]
print('Shortest Paths')
for pair in list(combinations(range(0, len(D)), 2)):
hasPath = True
p = [nodes[pair[0]]]
x = nodes[pair[0]]
while x != pair[1]:
x = S[int(x)][pair[1]]
if np.isnan(x):
hasPath = False
break
p.append(nodes[int(x)])
path = ''
pathWeight = 0
for one, two in zip(p[:-1], p[1:]):
if hasPath:
try:
pathWeight += graph.get_edge_data(one, two)['weight']
if two != p[-1]:
path += '(' + str(one) + ', ' + str(two) + ', ' + str(graph.get_edge_data(one, two)['weight']) + ') -> '
else:
path += '(' + str(one) + ', ' + str(two) + ', ' + str(graph.get_edge_data(one, two)['weight']) + ')'
except TypeError:
hasPath = False
pass
if hasPath:
print(str(pair[0]) + ' -> ' + str(pair[1]) + ' = ' + path)
print('Path Weight = ' + str(pathWeight))
else:
print(str(pair[0]) + ' -> ' + str(pair[1]) + ' = ' + 'Disconnected Graph (No Path)')
#file = str(sys.argv[1])
graph = GraphGenerator(file)
print('Depth First Search Traversal:')
print(', '.join(DepthFirstSearch(graph)[0]) + '\n')
print('Breadth First Search Traversal:')
print(', '.join(BreadthFirstSearch(graph)) + '\n')
MinimumSpanningTree(graph)
print('')
ShortestPath(graph)