Skip to content

Commit 6f9fddf

Browse files
committed
Add BGL book narrative and CLRS references to documentation
BGL Book Examples (doc-src/sphinx/examples/bglbook/): - Add detailed narrative from BGL book to all 11 example RST files - Add CLRS 4th Edition (2022) chapter references to each example - Include proper attribution to both BGL book and CLRS authors Algorithm Headers with CLRS references: - bfs.hpp: Chapter 20.2 (Breadth-First Search) - dijkstra.hpp: Chapter 22.3 (Dijkstras Algorithm) - kruskal.hpp: Chapter 21.2 (MST Algorithms) - prim.hpp: Chapter 21.2 (MST Algorithms) - connected_components.hpp: Chapters 20.3, 20.5 - max_flow.hpp: Chapter 24 (Maximum Flow) - delta_stepping.hpp: Chapter 22 (Single-Source Shortest Paths) Adaptor and Utility Headers: - bfs_range.hpp, new_dfs_range.hpp: Chapters 20.2, 20.3 - disjoint_set.hpp: Chapter 19 (Disjoint Sets)
1 parent 6d5316e commit 6f9fddf

21 files changed

+910
-355
lines changed
Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
..
2-
AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY
3-
4-
This file was generated by scripts/generate_example_docs.py
5-
from examples/bgl-book/ch3_toposort.cpp
6-
7-
Source hash: 8a661661c203
8-
Generated: 2026-01-01 07:17:17
9-
10-
To regenerate, run: python scripts/generate_example_docs.py
11-
121
.. SPDX-FileCopyrightText: 2022 Battelle Memorial Institute
132
.. SPDX-FileCopyrightText: 2022 University of Washington
143
..
@@ -18,58 +7,111 @@
187
File Dependencies - Topological Sort (BGL Book Chapter 3)
198
=========================================================
209

10+
*Based on "The Boost Graph Library" by Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine*
11+
2112
Overview
2213
--------
2314

24-
This example demonstrates topological sorting using DFS on a makefile
25-
dependency graph.
15+
A common use of the graph abstraction is to represent dependencies. One common type of
16+
dependency that we programmers deal with on a routine basis is that of compilation dependencies
17+
between files in programs that we write. Information about these dependencies is
18+
used by programs such as ``make``, or by IDEs such as Visual C++, to determine which files
19+
must be recompiled to generate a new version of a program (or, in general, of some target)
20+
after a change has been made to a source file.
21+
22+
Consider a graph that has a vertex for each source file, object file, and library that
23+
is used in a program. An edge in the graph shows that a target depends on another
24+
target in some way (such as a dependency due to inclusion of a header file in a source file, or
25+
due to an object file being compiled from a source file).
26+
27+
Answers to many of the questions that arise in creating a build system such as ``make`` can
28+
be formulated in terms of the dependency graph:
29+
30+
* If all of the targets need to be made, in what order should that be accomplished?
31+
* Are there any cycles in the dependencies? A dependency cycle is an error, and an
32+
appropriate message should be emitted.
33+
* How many steps are required to make all of the targets? How many steps are required
34+
to make all of the targets if independent targets are made simultaneously in parallel?
2635

2736
Algorithm Description
2837
--------------------
2938

30-
See the source code for algorithm details.
31-
32-
NWGraph Implementation
33-
---------------------
39+
The first question—specifying an order in which to build all of the targets—requires ensuring
40+
that before building a given target, all the targets that it depends on are already built.
41+
This is the problem of computing a **topological ordering**.
3442

35-
.. literalinclude:: ../../../../examples/bgl-book/ch3_toposort.cpp
36-
:language: cpp
37-
:linenos:
38-
:lines: 1-35
39-
:caption: File header and includes
43+
A topological ordering can be computed using a depth-first search (DFS). A DFS visits all of
44+
the vertices in a graph by starting at any vertex and then choosing an edge to follow. At the
45+
next vertex another edge is chosen to follow. This process continues until a dead end (a vertex
46+
with no out-edges that lead to a vertex not already discovered) is reached. The algorithm then
47+
backtracks to the last discovered vertex that is adjacent to a vertex that is not yet discovered.
4048

