modifiy BFS algorithm to find shortest between one source and a set of destination [closed]

I have a BFS algorithm to find the shortest path between two given node start and goal, I want to modify the code to find shortest paths between one source and set of destination

def BFS_SP(graph, start, goal,N):
    explored = []
    
    # Queue for traversing the
    # graph in the BFS
    queue = [[start]]
    
    # If the desired node is
    # reached
    if start == goal:
        #print("Same Node")
        return []
    
    # Loop to traverse the graph
    # with the help of the queue
    while queue:
        path = queue.pop(0)
        node = path[-1]
        
        # Condition to check if the
        # current node is not visited
        if node not in explored:
            neighbours = graph.edges[node] 
            # Loop to iterate over the
            # neighbours of the node
            for neighbour in neighbours:
                new_path = list(path)
                new_path.append(neighbour)
                queue.append(new_path)
                
                # Condition to check if the
                # neighbour node is the goal
                if neighbour == goal:
                    #print("Shortest path = ", new_path)
                    return new_path
            explored.append(node)

    # Condition when the nodes
    # are not connected
    #print("So sorry, but a connecting"\
                #"path doesn't exist :(")
    return []


Read more here: https://stackoverflow.com/questions/68475417/modifiy-bfs-algorithm-to-find-shortest-between-one-source-and-a-set-of-destinati

Content Attribution

This content was originally published by charafeddine at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: