-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBipartiteGraph.cs
More file actions
233 lines (202 loc) · 6.29 KB
/
BipartiteGraph.cs
File metadata and controls
233 lines (202 loc) · 6.29 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Linq;
namespace Algorithms.Graph;
/// <summary>
/// Checks if a graph is bipartite (2-colorable).
/// A bipartite graph can be divided into two independent sets where no two vertices
/// within the same set are adjacent.
/// </summary>
public static class BipartiteGraph
{
/// <summary>
/// Checks if a graph is bipartite using BFS-based coloring.
/// </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>True if graph is bipartite, false otherwise.</returns>
public static bool IsBipartite<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 true; // Empty graph is bipartite
}
var colors = new Dictionary<T, int>();
// Check each connected component
foreach (var start in vertexList)
{
if (colors.ContainsKey(start))
{
continue; // Already colored
}
if (!BfsColor(start, colors, getNeighbors))
{
return false;
}
}
return true;
}
/// <summary>
/// Gets the two partitions of a bipartite 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>Tuple of two sets representing the partitions, or null if not bipartite.</returns>
public static (HashSet<T> SetA, HashSet<T> SetB)? GetPartitions<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>(), new HashSet<T>());
}
var colors = new Dictionary<T, int>();
// Color all components
foreach (var start in vertexList)
{
if (colors.ContainsKey(start))
{
continue;
}
if (!BfsColor(start, colors, getNeighbors))
{
return null; // Not bipartite
}
}
// Split into two sets based on color
var setA = new HashSet<T>();
var setB = new HashSet<T>();
foreach (var vertex in vertexList)
{
if (colors[vertex] == 0)
{
setA.Add(vertex);
}
else
{
setB.Add(vertex);
}
}
return (setA, setB);
}
/// <summary>
/// Checks if a graph is bipartite using DFS-based coloring.
/// </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>True if graph is bipartite, false otherwise.</returns>
public static bool IsBipartiteDfs<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 true;
}
var colors = new Dictionary<T, int>();
foreach (var start in vertexList)
{
if (colors.ContainsKey(start))
{
continue;
}
if (!DfsColor(start, 0, colors, getNeighbors))
{
return false;
}
}
return true;
}
private static bool BfsColor<T>(
T start,
Dictionary<T, int> colors,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
var queue = new Queue<T>();
queue.Enqueue(start);
colors[start] = 0;
while (queue.Count > 0)
{
var current = queue.Dequeue();
var currentColor = colors[current];
var nextColor = 1 - currentColor;
foreach (var neighbor in getNeighbors(current))
{
if (!colors.ContainsKey(neighbor))
{
colors[neighbor] = nextColor;
queue.Enqueue(neighbor);
}
else if (colors[neighbor] == currentColor)
{
return false; // Same color as current - not bipartite
}
else
{
// Different color - valid
}
}
}
return true;
}
private static bool DfsColor<T>(
T vertex,
int color,
Dictionary<T, int> colors,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
colors[vertex] = color;
var nextColor = 1 - color;
foreach (var neighbor in getNeighbors(vertex))
{
if (!colors.ContainsKey(neighbor))
{
if (!DfsColor(neighbor, nextColor, colors, getNeighbors))
{
return false;
}
}
else if (colors[neighbor] == color)
{
return false; // Same color - not bipartite
}
else
{
// Different color - valid
}
}
return true;
}
}