Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. from Domain import *
  2. inf = 20000000000
  3.  
  4. def mini(x, y):
  5. if x < y:
  6. return x
  7. else:
  8. return y
  9.  
  10. def checkNegativeCycle(d,n):
  11. #check if there exists a negative cycle
  12. for i in range(0,n):
  13. if d[n*n-1][i]!=d[n*n-2][i]:
  14. return True
  15. return False
  16.  
  17.  
  18. def printPath(path,current,stop):
  19. if path[current] == -1 or path[current] == current:
  20. print(current,'->')
  21. return
  22.  
  23. printPath(path, path[current], stop)
  24. print(current)
  25. if current!=stop:
  26. print('->')
  27.  
  28.  
  29. def lowestCostWalk(graph,start,stop):
  30. n=graph.getSize()
  31. #print(n)
  32. #d=[[inf for x in range((n-1)*(n-1)+1)] for y in range(n)]
  33. d=[]
  34.  
  35. for i in range((n-2)*(n-2)+1):
  36. d.append([])
  37. for j in range(n):
  38. d[i].append(inf)
  39.  
  40. print(d[0][0])
  41. print(d[6][4])
  42.  
  43. #5/4
  44.  
  45.  
  46. path=[0 for x in range(n)]
  47.  
  48.  
  49.  
  50. #print(d[5][0])
  51. d[0][start]=0
  52. # print(d[n-1])
  53.  
  54. for k in range(1,(n-2)*(n-2)):
  55. #k=k+1
  56. for x in range(0,n,1):
  57. #print("k: ",k,"x: ",x,"\n")
  58. print("k:",k," x:",x)
  59. print("\n","list in for x:",graph.getListOut()[x],"\n")
  60. bestFromOutside=inf
  61. for y in graph.getListOut()[x]:
  62. print("y:",y)
  63. if graph.isEdge(y,x):
  64. if bestFromOutside>d[k-1][y]+graph.findCost(y,x):
  65. bestFromOutside=d[k-1][y]+graph.findCost(y,x)
  66. path[x]=y
  67. if bestFromOutside>d[k-1][x]:
  68. path[x]=x
  69.  
  70. d[k][x]=mini(d[k-1][x],bestFromOutside)
  71.  
  72. if checkNegativeCycle(d, n-2):
  73. print("Negative cycle!")
  74.  
  75. print("\n",d)
  76.  
  77. if d[(n-2) * (n-2) - 1][stop] == inf:
  78. print("No path from start to stop!")
  79. return
  80.  
  81. print("the cost is: ",d[n-2][stop])
  82. print("\n",d)
  83. print("\n")
  84. printPath(path, stop, stop)
  85. print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement