Python BFS Shortest Distance of Graph Calculation in Python

Click For Summary
The discussion revolves around a coding issue in a breadth-first search (BFS) implementation for a graph represented as a dictionary. The user successfully calculates distances from a starting vertex to other vertices but encounters an error where the distance from the starting vertex to itself is incorrectly reported as 2 instead of 0. The code initializes a distance dictionary and uses a queue for traversal. The problem was identified as the need to mark the starting vertex as traversed to prevent it from being processed again. Once the user updated the code to set traversed[v] = True, the issue was resolved, ensuring the distance from the starting vertex to itself is correctly returned as 0.
Vespero
Messages
26
Reaction score
0

Homework Statement



I have this problem essentially figured out. There's just one tiny problem that I can't seem to solve. I'm supposed to write a function bfs(G, v) which takes a graph G stored as a dictionary, and a starting vertex v. The function bfs performs a breadth-first-search while storing the distance from the starting vertex to each of the other vertices in a dictionary labeled distance{} and then prints and returns distance. On all of the test graphs I've run so far, the distance from the starting vertex v to all other vertices is correct. However, the value listed for the distance from vertex v to itself is being returned as 2, while it should be 0.

Homework Equations



My code so far is the following:

import sys

H = {1:[2], 2:[1,3], 3:[2,4], 4:[3,5,7],
5:[4,6], 6:[5,7], 7:[4,6]}

def build_traversed_dict(D):
"""Builds a dictionary that stores whether a vertex been reached."""
traversed = {}
for i in D:
traversed=False

return traversed


def bfs(G, v):
"""Performs a breadth first traversal of G, starting at v."""
traversed = build_traversed_dict(G)
queue = [v]
distance = {}
for i in G:
distance=-1

distance[v] = 0
while (queue != []):
curr_v = queue.pop(0)
for i in G[curr_v]:
for i in G.get(curr_v):
if (traversed == False):
print str(i)
traversed = True
distance = distance[curr_v]+1
queue.append(i)


print distance
return distance


The Attempt at a Solution



In the sample graph provided, choosing a couple of vertices at random gave the following results:

bfs(H, 2):

1
3
2
4
5
7
6
{1: 1, 2: 2, 3: 1, 4: 2, 5: 3, 6: 4, 7: 3}

bfs(H, 5):

4
6
3
5
7
2
1
{1: 4, 2: 3, 3: 2, 4: 1, 5: 2, 6: 1, 7: 2}

As you can see, it's returning the value 2 for the distance from vertex v to itself, which should be 0. I can't figure out why this is happening despite looking through the code repeatedly.
 
Last edited:
Technology news on Phys.org
Never mind, I got it. I needed to declare traversed[v] = True so that it wouldn't examine the starting vertex. Problem solved.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K