Two ways to explore everything reachable from a starting point.
The idea (a rumor in a town, or a maze)
A graph is dots (nodes) joined by lines (edges): cities and roads, people and friendships. To explore it you need a to-do list of nodes still to look at, and a note of the ones you have already seen (so you never go in circles).
BFS (breadth-first) uses a queue: first in, first out. It spreads like a rumor: everyone one step away hears first, then everyone two steps away. That makes it find the shortest path (fewest edges).
DFS (depth-first) uses a stack: last in, first out. It behaves like walking a maze: keep going down one path as far as you can, then back up and try the next turn.
The code is almost identical. Only the to-do list (queue vs stack) changes the order.
Watch it work
Nodes are numbered 0, 1, 2, ... with no gaps (at most 10 nodes, 15 edges). Neighbors are visited in increasing order.
Try it
Run BFS and DFS on the same graph. Compare the two visit orders.
Start BFS at node 0 and note when node 4 is reached. That level is its shortest distance from 0.
Add a node that is not connected, like 0-1, 2-3 with start 0. Is node 2 ever visited?
Make a cycle: 0-1, 1-2, 2-0. Watch the “already visited: skip” steps stop an endless loop.
Common mistakes
No visited set. On any graph with a cycle your program loops forever.
Marking visited too late in BFS. Mark a node when you add it to the queue, or the same node can be queued many times.
Using a slice as a queue forever.queue = queue[1:] is fine for practice. For huge inputs be aware that it keeps the old backing array alive.
Forgetting disconnected parts. One search only reaches one connected piece. To cover the whole graph, start a new search from every unvisited node.
Complexity, in plain English: each node is handled once and each edge is looked at a constant number of times, so the work grows with nodes plus edges (O(V + E)). The to-do list can hold up to all nodes, so extra memory is O(V).