-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch-bound.py
47 lines (47 loc) · 1.63 KB
/
branch-bound.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
from math import inf
A=[[0,1,0,0],[0,0,1,0],[0,0,0,1],[1,0,0,0]]
currentPath=[0]
currentPathValue=0
M=float(inf)
bestPath=[]
n=len(A)
adj=0
for i in range(n):
if(A[i][0]>0) : adj+=1
i=0
while(len(currentPath)>0):
if(len(currentPath)==n):
if(A[currentPath[-1]][0]>0):
currentPathValue+= A[currentPath[-1]][0]
if(currentPathValue<M):
M=currentPathValue
bestPath=currentPath.copy()
currentPathValue-=A[currentPath[-1]][0]
currentPathValue-=A[currentPath[-2]][currentPath[-1]]
i=currentPath.pop()+1
if(A[i-1][0]>0) : adj+=1
elif (adj==0):
currentPathValue-=A[currentPath[-2]][currentPath[-1]]
i=currentPath.pop()+1
if(A[i-1][0]>0) : adj+=1
else :
while(i<n):
if(not(i in currentPath)):
if(A[currentPath[-1]][i]>0):
currentPathValue+=A[currentPath[-1]][i]
if(currentPathValue<M):
currentPath.append(i)
if(A[i][0]>0) : adj-=1
i=0
break
else:
currentPathValue-=A[currentPath[-1]][i]
i+=1
if(i==n):
if(len(currentPath)>1):
currentPathValue-=A[currentPath[-2]][currentPath[-1]]
i=currentPath.pop()+1
if(A[i-1][0]>0) : adj+=1
print(bestPath)
print(M)
bestPath.append(0)