Exemplo n.º 1
0
        public int[]  Dijkstra(int startVertex, int endVertex)
        {
            int[] predecessors = new int[size]; //Definisemo prethodni cvor svakog elementa u najkracem putu i onda cemo unazad procitati, tj. iscrtati put
            for (int i = 0; i < size; i++)
            {
                predecessors[i] = -1;
            }



            double[] priceTo = new double[size];// Pamti cenu od poocetnog do i-tog cvora.
            //Postavljamo sve ceneDo u pocetku na "beskonacno", nama je ok da bude negativna vrednost, posto su sve tezine pozitivne.

            SortedSet <IncomingPath> paths = new SortedSet <IncomingPath>(); //Skladistimo predjene puteve

            double infinity = 1.7976931348623157E+308;

            for (int i = 0; i < size; i++)
            {
                priceTo[i] = infinity;
            }

            priceTo[startVertex] = 0;
            paths.Add(new IncomingPath(startVertex, 0));


            while (paths.Count != 0)
            {
                IncomingPath v = paths.Min;
                paths.Remove(v);
                int    previousVertex = v.getVertex();
                double previousWeight = v.getWeight();

                for (int i = 0; i < adjList[previousVertex].Count; i++)
                {
                    int    tmpVertex    = adjList[previousVertex][i].Item1;
                    double edge         = adjList[previousVertex][i].Item2;
                    double currentPrice = priceTo[previousVertex] + edge;
                    if (currentPrice < priceTo[tmpVertex])
                    {
                        paths.Remove(new IncomingPath(tmpVertex, priceTo[tmpVertex])); //Brisemo trenutni put do cvora, da ne bismo prolazili posle opet kroz sve susede
                        priceTo[tmpVertex] = currentPrice;
                        paths.Add(new IncomingPath(tmpVertex, priceTo[tmpVertex]));    // Dodali smo novi najkraci put nakon promene
                        predecessors[tmpVertex] = previousVertex;                      //Znamo da je prethodni cvor koji je vodio do naseg trenutnog cvora, zapravo ovaj cvor
                    }
                }
            }

            return(predecessors);
        }
Exemplo n.º 2
0
        public int CompareTo(Object obj)
        {
            IncomingPath second = obj as IncomingPath;

            return(this.weight.CompareTo(second.weight));
        }