Exemple #1
0
        public static List <int> Dijkstra(int Source, int dest, RGBPixel[,] ImageMatrix)
        {
            const double oo = 10000000000000000000; // infity value
            //Distance : the minimum cost between the source node and all the others nodes
            //initialized with infinty value
            int           Width        = ImageOperations.GetWidth(ImageMatrix);
            int           Height       = ImageOperations.GetHeight(ImageMatrix);
            int           nodes_number = Width * Height;
            List <double> Distance     = new List <double>();

            Distance = Enumerable.Repeat(oo, nodes_number).ToList();

            //Previous : saves the previous node that lead to the shortest path from the src node to current node
            List <int> Previous = new List <int>();

            Previous = Enumerable.Repeat(-1, nodes_number).ToList();

            // PeriorityQueue : always return the shortest bath btween src node and specific node
            PeriorityQueue <Edge> MinimumDistances = new PeriorityQueue <Edge>();

            MinimumDistances.Push(new Edge(-1, Source, 0));
            while (!MinimumDistances.IsEmpty())
            {
                // get the shortest path so far
                Edge CurrentEdge = MinimumDistances.Top();
                MinimumDistances.Pop();
                // check if this SP is vaild (i didn't vist this node with a less cost)
                if (CurrentEdge.Weight >= Distance[CurrentEdge.To])
                {
                    continue;
                }
                // save the previous
                Previous[CurrentEdge.To] = CurrentEdge.From;
                Distance[CurrentEdge.To] = CurrentEdge.Weight;
                if (CurrentEdge.To == dest)
                {
                    break;
                }
                // Relaxing
                List <Edge> neibours = GraphOperations.Get_neighbours(CurrentEdge.To, ImageMatrix);
                for (int i = 0; i < neibours.Count; i++)
                {
                    Edge HeldEdge = neibours[i];
                    // if the relaxed path cost of a neighbour node is less than  it's previous one
                    if (Distance[HeldEdge.To] > Distance[HeldEdge.From] + HeldEdge.Weight)
                    {
                        // set the relaxed cost to Distance  && pash it to the PQ
                        HeldEdge.Weight = Distance[HeldEdge.From] + HeldEdge.Weight;
                        MinimumDistances.Push(HeldEdge);
                    }
                }
            }
            return(Previous);  // re turn th shortest paths from src to all nodes
        }
        public static List <int> Dijkstra(int Source, int Dest, Color[] pixels, int w, int h)
        {
            const double oo = 10000000000000000000;

            int length = pixels.Length;

            List <double> Distance = new List <double>();

            Distance = Enumerable.Repeat(oo, length).ToList();

            List <int> Previous = new List <int>();

            Previous = Enumerable.Repeat(-1, length).ToList( );

            PeriorityQueue <Edge> MinimumDistances = new PeriorityQueue <Edge>();

            MinimumDistances.Push(new Edge(-1, Source, 0));

            while (!MinimumDistances.IsEmpty( ))
            {
                Edge CurrentEdge = MinimumDistances.Top();
                MinimumDistances.Pop( );

                if (CurrentEdge.Weight >= Distance [CurrentEdge.To])
                {
                    continue;
                }

                Previous [CurrentEdge.To] = CurrentEdge.From;
                Distance [CurrentEdge.To] = CurrentEdge.Weight;

                if (CurrentEdge.To == Dest)
                {
                    break;
                }

                List <Edge> neibours = Get_neighbours(CurrentEdge.To, pixels, w, h);
                for (int i = 0; i < neibours.Count; i++)
                {
                    Edge heldEdge = neibours[i];
                    if (Distance[heldEdge.To] > Distance[heldEdge.From] + heldEdge.Weight)
                    {
                        heldEdge.Weight = Distance [heldEdge.From] + heldEdge.Weight;
                        MinimumDistances.Push(heldEdge);
                    }
                }
            }

            return(Previous);
        }
        public static List <int> Dijkstra(int Source, Color [] pixels, int w, int h)
        {
            const double oo = 10000000000000000000; // infity value

            int length = pixels.Length;

            List <double> Distance = new List <double>();

            Distance = Enumerable.Repeat(oo, length).ToList( );

            List <int> Previous = new List <int>();

            Previous = Enumerable.Repeat(-1, length).ToList( );

            PeriorityQueue <Edge> MinimumDistances = new PeriorityQueue <Edge>();

            MinimumDistances.Push(new Edge(-1, Source, 0));

            while (!MinimumDistances.IsEmpty( ))
            {
                // get the shortest path so far
                Edge CurrentEdge = MinimumDistances.Top();
                MinimumDistances.Pop( );
                // check if this SP is vaild (i didn't vist this node with a less cost)
                if (CurrentEdge.Weight >= Distance [CurrentEdge.To])
                {
                    continue;
                }
                // save the previous
                Distance [CurrentEdge.To] = CurrentEdge.Weight;
                Previous [CurrentEdge.To] = CurrentEdge.From;
                // Relaxing
                List <Edge> neibours = Get_neighbours(CurrentEdge.To, pixels, w, h);
                for (int i = 0; i < neibours.Count; i++)
                {
                    Edge HeldEdge = neibours[i];
                    // if the relaxed path cost of a neighbour node is less than  it's previous one
                    if (Distance [HeldEdge.To] > Distance [HeldEdge.From] + HeldEdge.Weight)
                    {
                        // set the relaxed cost to Distance  && pash it to the PQ
                        HeldEdge.Weight = Distance [HeldEdge.From] + HeldEdge.Weight;
                        MinimumDistances.Push(HeldEdge);
                    }
                }
            }
            return(Previous);
        }
        public static List<int> Dijkstra(int Source, int dest, RGBPixel[,] ImageMatrix)
        {
            const double oo = 10000000000000000000; // infity value
            //Distance : the minimum cost between the source node and all the others nodes
            //initialized with infinty value
            int Width = ImageOperations.GetWidth(ImageMatrix);
            int Height = ImageOperations.GetHeight(ImageMatrix);
            int nodes_number = Width * Height;
            List<double> Distance = new List<double>();
            Distance = Enumerable.Repeat(oo, nodes_number).ToList();

            //Previous : saves the previous node that lead to the shortest path from the src node to current node 
            List<int> Previous = new List<int>();
            Previous = Enumerable.Repeat(-1, nodes_number).ToList();

            // PeriorityQueue : always return the shortest bath btween src node and specific node  
            PeriorityQueue MinimumDistances = new PeriorityQueue();
            MinimumDistances.Push(new Edge(-1, Source, 0));
            while (!MinimumDistances.IsEmpty())
            {
                // get the shortest path so far 
                Edge CurrentEdge = MinimumDistances.Top();
                MinimumDistances.Pop();
                // check if this SP is vaild (i didn't vist this node with a less cost)
                if (CurrentEdge.Weight >= Distance[CurrentEdge.To])
                    continue;
                // save the previous 
                Previous[CurrentEdge.To] = CurrentEdge.From;
                Distance[CurrentEdge.To] = CurrentEdge.Weight;
                if (CurrentEdge.To == dest) break;
                // Relaxing 
                List<Edge> neibours = GraphOperations.Get_neighbours(CurrentEdge.To, ImageMatrix);
                for (int i = 0; i < neibours.Count; i++)
                {
                    Edge HeldEdge = neibours[i];
                    // if the relaxed path cost of a neighbour node is less than  it's previous one
                    if (Distance[HeldEdge.To] > Distance[HeldEdge.From] + HeldEdge.Weight)
                    {
                        // set the relaxed cost to Distance  && pash it to the PQ
                        HeldEdge.Weight = Distance[HeldEdge.From] + HeldEdge.Weight;
                        MinimumDistances.Push(HeldEdge);
                    }
                }
            }
            return Previous;  // re turn th shortest paths from src to all nodes
        }