-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTarjansAlgorithmForBiconnectedComponents.cs
More file actions
107 lines (89 loc) · 3.83 KB
/
TarjansAlgorithmForBiconnectedComponents.cs
File metadata and controls
107 lines (89 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
using AlgorithmsAndDataStructures.Algorithms.Graph.Common;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.Misc;
public class TarjansAlgorithmForBiconnectedComponents
{
private const int NullParent = -1;
#pragma warning disable CA1822 // Mark members as static
public List<List<int>> GetBiconnectedComponents(UndirectedGraph graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null) return new List<List<int>>(0);
var vertices = graph.Vertices();
if (vertices.Length == 0) return new List<List<int>>();
var visited = new bool[vertices.Length];
var parents = new int[vertices.Length];
var discoveryTime = new int[vertices.Length];
var lowestSubTreeDiscoveryTime = new int[vertices.Length];
var dfs = new Stack<int>(vertices.Length);
var biconnectedComponents = new List<List<int>>();
for (var i = 0; i < vertices.Length; i++)
if (!visited[i])
{
parents[i] = NullParent;
DfsArticulationTraversal(vertices, 0, visited, parents, discoveryTime, lowestSubTreeDiscoveryTime, dfs,
biconnectedComponents, 0);
}
if (dfs.Any())
{
var component = new List<int>();
while (dfs.Any()) component.Add(dfs.Pop());
biconnectedComponents.Add(component);
}
return biconnectedComponents;
}
private static void DfsArticulationTraversal(
IReadOnlyList<List<int>> vertices,
int currentVertex,
IList<bool> visited,
IList<int> parents,
IList<int> discoveryTime,
IList<int> lowestSubTreeDiscoveryTime,
Stack<int> dfs,
ICollection<List<int>> biconnectedComponents,
int time)
{
visited[currentVertex] = true;
var children = 0;
var currentDiscoveryTime = time++;
discoveryTime[currentVertex] = currentDiscoveryTime;
lowestSubTreeDiscoveryTime[currentVertex] = currentDiscoveryTime;
dfs.Push(currentVertex);
foreach (var adjacentVertex in vertices[currentVertex])
if (!visited[adjacentVertex])
{
children++;
parents[adjacentVertex] = currentVertex;
DfsArticulationTraversal(
vertices,
adjacentVertex,
visited,
parents,
discoveryTime,
lowestSubTreeDiscoveryTime,
dfs,
biconnectedComponents,
time);
lowestSubTreeDiscoveryTime[currentVertex] = Math.Min(lowestSubTreeDiscoveryTime[currentVertex],
lowestSubTreeDiscoveryTime[adjacentVertex]);
var isRootNode = parents[currentVertex] == NullParent && children > 1;
var isChildrenCantReachBeyondParent = parents[currentVertex] != NullParent &&
lowestSubTreeDiscoveryTime[adjacentVertex] >=
discoveryTime[currentVertex];
var isArticulationPoint = isRootNode || isChildrenCantReachBeyondParent;
if (isArticulationPoint)
{
var component = new List<int> { currentVertex };
while (dfs.Peek() != currentVertex) component.Add(dfs.Pop());
biconnectedComponents.Add(component);
}
}
else if (adjacentVertex != parents[currentVertex])
{
lowestSubTreeDiscoveryTime[currentVertex] = Math.Min(lowestSubTreeDiscoveryTime[currentVertex],
discoveryTime[adjacentVertex]);
}
}
}