私はこの質問に対する答えを数時間ウェブとこのサイトで見つけようとしてきましたが、まだ答えは出ていません。
.NET がアプリに 1 MB を割り当てること、およびスタック サイズを強制するのではなく、再コーディングしてスタック オーバーフローを回避することが最善であることを理解しています。
私は、約 3000 ノードまでうまく機能する「最短パス」アプリに取り組んでおり、その時点でオーバーフローします。問題を引き起こすメソッドは次のとおりです。
public void findShortestPath(int current, int end, int currentCost)
{
if (!weight.ContainsKey(current))
{
weight.Add(current, currentCost);
}
Node currentNode = graph[current];
var sortedEdges = (from entry in currentNode.edges orderby entry.Value ascending select entry);
foreach (KeyValuePair<int, int> nextNode in sortedEdges)
{
if (!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key])
{
int nextNodeCost = currentCost + nextNode.Value;
if (!weight.ContainsKey(nextNode.Key))
{
weight.Add(nextNode.Key, nextNodeCost);
}
else if (weight[nextNode.Key] > nextNodeCost)
{
weight[nextNode.Key] = nextNodeCost;
}
}
}
visited.Add(current, true);
foreach (KeyValuePair<int, int> nextNode in sortedEdges)
{
if(!visited.ContainsKey(nextNode.Key) || !visited[nextNode.Key]){
findShortestPath(nextNode.Key, end, weight[nextNode.Key]);
}
}
}//findShortestPath
参考までに、Node クラスには 1 つのメンバーがあります。
public Dictionary<int, int> edges = new Dictionary<int, int>();
グラフ[]は次のとおりです。
private Dictionary<int, Node> graph = new Dictonary<int, Node>();
ある反復 (再帰?) から次の反復まで必要以上の荷物を運ばないようにコードを最適化しようとしましたが、各ノードが 1 ~ 9 個のエッジを持つ 100K ノード グラフを使用すると、次のようになります。すぐに 1MB の制限に達します。
とにかく、私はC#とコードの最適化が初めてです。誰かが私にいくつかのポインタを与えることができれば(これは好きではありません)、私はそれを感謝します。