Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. a_matrix = []
  2. infection = {}
  3. seeds = []
  4.  
  5. def fill_matrix():
  6. global threshold
  7. global numNodes
  8. global firstNode
  9.  
  10. fileVectors = open("decisionTestText.txt")
  11. numNodes = int(fileVectors.readline().strip())
  12. firstNode = int(fileVectors.readline().strip())
  13. threshold = float(fileVectors.readline().strip())
  14. seededNodes = fileVectors.readline().strip()
  15.  
  16. lastComma = -1
  17. while(not seededNodes.find(",", lastComma + 1) == -1):
  18. seeds.append(int(seededNodes[lastComma+1: seededNodes.index(",", lastComma+1)].strip())-firstNode+1)
  19. lastComma = seededNodes.index(",", lastComma + 1)
  20.  
  21. seeds.append(int(seededNodes[lastComma+1:].strip())-firstNode+1)
  22.  
  23. for i in range(numNodes):
  24. if not i+1 in seeds:
  25. infection.update({i+1: 0})
  26. else:
  27. infection.update({i+1: 1})
  28.  
  29. rowArray = []
  30. for i in range(numNodes):
  31. rowArray.append(0)
  32. for j in range(numNodes):
  33. a_matrix.append(rowArray.copy())
  34.  
  35.  
  36. for line in fileVectors:
  37. first = int(line[0:line.index(",")].strip())-firstNode+1
  38. second = int(line[line.index(",")+1:].strip())-firstNode+1
  39.  
  40. a_matrix[first-1][second-1] = 1
  41. a_matrix[second-1][first-1] = 1
  42.  
  43. fileVectors.close()
  44.  
  45. def check_infection(node):
  46. infectedCount = 0
  47. friendCount = 0
  48. for i in range(len(a_matrix[node-1])):
  49. if a_matrix[node-1][i] == 1:
  50. friendCount += 1
  51. if infection.get(i+1) == 1:
  52. infectedCount += 1
  53.  
  54.  
  55. if not friendCount == 0 and infectedCount/friendCount > threshold:
  56. infection[node] = 1
  57.  
  58.  
  59. def main():
  60. print("\n\n")
  61.  
  62. fill_matrix()
  63. new_infection = {}
  64. counter = 0
  65.  
  66. for i in range(numNodes):
  67. new_infection.update({i+firstNode-1:0})
  68.  
  69. while not new_infection == infection:
  70. changedArray = []
  71.  
  72. print("t" + str(counter) + ":", end = ' ')
  73. for i in infection:
  74. if not infection.get(i) == new_infection.get(i):
  75. changedArray.append(i+firstNode - 1)
  76. print(changedArray)
  77.  
  78. new_infection = infection.copy()
  79.  
  80. for i in range(len(a_matrix)):
  81. check_infection(i+1)
  82.  
  83. counter += 1
  84.  
  85. print("\n")
  86.  
  87. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement