Skip to main content

Graphs for Data Structures and Algorithmss

Graph algorithms work on nodes connected by edges, modelling networks, maps and dependencies. The unit covers representing graphs with adjacency lists, traversing with BFS and DFS, topological sort for dependency ordering, union-find for connectivity, and shortest paths with Dijkstra's algorithm on weighted edges.

Stage: Trees and Graphs · Part of the Data Structures and Algorithms roadmap · Last reviewed 19 November 2026

What to learn in Graphs

Graphs interview questions

When do you use BFS instead of DFS?

Use BFS to find the shortest path in an unweighted graph or to explore level by level, because it visits nodes in order of distance. DFS suits exhaustive exploration, cycle detection and topological ordering, and it uses less memory on wide graphs.

Why does Dijkstra's algorithm fail with negative edge weights?

Dijkstra assumes that once a node is finalised, no later path can be shorter. A negative edge can make a longer route become cheaper afterwards, breaking that assumption. Use Bellman-Ford for graphs with negative weights.

Where Graphs fits in the Data Structures and Algorithms roadmap

Graphs belongs to the Trees and Graphs stage (intermediate level), which takes about 32 hours. Non-linear structures.

Back to the full Data Structures and Algorithms roadmap