Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #RM4MP Rahat Maini HW7
  2. import itertools
  3.  
  4. with open("chapel.txt", 'r') as inputFile:
  5. lines=inputFile.read()
  6. inputFile.close()
  7. lines=lines.split("\n")
  8.  
  9. with open("chapel.txt", 'r') as inputFile2:
  10. lines2=inputFile2.read()
  11. inputFile2.close()
  12. lines2=lines2.split()
  13.  
  14.  
  15.  
  16.  
  17. #lines[0] is number of rooms/vertices
  18. #lines[1] is start end for luke
  19. #lines[2] is start end for lorelai
  20. #rest of lines are adjacencies to their index (basically n-3 is room number)
  21.  
  22. adjacencies=[]
  23. graph={}
  24. i=3
  25.  
  26. while (i<len(lines)):
  27. adjacencies.append(lines[i].split(" "))
  28. adjacencies[i-3].append(str(i-3))
  29. graph[str(i-3)]=adjacencies[i-3]
  30. i+=1
  31.  
  32.  
  33. tuplList=[]
  34.  
  35. x=0
  36. while (x<len(adjacencies)):
  37. y=0
  38. while (y<len(adjacencies[x])):
  39. tup=(x,int(adjacencies[x][y]))
  40. tuplList.append(tup)
  41. y+=1
  42. x+=1
  43.  
  44. nodeList=[]
  45. i=0
  46. while(i<int(lines[0])):
  47. nodeList.append(i)
  48. i+=1
  49.  
  50.  
  51. theSet = set(zip(nodeList, nodeList))
  52. cartesian = [i for i in itertools.product(nodeList, nodeList) if i not in theSet]
  53.  
  54. cartesian = [x for x in cartesian if x not in tuplList]
  55.  
  56.  
  57.  
  58. cartesianAdjacency={} #connected tuples (each index has those connected)
  59. i=0
  60. while (i<len(cartesian)): #slow, one by the rest
  61. j=0
  62. listConnections=[]
  63. while (j<len(cartesian)): #the rest
  64.  
  65. # print (graph[str(cartesian[i][0])])
  66. if ( (str(cartesian[j][0]) in graph[str(cartesian[i][0])]) and (str(cartesian[j][1]) in graph[str(cartesian[i][1])]) ):
  67. if (cartesian[j]!=cartesian[i]):
  68. listConnections.append(cartesian[j])
  69. cartesianAdjacency[cartesian[i]]=cartesian[j]
  70. j+=1
  71. cartesianAdjacency[cartesian[i]]=listConnections
  72. i+=1
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. #https://stackoverflow.com/questions/8922060/how-to-trace-the-path-in-a-breadth-first-search
  80. def bfs(start, end):
  81. queue = []
  82. queue.append([start])
  83. while queue:
  84. path = queue.pop(0)
  85. node = path[-1]
  86. if node == end:
  87. return path
  88. for adjacent in cartesianAdjacency.get(node):
  89. new_path = list(path)
  90. new_path.append(adjacent)
  91. queue.append(new_path)
  92.  
  93.  
  94. tuple1=( (int(lines2[1]) , int(lines2[2])) )
  95. tuple2=((int(lines2[3]),int(lines2[4])))
  96. theBFS=(bfs(tuple1,tuple2))
  97. lukesPath=""
  98. lorelaisPath=""
  99. i=0
  100. while (i<len(theBFS)):
  101. lukesPath+=str(((theBFS[i][0])))+" "
  102.  
  103. lorelaisPath+=str(((theBFS[i][1])))+" "
  104. i+=1
  105.  
  106.  
  107. print (lukesPath)
  108. print (lorelaisPath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement