Exemplo n.º 1
0
        private Path GetMin(GraphWithPath branch, GraphWithPath main)
        {
            var resultA = Find(main);

            if (resultA.Weight <= branch.Potential)
            {
                return(resultA);
            }
            var resultB = Find(branch);

            return(resultA.Weight > resultB.Weight ? resultB : resultA);
        }
Exemplo n.º 2
0
        private Path Find(GraphWithPath graph)
        {
            _log(graph.ToString());
            var arc = graph.GetArc();

            if (arc is null)
            {
                return(graph.GetPath());
            }

            //var marked = arc.To;

            var branch = graph.Clone();

            branch.Remove(arc);
            var main = graph.Clone();

            main.AddToPath(arc);
            //main.Remove(marked);

            return(GetMin(branch, main));
        }
Exemplo n.º 3
0
        public void Applay(int[,] incidence, params string[] names)
        {
            var nodeCount = incidence.GetLength(0);

            if (nodeCount != incidence.GetLength(1))
            {
                throw new ArgumentException("matrix must be square");
            }
            if (names.Length == 0)
            {
                names = Enumerable.Range(1, nodeCount).Select(i => i.ToString()).ToArray();
            }
            else if (names.Length != nodeCount)
            {
                throw new ArgumentException("count of names must be eaquals count of nodes");
            }
            var nodes = new Dictionary <string, Node>(nodeCount);
            var arcs  = new List <Arc>(nodeCount);

            for (int i = 0; i < nodeCount; i++)
            {
                var node1 = nodes.GetOrAdd(names[i], name => new Node(name));
                for (int j = 0; j < nodeCount; j++)
                {
                    if (incidence[i, j] == -1)
                    {
                        continue;
                    }
                    var node2 = nodes.GetOrAdd(names[j], name => new Node(name));
                    arcs.Add(new Arc(node1, node2, incidence[i, j]));
                }
            }
            GraphWithPath graph = new GraphWithPath(arcs, _log);
            Path          path  = Find(graph);

            _log(path.ToString());
        }