Esempio n. 1
0
        public void Run()
        {
            var nodeA = new DjikstraNode
            {
                Key = "A",
            };
            var nodeB = new DjikstraNode
            {
                Key = "B"
            };
            var nodeC = new DjikstraNode
            {
                Key = "C"
            };
            var nodeD = new DjikstraNode
            {
                Key = "D"
            };
            var nodeE = new DjikstraNode
            {
                Key = "E"
            };
            var nodeF = new DjikstraNode
            {
                Key = "F"
            };
            var nodeG = new DjikstraNode
            {
                Key = "G"
            };
            var nodeH = new DjikstraNode
            {
                Key = "H"
            };

            var tree = new List <DjikstraNode>
            {
                nodeA.AddConnection(nodeB, 8).AddConnection(nodeD, 5).AddConnection(nodeC, 2),
                nodeB.AddConnection(nodeD, 2).AddConnection(nodeF, 13),
                nodeC.AddConnection(nodeD, 2).AddConnection(nodeE, 5),
                nodeD.AddConnection(nodeE, 1).AddConnection(nodeF, 6).AddConnection(nodeG, 3),
                nodeE.AddConnection(nodeG, 1),
                nodeF.AddConnection(nodeG, 2).AddConnection(nodeH, 3),
                nodeG.AddConnection(nodeH, 6),
                nodeH
            };

            FindShortestPath(tree, start: nodeA, end: nodeH);
        }
Esempio n. 2
0
        public DjikstraNode AddConnection(DjikstraNode node, int cost, bool twoWay = true)
        {
            Edges.Add(new Edge
            {
                From = this,
                To   = node,
                Cost = cost
            });
            if (twoWay)
            {
                node.AddConnection(this, cost, false);
            }

            return(this);
        }