Skip to content

Commit 4779aaf

Browse files
authored
Enhance graphs.md with graph examples and explanations
Updated the graph representation and added explanations for topological order and cycles.
1 parent eedfb08 commit 4779aaf

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

notes/graphs.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,7 @@ TopoSort_Kahn(G):
15031503
DAG; we’ll start with all indegree-0 vertices. (Edges shown as arrows.)
15041504

15051505
```
1506+
DAG
15061507
┌───────┐
15071508
│ A │
15081509
└───┬───┘
@@ -1548,6 +1549,7 @@ Step | Pop u | Emit order | Decrease indeg[...] | Newly 0 → En
15481549
```
15491550

15501551
*A valid topological order:*
1552+
15511553
`A, B, C, E, D, F, G` (others like `B, A, C, E, D, F, G` are also valid.)
15521554

15531555
*Clean left-to-right view (one possible ordering):*
@@ -1563,11 +1565,11 @@ A B F C E D G
15631565
If there’s a cycle, some vertices **never** reach indegree 0. Example:
15641566

15651567
```
1566-
┌─────┐ ┌─────┐
1567-
│ X │ ───► │ Y │
1568-
└──┬──┘ └──┬──┘
1569-
└───────────►┘
1570-
(Y ───► X creates a cycle)
1568+
┌─────┐ ┌─────┐
1569+
│ X │ ───► │ Y │
1570+
└──┬──┘ └──┬──┘
1571+
└───────────►┘
1572+
(Y ───► X creates a cycle)
15711573
```
15721574

15731575
Here `indeg[X]=indeg[Y]=1` initially; `Q` starts empty ⇒ `order=[]` and `len(order) < V`**cycle reported**.

0 commit comments

Comments
 (0)