2 つの幅優先探索アルゴリズムの実行時間の違いを比較しています。平行と非平行。しかし、私が作成するすべてのグラフについて、非並列バージョンは並列バージョンよりも 10 倍高速です。
これは並列幅優先検索です。どこに問題があるのか わかりませんが、この方法のどこかにあると思います:
public static int PBFS(Node start, Node end)
{
var queue = new ConcurrentQueue<Node>();
queue.Enqueue(start);
while (queue.Count != 0)
{
bool isFinished = false;
if (isFinished) break;
Parallel.ForEach<Node>(queue, CurrentNode =>
{
if (queue.TryDequeue(out CurrentNode))
{
foreach (Node Child in CurrentNode.Children.Keys)
{
if (Child.IsVisited == false)
{
Child.IsVisited = true;
if (Child == end) { isFinished = true; return; }
}
queue.Enqueue(Child);
}
}
return;
});
} return 1;
}
これは非並列 BFS です。
public static int BFS(Node start, Node end)
{
Queue<Node> queue = new Queue<Node>();
queue.Enqueue(start);
while (queue.Count != 0)
{
Node CurrentNode = queue.Dequeue();
CurrentNode.IsVisited = true;
foreach (Node Child in CurrentNode.Children.Keys)
{
if (Child.IsVisited == false)
{
Child.IsVisited = true;
//Console.Write(Child.Name+" ");
if (Child == end) return 1;
}
queue.Enqueue(Child);
}
// Console.WriteLine();
}
// Console.WriteLine();
return 0;
}