how to print vertex using BFS Algo in python?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


how to print vertex using BFS Algo in python?



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 .


(graph.vertList[currentVert].getConnections())


currentVert


print(g.vertList[1].getConnections())
dict_keys([<__main__.Vertex object at 0x7f7a7db9a3c8>, <__main__.Vertex object at 0x7f7a7db9a2e8>])



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


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


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) :


bfs


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 ?





Similar question: stackoverflow.com/questions/43675771/object-to-string-in-python/…
– 0605002
yesterday





Sorry to say but it is not help. I want to get the list of all node connect to currentNode so i can Travers to connect node.
– navneet arya
yesterday






1 Answer
1



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.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

H5,d3RzrD
dI9MfZ M1w

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

415 Unsupported Media Type while sending json file over REST Template