-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.cs
More file actions
190 lines (165 loc) · 5.91 KB
/
Copy pathdfs.cs
File metadata and controls
190 lines (165 loc) · 5.91 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
using System.Numerics;
namespace DFS
{
public class Dfs<TNodeKey, TDim>
where TNodeKey : IEquatable<TNodeKey>
where TDim : IBinaryInteger<TDim>, IMinMaxValue<TDim>, IUnsignedNumber<TDim>
{
private class DfsException : Exception
{
public DfsException()
{
}
public DfsException(string message) : base(message)
{
}
}
private record NodeKeyData
{
public bool Visited;
public TDim AccLength;
public TNodeKey From;
}
private readonly Dictionary<TNodeKey, Dictionary<TNodeKey, TDim>> _map;
public Dfs(Dictionary<TNodeKey, Dictionary<TNodeKey, TDim>> map)
{
_map = map;
}
public (Stack<TNodeKey> path, TDim accLength) Path(TNodeKey nodeKeyFrom, TNodeKey nodeKeyTo)
{
var nodeKeys = _map.Select(_ => _.Key).Union(_map.SelectMany(_ => _.Value).Select(__ => __.Key)).ToArray();
var path = new Stack<TNodeKey>();
TDim length;
try
{
var q = new Stack<TNodeKey>();
var s = nodeKeys.ToDictionary(_ => _, _ => new NodeKeyData
{
Visited = false,
AccLength = default,
From = default
});
q.Push(nodeKeyFrom);
s[nodeKeyFrom].Visited = true;
s[nodeKeyFrom].AccLength = default;
while (q.Count > 0)
{
var v = q.Pop();
foreach (var n in _map[v].Where(_ => !s[_.Key].Visited || s[_.Key].AccLength > SumClampMaxValue(s[v].AccLength, _.Value)))
{
s[n.Key].Visited = true;
q.Push(n.Key);
s[n.Key].AccLength = SumClampMaxValue(s[v].AccLength, n.Value);
s[n.Key].From = v;
}
}
length = s[nodeKeyTo].AccLength;
for(var nextNodeKey = nodeKeyTo; !nextNodeKey.Equals(nodeKeyFrom);)
{
path.Push(nextNodeKey);
nextNodeKey = s[nextNodeKey].From;
}
path.Push(nodeKeyFrom);
}
catch (Exception)
{
throw new DfsException("Graph data must follow DFS algorithm requirements!");
}
return (path, length);
}
private static TDim SumClampMaxValue(TDim value1, TDim value2)
{
return TDim.MaxValue - value1 < value2 || TDim.MaxValue - value2 < value1 ? TDim.MaxValue : value1 + value2;
}
}
internal class Program
{
static void Main(string[] args)
{
//It takes small amount of time to actually find path in a large graph
var w = 1000;
var h = 1000;
var map = new Dictionary<int, Dictionary<int, ulong>>();
/*
* Generates graph:
*
* • → • → • → … → • ⮝
* ↓ ↓ ↓ ↓
* • → • → • → … → •
* ↓ ↓ ↓ ↓
* h
* … … … … …
*
* ↓ ↓ ↓ ↓
* • → • → • → … → • ⮟
*
* ⮜ w ⮞
*/
for (var row = 0; row < h; row++)
{
for (var col = 0; col < w; col++)
{
var idx = row * w + col;
map[idx] = new Dictionary<int, ulong>();
if (col + 1 < w)
{
map[idx].Add(idx + 1, 1);
}
if (row + 1 < h)
{
map[idx].Add(idx + w, 1);
}
}
}
var dfs = new Dfs<int, ulong>(map);
var result = dfs.Path(0, w * h - 1);
Console.WriteLine($"{string.Join(" -> ", result.path)} : {result.accLength}");
//Another proof to find shortest path
map = new Dictionary<int, Dictionary<int, ulong>>
{
{
0, new Dictionary<int, ulong>
{
{1, 25},
{2, 15},
{3, 7},
{4, 2}
}
},
{
1, new Dictionary<int, ulong>
{
{0, 25},
{2, 6}
}
},
{
2, new Dictionary<int, ulong>
{
{0, 15},
{1, 6},
{3, 4}
}
},
{
3, new Dictionary<int, ulong>
{
{0, 7},
{2, 4},
{4, 3}
}
},
{
4, new Dictionary<int, ulong>
{
{0, 2},
{3, 3}
}
},
};
dfs = new Dfs<int, ulong>(map);
result = dfs.Path(0, 1);
Console.WriteLine($"{string.Join(" -> ", result.path)} : {result.accLength}");
}
}
}