1. Learn
  2. /
  3. Courses
  4. /
  5. Data Structures and Algorithms in Python

Connected

Exercise

Finding a graph vertex using BFS

In this exercise, you will modify the BFS algorithm to search for a given vertex within a graph.

To help you test your code, the following graph has been loaded using a dictionary.

Graphical representation of a graph.

graph = {
  '4' : ['6','7'],
  '6' : ['4', '7', '8'],
  '7' : ['4', '6', '9'],
  '8' : ['6', '9'],
  '9' : ['7', '8']
}

Instructions

100 XP
  • Check if you found the search value.
  • Return True if you found the search value.
  • Inside the for loop, check if the adjacent vertex has been visited.
  • Return False if you didn't find the search value.