-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathArticulationPoints.cs
More file actions
177 lines (157 loc) · 5.67 KB
/
ArticulationPoints.cs
File metadata and controls
177 lines (157 loc) · 5.67 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Graph;
/// <summary>
/// Finds articulation points (cut vertices) in an undirected graph.
/// An articulation point is a vertex whose removal increases the number of connected components.
/// </summary>
public static class ArticulationPoints
{
/// <summary>
/// Finds all articulation points in an undirected graph.
/// </summary>
/// <typeparam name="T">Type of vertex.</typeparam>
/// <param name="vertices">All vertices in the graph.</param>
/// <param name="getNeighbors">Function to get neighbors of a vertex.</param>
/// <returns>Set of articulation points.</returns>
public static HashSet<T> Find<T>(
IEnumerable<T> vertices,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
if (vertices == null)
{
throw new ArgumentNullException(nameof(vertices));
}
if (getNeighbors == null)
{
throw new ArgumentNullException(nameof(getNeighbors));
}
var vertexList = vertices.ToList();
if (vertexList.Count == 0)
{
return new HashSet<T>();
}
var articulationPoints = new HashSet<T>();
var visited = new HashSet<T>();
var discoveryTime = new Dictionary<T, int>();
var low = new Dictionary<T, int>();
var parent = new Dictionary<T, T?>();
var time = 0;
foreach (var vertex in vertexList)
{
if (!visited.Contains(vertex))
{
var state = new DfsState<T>
{
Visited = visited,
DiscoveryTime = discoveryTime,
Low = low,
Parent = parent,
ArticulationPoints = articulationPoints,
};
Dfs(vertex, ref time, state, getNeighbors);
}
}
return articulationPoints;
}
/// <summary>
/// Checks if a vertex is an articulation point.
/// </summary>
/// <typeparam name="T">Type of vertex.</typeparam>
/// <param name="vertex">Vertex to check.</param>
/// <param name="vertices">All vertices in the graph.</param>
/// <param name="getNeighbors">Function to get neighbors of a vertex.</param>
/// <returns>True if vertex is an articulation point.</returns>
public static bool IsArticulationPoint<T>(
T vertex,
IEnumerable<T> vertices,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
var articulationPoints = Find(vertices, getNeighbors);
return articulationPoints.Contains(vertex);
}
/// <summary>
/// Counts the number of articulation points in the graph.
/// </summary>
/// <typeparam name="T">Type of vertex.</typeparam>
/// <param name="vertices">All vertices in the graph.</param>
/// <param name="getNeighbors">Function to get neighbors of a vertex.</param>
/// <returns>Number of articulation points.</returns>
public static int Count<T>(
IEnumerable<T> vertices,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
return Find(vertices, getNeighbors).Count;
}
private static void Dfs<T>(
T u,
ref int time,
DfsState<T> state,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
state.Visited.Add(u);
state.DiscoveryTime[u] = time;
state.Low[u] = time;
time++;
int children = 0;
foreach (var v in getNeighbors(u))
{
if (!state.Visited.Contains(v))
{
children++;
state.Parent[v] = u;
Dfs(v, ref time, state, getNeighbors);
state.Low[u] = Math.Min(state.Low[u], state.Low[v]);
// Check if u is an articulation point
bool isRoot = !state.Parent.ContainsKey(u);
if (isRoot && children > 1)
{
state.ArticulationPoints.Add(u);
}
bool isNonRootArticulation = state.Parent.ContainsKey(u) && state.Low[v] >= state.DiscoveryTime[u];
if (isNonRootArticulation)
{
state.ArticulationPoints.Add(u);
}
}
else if (!EqualityComparer<T>.Default.Equals(v, state.Parent.GetValueOrDefault(u)))
{
// Back edge: update low value
state.Low[u] = Math.Min(state.Low[u], state.DiscoveryTime[v]);
}
else
{
// Edge to parent: no action needed
}
}
}
/// <summary>
/// Encapsulates the state for DFS traversal in articulation point detection.
/// </summary>
/// <typeparam name="T">Type of vertex.</typeparam>
private sealed class DfsState<T>
where T : notnull
{
/// <summary>
/// Gets set of visited vertices.
/// </summary>
public required HashSet<T> Visited { get; init; }
/// <summary>
/// Gets discovery time for each vertex.
/// </summary>
public required Dictionary<T, int> DiscoveryTime { get; init; }
/// <summary>
/// Gets lowest discovery time reachable from each vertex.
/// </summary>
public required Dictionary<T, int> Low { get; init; }
/// <summary>
/// Gets parent vertex in DFS tree.
/// </summary>
public required Dictionary<T, T?> Parent { get; init; }
/// <summary>
/// Gets set of detected articulation points.
/// </summary>
public required HashSet<T> ArticulationPoints { get; init; }
}
}