-
Notifications
You must be signed in to change notification settings - Fork 1
/
BridgesInGraph.cs
77 lines (67 loc) · 3.1 KB
/
BridgesInGraph.cs
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
using AlgorithmsAndDataStructures.Algorithms.Graph.Common;
using System;
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.Misc
{
public class BridgesInGraph
{
private const int NullParent = -1;
#pragma warning disable CA1822 // Mark members as static
public List<Tuple<int, int>> GetBridges(UndirectedGraph graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null)
{
return new List<Tuple<int, int>>(0);
}
var vertices = graph.Vertices();
var discoveryTimes = new int[vertices.Length];
var lowestReachableDiscoveryTimeInSubtree = new int[vertices.Length];
var visited = new bool[vertices.Length];
var bridges = new List<Tuple<int, int>>();
var parents = new int[vertices.Length];
for (var i = 0; i < vertices.Length; i++)
{
if (!visited[i])
GetBridges(i, 0, vertices, discoveryTimes, lowestReachableDiscoveryTimeInSubtree, visited, parents, bridges);
}
return bridges;
}
private static void GetBridges(
int currentVertex,
int discoveryTime,
IReadOnlyList<List<int>> vertices,
IList<int> discoveryTimes,
IList<int> lowestReachableDiscoveryTimeInSubtree,
IList<bool> visited,
IList<int> parents,
ICollection<Tuple<int, int>> bridges)
{
discoveryTimes[currentVertex] = lowestReachableDiscoveryTimeInSubtree[currentVertex] = discoveryTime++;
visited[currentVertex] = true;
var children = 0;
foreach (var adjacentVertex in vertices[currentVertex])
{
if (!visited[adjacentVertex])
{
children++;
parents[adjacentVertex] = currentVertex;
GetBridges(adjacentVertex, discoveryTime, vertices, discoveryTimes, lowestReachableDiscoveryTimeInSubtree, visited, parents, bridges);
lowestReachableDiscoveryTimeInSubtree[currentVertex] = Math.Min(lowestReachableDiscoveryTimeInSubtree[currentVertex], lowestReachableDiscoveryTimeInSubtree[adjacentVertex]);
if (parents[currentVertex] == NullParent && children > 1)
{
bridges.Add(new Tuple<int, int>(currentVertex, adjacentVertex));
}
if (parents[currentVertex] != NullParent && lowestReachableDiscoveryTimeInSubtree[adjacentVertex] > discoveryTimes[currentVertex])
{
bridges.Add(new Tuple<int, int>(currentVertex, adjacentVertex));
}
}
else if (adjacentVertex != parents[currentVertex])
{
lowestReachableDiscoveryTimeInSubtree[currentVertex] = Math.Min(lowestReachableDiscoveryTimeInSubtree[currentVertex], discoveryTimes[adjacentVertex]);
}
}
}
}
}