BFS Shortest Distance of Graph Calculation in Python

In summary, the conversation discusses a problem where the distance from a starting vertex to itself is being returned as 2 instead of 0 when using the function bfs to perform a breadth-first-search on a graph stored as a dictionary. The code provided and the output from sample graphs are also mentioned. The solution to the problem is found by declaring traversed[v] = True in order to avoid examining the starting vertex.
  • #1
Vespero
28
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
  • #2
Never mind, I got it. I needed to declare traversed[v] = True so that it wouldn't examine the starting vertex. Problem solved.
 

1. What is BFS Shortest Distance of Graph Calculation in Python?

BFS (Breadth-First Search) is a graph traversal algorithm that is used to explore a graph level by level. It starts at a specific node and explores all of its neighbors before moving on to the next level of nodes. The shortest distance of a graph calculation using BFS in Python refers to finding the minimum number of edges between two given nodes in a graph using BFS.

2. How does BFS Shortest Distance of Graph Calculation work in Python?

In order to calculate the shortest distance between two nodes using BFS in Python, the algorithm starts by creating a queue and adding the starting node to it. Then, it checks if the queue is empty. If not, it removes the first node from the queue, checks if it is the target node, and if not, it adds all of its neighbors to the queue. This process continues until the target node is found, or until the queue is empty. The shortest distance is then calculated by counting the number of levels that were traversed to reach the target node.

3. What is the time complexity of BFS Shortest Distance of Graph Calculation in Python?

The time complexity of BFS Shortest Distance of Graph Calculation in Python is O(V+E), where V is the number of vertices (nodes) and E is the number of edges in the graph. This is because the algorithm has to visit each vertex and edge at most once during the traversal.

4. What are some use cases for BFS Shortest Distance of Graph Calculation in Python?

BFS Shortest Distance of Graph Calculation in Python is commonly used in GPS systems to find the shortest route between two locations. It is also used in social networks to find the shortest path between two users, and in network routing algorithms to find the shortest path between two nodes in a network.

5. Can BFS Shortest Distance of Graph Calculation be used for directed and undirected graphs?

Yes, BFS Shortest Distance of Graph Calculation can be used for both directed and undirected graphs. In directed graphs, the algorithm will follow the direction of the edges, while in undirected graphs, it will consider all edges bidirectionally.

Similar threads

  • Programming and Computer Science
Replies
4
Views
866
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
713
  • Programming and Computer Science
Replies
1
Views
964
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
764
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top