This community-built FAQ covers the “Breadth-First Search 2: The Breadthening” exercise from the lesson “Graph Search: Python”.
Paths and Courses
This exercise can be found in the following Codecademy content:
FAQs on the exercise Breadth-First Search 2: The Breadthening
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hello, please can someone explain what this line of code is doing?
current_vertex, path = bfs_queue.pop(0)
I’m getting confused because bfs_queue is a list that contains a list (and potentially multiple lists). I don’t understand how this line of code is able to access the items in the list that’s inside the bfs_queue.
We would need to have greater wherewithal as to just what ‘bfs_queue’ pertains to, in terms of structure. From the assignment we can only ascertain that each element has two members which we unpack from the popped item and give to respective variables.
Oops, didn’t read that far; my bad. Okay, so the above assumption goes along with that. Is the structure,
Yes, something like that. Here’s a snippet of the actual code for greater context:
def bfs(graph, start_vertex, target_value):
path = [start_vertex]
vertex_and_path = [start_vertex, path]
bfs_queue = [vertex_and_path]
visited = set()
# Continue the function here:
while bfs_queue:
current_vertex, path = bfs_queue.pop(0)
I don’t understand how the last line of code is actually able to access the items in the list inside bfs_queue. Shouldn’t that line of code be something like:
Oh, so does python allow you to access elements in a list without having to pop them out of the list using this syntax, then? I think that’s what got me confused. If that’s the case, then it makes a lot of sense now. Thank you!
I see. Didn’t even know there’s a data structure that’s literally a sequence of literals. Thank you so much for explaining this to me. It makes so much sense now.
For this language, anyway… Internally Python dresses it up as a tuple, or immutable sequence. It is indicative of an unknown number of sequence members and the basis of *args in function parameters, which I’m sure you will enjoy reading, and learning from, now that this idea has been revealed.
Be mindful to not take liberties as much as make prudent and apt use of this built in behavioral feature. Utilize the part that makes sense; otherwise make it clear the data structure you want your reader to recognize.
Always excited to see a learner tackling the curve. Happy coding!
Draw from this whatever meaningful information you can carry forward with you. And really go to town messing with this stuff while you have the mind to, before you move on from it.
This is interesting. I’m not quite sure that I fully understand, but it looks like the keeping * allows the sequence maintain it’s status as simply a sequence of literals, otherwise it defaults to a tuple? I’ll definitely test it out on a code editor to see if that’s the case. Thank you!
The star is what we used to call the splat operator. It may still be called that, for all I know. It is part of the suite of unpacking tools, so definitely something to become fully aware of.
Oh, yeah, the default is always a tuple since that is the most efficient data structure that Python has at its disposal (we have to create a more efficient one, if we want it for our purposes).
I’m glad I understand how this works a little more than I did before, and I’ll definitely read up about it to see where it might be useful. Thank you so much!
Sets just look like dictionaries with values that don’t have keys haha. I’ve never had to use them before asides this lesson, but I’ll definitely remember that.
They only look like dictionaries because there is no other suitable bracket character for their type. I guess we could call them, ‘keyless dictionaries’, although that would be a misnomer. They are a unique form of data structure so we should not confuse them, or associate them in any way with dictionaries. If you don’t see the colon, it’s a set. Completely different set of rules apply.
So we have so far covered how many types of unique data structures?
Oh that makes sense. I gave a sets a quick read and see that they indeed have methods that are quite unique to them. They have methods that resemble the operations we can perform on sets in mathematics.
Precisely. Once we learn the syntax and usage, we can do with Python what we can do with pencil on paper.
Unions, intersections and disparity are mathematical principles that the language gives us tools for ready use. That is another lesson/course just waiting for you. Get past the language hurdles, first, though, so definitely back burner this one.
There is a relation between sets and dictionaries, but more ethereal than programmatically significant. Sets have no duplicate members. Dictionaries, if we confine the keys to an object we will also discover to be a set. No duplicate keys. So it’s actually okay to call the contents of a set or a dictionary, ‘members’.
We can actually leverage set theory in working with dictionaries along the lines mentioned above. Don’t ask me how that would play out, but leave it on the back burner as something that may come in handy, some day, since you are on the subject of Search algorithms. Could be these ideas might go a long way to optimizing your code. Who knows?
I will keep these in mind. I have definitely enjoyed applying what I know from maths to programming; it also makes it easier to understand programming concepts. I think it would also be interesting to see what kinds of problems will require me to use dictionaries in the way that I would normally use sets. Maybe I can write my own set functions but for dictionaries.