Example #1
0
        public void dijkstra2(Dictionary <long, LinkedList <Node3> > g, long start, long end)
        {
            Dictionary <long, double> dist = new Dictionary <long, double>();
            Dictionary <long, Node3>  prev = new Dictionary <long, Node3>();

            foreach (KeyValuePair <long, LinkedList <Node3> > entry in g)
            {
                dist.Add(entry.Key, 0.0);
                prev.Add(entry.Key, null);
            }
            dist[start] = 1.0;

            PriorityQueue pq = new PriorityQueue();

            pq.insertOrChange(new Node3(start), 1.0);
            pq.firstTime();

            while (!pq.isEmpty())
            {
                Node3 u = pq.deleteMax();


                // Ignore this if we've already seen it
                if (Math.Abs(u.factor - dist[u.next]) > 0.00000000000000001)
                {
                    pq.decrease();
                    continue;
                }


                LinkedList <Node3> edges = g[u.next];
                foreach (Node3 v in edges)
                {
                    //if (dist[v.next] > dist[u.myName] * v.factor)
                    //{
                    //    dist[v.next] = dist[u.myName] * v.factor;
                    //    prev[v.next] = u;
                    //    pq.insertOrChange(v, dist[v.next]);
                    //}
                    //if (u.factor * v.factor > dist[v.next])
                    //{
                    //    dist[v.next] = u.factor * v.factor;
                    //    pq.insertOrChange(new Node3(v.next), dist[v.next]);
                    //}
                    if (dist[v.next] < u.factor * v.factor)
                    {
                        dist[v.next] = u.factor * v.factor;
                        pq.insertOrChange(new Node3(v.next), dist[v.next]);
                    }
                }
            }
            decimal d = Convert.ToDecimal(dist[end]);

            Console.WriteLine(string.Format("{0:f4}", d));
        }
Example #2
0
        public void insertOrChange(Node3 v, double w)
        {
            //if (hash.Contains(v))
            //{
            //    v.factor = w;
            //}
            //else
            //{
            //    v.factor = w;
            //    list.Add(v);
            //    hash.Add(v);
            //}

            first++;


            //if (first == 1)
            //{
            //    firstMax = v;
            //    firstMax.factor = w;
            //    max = new Node3();
            //    maxes.Add(firstMax);
            //}
            //else if (w > max.factor || !maxes.Contains(max))
            //{
            //    max = v;
            //    max.factor = w;
            //    maxes.Add(max);
            //}


            // if it's a new max or greater than our old max update
            if (w > max.factor || !maxes.Contains(max))
            {
                max        = v;
                max.factor = w;
                maxes.Add(max);
            }


            size++;
            v.factor = w;
            list.Add(v);
        }