Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1.  
  2. INFINITE = 99999999999
  3.  
  4.  
  5. # Noel -> Robbe: 1 cent
  6. # Noel -> Seppe: 1 cent
  7.  
  8. Graph = {'Alice': {'R2':2},
  9.          'R2': {'Alice': 2, 'R5':6, 'R6':2, 'R7':6},
  10.          'R3': {'R7':5,'R5':10,'R4':1},
  11.          'R4':{'Bob': 8, 'R3':1, 'R5': 1, 'R6':4},
  12.          'R5':{'R2':6,'R7':9,'R3':10,'R4':1},
  13.          'R6':{'R2':2,'R4':4},
  14.          'R7':{'R2':6,'R3':5,'R5':9},
  15.          'Bob':{'R4':8}}
  16.  
  17.  
  18. # Object example:
  19. # {
  20. #   id: 'R2',
  21. #   length: 5,
  22. #   done: False,
  23. #   path: {'Alice', 'R1'}
  24. # }
  25.  
  26. priorityQueue = []
  27.  
  28. def sortPriorityQueue(pq):
  29.     pq.sort(key=lambda x: x['length'], reverse=False)
  30.     return pq
  31.  
  32. def getIndexOfRelayInPQ(pq, id):
  33.     for i in range(len(pq)):
  34.         if pq[i]['id'] == id:
  35.             return i
  36.     return -1
  37.  
  38. def getIndexOfFirstNonDone(pq):
  39.     for i in range(len(pq)):
  40.         if pq[i]['done'] == False:
  41.             return i
  42.     return -1
  43.  
  44. def getNonDoneCount(pq):
  45.     result = 0
  46.     for i in range(len(pq)):
  47.         if pq[i]['done'] == False:
  48.             result += 1
  49.     return result
  50.  
  51. def dijkstra(graph, startNode, endNode):
  52.     global priorityQueue
  53.  
  54.     # Put all relays in PQ, and set length 0 for starting point
  55.     priorityQueue = []
  56.     for relay in graph:
  57.         priorityQueue.append({
  58.             'id': relay,
  59.             'length': 0 if (relay == startNode) else INFINITE,
  60.             'done': False,
  61.             'path': []
  62.         })
  63.  
  64.     while getNonDoneCount(priorityQueue) > 0:
  65.         shortestIndex = getIndexOfFirstNonDone(priorityQueue)
  66.         shortest = priorityQueue[shortestIndex]
  67.  
  68.         print('Doing ' + str(shortest['id'] + ': '))
  69.  
  70.         if(shortest['id'] == endNode):
  71.             return
  72.  
  73.         for neighbour in Graph[shortest['id']]:
  74.             lengthToNeighbour = shortest['length'] + Graph[shortest['id']][neighbour]
  75.             indexOfNeighbour = getIndexOfRelayInPQ(priorityQueue, neighbour)
  76.             print('\tUpdating ' + str(priorityQueue[indexOfNeighbour]['id']))
  77.             if lengthToNeighbour < priorityQueue[indexOfNeighbour]['length']:
  78.                 print('\tUpdating length to ' + str(lengthToNeighbour))
  79.                 priorityQueue[indexOfNeighbour]['length'] = lengthToNeighbour
  80.                 priorityQueue[indexOfNeighbour]['path'] = shortest['path'][:]
  81.                 priorityQueue[indexOfNeighbour]['path'].append(shortest['id'])
  82.         priorityQueue[getIndexOfFirstNonDone(priorityQueue)]['done'] = True
  83.  
  84.         priorityQueue = sortPriorityQueue(priorityQueue)
  85.  
  86. dijkstra(Graph, 'Alice', 'Bob')
  87.  
  88. # Printing result
  89. print('####################################################')
  90. bobIndex = getIndexOfRelayInPQ(priorityQueue, 'Bob')
  91. print('Shortest path from Alice to Bob: ')
  92. print('\tLength: ' + str(priorityQueue[bobIndex]['length']))
  93. print('\tPath: ' + str(priorityQueue[bobIndex]['path']))
  94. print('####################################################')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement