1

ノードへの最小コストでルートを見つけたい。いくつかのルートが見つかりましたが、最小コストのルートはありません。

ノードに関する情報を格納するための私のクラス:

public class Graph
{
    //public int[][] childNodes;
    //public Graph(int[][] nodes)
    //{
    //    this.childNodes = nodes;
    //}

    public List<Node> nodes = new List<Node>();
}

public class Node
{
    public Node(int node)
    {
        this.node = node;
    }

    public int node { get; set; }
    public List<Tuple<int,int>> endNodes = new List<Tuple<int,int>>();
}

public class Routes
{
    public int depth { get; set; }
    public int cost { get; set; }
    public List<string> nodes = new List<string>();
}

タプルにはint、intがあります-最初のintはエンドノード、2番目のintはこのノードへのルートのコストです

グラフをトラバースするクラス:

public List<string> visited = new List<string>();
public List<Routes> routes = new List<Routes>();





   public void dfs2(Node node, List<Routes> routes, Routes route, int depth)
        {
            depth++;
            foreach (Tuple<int,int> childNode in g.nodes[node.node].endNodes)
            {
                string path = string.Format("{0}{1}", node.node, childNode.Item1);
                string pathReverse = string.Format("{0}{1}", childNode.Item1, node.node);
               // if (!visited.Contains(path)) // && !visited.Contains(pathReverse))
                if(!route.nodes.Contains(path)) // && !route.nodes.Contains(pathReverse))
                {
                    visited.Add(path);
                    if (childNode.Item1 == 6)
                    {
                        Routes newRoutes = new Routes();
                        newRoutes.depth = depth;
                        newRoutes.cost = route.cost + childNode.Item2;
                        newRoutes.nodes.AddRange(route.nodes);
                        newRoutes.nodes.Add(path);
                        routes.Add(newRoutes);
                    }
                    else
                    {
                        route.depth = depth;
                        route.cost += childNode.Item2; // 2;//childNode.
                        route.nodes.Add(path);

                        dfs2(g.nodes[childNode.Item1], routes, route, depth);
                    }
                    Debug.WriteLine("przeszedłem: " + path);

                }
            }
        }

私の例では、ノード 6 への最小コストのルートを見つけたい

4

1 に答える 1