4
4
"""
5
5
from collections import deque
6
6
from concurrent .futures import ThreadPoolExecutor
7
- from pydatastructs .utils import GraphEdge
8
- from pydatastructs . utils . misc_util import _comp
7
+ from pydatastructs .utils . misc_util import (
8
+ _comp , raise_if_backend_is_not_python , Backend )
9
9
from pydatastructs .miscellaneous_data_structures import (
10
10
DisjointSetForest , PriorityQueue )
11
11
from pydatastructs .graphs .graph import Graph
@@ -51,6 +51,10 @@ def breadth_first_search(
51
51
current node and the node next to current node.
52
52
The rest of the arguments are optional and you can
53
53
provide your own stuff there.
54
+ backend: pydatastructs.Backend
55
+ The backend to be used.
56
+ Optional, by default, the best available
57
+ backend is used.
54
58
55
59
Note
56
60
====
@@ -75,6 +79,8 @@ def breadth_first_search(
75
79
>>> G.add_edge(V2.name, V3.name)
76
80
>>> breadth_first_search(G, V1.name, f, V3.name)
77
81
"""
82
+ raise_if_backend_is_not_python (
83
+ breadth_first_search , kwargs .get ('backend' , Backend .PYTHON ))
78
84
import pydatastructs .graphs .algorithms as algorithms
79
85
func = "_breadth_first_search_" + graph ._impl
80
86
if not hasattr (algorithms , func ):
@@ -133,6 +139,10 @@ def breadth_first_search_parallel(
133
139
current node and the node next to current node.
134
140
The rest of the arguments are optional and you can
135
141
provide your own stuff there.
142
+ backend: pydatastructs.Backend
143
+ The backend to be used.
144
+ Optional, by default, the best available
145
+ backend is used.
136
146
137
147
Note
138
148
====
@@ -157,6 +167,8 @@ def breadth_first_search_parallel(
157
167
>>> G.add_edge(V2.name, V3.name)
158
168
>>> breadth_first_search_parallel(G, V1.name, 3, f, V3.name)
159
169
"""
170
+ raise_if_backend_is_not_python (
171
+ breadth_first_search_parallel , kwargs .get ('backend' , Backend .PYTHON ))
160
172
import pydatastructs .graphs .algorithms as algorithms
161
173
func = "_breadth_first_search_parallel_" + graph ._impl
162
174
if not hasattr (algorithms , func ):
@@ -256,7 +268,7 @@ def _minimum_spanning_tree_prim_adjacency_list(graph):
256
268
e [w ] = vw
257
269
return mst
258
270
259
- def minimum_spanning_tree (graph , algorithm ):
271
+ def minimum_spanning_tree (graph , algorithm , ** kwargs ):
260
272
"""
261
273
Computes a minimum spanning tree for the given
262
274
graph and algorithm.
@@ -276,6 +288,10 @@ def minimum_spanning_tree(graph, algorithm):
276
288
'kruskal' -> Kruskal's algorithm as given in [1].
277
289
278
290
'prim' -> Prim's algorithm as given in [2].
291
+ backend: pydatastructs.Backend
292
+ The backend to be used.
293
+ Optional, by default, the best available
294
+ backend is used.
279
295
280
296
Returns
281
297
=======
@@ -312,6 +328,8 @@ def minimum_spanning_tree(graph, algorithm):
312
328
should be used only for such graphs. Using with other
313
329
types of graphs may lead to unwanted results.
314
330
"""
331
+ raise_if_backend_is_not_python (
332
+ minimum_spanning_tree , kwargs .get ('backend' , Backend .PYTHON ))
315
333
import pydatastructs .graphs .algorithms as algorithms
316
334
func = "_minimum_spanning_tree_" + algorithm + "_" + graph ._impl
317
335
if not hasattr (algorithms , func ):
@@ -390,7 +408,7 @@ def _minimum_spanning_tree_parallel_prim_adjacency_list(graph, num_threads):
390
408
391
409
return mst
392
410
393
- def minimum_spanning_tree_parallel (graph , algorithm , num_threads ):
411
+ def minimum_spanning_tree_parallel (graph , algorithm , num_threads , ** kwargs ):
394
412
"""
395
413
Computes a minimum spanning tree for the given
396
414
graph and algorithm using the given number of threads.
@@ -412,6 +430,10 @@ def minimum_spanning_tree_parallel(graph, algorithm, num_threads):
412
430
'prim' -> Prim's algorithm as given in [2].
413
431
num_threads: int
414
432
The number of threads to be used.
433
+ backend: pydatastructs.Backend
434
+ The backend to be used.
435
+ Optional, by default, the best available
436
+ backend is used.
415
437
416
438
Returns
417
439
=======
@@ -448,6 +470,8 @@ def minimum_spanning_tree_parallel(graph, algorithm, num_threads):
448
470
should be used only for such graphs. Using with other
449
471
types of graphs will lead to unwanted results.
450
472
"""
473
+ raise_if_backend_is_not_python (
474
+ minimum_spanning_tree_parallel , kwargs .get ('backend' , Backend .PYTHON ))
451
475
import pydatastructs .graphs .algorithms as algorithms
452
476
func = "_minimum_spanning_tree_parallel_" + algorithm + "_" + graph ._impl
453
477
if not hasattr (algorithms , func ):
@@ -505,7 +529,7 @@ def _strongly_connected_components_kosaraju_adjacency_list(graph):
505
529
_strongly_connected_components_kosaraju_adjacency_matrix = \
506
530
_strongly_connected_components_kosaraju_adjacency_list
507
531
508
- def strongly_connected_components (graph , algorithm ):
532
+ def strongly_connected_components (graph , algorithm , ** kwargs ):
509
533
"""
510
534
Computes strongly connected components for the given
511
535
graph and algorithm.
@@ -523,6 +547,10 @@ def strongly_connected_components(graph, algorithm):
523
547
supported,
524
548
525
549
'kosaraju' -> Kosaraju's algorithm as given in [1].
550
+ backend: pydatastructs.Backend
551
+ The backend to be used.
552
+ Optional, by default, the best available
553
+ backend is used.
526
554
527
555
Returns
528
556
=======
@@ -550,6 +578,8 @@ def strongly_connected_components(graph, algorithm):
550
578
.. [1] https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
551
579
552
580
"""
581
+ raise_if_backend_is_not_python (
582
+ strongly_connected_components , kwargs .get ('backend' , Backend .PYTHON ))
553
583
import pydatastructs .graphs .algorithms as algorithms
554
584
func = "_strongly_connected_components_" + algorithm + "_" + graph ._impl
555
585
if not hasattr (algorithms , func ):
@@ -583,6 +613,10 @@ def depth_first_search(
583
613
current node and the node next to current node.
584
614
The rest of the arguments are optional and you can
585
615
provide your own stuff there.
616
+ backend: pydatastructs.Backend
617
+ The backend to be used.
618
+ Optional, by default, the best available
619
+ backend is used.
586
620
587
621
Note
588
622
====
@@ -612,6 +646,8 @@ def depth_first_search(
612
646
613
647
.. [1] https://en.wikipedia.org/wiki/Depth-first_search
614
648
"""
649
+ raise_if_backend_is_not_python (
650
+ depth_first_search , kwargs .get ('backend' , Backend .PYTHON ))
615
651
import pydatastructs .graphs .algorithms as algorithms
616
652
func = "_depth_first_search_" + graph ._impl
617
653
if not hasattr (algorithms , func ):
@@ -646,7 +682,8 @@ def _depth_first_search_adjacency_list(
646
682
_depth_first_search_adjacency_matrix = _depth_first_search_adjacency_list
647
683
648
684
def shortest_paths (graph : Graph , algorithm : str ,
649
- source : str , target : str = "" ) -> tuple :
685
+ source : str , target : str = "" ,
686
+ ** kwargs ) -> tuple :
650
687
"""
651
688
Finds shortest paths in the given graph from a given source.
652
689
@@ -668,6 +705,10 @@ def shortest_paths(graph: Graph, algorithm: str,
668
705
The name of the target node.
669
706
Optional, by default, all pair shortest paths
670
707
are returned.
708
+ backend: pydatastructs.Backend
709
+ The backend to be used.
710
+ Optional, by default, the best available
711
+ backend is used.
671
712
672
713
Returns
673
714
=======
@@ -701,6 +742,8 @@ def shortest_paths(graph: Graph, algorithm: str,
701
742
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
702
743
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
703
744
"""
745
+ raise_if_backend_is_not_python (
746
+ shortest_paths , kwargs .get ('backend' , Backend .PYTHON ))
704
747
import pydatastructs .graphs .algorithms as algorithms
705
748
func = "_" + algorithm + "_" + graph ._impl
706
749
if not hasattr (algorithms , func ):
@@ -767,7 +810,8 @@ def _dijkstra_adjacency_list(graph: Graph, start: str, target: str):
767
810
768
811
_dijkstra_adjacency_matrix = _dijkstra_adjacency_list
769
812
770
- def all_pair_shortest_paths (graph : Graph , algorithm : str ) -> tuple :
813
+ def all_pair_shortest_paths (graph : Graph , algorithm : str ,
814
+ ** kwargs ) -> tuple :
771
815
"""
772
816
Finds shortest paths between all pairs of vertices in the given graph.
773
817
@@ -781,6 +825,10 @@ def all_pair_shortest_paths(graph: Graph, algorithm: str) -> tuple:
781
825
are implemented,
782
826
783
827
'floyd_warshall' -> Floyd Warshall algorithm as given in [1].
828
+ backend: pydatastructs.Backend
829
+ The backend to be used.
830
+ Optional, by default, the best available
831
+ backend is used.
784
832
785
833
Returns
786
834
=======
@@ -810,6 +858,8 @@ def all_pair_shortest_paths(graph: Graph, algorithm: str) -> tuple:
810
858
811
859
.. [1] https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
812
860
"""
861
+ raise_if_backend_is_not_python (
862
+ all_pair_shortest_paths , kwargs .get ('backend' , Backend .PYTHON ))
813
863
import pydatastructs .graphs .algorithms as algorithms
814
864
func = "_" + algorithm + "_" + graph ._impl
815
865
if not hasattr (algorithms , func ):
@@ -849,7 +899,8 @@ def _floyd_warshall_adjacency_list(graph: Graph):
849
899
850
900
_floyd_warshall_adjacency_matrix = _floyd_warshall_adjacency_list
851
901
852
- def topological_sort (graph : Graph , algorithm : str ) -> list :
902
+ def topological_sort (graph : Graph , algorithm : str ,
903
+ ** kwargs ) -> list :
853
904
"""
854
905
Performs topological sort on the given graph using given algorithm.
855
906
@@ -863,6 +914,10 @@ def topological_sort(graph: Graph, algorithm: str) -> list:
863
914
Currently, following are supported,
864
915
865
916
'kahn' -> Kahn's algorithm as given in [1].
917
+ backend: pydatastructs.Backend
918
+ The backend to be used.
919
+ Optional, by default, the best available
920
+ backend is used.
866
921
867
922
Returns
868
923
=======
@@ -886,6 +941,8 @@ def topological_sort(graph: Graph, algorithm: str) -> list:
886
941
887
942
.. [1] https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
888
943
"""
944
+ raise_if_backend_is_not_python (
945
+ topological_sort , kwargs .get ('backend' , Backend .PYTHON ))
889
946
import pydatastructs .graphs .algorithms as algorithms
890
947
func = "_" + algorithm + "_" + graph ._impl
891
948
if not hasattr (algorithms , func ):
@@ -920,7 +977,8 @@ def _kahn_adjacency_list(graph: Graph) -> list:
920
977
raise ValueError ("Graph is not acyclic." )
921
978
return L
922
979
923
- def topological_sort_parallel (graph : Graph , algorithm : str , num_threads : int ) -> list :
980
+ def topological_sort_parallel (graph : Graph , algorithm : str , num_threads : int ,
981
+ ** kwargs ) -> list :
924
982
"""
925
983
Performs topological sort on the given graph using given algorithm using
926
984
given number of threads.
@@ -937,6 +995,10 @@ def topological_sort_parallel(graph: Graph, algorithm: str, num_threads: int) ->
937
995
'kahn' -> Kahn's algorithm as given in [1].
938
996
num_threads: int
939
997
The maximum number of threads to be used.
998
+ backend: pydatastructs.Backend
999
+ The backend to be used.
1000
+ Optional, by default, the best available
1001
+ backend is used.
940
1002
941
1003
Returns
942
1004
=======
@@ -960,6 +1022,8 @@ def topological_sort_parallel(graph: Graph, algorithm: str, num_threads: int) ->
960
1022
961
1023
.. [1] https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
962
1024
"""
1025
+ raise_if_backend_is_not_python (
1026
+ topological_sort_parallel , kwargs .get ('backend' , Backend .PYTHON ))
963
1027
import pydatastructs .graphs .algorithms as algorithms
964
1028
func = "_" + algorithm + "_" + graph ._impl + '_parallel'
965
1029
if not hasattr (algorithms , func ):
0 commit comments