-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler67.py
66 lines (50 loc) · 2.02 KB
/
euler67.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
import networkx
import EulerUtilities
def getNumLinesInFile(fileName):
with open(fileName) as f:
for i, l in enumerate(f):
pass
return i + 1
def buildTriangleGraphFromTxt(txtFileName):
triangleTree = networkx.DiGraph()
with open(txtFileName) as triangleInputFile:
weightNumber = 0
sourceNodeNumber = 0
triangleSeed = 0
nextTriangleNumber = EulerUtilities.generateTriangle(triangleSeed)
for line in triangleInputFile:
splitLine = line.split()
for edgeWeight in splitLine:
if weightNumber == nextTriangleNumber:
sourceNodeNumber += 1
triangleSeed += 1
nextTriangleNumber = EulerUtilities.generateTriangle(triangleSeed)
triangleTree.add_edge(weightNumber, sourceNodeNumber, {'capacity': int(edgeWeight)})
sourceNodeNumber += 1
triangleTree.add_edge(weightNumber, sourceNodeNumber, {'capacity': int(edgeWeight)})
weightNumber += 1
return triangleTree, EulerUtilities.generateTriangle(triangleSeed)
triangleTree, triangleNumber = buildTriangleGraphFromTxt("/Users/203369/Downloads/triangle.txt")
# print triangleTree.number_of_nodes()
# print triangleNumber
for x in xrange(triangleNumber - 1, -1, -1):
neighbors = triangleTree.neighbors(x)
maxCapacity = 0
for neighbor in neighbors:
capacity = triangleTree[x][neighbor]['capacity']
if 'capacity' in triangleTree[neighbor]:
capacity += triangleTree[neighbor]['capacity']
if capacity > maxCapacity:
maxCapacity = capacity
triangleTree[x]['capacity'] = maxCapacity
print triangleTree[0]
# leaves = (n for n, d in triangleTree.in_degree_iter() if d == 0)
#
# for leaf in leaves:
# leafMaxFlow = networkx.max_flow(triangleTree, leaf, 0)
# print leafMaxFlow
# if leafMaxFlow > treeMaxFlow:
# treeMaxFlow = leafMaxFlow
#
# print treeMaxFlow
# print EulerUtilities.generateTriangle(2)