You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/graphs.md
+37-36Lines changed: 37 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -314,7 +314,7 @@ What does it mean to traverse a graph?
314
314
Graph traversal **can** be done in a way that visits *all* vertices and edges (like a full DFS/BFS), but it doesn’t *have to*.
315
315
316
316
* If you start DFS or BFS from a single source vertex, you’ll only reach the **connected component** containing that vertex. Any vertices in other components won’t be visited.
317
-
* Some algorithms (like shortest path searches, A\*, or even partial DFS) intentionally stop early, meaning not all vertices or edges are visited.
317
+
* Some algorithms (like shortest path searches, A*, or even partial DFS) intentionally stop early, meaning not all vertices or edges are visited.
318
318
* In weighted or directed graphs, you may also skip certain edges depending on the traversal rules.
319
319
320
320
So the precise way to answer that question is:
@@ -784,9 +784,9 @@ Shortest path A→E: A → C → B → E (total cost 4)
784
784
* In *single-source shortest paths* with non-negative edge weights, Dijkstra’s algorithm efficiently finds minimum-cost routes in settings like roads, communication networks, or transit systems; without it, travel times or costs could not be computed reliably when distances vary.
785
785
* For *navigation and routing*, stopping the search as soon as the destination is extracted from the priority queue avoids unnecessary work; without this early stop, route planning in a road map continues exploring irrelevant regions of the network.
786
786
* In *network planning and quality of service (QoS)*, Dijkstra selects minimum-latency or minimum-cost routes when weights are additive and non-negative; without this, designing efficient data or logistics paths becomes more error-prone.
787
-
* As a *building block*, Dijkstra underlies algorithms like A\* (with zero heuristic), Johnson’s algorithm for all-pairs shortest paths in sparse graphs, and $k$-shortest path variants; without it, these higher-level methods would lack a reliable core procedure.
787
+
* As a *building block*, Dijkstra underlies algorithms like A* (with zero heuristic), Johnson’s algorithm for all-pairs shortest paths in sparse graphs, and $k$-shortest path variants; without it, these higher-level methods would lack a reliable core procedure.
788
788
* In *multi-source Dijkstra*, initializing the priority queue with several starting nodes at distance zero solves nearest-facility queries, such as finding the closest hospital; without this extension, repeated single-source runs would waste time.
789
-
* As a *label-setting baseline*, Dijkstra provides the reference solution against which heuristics like A\*, ALT landmarks, or contraction hierarchies are compared; without this baseline, heuristic correctness and performance cannot be properly evaluated.
789
+
* As a *label-setting baseline*, Dijkstra provides the reference solution against which heuristics like A*, ALT landmarks, or contraction hierarchies are compared; without this baseline, heuristic correctness and performance cannot be properly evaluated.
790
790
* For *grid pathfinding with terrain costs*, Dijkstra handles non-negative cell costs when no admissible heuristic is available; without it, finding a least-effort path across weighted terrain would require less efficient exhaustive search.
791
791
792
792
**Implementation**
@@ -932,7 +932,7 @@ Bellman–Ford would perform a $V$-th pass and still find an improvement (e.g.,
932
932
933
933
**Applications**
934
934
935
-
* In *shortest path problems with negative edges*, Bellman–Ford is applicable where Dijkstra or A\* fail, such as road networks with toll credits; without this method, these graphs cannot be handled correctly.
935
+
* In *shortest path problems with negative edges*, Bellman–Ford is applicable where Dijkstra or A* fail, such as road networks with toll credits; without this method, these graphs cannot be handled correctly.
936
936
* For *arbitrage detection* in currency or financial markets, converting exchange rates into $\log$ weights makes profit loops appear as negative cycles; without Bellman–Ford, such opportunities cannot be systematically identified.
937
937
* In solving *difference constraints* of the form $x_v - x_u \leq w$, the algorithm checks feasibility by detecting whether any negative cycles exist; without this check, inconsistent scheduling or timing systems may go unnoticed.
938
938
* As a *robust baseline*, Bellman–Ford verifies results of faster algorithms or initializes methods like Johnson’s for all-pairs shortest paths; without it, correctness guarantees in sparse-graph all-pairs problems would be weaker.
@@ -947,7 +947,7 @@ Bellman–Ford would perform a $V$-th pass and still find an improvement (e.g.,
947
947
948
948
#### A* (A-Star) Algorithm
949
949
950
-
A\* is a best-first search that finds a **least-cost path** from a start to a goal by minimizing
950
+
A* is a best-first search that finds a **least-cost path** from a start to a goal by minimizing
951
951
952
952
$$
953
953
f(n) = g(n) + h(n),
@@ -958,7 +958,7 @@ where:
958
958
* $g(n)$ = cost from start to $n$ (so far),
959
959
* $h(n)$ = heuristic estimate of the remaining cost from $n$ to the goal.
960
960
961
-
If $h$ is **admissible** (never overestimates) and **consistent** (triangle inequality), A\* is **optimal** and never needs to “reopen” closed nodes.
961
+
If $h$ is **admissible** (never overestimates) and **consistent** (triangle inequality), A* is **optimal** and never needs to “reopen” closed nodes.
* The *time* complexity of A\* is worst-case exponential, though in practice it runs much faster when the heuristic $h$ provides useful guidance; without an informative heuristic, the search can expand nearly the entire graph, as in navigating a large grid without directional hints.
1025
-
* The *space* complexity is $O(V)$, covering the priority queue and bookkeeping maps, which makes A\* memory-intensive; without recognizing this, applications such as robotics pathfinding may exceed available memory on large maps.
1026
-
* In *special cases*, A\* reduces to Dijkstra’s algorithm when $h \equiv 0$, and further reduces to BFS when all edges have cost 1 and $h \equiv 0$; without this perspective, one might overlook how A\* generalizes these familiar shortest-path algorithms.
1024
+
* The *time* complexity of A* is worst-case exponential, though in practice it runs much faster when the heuristic $h$ provides useful guidance; without an informative heuristic, the search can expand nearly the entire graph, as in navigating a large grid without directional hints.
1025
+
* The *space* complexity is $O(V)$, covering the priority queue and bookkeeping maps, which makes A* memory-intensive; without recognizing this, applications such as robotics pathfinding may exceed available memory on large maps.
1026
+
* In *special cases*, A* reduces to Dijkstra’s algorithm when $h \equiv 0$, and further reduces to BFS when all edges have cost 1 and $h \equiv 0$; without this perspective, one might overlook how A* generalizes these familiar shortest-path algorithms.
1027
1027
1028
1028
**Visual walkthrough (grid with 4-neighborhood, Manhattan $h$)**
Movement cost = 1 per step; 4-dir moves; h = Manhattan distance
1042
1042
```
1043
1043
@@ -1062,13 +1062,14 @@ Nodes near the straight line to G are preferred over detours around '#'.
1062
1062
1063
1063
```
1064
1064
Final path (example rendering):
1065
-
┌───────────────────┐
1066
-
1 × × × × . # . . . │
1067
-
2 × # # × × # . # . │
1068
-
3 × × × × × × × # . │
1069
-
4 # . # # × # × × × │
1070
-
5 . . . # × × × # G │
1071
-
└───────────────────┘
1065
+
Row/Col → 1 2 3 4 5 6 7 8 9
1066
+
┌─────────────────────────────┐
1067
+
1 │ × × × × . # . . . │
1068
+
2 │ × # # × × # . # . │
1069
+
3 │ × × × × × × × # . │
1070
+
4 │ # . # # × # × × × │
1071
+
5 │ . . . # × × × # G │
1072
+
└─────────────────────────────┘
1072
1073
Path length (g at G) equals number of × steps (optimal with admissible/consistent h).
1073
1074
```
1074
1075
@@ -1102,31 +1103,31 @@ For **sliding puzzles (e.g., 8/15-puzzle)**:
1102
1103
1103
1104
**Admissible vs. consistent**
1104
1105
1105
-
* An *admissible* heuristic satisfies $h(n) \leq h^*(n)$, meaning it never overestimates the true remaining cost, which guarantees that A\* finds an optimal path; without admissibility, the algorithm may return a suboptimal route, such as a longer-than-necessary driving path.
1106
+
* An *admissible* heuristic satisfies $h(n) \leq h^*(n)$, meaning it never overestimates the true remaining cost, which guarantees that A* finds an optimal path; without admissibility, the algorithm may return a suboptimal route, such as a longer-than-necessary driving path.
1106
1107
* A *consistent (monotone)* heuristic obeys $h(u) \leq w(u,v) + h(v)$ for every edge, ensuring that $f$-values do not decrease along paths and that once a node is removed from the open set, its $g$-value is final; without consistency, nodes may need to be reopened, increasing complexity in searches like grid navigation.
1107
1108
1108
1109
**Applications**
1109
1110
1110
-
* In *pathfinding* for maps, games, and robotics, A\* computes shortest or least-risk routes by combining actual travel cost with heuristic guidance; without it, movement planning in virtual or physical environments becomes slower or less efficient.
1111
-
* For *route planning* with road metrics such as travel time, distance, or tolls, A\* incorporates these costs and constraints into its evaluation; without heuristic search, navigation systems must fall back to slower methods like plain Dijkstra.
1112
-
* In *planning and scheduling* tasks, A\* serves as a general shortest-path algorithm in abstract state spaces, supporting AI decision-making; without it, solving resource allocation or task sequencing problems may require less efficient exhaustive search.
1113
-
* In *puzzle solving* domains such as the 8-puzzle or Sokoban, A\* uses problem-specific heuristics to guide the search efficiently; without heuristics, the state space may grow exponentially and become impractical to explore.
1114
-
* For *network optimization* problems with nonnegative edge costs, A\* applies whenever a useful heuristic is available to speed convergence; without heuristics, computations on communication or logistics networks may take longer than necessary.
1111
+
* In *pathfinding* for maps, games, and robotics, A* computes shortest or least-risk routes by combining actual travel cost with heuristic guidance; without it, movement planning in virtual or physical environments becomes slower or less efficient.
1112
+
* For *route planning* with road metrics such as travel time, distance, or tolls, A* incorporates these costs and constraints into its evaluation; without heuristic search, navigation systems must fall back to slower methods like plain Dijkstra.
1113
+
* In *planning and scheduling* tasks, A* serves as a general shortest-path algorithm in abstract state spaces, supporting AI decision-making; without it, solving resource allocation or task sequencing problems may require less efficient exhaustive search.
1114
+
* In *puzzle solving* domains such as the 8-puzzle or Sokoban, A* uses problem-specific heuristics to guide the search efficiently; without heuristics, the state space may grow exponentially and become impractical to explore.
1115
+
* For *network optimization* problems with nonnegative edge costs, A* applies whenever a useful heuristic is available to speed convergence; without heuristics, computations on communication or logistics networks may take longer than necessary.
1115
1116
1116
1117
**Variants & practical tweaks**
1117
1118
1118
-
* Viewing *Dijkstra* as A\* with $h \equiv 0$ shows that A\* generalizes the classic shortest-path algorithm; without this equivalence, the connection between uninformed and heuristic search may be overlooked.
1119
-
* In *Weighted A\**, the evaluation function becomes $f = g + \varepsilon h$ with $\varepsilon > 1$, trading exact optimality for faster performance with bounded suboptimality; without this variant, applications needing quick approximate routing, like logistics planning, would run slower.
1120
-
* The *A\*ε / Anytime A\** approach begins with $\varepsilon > 1$ for speed and gradually reduces it to converge toward optimal paths; without this strategy, incremental refinement in real-time systems like navigation aids is harder to achieve.
1121
-
* With *IDA\** (Iterative Deepening A\*), the search is conducted by gradually increasing an $f$-cost threshold, greatly reducing memory usage but sometimes increasing runtime; without it, problems like puzzle solving could exceed memory limits.
1119
+
* Viewing *Dijkstra* as A* with $h \equiv 0$ shows that A* generalizes the classic shortest-path algorithm; without this equivalence, the connection between uninformed and heuristic search may be overlooked.
1120
+
* In *Weighted A**, the evaluation function becomes $f = g + \varepsilon h$ with $\varepsilon > 1$, trading exact optimality for faster performance with bounded suboptimality; without this variant, applications needing quick approximate routing, like logistics planning, would run slower.
1121
+
* The *A*ε / Anytime A** approach begins with $\varepsilon > 1$ for speed and gradually reduces it to converge toward optimal paths; without this strategy, incremental refinement in real-time systems like navigation aids is harder to achieve.
1122
+
* With *IDA** (Iterative Deepening A*), the search is conducted by gradually increasing an $f$-cost threshold, greatly reducing memory usage but sometimes increasing runtime; without it, problems like puzzle solving could exceed memory limits.
1122
1123
**RBFS and Fringe Search* are memory-bounded alternatives that manage recursion depth or fringe sets more carefully; without these, large state spaces in AI planning can overwhelm storage.
1123
1124
* In *tie-breaking*, preferring larger $g$ or smaller $h$ when $f$ ties reduces unnecessary re-expansions; without careful tie-breaking, searches on uniform-cost grids may explore more nodes than needed.
1124
1125
* For the *closed-set policy*, when heuristics are inconsistent, nodes must be reopened if a better $g$ value is found; without allowing this, the algorithm may miss shorter paths, as in road networks with varying travel times.
1125
1126
1126
1127
**Pitfalls & tips**
1127
1128
1128
-
* The algorithm requires *non-negative edge weights* because A\* assumes $w(u,v) \ge 0$; without this, negative costs can cause nodes to be expanded too early, breaking correctness in applications like navigation.
1129
-
* If the heuristic *overestimates* actual costs, A\* loses its guarantee of optimality; without enforcing admissibility, a routing system may return a path that is faster to compute but longer in distance.
1129
+
* The algorithm requires *non-negative edge weights* because A* assumes $w(u,v) \ge 0$; without this, negative costs can cause nodes to be expanded too early, breaking correctness in applications like navigation.
1130
+
* If the heuristic *overestimates* actual costs, A* loses its guarantee of optimality; without enforcing admissibility, a routing system may return a path that is faster to compute but longer in distance.
1130
1131
* With *floating-point precision issues*, comparisons of $f$-values should include small epsilons to avoid instability; without this safeguard, two nearly equal paths may lead to inconsistent queue ordering in large-scale searches.
1131
1132
* In *state hashing*, equivalent states must hash identically so duplicates are merged properly; without this, search in puzzles or planning domains may blow up due to treating the same state as multiple distinct ones.
1132
1133
* While *neighbor order* does not affect correctness, it influences performance and the aesthetics of the returned path trace; without considering this, two identical problems might yield very different expansion sequences or outputs.
0 commit comments