@@ -531,6 +531,52 @@ def _strongly_connected_components_kosaraju_adjacency_list(graph):
531
531
_strongly_connected_components_kosaraju_adjacency_matrix = \
532
532
_strongly_connected_components_kosaraju_adjacency_list
533
533
534
+ def _tarjan_dfs (u , graph , index , stack , indices , low_links , on_stacks , components ):
535
+ indices [u ] = index [0 ]
536
+ low_links [u ] = index [0 ]
537
+ index [0 ] += 1
538
+ stack .append (u )
539
+ on_stacks [u ] = True
540
+
541
+ for node in graph .neighbors (u ):
542
+ v = node .name
543
+ if indices [v ] == - 1 :
544
+ _tarjan_dfs (v , graph , index , stack , indices , low_links , on_stacks , components )
545
+ low_links [u ] = min (low_links [u ], low_links [v ])
546
+ elif on_stacks [v ]:
547
+ low_links [u ] = min (low_links [u ], low_links [v ])
548
+
549
+ if low_links [u ] == indices [u ]:
550
+ component = set ()
551
+ while stack :
552
+ w = stack .pop ()
553
+ on_stacks [w ] = False
554
+ component .add (w )
555
+ if w == u :
556
+ break
557
+ components .append (component )
558
+
559
+ def _strongly_connected_components_tarjan_adjacency_list (graph ):
560
+ index = [0 ] # mutable object
561
+ stack = Stack ([])
562
+ indices , low_links , on_stacks = {}, {}, {}
563
+
564
+ for u in graph .vertices :
565
+ indices [u ] = - 1
566
+ low_links [u ] = - 1
567
+ on_stacks [u ] = False
568
+
569
+ components = []
570
+
571
+ for u in graph .vertices :
572
+ if indices [u ] == - 1 :
573
+ _tarjan_dfs (u , graph , index , stack , indices , low_links , on_stacks , components )
574
+
575
+ return components
576
+
577
+ _strongly_connected_components_tarjan_adjacency_matrix = \
578
+ _strongly_connected_components_tarjan_adjacency_list
579
+
534
580
def strongly_connected_components (graph , algorithm , ** kwargs ):
535
581
"""
536
582
Computes strongly connected components for the given
@@ -549,6 +595,7 @@ def strongly_connected_components(graph, algorithm, **kwargs):
549
595
supported,
550
596
551
597
'kosaraju' -> Kosaraju's algorithm as given in [1].
598
+ 'tarjan' -> Tarjan's algorithm as given in [2].
552
599
backend: pydatastructs.Backend
553
600
The backend to be used.
554
601
Optional, by default, the best available
@@ -578,6 +625,7 @@ def strongly_connected_components(graph, algorithm, **kwargs):
578
625
==========
579
626
580
627
.. [1] https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm
628
+ .. [2] https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
581
629
582
630
"""
583
631
raise_if_backend_is_not_python (
0 commit comments