LearnThatStack Ace your next interview
Graphs · question
Question 4 of 60

What does it mean for a graph to be weighted, and why does it matter?

beginner
← All Graphs questions
Re-explain

A weighted graph attaches a number to every edge, standing for cost, distance or time. Some graphs use it for capacity instead. An unweighted graph treats all edges as equal, so every edge counts as one step. Weights change the question from "how few edges" to "how little total cost".

That matters because the cheapest route is often not the shortest by edge count. Three short highway segments can beat one long back road. Weights are miles or minutes on a map, and latency or price on a network.

Breadth-first search finds fewest-edge paths, but it ignores weights and gives wrong answers once they exist. You then need a weight-aware method like Dijkstra. So first ask whether edges carry a real cost, because that decides which algorithm is correct.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Graphs One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Why does BFS give wrong answers once edges have different weights? BFS settles a node at its first visit, by edge count. A later path with more edges but lower total cost can still win.
  • If every edge has the same weight, do you still need Dijkstra? With equal weights, total cost grows with edge count, so BFS stays correct and runs in linear time without a heap.
  • Can Dijkstra handle negative edge weights? What do you use if not? Dijkstra finalizes the nearest node assuming no later edge can lower its cost. A negative edge breaks that, and Bellman-Ford handles it.
  • If edges weigh only 0 or 1, can you beat Dijkstra? Name 0-1 BFS. A deque takes 0-weight neighbors at the front and 1-weight ones at the back, so it runs in linear time without a heap.
  • How do you store edge weights in an adjacency list versus a matrix? List entries become neighbor-weight pairs. Matrix cells hold the weight, with a no-edge marker like infinity, since 0 can be a real weight.

What you can say

  1. A weighted graph puts a number on every edge, usually a cost, a distance or a time, and sometimes a capacity.
  2. In an unweighted graph every edge counts as one step, so the question is how few edges get you from A to B.
  3. Weights change that to how little total cost, and the cheapest route often isn't the one with the fewest edges. Three short highway segments can beat one long back road.
  4. On a map the weights are miles or minutes, and on a network they're latency or price.
  5. BFS finds fewest-edge paths, but it ignores weights, so once they exist it can return the wrong path and you need something weight-aware like Dijkstra.
  6. So the first thing I ask is whether the edges carry a real cost. If they don't, BFS is correct and cheaper, and if they do, BFS gives wrong answers.

Weak answers to avoid

  • Defines the term and stops The question asks why it matters. Say that weights turn fewest edges into lowest total cost, and that this decides which algorithm returns the right path.
  • Uses BFS for shortest paths on weighted edges BFS counts edges, not cost, so it can return a path with fewer edges and a higher total. Name a weight-aware method like Dijkstra.
  • Says Dijkstra works for any weights Dijkstra assumes no edge weight is negative. State that limit, and mention Bellman-Ford for graphs with negative edges.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Graphs cheatsheet.

← Back to all Graphs questions
Pro · $10/mo

51 of 60 Graphs answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime