CodeGround
DashboardProblemsInterviewsAssessmentsCourses
CodeGround
DashboardProblemsInterviewsAssessmentsCourses
The Technical Interview Course

0 of 170 lessons complete

  • Shortest paths when edges have costs
  • When greedy is not allowed
  • Connecting everything cheaply
  • Using every edge exactly once
  • Deriving the graph from the input
  • Retrieval: which graph algorithm?

Advanced Graphs

121 / 170

Advanced Graphs/Lesson 6 of 6·About 48 min

Retrieval: which graph algorithm?

Two problems rebuilt cold, plus the preconditions that pick the algorithm for you.

Key idea

Read the constraints, not the story

Every choice in this unit is forced by a precondition stated in the problem. Find the precondition and the algorithm follows.

SituationUseBecause
Unweighted, fewest stepsbreadth-first searchequal edges make first arrival cheapest
Non-negative weights, cheapest pathheap-based greedypopping finalizes, nothing can improve it
Negative weights, or a cap on edges usedrelaxation in roundsfinalization is not valid
Connect everything at least total costspanning treenot a path problem at all
Use every edge exactly oncerecord on dead end, reversegreedy forward walk can strand

Tip

And the meta-skill

Two of this unit's problems hide the graph. When a problem has no explicit edges but describes constraints, ordering, or transformations, write down what a node is and what an edge means before anything else.

Quick check· 5 min

Which algorithm?

Flights between cities have prices, some routes offer a rebate making the effective price negative, and you need the cheapest total from one city to another. Which approach is correct?

Problem· 20 min

Network Delay Time, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

Problem· 18 min

Course Schedule II, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

PreviousDeriving the graph from the input
NextCold recall

0 of 4 parts complete