-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCC_Kosaraju.cs
239 lines (187 loc) · 5.86 KB
/
SCC_Kosaraju.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
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
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
//
//
//
namespace SCC
{
public class Program
{
public struct Vertex
{
public int N;
public List<int> Edges;
public Vertex(int n, IEnumerable<int> edges)
{
N = n;
Edges = edges.ToList();
}
public Vertex(int n)
{
N = n;
Edges = new List<int>();
}
public override string ToString()
{
return string.Join(", ", Edges);
}
};
static int T = 0;
static int S;
static int[] F;
static Dictionary<int, int> leader = new Dictionary<int, int>();
static bool[] explored;
static void DFS_Recur(Dictionary<int, Vertex> g, int i)
{
explored[i] = true;
leader[i] = S;
if (!g.ContainsKey(i))
{
g.Add(i, new Vertex(i));
}
foreach (var j in g[i].Edges)
{
if (!explored[j])
{
//Task.Run(() => DFS_Recur(g, j)).Wait();
DFS_Recur(g, j);
}
}
T++;
F[i] = T;
}
//Depth First Search Loop
static void DFS_Loop(Dictionary<int, Vertex> g, int n)
{
Console.WriteLine("DFS Loop started \n");
T = 0;
explored = new bool[n + 1];
F = new int[n + 1];
leader.Clear();
for (int i = n; i >= 1; i--)
{
if (!g.ContainsKey(i))
{
g.Add(i, new Vertex(i));
}
if (!explored[i])
{
S = i;
DFS_Recur(g, i);
}
}
Console.Write("DFS Loop finished \n");
}
static Dictionary<int, Vertex> reverse_arcs(Dictionary<int, Vertex> g)
{
Console.Write("reverse started\n");
var r = new Dictionary<int, Vertex>();
foreach (var it in g)
{
foreach (var it2 in it.Value.Edges)
{
if (!r.ContainsKey(it2))
r[it2] = new Vertex(it2, new List<int>());
r[it2].Edges.Add(it.Key);
}
}
Console.Write("reverse finished\n");
return r;
}
static Dictionary<int, Vertex> applyF(Dictionary<int, Vertex> g)
{
Console.Write("apply F started \n");
var r = new Dictionary<int, Vertex>();
foreach (var it in g)
{
r[F[it.Key]] = new Vertex(F[it.Key], new List<int>());
foreach (var it2 in it.Value.Edges)
{
r[F[it.Key]].Edges.Add(F[it2]);
}
}
Console.Write("apply F finished \n");
return r;
}
static List<KeyValuePair<int, int>> getSccSizes(int qty)
{
Console.Write("getSccSize started \n");
var ret = new Dictionary<int, int>();
foreach (var it in leader)
{
if (!ret.ContainsKey(it.Value))
ret[it.Value] = 0;
ret[it.Value]++;
}
return ret
.OrderByDescending(kvp => kvp.Value)
.Take(qty)
.ToList();
}
//Computing Strong Components
static void Kosaraju(Dictionary<int, Vertex> g, int n)
{
Console.Write("Kosaraju started\n");
var gRev = reverse_arcs(g);
DFS_Loop(gRev, n);
g = applyF(g);
DFS_Loop(g, n);
var l = getSccSizes(5);
foreach (var it in l)
{
Console.Write(it.Key + ":" + it.Value + "\n");
}
Console.Write("----------\n");
foreach (var it in l)
{
Console.Write(it.Value + ",");
}
}
static void gAdd(Dictionary<int, Vertex> g, HashSet<int> nodes, int head, int tail)
{
if (!g.ContainsKey(head))
g.Add(head, new Vertex(head, new List<int>()));
g[head].Edges.Add(tail);
nodes.Add(head);
nodes.Add(tail);
}
static void gInit(Dictionary<int, Vertex> g, int begin, int end)
{
Console.Write("starting map\n");
for (int i = begin; i <= end; i++)
{
g.Add(i, new Vertex(i, new List<int>()));
}
Console.Write("map finished\n");
}
static int Main()
{
Dictionary<int, Vertex> g = new Dictionary<int, Vertex>();
HashSet<int> nodes = new HashSet<int>();
Console.Write("read file started\n");
//Sample1.txt Answer: 3,3,3,0,0
//Sample2.txt Answer: 3,3,2,0,0
//Sample3.txt Answer: 3,3,1,1,0
//Sample4.txt Answer: 7,1,0,0,0
//Sample5.txt Answer: 6,3,2,1,0;
//SCC.txt Answer: 434821,968,459,313,211
int h, t;
var ifstream = new StreamReader(File.Open("C:/temp/Sample1.txt", FileMode.Open));
while (!ifstream.EndOfStream)
{
var line = ifstream.ReadLine().Split(' ');
h = int.Parse(line[0]);
t = int.Parse(line[1]);
gAdd(g, nodes, h, t);
}
Console.Write("read file finished\n");
Kosaraju(g, nodes.Count);
Console.Write("fim\n");
Console.ReadLine();
return 0;
}
}
}