BFS Shortest Distance of Graph Calculation in Python

Click For Summary
SUMMARY

The forum discussion focuses on a Python implementation of the breadth-first search (BFS) algorithm to calculate the shortest distance from a starting vertex in a graph represented as a dictionary. The user encountered an issue where the distance from the starting vertex to itself was incorrectly reported as 2 instead of 0. The solution involved adding a line to mark the starting vertex as traversed immediately after initializing the distance dictionary, specifically by setting traversed[v] = True.

PREREQUISITES
  • Understanding of Python programming and syntax
  • Familiarity with graph data structures, specifically dictionaries
  • Knowledge of breadth-first search (BFS) algorithm principles
  • Experience with debugging and testing Python functions
NEXT STEPS
  • Explore advanced graph algorithms such as Dijkstra's algorithm for shortest paths
  • Learn about Python's collections.deque for efficient queue operations
  • Investigate graph representation techniques beyond dictionaries, such as adjacency lists
  • Study unit testing in Python to validate graph algorithms
USEFUL FOR

Software developers, computer science students, and anyone interested in implementing graph algorithms in Python will benefit from this discussion.

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.
 

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
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 15 ·
Replies
15
Views
2K