-
Notifications
You must be signed in to change notification settings - Fork 0
Fix doctests in SymbolGraph and SymbolDigraph #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tekawade
merged 1 commit into
claude/algs4-java-to-rust-011CV5y1Rjk75TTTXhAdfXbX
from
claude/fix-symbolgraph-doctests-01WZV6i4B2uJ8VGjD8cfuM41
Nov 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,249 @@ | ||
| # algs4-graphs | ||
|
|
||
| Graph algorithms and data structures from *Algorithms, 4th Edition* by Robert Sedgewick and Kevin Wayne. | ||
|
|
||
| [](https://www.gnu.org/licenses/gpl-3.0) | ||
|
|
||
| ## Features | ||
|
|
||
| This crate provides comprehensive graph algorithms and data structures: | ||
|
|
||
| ### Graph Representations | ||
| - **Graph** - Undirected graph with adjacency lists | ||
| - **Digraph** - Directed graph with adjacency lists | ||
| - **EdgeWeightedGraph** - Edge-weighted undirected graph | ||
| - **EdgeWeightedDigraph** - Edge-weighted directed graph | ||
| - **SymbolGraph** - Graph with string vertex names | ||
| - **SymbolDigraph** - Directed graph with string vertex names | ||
|
|
||
| ### Graph Generators | ||
| - **GraphGenerator** - Random graph generation (simple, complete, bipartite, trees, regular graphs, etc.) | ||
| - **DigraphGenerator** - Random digraph generation (DAGs, tournaments, strong digraphs, etc.) | ||
|
|
||
| ### Graph Traversal | ||
| - **DepthFirstPaths** - Find paths using depth-first search | ||
| - **BreadthFirstPaths** - Find shortest paths using breadth-first search | ||
| - **DepthFirstDirectedPaths** - DFS for directed graphs | ||
| - **BreadthFirstDirectedPaths** - BFS for directed graphs | ||
|
|
||
| ### Connectivity | ||
| - **CC** - Connected components in undirected graphs | ||
| - **KosarajuSharirSCC** - Strongly connected components (Kosaraju-Sharir algorithm) | ||
| - **TarjanSCC** - Strongly connected components (Tarjan algorithm) | ||
| - **GabowSCC** - Strongly connected components (Gabow algorithm) | ||
|
|
||
| ### Shortest Paths | ||
| - **DijkstraSP** - Dijkstra's algorithm for shortest paths | ||
| - **DijkstraUndirectedSP** - Dijkstra for undirected graphs | ||
| - **DijkstraAllPairsSP** - All-pairs shortest paths | ||
| - **BellmanFordSP** - Bellman-Ford algorithm (handles negative weights) | ||
| - **AcyclicSP** - Shortest paths in DAGs | ||
| - **AcyclicLP** - Longest paths in DAGs | ||
| - **FloydWarshall** - All-pairs shortest paths with negative weights | ||
|
|
||
| ### Minimum Spanning Trees | ||
| - **PrimMST** - Prim's algorithm (eager version) | ||
| - **LazyPrimMST** - Prim's algorithm (lazy version) | ||
| - **KruskalMST** - Kruskal's algorithm | ||
| - **BoruvkaMST** - Borůvka's algorithm | ||
|
|
||
| ### Topological Sort & DAGs | ||
| - **Topological** - Topological sort for DAGs | ||
| - **DepthFirstOrder** - DFS-based vertex orderings | ||
|
|
||
| ### Cycle Detection | ||
| - **Cycle** - Find cycles in undirected graphs | ||
| - **DirectedCycle** - Find cycles in directed graphs | ||
| - **DirectedCycleX** - Cycle detection with edge tracking | ||
| - **EdgeWeightedDirectedCycle** - Cycle detection in edge-weighted digraphs | ||
|
|
||
| ### Special Graph Problems | ||
| - **Bipartite** - Bipartite graph detection | ||
| - **BipartiteX** - Enhanced bipartite detection | ||
| - **EulerianCycle** - Find Eulerian cycles | ||
| - **EulerianPath** - Find Eulerian paths | ||
| - **DirectedEulerianCycle** - Eulerian cycles in digraphs | ||
| - **DirectedEulerianPath** - Eulerian paths in digraphs | ||
|
|
||
| ### Maximum Flow | ||
| - **FordFulkerson** - Ford-Fulkerson algorithm for maximum flow | ||
| - **FlowNetwork** - Flow network representation | ||
| - **FlowEdge** - Edge with flow and capacity | ||
|
|
||
| ### Other | ||
| - **TransitiveClosure** - Compute transitive closure | ||
|
|
||
| ## Installation | ||
|
|
||
| Add this to your `Cargo.toml`: | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| algs4-graphs = "0.1" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Graph Operations | ||
|
|
||
| ```rust | ||
| use algs4_graphs::Graph; | ||
|
|
||
| // Create an undirected graph with 6 vertices | ||
| let mut graph = Graph::new(6); | ||
| graph.add_edge(0, 1); | ||
| graph.add_edge(0, 2); | ||
| graph.add_edge(1, 3); | ||
| graph.add_edge(2, 3); | ||
| graph.add_edge(4, 5); | ||
|
|
||
| println!("Vertices: {}", graph.v()); | ||
| println!("Edges: {}", graph.e()); | ||
| ``` | ||
|
|
||
| ### Finding Paths | ||
|
|
||
| ```rust | ||
| use algs4_graphs::{Graph, DepthFirstPaths, BreadthFirstPaths}; | ||
|
|
||
| let mut graph = Graph::new(6); | ||
| graph.add_edge(0, 1); | ||
| graph.add_edge(0, 2); | ||
| graph.add_edge(1, 3); | ||
| graph.add_edge(2, 3); | ||
|
|
||
| // DFS paths from vertex 0 | ||
| let dfs = DepthFirstPaths::new(&graph, 0); | ||
| if let Some(path) = dfs.path_to(3) { | ||
| println!("Path: {:?}", path); | ||
| } | ||
|
|
||
| // BFS shortest paths from vertex 0 | ||
| let bfs = BreadthFirstPaths::new(&graph, 0); | ||
| if let Some(dist) = bfs.dist_to(3) { | ||
| println!("Distance: {}", dist); | ||
| } | ||
| ``` | ||
|
|
||
| ### Connected Components | ||
|
|
||
| ```rust | ||
| use algs4_graphs::{Graph, CC}; | ||
|
|
||
| let mut graph = Graph::new(6); | ||
| graph.add_edge(0, 1); | ||
| graph.add_edge(0, 2); | ||
| graph.add_edge(4, 5); | ||
|
|
||
| let cc = CC::new(&graph); | ||
| println!("Components: {}", cc.count()); | ||
| println!("0 and 3 connected? {}", cc.connected(0, 3)); | ||
| ``` | ||
|
|
||
| ### Shortest Paths with Dijkstra | ||
|
|
||
| ```rust | ||
| use algs4_graphs::{EdgeWeightedDigraph, DirectedEdge, DijkstraSP}; | ||
|
|
||
| let mut graph = EdgeWeightedDigraph::new(5); | ||
| graph.add_edge(DirectedEdge::new(0, 1, 5.0)); | ||
| graph.add_edge(DirectedEdge::new(0, 2, 3.0)); | ||
| graph.add_edge(DirectedEdge::new(1, 3, 2.0)); | ||
| graph.add_edge(DirectedEdge::new(2, 3, 7.0)); | ||
|
|
||
| let sp = DijkstraSP::new(&graph, 0); | ||
| if let Some(dist) = sp.dist_to(3) { | ||
| println!("Shortest distance: {}", dist); | ||
| } | ||
| ``` | ||
|
|
||
| ### Minimum Spanning Tree | ||
|
|
||
| ```rust | ||
| use algs4_graphs::{EdgeWeightedGraph, Edge, PrimMST}; | ||
|
|
||
| let mut graph = EdgeWeightedGraph::new(5); | ||
| graph.add_edge(Edge::new(0, 1, 1.0)); | ||
| graph.add_edge(Edge::new(0, 2, 2.0)); | ||
| graph.add_edge(Edge::new(1, 2, 3.0)); | ||
| graph.add_edge(Edge::new(1, 3, 4.0)); | ||
| graph.add_edge(Edge::new(2, 3, 5.0)); | ||
|
|
||
| let mst = PrimMST::new(&graph); | ||
| println!("MST weight: {}", mst.weight()); | ||
| ``` | ||
|
|
||
| ### Generating Random Graphs | ||
|
|
||
| ```rust | ||
| use algs4_graphs::{GraphGenerator, DigraphGenerator}; | ||
|
|
||
| // Generate a random simple graph with 10 vertices and 15 edges | ||
| let graph = GraphGenerator::simple(10, 15); | ||
|
|
||
| // Generate a random tree with 10 vertices | ||
| let tree = GraphGenerator::tree(10); | ||
|
|
||
| // Generate a random DAG with 10 vertices and 20 edges | ||
| let dag = DigraphGenerator::dag(10, 20); | ||
|
|
||
| // Generate a complete bipartite graph | ||
| let bipartite = GraphGenerator::complete_bipartite(3, 4); | ||
| ``` | ||
|
|
||
| ### Symbol Graphs | ||
|
|
||
| ```rust | ||
| use algs4_graphs::SymbolGraph; | ||
|
|
||
| let mut sg = SymbolGraph::new(); | ||
| sg.add_vertex("Alice".to_string()); | ||
| sg.add_vertex("Bob".to_string()); | ||
| sg.add_vertex("Charlie".to_string()); | ||
| sg.add_edge("Alice", "Bob"); | ||
| sg.add_edge("Bob", "Charlie"); | ||
|
|
||
| assert_eq!(sg.degree("Bob"), 2); | ||
| ``` | ||
|
|
||
| ## Performance | ||
|
|
||
| All algorithms are implemented with performance in mind, using efficient data structures and following the textbook's proven approaches. Benchmarks are included in the `benches/` directory. | ||
|
|
||
| ## Documentation | ||
|
|
||
| For detailed API documentation, run: | ||
|
|
||
| ```bash | ||
| cargo doc --open | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| Run tests with: | ||
|
|
||
| ```bash | ||
| cargo test | ||
| ``` | ||
|
|
||
| Run benchmarks with: | ||
|
|
||
| ```bash | ||
| cargo bench | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details. | ||
|
|
||
| This license is inherited from the original [kevin-wayne/algs4](https://github.com/kevin-wayne/algs4) repository. | ||
|
|
||
| ## Credits | ||
|
|
||
| - **Original Authors:** Robert Sedgewick and Kevin Wayne | ||
| - **Textbook:** *Algorithms, 4th Edition* by Sedgewick & Wayne | ||
| - **Original Java Implementation:** [kevin-wayne/algs4](https://github.com/kevin-wayne/algs4) | ||
|
|
||
| ## Contributing | ||
|
|
||
| Contributions are welcome! Please ensure all tests pass and documentation is updated. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The version specification
"0.1"should be more specific to follow semantic versioning best practices. Consider using"0.1.0"for clarity and consistency with the dependency versions specified in the updated Cargo.toml.