Python BFS Shortest Distance of Graph Calculation in Python

AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
3
Views
1K
Replies
2
Views
1K
Replies
4
Views
1K
Replies
16
Views
2K
Replies
18
Views
1K
Replies
34
Views
3K
Replies
15
Views
2K
Back
Top