Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running problem
#1
Does anyone know why my code returns only a few directories, instead of the needed output?
Reply
#2
How could we?
Reply
#3
graph = {
    'A' : ['B', 'C', 'E'],
    'B' : ['A', 'D', 'E'],
    'C' : ['A', 'F', 'G'],
    'D' : ['B'],
    'E' : ['A', 'B', 'D'],
    'F' : ['C'],
    'G' : ['C']
}

#visit all the nodes of a graph (connected components)
def bfs_connected_component(graph, start):
    #keep track of all visited nodes
    explored = []
    #keep track of nodes to be checked
    queue = [start]

    #keep looping until there are nodes still to be checked
    while queue:
        #pop shallowest node from queue
        node = queue.pop(0)
        if node not in explored:
            #add node to list of checked nodes
            explored.append(node)
            neighbours = graph[node]

            #add neighbours of node to queue
            for neighbour in neighbours:   
                queue.append(neighbour)
    return explored

bfs_connected_component(graph, 'A')

#find shortest path between 2 nodes of a graph
def bfs_shortest_path(graph,start,goal):
    #keep track of th explored nodes
    explored = []
    #keep track of all the paths to be checked
    queue = [[start]]

    #return path if start is goal
    if start == goal:
        return "Start = goal"
    
    #keeps looping until all possible paths have been found
    while queue:
        #pop the first path from the queue
        path = queue.pop(0)
        #get the last node from the path
        node = path[-1]

        if node not in explored:
            neighbours = graph[node]
            #go through all naighbour nodes, construct
            #ppush it into the queue
            for neighbour in neighbours:
                new_path = list(path)
                new_path.append(neighbour)
                queue.append(new_path)
                #return path if neighbour is goal
                if neighbour == goal:
                    return new_path
            
            #mark node as explored
            explored.append(node)
    return "Sorry, there is no connecting path between the selected nodes."

bfs_shortest_path(graph,'G','D')
[Image: 86661850_2583944158507501_84334962622836...e=5EC00071]
Reply
#4
You never print anything.
Reply
#5
You do nothing with the returned values from the functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 631 Feb-14-2024, 02:57 PM
Last Post: snippsat
  rtmidi problem after running the code x times philipbergwerf 1 2,420 Apr-04-2021, 07:07 PM
Last Post: philipbergwerf
  python to exe running problem Roshan 2 5,582 Mar-30-2021, 08:59 AM
Last Post: Roshan
  Problem running script within console koepjo 3 9,890 Mar-26-2020, 07:11 AM
Last Post: koepjo
  problem running python file using cmd panzers 2 2,295 Dec-19-2019, 04:04 PM
Last Post: panzers
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 3,796 Sep-12-2018, 01:08 PM
Last Post: volcano63
  problem running program in VScode LavaCreeperKing 4 3,863 Mar-25-2018, 04:34 PM
Last Post: LavaCreeperKing
  Program not running (Overloading problem) anurag123 2 2,553 Feb-19-2018, 07:23 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020