def BFS(graph, size, src_node, hospitals, pred, k): queue = Queue() visited = [False] * size hospitals_found = 0 hospital_list = [] # stores all the hospitals found visited[src_node] = True queue.EnQueue(src_node) if str(src_node) in hospitals: hospitals_found += 1 hospital_list.append(src_node) while not queue.isEmpty() and hospitals_found < k: current_node = queue.DeQueue() for neighbor in graph[current_node]: if not visited[neighbor]: visited[neighbor] = True pred[neighbor] = current_node queue.EnQueue(neighbor) if str(neighbor) in hospitals: hospitals_found += 1 hospital_list.append(neighbor) if hospitals_found >= k: break return hospital_list
def BFS(graph, size, src_node, hospitals, parent, k): # Start a queue queue = Queue() # Set every single node to not visited visited = [False] * size # Number of hospitals found tmp_k = 0 # Create a list for hospitals hospitalList = [] # Set starting node as visited visited[src_node] = True # Add starting node to the queue queue.EnQueue(src_node) # If starting node is a hospital, if str(src_node) in hospitals: # Add 1 to hospitals found tmp_k += 1 # Add source node to hospital list hospitalList.append(src_node) # parent = [-1, 2 ,0 ,1] # if current = hopital_node = 1 # parent[current] = 2 # path.append(2) # current = parent[current] = 2 # parent[current] = 0 # path.append(0) # current = parent[current] = 0 # parent[current] = -1 # path = [2, 0] # path to hospital 1 from node 0 is [0, 2, 1] # When there is something in the queue and number of hospitals found is less than k, while not queue.isEmpty() and tmp_k < k: # Get current node from the queue current_node = queue.DeQueue() # Get the next node base on the graph for nextNode in graph[current_node]: # Checks if the node is visited before or not, if not visited, if not visited[nextNode]: # Next node will be marked as visited visited[nextNode] = True # Set the neighbour as the current node parent[nextNode] = current_node # Add next node to the queue queue.EnQueue(nextNode) # if next node is a hospital, if str(nextNode) in hospitals: tmp_k += 1 # Add 1 to hospitals found # Add source node to hospital list hospitalList.append(nextNode) if tmp_k >= k: # Check if number of hospitals is more than k break return hospitalList
def fill_day(busy_list): # day = SLL() day = Queue() for event in busy_list: start = event['start'] end = event['end'] day.EnQueue(start, end) return day