【问题标题】:Depth first search in a distributed way分布式深度优先搜索
【发布时间】:2012-06-01 14:39:11
【问题描述】:

我曾尝试在 c# 中实现深度优先搜索,但我不确定如何以分布式计算方式执行此操作。如果你们能在这方面帮助我,我将非常感激:) 你可以在下面找到我的 DFS 代码

public class DFS
{ 
static List<string> traversedList = new List<string>();
static List<string> parentList = new List<string>();

public static void Main(string[] args)
{

    int N = 100;
    int M = N * 4;
    int P = N * 16;

    Stack newstack = new Stack();

    List<string> global_list=new List<string>();

    StreamReader file = new StreamReader("my input file");

    string text = file.ReadToEnd();

    string[] lines = text.Split('\n');

    string[][] array1 = new string[lines.Length][];

    for (int i = 0; i < lines.Length; i++)
    {
        lines[i] = lines[i].Trim();
        string[] words = lines[i].Split(' ');

        array1[i] = new string[words.Length];

        for (int j = 0; j < words.Length; j++)
        {
            array1[i][j] = words[j];
        }
    }

    StreamWriter sr = new StreamWriter(args[0]);

    for (int i = 0; i < array1.Length; i++)
    {
        for (int j = 0; j < array1[i].Length; j++)
        {
            if (j != 0 )
            {
                sr.Write(array1[i][0] + ":" + array1[i][j]);
                Console.WriteLine(array1[i][0] + ":" + array1[i][j]);
                sr.Write(sr.NewLine);
            }
        }

    }

    int start_no = Convert.ToInt32(args[args.Length-1]);

    traversedList.Add(start_no.ToString());
    parentList.Add("root");
    dfs(array1, start_no);

    for (int z = 0; z < traversedList.Count; z++)
    {
            Console.WriteLine(traversedList.ElementAt(z) + " "+parentList.ElementAt(z)+" "+(z+1));
     }
    Console.ReadLine();
}

private static void dfs(string[][] array, int point)
{
    for (int z = 1; z < array[point].Length; z++)
        {
            if ((!traversedList.Contains(array[point][z])))
            {
                traversedList.Add(array[point][z]);
                parentList.Add(point.ToString());
                dfs(array, int.Parse(array[point][z]));
            }
        }
        return;
}   

}

【问题讨论】:

  • 你能澄清一下你的意思吗? “分布式计算”通常意味着将计算分散到几台不同的计算机上。您的意思是“并行计算”吗?另外,你试过什么?那是怎么失败的?
  • 没错!!实现 DFS 的并行方式......我不知道从哪里开始......你能给我一些关于这个问题的提示吗?

标签: c# parallel-processing task-parallel-library depth-first-search


【解决方案1】:

您应该从计算机国际象棋(现已演变为世界冠军计算机游戏玩家)世界中阅读文献。他们已经进行分布式深度优先搜索大约 30 年了,并且有很多想法。正确处理很棘手,因为您必须在不知道特定分支可能包含多少工作的情况下平均分配工作。

查看Monty Newborn 或麦吉尔大学,这似乎是大约 10 年前的温床。

当然,GIYF:“分布式深度优先搜索”产生了对这篇论文的引用:Distributed Algorithms for Depth-First Search。我猜它包含了很多来自计算机国际象棋世界的想法。

*shared memory" DFS 的问题稍微简单一些;您不必向分布式助手发送消息,您只需传递一个指针即可:-} 拥有一种为您提供并行性和管理如果您的程序在每个分支上分叉时可能发生的爆炸性并行增长。我提供了一个使用我设计的并行编程语言构建的example 4x4 N-puzzle solver。(这个示例是我最早的 SO 帖子之一!)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多