Shortest Path I
You can leverage what you know about finding neighbors to try finding paths in a network. One algorithm for path-finding between two nodes is the "breadth-first search" (BFS) algorithm. In a BFS algorithm, you start from a particular node and iteratively search through its neighbors and neighbors' neighbors until you find the destination node.
Pathfinding algorithms are important because they provide another way of assessing node importance; you'll see this in a later exercise.
In this set of 3 exercises, you're going to build up slowly to get to the final BFS algorithm. The problem has been broken into 3 parts that, if you complete in succession, will get you to a first pass implementation of the BFS algorithm.
This exercise is part of the course
Introduction to Network Analysis in Python
Exercise instructions
- Create a function called
path_exists()
that has 3 parameters -G
,node1
, andnode2
- and returns whether or not a path exists between the two nodes. - Initialize the queue of nodes to visit with the first node,
node1
.queue
should be a list. - Iterate over the nodes in
queue
. - Get the neighbors of the node using the
.neighbors()
method of the graphG
. - Check to see if the destination node
node2
is in the set ofneighbors
. If it is, returnTrue
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define path_exists()
def ____:
"""
This function checks whether a path exists between two nodes (node1, node2) in graph G.
"""
visited_nodes = set()
# Initialize the queue of nodes to visit with the first node: queue
queue = ____
# Iterate over the nodes in the queue
for node in ____:
# Get neighbors of the node
neighbors = ____
# Check to see if the destination node is in the set of neighbors
if node2 in ____:
print('Path exists between nodes {0} and {1}'.format(node1, node2))
return ____
break