Example #1
0
        public int CompareTo(object obj)
        {
            GraphEdge cobj = obj as GraphEdge;

            return(Weights.CompareTo(cobj.Weights));
        }
        public override List <GraphEdge> MiniDistance_BSF(int startidx, int destidx)
        {
            if (startidx < 0 && startidx >= NodesCount)
            {
                return(new List <GraphEdge>());
            }

            if (destidx < 0 && destidx >= NodesCount)
            {
                return(new List <GraphEdge>());
            }

            if (startidx == destidx)
            {
                return(new List <GraphEdge>());
            }

            Queue.Queue961 <int> queue = new Queue961 <int>();
            queue.enqueue(startidx);

            int[] visitednodes    = new int[NodesCount];
            int[] parentpathnodes = new int[NodesCount];
            int[] nodedistance    = new int[NodesCount];

            for (int i = 0; i < NodesCount; i++)
            {
                nodedistance[i]    = int.MaxValue;
                visitednodes[i]    = 0;
                parentpathnodes[i] = -1;
            }

            visitednodes[startidx] = 1;
            nodedistance[startidx] = 0;

            while (!queue.isEmpty())
            {
                var curidx          = queue.dequeue();
                var neighbourenodes = getNeighbourNodes(curidx);
                for (int i = 0; i < neighbourenodes.Count; i++)
                {
                    int nodeidx = neighbourenodes[i];
                    if (visitednodes[nodeidx] == 0)
                    {
                        visitednodes[nodeidx]    = 1; //已访问
                        nodedistance[nodeidx]    = nodedistance[curidx] + 1;
                        parentpathnodes[nodeidx] = curidx;

                        if (nodeidx == destidx) //找到跳出循环
                        {
                            queue.clear();
                            break;
                        }

                        queue.enqueue(nodeidx);
                    }
                }
            }


            List <GraphEdge> edges = new List <GraphEdge>();
            int pathendidx         = destidx;

            while (parentpathnodes[pathendidx] >= 0)
            {
                GraphEdge edge = new GraphEdge {
                    Weights = 1, StartNode = _nodesarray[parentpathnodes[pathendidx]], EndNode = _nodesarray[pathendidx]
                };
                pathendidx = parentpathnodes[pathendidx];
                edges.Insert(0, edge);
            }

            return(edges);
        }