【问题标题】:Implementing dijkstra's algorithm into windows form chart control将dijkstra算法实现到windows窗体图表控件中
【发布时间】:2012-10-08 00:21:41
【问题描述】:

我正在尝试实现一个 dijkstra 模型来寻找连通图的最短路径。 我所拥有的是一个图表,点击按钮后,它会在图表上随机生成节点。

我想做的是:

  1. 判断图是否连通
  2. 如果已连接,则选择三种不同方法中的一种来查找最短路径: 一种。起点和终点之间距离的最短路径 湾。按边数计算的最短路径 C。边总权重的最短路径(在这里,我们想要的是更小的权重......)

其他一些注释。

因为这些数据点是在这个图表控件中随机生成的,所以我实际上没有 Vertex 类来生成顶点。我一直在四处寻找,发现大多数寻路功能都使用顶点类。所以基本上我的列表将从图表控件之外的节点填充。

谁能提供任何关于我如何解决上述两个问题的见解?

    //TODO:  Change to a function with return bool.  Void for purposes of testing at the moment.
    public void isConnected()
    {

        List<DataPoint> ParentPoints = new List<DataPoint>();

        //Gather all the non data generator into the same point array
        foreach (DataPoint pNonDG in chtGraph.Series[0].Points)
        {
            ParentPoints.Add(pNonDG);
        }
    }

【问题讨论】:

  • 您能更清楚地了解您正在使用的数据类型吗?
  • 现在我只有DataPoint。但是,我可以将它们转换为“Point”类型。完成后,我可以进一步将点坐标转换为整数、浮点数,无论我需要什么。我还没有为此构建任何其他类,因为我想如果我需要获取节点的数据,我可以查看图表控件系列来找到一个点。我也是一个非常业余的程序员,所以有时我会迷失在我正在做的事情中:)
  • 你愿意制作自己的数据类型吗?
  • 是的,我是。好像现在我没有走正确的道路:(

标签: c# algorithm windows-forms-designer


【解决方案1】:

计算科学图表不同于我们在统计或数学中制作的“图表”图表。计算机科学中的图是通过一系列“边”连接的“节点”的集合

一个节点是通过一条边连接的,但这并不意味着它是连接回来的。一些边可以是单向连接。

边缘通常具有与之相关的“权重”或“成本”。这就是您的 dijkstra 算法将派上用场的地方。它将使用此成本来计算到目标的最短路径。

让我们看看我们可能使用的一些数据类型

class GraphNode {
    List<GraphEdge> Edges = new List<GraphEdge>();
    public void AddEdge(GraphEdge edge) {
        Edges.Add(edge);
    }
    //you get the idea, this should have much more
    //it also manages edge connections
}

class GraphEdge { //this is a one way connection
    GraphNode ConnectedTo = null;
    //GraphNode ConnectedFrom = null; //if you uncomment this, it can be a two-way
                                      //connection, but you will need more code to
                                      //manage it
    float Cost = 0f;
    //you might consider adding a destructor that clears the pointers
    //otherwise gc might have a hard time getting rid of the nodes
}

class Graph {
    List<GraphNodes> Nodes = new List<GraphNodes>();
    //this class manages the nodes
    //it also provides help for connecting nodes
}

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2015-02-05
    • 1970-01-01
    相关资源
    最近更新 更多