0

The problem is in this line (graph.vertList[currentVert].getConnections()) I am unable to get the list of all connected node which is currently connected to currentVert , when I print this code i get the all connected object print(g.vertList[1].getConnections()) dict_keys([<__main__.Vertex object at 0x7f7a7db9a3c8>, <__main__.Vertex object at 0x7f7a7db9a2e8>])
I am unable to find node(vertex) or id for these object so that ,I can Travers easily from one node to another node and print the result at same time .

I am unable to fine the bug , here is the complete code :-

class Queue:

def __init__(self):
    self.queue=[]

def enqueue(self,item):
    self.queue.insert(0,item)

def isEmpty(self):
    return self.queue == []

def dequeue(self):
    return self.queue.pop()

def size(self):
    return len(self.queue)  

Then I create an another class Vertex :

class Vertex:  
def __init__(self,key):  
    self.id=key  
    self.connectedTo={}  

def addNeighbor(self,nbr,weight=0):
    self.connectedTo[nbr]=weight

def __str__(self):
    return str(self.id)+' Connected To : '+str([x.id for x in self.connectedTo])

def getConnections(self):
    return self.connectedTo.keys()

def getId(self):
    return self.id

def getWeight(self,nbr):
    return self.connectedTo[nbr]

Another class Graph :

class Graph:  
def __init__(self):
    self.vertList={}
    self.numVertices=0

def addVertex(self,key):
    self.numVertices=self.numVertices+1
    newVertex=Vertex(key)
    self.vertList[key]=newVertex
    return newVertex

def addEdges(self,f,t,cost=0):
    if f in self.vertList:
        if t in self.vertList:
            self.vertList[f].addNeighbor(self.vertList[t],cost)
        else:
            return "Not present in Graph"
    else:
        return "Not present in Graph"

def getVertex(self,n):
    if n in self.vertList:
        return self.vertList[n]
    else:
        return None

def getVertices(self):
    return self.vertList.keys()

After that I created a function bfs (Breadth First Search) :

def bfs(graph,start):
    #Keep track of all visited nodes
    visited=[]
    #keep track of nodes to be checked using queue
    vertQueue= Queue()
    vertQueue.enqueue(start)
    #Keep looking until there are nodes still to be checked
    while vertQueue:
        #pop shallowest node (first node ) from queue
        currentVert=vertQueue.dequeue()
        print(currentVert,end="")
        for nbr in (graph.vertList[currentVert].getConnections()):
            if nbr not in visited:
                #add node to list of checked nodes
                vertQueue.enqueue(nbr)
                visited.append(currentVert)

How can I fix this problem ?

2

1 Answer 1

0

The problem is here:

self.vertList[f].addNeighbor(self.vertList[t],cost)

Change this to

self.vertList[f].addNeighbor(t,cost)

and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.