@@ -109,6 +109,32 @@ pub fn graph_dijkstra_shortest_paths(
109109 } )
110110}
111111
112+ /// Check if a graph has a path between source and target nodes
113+ ///
114+ /// :param PyGraph graph:
115+ /// :param int source: The node index to find paths from
116+ /// :param int target: The index of the target node
117+ ///
118+ /// :return: True if a path exists, False if not.
119+ /// :rtype: bool
120+ /// :raises ValueError: when an edge weight with NaN or negative value
121+ /// is provided.
122+ #[ pyfunction]
123+ #[ pyo3(
124+ signature=( graph, source, target) ,
125+ text_signature = "(graph, source, target)"
126+ ) ]
127+ pub fn graph_has_path (
128+ py : Python ,
129+ graph : & graph:: PyGraph ,
130+ source : usize ,
131+ target : usize ,
132+ ) -> PyResult < bool > {
133+ let path_mapping = graph_dijkstra_shortest_paths ( py, graph, source, Some ( target) , None , 1.0 ) ?;
134+
135+ Ok ( !path_mapping. paths . is_empty ( ) )
136+ }
137+
112138/// Find the shortest path from a node
113139///
114140/// This function will generate the shortest path from a source node using
@@ -184,6 +210,36 @@ pub fn digraph_dijkstra_shortest_paths(
184210 } )
185211}
186212
213+ /// Check if a digraph has a path between source and target nodes
214+ ///
215+ /// :param PyDiGraph graph:
216+ /// :param int source: The node index to find paths from
217+ /// :param int target: The index of the target node
218+ /// :param bool as_undirected: If set to true the graph will be treated as
219+ /// undirected for finding a path
220+ ///
221+ /// :return: True if a path exists, False if not.
222+ /// :rtype: bool
223+ /// :raises ValueError: when an edge weight with NaN or negative value
224+ /// is provided.
225+ #[ pyfunction]
226+ #[ pyo3(
227+ signature=( graph, source, target, as_undirected=false ) ,
228+ text_signature = "(graph, source, target, /, as_undirected=false)"
229+ ) ]
230+ pub fn digraph_has_path (
231+ py : Python ,
232+ graph : & digraph:: PyDiGraph ,
233+ source : usize ,
234+ target : usize ,
235+ as_undirected : bool ,
236+ ) -> PyResult < bool > {
237+ let path_mapping =
238+ digraph_dijkstra_shortest_paths ( py, graph, source, Some ( target) , None , 1.0 , as_undirected) ?;
239+
240+ Ok ( !path_mapping. paths . is_empty ( ) )
241+ }
242+
187243/// Compute the lengths of the shortest paths for a PyGraph object using
188244/// Dijkstra's algorithm
189245///
0 commit comments