Graph
Vertices and edges for relationships that are not purely linear
Model connections explicitly
A graph contains vertices and edges. Edges may be directed or undirected, weighted or unweighted. An adjacency list stores only existing neighbors and uses O(V + E) space; an adjacency matrix uses O(V^2) space but answers edge-existence queries in constant time.
BFS and DFS differ only in the frontier
Breadth-first search uses a queue, so it expands one distance layer at a time and finds shortest paths in an unweighted graph. Depth-first search uses a stack, so it follows a branch until it must backtrack. Both mark vertices to avoid revisiting cycles.
Choose a start vertex, then run BFS or DFS. The current start is A.
O(V + E) time. Continue with Dijkstra's algorithm for weighted shortest paths or Topological Sort for dependency order.