-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathKosarajusAlgorithm.cs
More file actions
71 lines (53 loc) · 2.04 KB
/
KosarajusAlgorithm.cs
File metadata and controls
71 lines (53 loc) · 2.04 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
using System.Collections.Generic;
using AlgorithmsAndDataStructures.Algorithms.Graph.Common;
namespace AlgorithmsAndDataStructures.Algorithms.Graph.Misc;
public class KosarajusAlgorithm
{
#pragma warning disable CA1822 // Mark members as static
public bool IsConnected(WeightedGraphVertex[] graph)
#pragma warning restore CA1822 // Mark members as static
{
if (graph is null) return default;
var visited = new bool[graph.Length];
Dfs(graph, 0, visited);
for (var i = 0; i < visited.Length; i++)
if (!visited[i])
return false;
var transposedGraph = GetTransposed(graph);
var transposedVisited = new bool[graph.Length];
Dfs(transposedGraph, 0, transposedVisited);
for (var i = 0; i < transposedVisited.Length; i++)
if (!transposedVisited[i])
return false;
return true;
}
private static void Dfs(WeightedGraphVertex[] graph, int currentVertex, IList<bool> visited)
{
visited[currentVertex] = true;
for (var i = 0; i < graph[currentVertex].Edges.Count; i++)
{
var adjacentVertex = graph[currentVertex].Edges[i].To;
if (!visited[adjacentVertex]) Dfs(graph, adjacentVertex, visited);
}
}
private static WeightedGraphVertex[] GetTransposed(IReadOnlyList<WeightedGraphVertex> graph)
{
var result = new WeightedGraphVertex[graph.Count];
for (var i = 0; i < graph.Count; i++)
{
result[i] = result[i] == null ? new WeightedGraphVertex() : result[i];
for (var j = 0; j < graph[i].Edges.Count; j++)
{
var from = graph[i].Edges[j].To;
result[from] ??= new WeightedGraphVertex();
result[from].Edges.Add(new WeightedGraphNodeEdge
{
From = from,
To = graph[i].Edges[j].From,
Weight = graph[i].Edges[j].Weight
});
}
}
return result;
}
}