Watch breadth-first search visit a graph level by level using a queue. Worked example from A to E, draw your own nodes and edges, and see O(V+E) time explained.
About this algorithm
Breadth-first search (BFS) explores a graph outward from a start node in rings, using a queue: first every node one edge away, then every node two edges away, and so on. Because of that order, the first time it reaches a node it has used the fewest possible edges, so BFS finds the shortest path in a graph where every edge counts the same.
See BFS search the streets of your city →Use BFS when you want the fewest steps: the fewest flights between two airports, the fewest friend links between two people, the fewest moves in a puzzle. Pick DFS instead when you only need to know whether a route exists, or when you are exploring everything for cycle detection or topological order, since DFS follows one path to its end before trying another. When edges have different costs, the fewest edges is no longer the cheapest route: use Dijkstra for weights of zero or more and Bellman-Ford when some weights are negative.
Create a queue holding only the source and mark the source as visited. A queue hands nodes back in the order they went in, and that is what keeps the search moving one ring at a time.
Remove the node at the front of the queue. If it is the target, stop: the search is done.
Look at each neighbour of that node. Skip any neighbour already marked visited. For a new one, mark it visited now, record which node you reached it from (its parent), and add it to the back of the queue.
Keep taking nodes from the front until the target comes out or the queue runs empty. An empty queue means the target cannot be reached. To read the route, start at the target, follow parent links back to the source, and reverse that list.
The page loads six nodes, A to F, with source A and target E, joined by the edges A-B, A-D, B-C, B-D, B-E, C-E, D-E, D-F and E-F. Neighbours are checked in alphabetical order. The queue starts as [A]. Take A: B and D are new, so the queue is [B, D]. Take B: A and D are already visited, C and E are new, giving [D, C, E]. Take D: its only new neighbour is F, giving [C, E, F]. Take C: both its neighbours are visited. Take E: it is the target, so stop. Nodes came out in the order A, B, D, C, E, and the parent links from E give A → B → E, two edges. The edge weights are hidden on this page because BFS never reads them. The Dijkstra page uses the same graph and shows that A → B → E costs 7 while A → D → B → E costs 6: the fewest edges and the lowest cost are different questions.
Marking a node visited only when it leaves the queue. On this graph D would then be queued twice, once from A and again from B, and on a dense graph the queue can grow far past one entry per node. Mark a node the moment it joins the queue.
Time: each node joins the queue at most once and each edge is looked at once from each end, so the work grows with V + E. Space: the queue, the visited set and the parent links each hold at most one entry per node.