@@ -12,9 +12,15 @@ Quick links:
1212- [ CLI Reference] ( cli.md )
1313- [ DSL Reference] ( dsl.md )
1414
15- Generated from source code on: December 06, 2025 at 12:38 UTC
15+ Generated from source code on: December 06, 2025 at 18:54 UTC
1616
17- Modules auto-discovered: 42
17+ Modules auto-discovered: 44
18+
19+ ---
20+
21+ ## ngraph._ version
22+
23+ ngraph version metadata.
1824
1925---
2026
@@ -2600,6 +2606,152 @@ Attributes:
26002606
26012607---
26022608
2609+ ## ngraph.lib.nx
2610+
2611+ NetworkX graph conversion utilities.
2612+
2613+ This module provides functions to convert between NetworkX graphs and the
2614+ internal graph representation used by ngraph for high-performance algorithms.
2615+
2616+ Example:
2617+ >>> import networkx as nx
2618+ >>> from ngraph.lib.nx import from_networkx, to_networkx
2619+ >>>
2620+ >>> # Create a NetworkX graph
2621+ >>> G = nx.DiGraph()
2622+ >>> G.add_edge("A", "B", capacity=100.0, cost=10)
2623+ >>> G.add_edge("B", "C", capacity=50.0, cost=5)
2624+ >>>
2625+ >>> # Convert to ngraph format for analysis
2626+ >>> graph, node_map, edge_map = from_networkx(G)
2627+ >>>
2628+ >>> # Use with ngraph algorithms...
2629+ >>>
2630+ >>> # Convert back to NetworkX
2631+ >>> G_out = to_networkx(graph, node_map)
2632+
2633+ ### EdgeMap
2634+
2635+ Bidirectional mapping between internal edge IDs and original edge references.
2636+
2637+ When converting a NetworkX graph, each edge is assigned an internal integer ID
2638+ (ext_edge_id). This class preserves the mapping for interpreting algorithm
2639+ results and updating the original graph.
2640+
2641+ Attributes:
2642+ to_ref: Maps internal edge ID to original (source, target, key) tuple
2643+ from_ref: Maps original (source, target, key) to list of internal edge IDs
2644+ (list because bidirectional=True creates two IDs per edge)
2645+
2646+ Example:
2647+ >>> graph, node_map, edge_map = from_networkx(G)
2648+ >>> # After running algorithms, map flow results back to original edges
2649+ >>> for ext_id, flow in enumerate(flow_state.edge_flow_view()):
2650+ ... if flow > 0:
2651+ ... u, v, key = edge_map.to_ref[ ext_id]
2652+ ... G.edges[ u, v, key] [ "flow" ] = flow
2653+
2654+ ** Attributes:**
2655+
2656+ - ` to_ref ` (Dict[ int, EdgeRef] ) = {}
2657+ - ` from_ref ` (Dict[ EdgeRef, List[ int]] ) = {}
2658+
2659+ ### NodeMap
2660+
2661+ Bidirectional mapping between node names and integer indices.
2662+
2663+ When converting a NetworkX graph to the internal representation, node names
2664+ (which can be any hashable type) are mapped to contiguous integer indices
2665+ starting from 0. This class preserves the mapping for result interpretation
2666+ and back-conversion.
2667+
2668+ Attributes:
2669+ to_index: Maps original node names to integer indices
2670+ to_name: Maps integer indices back to original node names
2671+
2672+ Example:
2673+ >>> node_map = NodeMap.from_names([ "A", "B", "C"] )
2674+ >>> node_map.to_index[ "A"]
2675+ 0
2676+ >>> node_map.to_name[ 1]
2677+ 'B'
2678+
2679+ ** Attributes:**
2680+
2681+ - ` to_index ` (Dict[ Hashable, int] ) = {}
2682+ - ` to_name ` (Dict[ int, Hashable] ) = {}
2683+
2684+ ** Methods:**
2685+
2686+ - ` from_names(names: 'List[Hashable]') -> "'NodeMap'" ` - Create a NodeMap from a list of node names.
2687+
2688+ ### from_networkx(G: 'NxGraph', * , capacity_attr: 'str' = 'capacity', cost_attr: 'str' = 'cost', default_capacity: 'float' = 1.0, default_cost: 'int' = 1, bidirectional: 'bool' = False) -> 'Tuple[ netgraph_core.StrictMultiDiGraph, NodeMap, EdgeMap] '
2689+
2690+ Convert a NetworkX graph to ngraph's internal graph format.
2691+
2692+ Converts any NetworkX graph (DiGraph, MultiDiGraph, Graph, MultiGraph) to
2693+ netgraph_core.StrictMultiDiGraph. Node names are mapped to integer indices;
2694+ the returned NodeMap and EdgeMap preserve mappings for result interpretation.
2695+
2696+ Args:
2697+ G: NetworkX graph (DiGraph, MultiDiGraph, Graph, or MultiGraph)
2698+ capacity_attr: Edge attribute name for capacity (default: "capacity")
2699+ cost_attr: Edge attribute name for cost (default: "cost")
2700+ default_capacity: Capacity value when attribute is missing (default: 1.0)
2701+ default_cost: Cost value when attribute is missing (default: 1)
2702+ bidirectional: If True, add reverse edge for each edge. Useful for
2703+ undirected connectivity analysis. (default: False)
2704+
2705+ Returns:
2706+ Tuple of (graph, node_map, edge_map) where:
2707+
2708+ - graph: netgraph_core.StrictMultiDiGraph ready for algorithms
2709+ - node_map: NodeMap for converting node indices back to names
2710+ - edge_map: EdgeMap for converting edge IDs back to (u, v, key) refs
2711+
2712+ Raises:
2713+ TypeError: If G is not a NetworkX graph
2714+ ValueError: If graph has no nodes
2715+
2716+ Example:
2717+ >>> import networkx as nx
2718+ >>> G = nx.DiGraph()
2719+ >>> G.add_edge("src", "dst", capacity=100.0, cost=10)
2720+ >>> graph, node_map, edge_map = from_networkx(G)
2721+ >>> graph.num_nodes()
2722+ 2
2723+ >>> node_map.to_index[ "src"]
2724+ 0
2725+ >>> edge_map.to_ref[ 0] # First edge
2726+ ('dst', 'src', 0) # sorted node order: dst < src
2727+
2728+ ### to_networkx(graph: 'netgraph_core.StrictMultiDiGraph', node_map: 'Optional[ NodeMap] ' = None, * , capacity_attr: 'str' = 'capacity', cost_attr: 'str' = 'cost') -> "'nx.MultiDiGraph'"
2729+
2730+ Convert ngraph's internal graph format back to NetworkX MultiDiGraph.
2731+
2732+ Reconstructs a NetworkX graph from the internal representation. If a
2733+ NodeMap is provided, original node names are restored; otherwise, nodes
2734+ are labeled with integer indices.
2735+
2736+ Args:
2737+ graph: netgraph_core.StrictMultiDiGraph to convert
2738+ node_map: Optional NodeMap to restore original node names.
2739+ If None, nodes are labeled 0, 1, 2, ...
2740+ capacity_attr: Edge attribute name for capacity (default: "capacity")
2741+ cost_attr: Edge attribute name for cost (default: "cost")
2742+
2743+ Returns:
2744+ nx.MultiDiGraph with edges and attributes from the internal graph
2745+
2746+ Example:
2747+ >>> graph, node_map, edge_map = from_networkx(G)
2748+ >>> # ... run algorithms ...
2749+ >>> G_out = to_networkx(graph, node_map)
2750+ >>> list(G_out.nodes())
2751+ [ 'A', 'B', 'C']
2752+
2753+ ---
2754+
26032755
26042756## Error Handling
26052757
0 commit comments