49+
In the context of topological sorting, we record each vertex as it is **finished** (when the DFS
50+
backtracks from that vertex). The reverse of this finish order is the topological order.
4151

42-
The main function demonstrates the algorithm:
52+
NWGraph Implementation
53+
---------------------
4354

55+
NWGraph provides modern C++20 implementations of DFS and topological sorting that differ
56+
from the BGL approach by using range adaptors instead of visitors.
4457

4558
.. literalinclude:: ../../../../examples/bgl-book/ch3_toposort.cpp
4659
:language: cpp
4760
:linenos:
48-
:lines: 31-58
49-
:caption: Main function
50-
61+
:caption: Complete source code
5162

5263
Running the Example
5364
------------------
5465

66+
Build and run the example:
67+
5568
.. code-block:: bash
5669
5770
cd build/examples/bgl-book
5871
./ch3_toposort
5972
73+
The program reads a makefile dependency graph from a file and outputs the compilation order
74+
(files listed in order such that each file appears before any files that depend on it).
75+
6076
Sample Output
6177
------------
6278

6379
.. code-block:: text
6480
65-
(Run the example to see output)
81+
Compilation order:
82+
dax.h
83+
yow.h
84+
zow.h
85+
boz.h
86+
zig.cpp
87+
zig.o
88+
zag.cpp
89+
zag.o
90+
libzigzag.a
91+
foo.cpp
92+
foo.o
93+
bar.cpp
94+
bar.o
95+
libfoobar.a
96+
killerapp
6697
6798
Key NWGraph Features Demonstrated
6899
--------------------------------
69100

70-
- See source code for NWGraph features used
101+
- **Edge list construction**: Building graphs from edge pairs
102+
- **DFS range adaptor**: Using ``dfs_range`` to traverse the graph
103+
- **Topological sorting**: Computing a valid build order for dependencies
104+
- **Modern C++ idioms**: Range-based for loops and structured bindings
105+
106+
References
107+
----------
108+
109+
- Cormen, Leiserson, Rivest, and Stein. *Introduction to Algorithms*, 4th Edition (2022),
110+
**Chapter 20.4: Topological Sort** and **Chapter 20.3: Depth-First Search**
111+
- Siek, Lee, and Lumsdaine. *The Boost Graph Library* (2002), Chapter 3
71112

72113
See Also
73114
--------
74115

116+
- :doc:`ch4_loop_detection` - Detecting cycles in dependency graphs
75117
- :doc:`index` - BGL Book examples overview
Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
..
2-
AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY
3-
4-
This file was generated by scripts/generate_example_docs.py
5-
from examples/bgl-book/ch4_kevin_bacon.cpp
6-
7-
Source hash: cf241cb6a9fa
8-
Generated: 2026-01-01 07:17:17
9-
10-
To regenerate, run: python scripts/generate_example_docs.py
11-
121
.. SPDX-FileCopyrightText: 2022 Battelle Memorial Institute
132
.. SPDX-FileCopyrightText: 2022 University of Washington
143
..
@@ -18,58 +7,110 @@
187
Six Degrees of Kevin Bacon (BGL Book Chapter 4.1)
198
=================================================
209

10+
*Based on "The Boost Graph Library" by Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine*
11+
2112
Overview
2213
--------
2314

24-
This example demonstrates BFS to compute the "Bacon number" - the number
25-
of connections from any actor to Kevin Bacon through co-starring in movies.
15+
An amusing application of breadth-first search comes up in the popular game "Six Degrees
16+
of Kevin Bacon." The idea of the game is to connect an actor to Kevin Bacon through a trail
17+
of actors who appeared together in movies, and do so in less than six steps.
18+
19+
For example, Theodore Hesburgh (President Emeritus of the University of Notre Dame) was
20+
in the movie *Rudy* with the actor Gerry Becker, who was in the movie *Sleepers* with
21+
Kevin Bacon. Why Kevin Bacon? For some reason, the three students who invented the game—
22+
Mike Ginelli, Craig Fass, and Brian Turtle—decided that Kevin Bacon was the center of the
23+
entertainment world.
24+
25+
Mathematicians play a similar game; they keep track of their *Erdős number*, which is
26+
the number of co-authored publications that separate them from the famous Paul Erdős.
27+
28+
The Graph Model
29+
---------------
30+
31+
The "Six Degrees of Kevin Bacon" game is really a graph problem. The graph representing
32+
the problem can be modeled by:
33+
34+
* Assigning a **vertex** for each actor
35+
* Creating an **edge** between two vertices if the corresponding actors have appeared
36+
together in a movie
37+
38+
Since the relationship between actors appearing together in a movie is symmetric, edges
39+
between actors can be undirected, resulting in an **undirected graph**.
2640

