DEV Community

Harsh Mishra
Harsh Mishra

Posted on

BFS Graph Traversal C++: Story

๐ŸŒŠ THE BREATH OF THE CITY โ€” A Tale of BFS Traversal

"The city doesnโ€™t speak all at once.
It breathes โ€” one breath, one ripple, one step at a time."

โ€” From โ€œThe Heart of Every Graphโ€, by Sora of the Gates


๐ŸŒ† Prologue: The City Sleeps in Silence

Once, in the land of Netra, there stood a city like no other.

Its walls were built not in stone, but in connections โ€”
streets and alleys, winding webs between neighborhoods.

Each corner, each plaza, each shadowed gate was a node.
Some touched many others; some were isolated, forgotten.

But something happened.

One night, the city's pulse stopped.

No footsteps. No laughter. No word from its distant corners.

The Watchers of Netra declared:

โ€œWe must awaken it.
We must explore every path, every street โ€”
not in chaos, but in order, one breath at a time.โ€

And thus began the Breath of the City, the ancient ritual now known as Breadth-First Search (BFS).


๐Ÿ”ง The Tools of the Sentinel

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
Enter fullscreen mode Exit fullscreen mode

The Watchers packed their essentials:

  • A map: connections between places.
  • A horn: to call neighbors when needed.
  • A memory scroll: to remember who had already been seen.

Their traversal was not random. It was systematic โ€”
like a wave rippling out from the center of a pond.


๐Ÿ“œ The Scroll of Echoing Steps

class Graph {
    int V;
    vector<vector<int>> adj;

public:
    Graph(int V) : V(V) {
        adj.resize(V);
    }

    void addEdge(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
Enter fullscreen mode Exit fullscreen mode

The Graph was not just a structure โ€”
it was the city itself.

With addEdge(u, v), the Watchers carved a passage between u and v.

Every alley was bi-directional, every bond mutual.

These paths were not yet traveled โ€” only potential, like veins before blood flows.


๐ŸŒฌ๏ธ The Ritual of Awakening โ€” BFS Begins

    void bfs(int start) {
        vector<bool> visited(V, false);
        queue<int> q;

        visited[start] = true;
        q.push(start);

        cout << "BFS Traversal: ";
Enter fullscreen mode Exit fullscreen mode

The Watchers gathered at gate start.

They marked it visited in their scroll โ€” they had stood there, awakened it.

Then, they called out through the gate โ€” the call echoed down its passages.

This was the first breath: the starting node placed into the queue โ€”
a sacred vessel that always calls the oldest unvisited corner first.

The traversal began.


๐ŸŒŠ The Wave of the City Moves

        while (!q.empty()) {
            int u = q.front(); q.pop();
            cout << u << " ";
Enter fullscreen mode Exit fullscreen mode

The city answered.

They stepped into the gate u that had waited longest.

They recorded it in the Great Song (cout << u) โ€” the traversal echo.

Each step was remembered, each breath heard.

The breath had begun โ€” and now, it spread.


๐Ÿ—ฃ๏ธ Calling All Neighbors

            for (int v : adj[u]) {
                if (!visited[v]) {
                    visited[v] = true;
                    q.push(v);
                }
            }
        }
        cout << "\n";
    }
};
Enter fullscreen mode Exit fullscreen mode

From every awakened street, they looked around:

โ€œWho among you still sleeps?โ€

For each unseen neighbor v, they:

  1. Marked them as visited โ€” to prevent waking them twice.
  2. Added them to the queue โ€” to be awakened in their rightful order.

No leaps. No chaos.

The breath of the city must touch all corners level by level,
neighbor by neighbor, like sunlight rising over rooftops.


๐ŸŒ‡ The Watcherโ€™s Walk โ€” An Example

int main() {
    Graph g(7);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 3);
    g.addEdge(1, 4);
    g.addEdge(2, 5);
    g.addEdge(5, 6);

    g.bfs(0);
}
Enter fullscreen mode Exit fullscreen mode

In the city of 7 districts,
the Watchers lit the first torch at district 0.

From there:

  • They reached 1 and 2
  • Then 3, 4, and 5
  • And finally 6

Each breath touched the next closest layer,
and the city began to stir โ€” one neighborhood at a time.

BFS Traversal: 0 1 2 3 4 5 6

A new day had dawned.


๐Ÿง  The Wisdom in the Walk

  • queue<int> q โ€” the sacred procession; whoโ€™s next to awaken
  • visited[] โ€” the mark of breath, to avoid chaos
  • adj[] โ€” the blueprint of the city's potential
  • bfs(start) โ€” the breath itself, rippling outward from one spark

Unlike depth-first wanderers who dive into alleys and may get lost,
BFS watches every level, every ripple โ€” calm, controlled, complete.


โ€œLet no corner remain untouched.
Let every whisper carry forward.
Let the city know itself โ€” not all at once,
but with breath, and memory, and time.โ€

Thus the city of Netra awoke once again โ€”
with the ancient art of BFS,
and the memory of the Watchers who walked not to conquer,
but to listen.

Top comments (0)