2741
Algorithm Description
2842
--------------------
2943

30-
See the source code for algorithm details.
44+
The problem of finding a trail of actors to Kevin Bacon becomes a traditional graph
45+
problem—that of finding a path between two vertices. Since we wish to find a path that
46+
is shorter than six steps, ideally we would like to find the **shortest path** between vertices.
3147

32-
NWGraph Implementation
33-
---------------------
48+
Breadth-first search (BFS) can be used to find shortest paths in unweighted graphs.
49+
Similar to the Erdős number, we use the term **Bacon number** to mean the shortest path
50+
length from a given actor to Kevin Bacon.
3451

35-
.. literalinclude:: ../../../../examples/bgl-book/ch4_kevin_bacon.cpp
36-
:language: cpp
37-
:linenos:
38-
:lines: 1-36
39-
:caption: File header and includes
52+
BFS works by exploring vertices in order of their distance from a source vertex:
53+
54+
1. Start at the source vertex (Kevin Bacon)
55+
2. Visit all vertices one edge away
56+
3. Then visit all vertices two edges away
57+
4. Continue until all reachable vertices are visited
4058

59+
The distance at which each vertex is discovered is its shortest distance from the source.
4160

42-
The main function demonstrates the algorithm:
61+
NWGraph Implementation
62+
---------------------
4363

64+
NWGraph provides a ``bfs_range`` adaptor that yields vertices in BFS order, making it
65+
easy to compute shortest path distances.
4466

4567
.. literalinclude:: ../../../../examples/bgl-book/ch4_kevin_bacon.cpp
4668
:language: cpp
4769
:linenos:
48-
:lines: 67-99
49-
:caption: Main function
50-
70+
:caption: Complete source code
5171

5272
Running the Example
5373
------------------
5474

75+
Build and run the example:
76+
5577
.. code-block:: bash
5678
5779
cd build/examples/bgl-book
58-
./ch4_kevin_bacon
80+
./ch4_kevin_bacon kevin-bacon.dat
81+
82+
The program reads an actor-movie database and computes the Bacon number for each actor.
5983

6084
Sample Output
6185
------------
6286

6387
.. code-block:: text
6488
65-
(Run the example to see output)
89+
Kevin Bacon has a Bacon number of 0
90+
Patrick Stewart has a Bacon number of 1
91+
Steve Martin has a Bacon number of 1
92+
Glenn Close has a Bacon number of 1
93+
Tom Hanks has a Bacon number of 1
94+
...
6695
6796
Key NWGraph Features Demonstrated
6897
--------------------------------
6998

70-
- See source code for NWGraph features used
99+
- **Undirected graphs**: Using symmetric edges for actor relationships
100+
- **BFS traversal**: Using ``bfs_range`` to compute shortest paths
101+
- **Property maps**: Associating actor names with vertices
102+
- **Unweighted shortest paths**: Computing Bacon numbers via BFS distances
103+
104+
References
105+
----------
106+
107+
- Cormen, Leiserson, Rivest, and Stein. *Introduction to Algorithms*, 4th Edition (2022),
108+
**Chapter 20.2: Breadth-First Search**
109+
- Siek, Lee, and Lumsdaine. *The Boost Graph Library* (2002), Chapter 4.1
71110

72111
See Also
73112
--------
74113

114+
- :doc:`ch4_loop_detection` - Using DFS to detect cycles
115+
- :doc:`ch5_dijkstra` - Weighted shortest paths
75116
- :doc:`index` - BGL Book examples overview

0 commit comments

Comments
 (0)