static void add(PriorityHeap lows, PriorityHeap highs, int a)
    {
        if (lows.Count() == 0)
        {
            lows.Add(a);
            return;
        }

        if (a < lows.Peek())
        {
            lows.Add(a);
            if (lows.Count() > highs.Count() + 1)
            {
                highs.Add(lows.Remove());
            }
        }
        else
        {
            highs.Add(a);
            if (highs.Count() > lows.Count() + 1)
            {
                lows.Add(highs.Remove());
            }
        }
    }
Exemple #2
0
        public static List <Edge> SpanningTree(Graph graph, int startNode)
        {
            // idea:
            // greedy approach,
            // iteratively grow spanning tree by respective shortest connection
            // reachable nodes are efficiently stored in a heap
            PriorityHeap distances = new PriorityHeap(graph.Nodes());

            // these are the edges that connect a new node with our already
            // constructed spanning tree.
            Edge[] connectingEdge = new Edge[graph.Nodes()];

            // store all reachable nodes with their distance.
            foreach (Edge edge in graph.Edges(startNode))
            {
                int w = edge.Other(startNode);
                distances.Insert(w, edge.Weight());
                connectingEdge[w] = edge;
            }

            // for all other nodes store an infinite distance.
            for (int v = 0; v < graph.Nodes(); v++)
            {
                if (v == startNode)
                {
                    continue;
                }
                if (!distances.Contains(v))
                {
                    distances.Insert(v, System.Double.PositiveInfinity);
                }
            }

            // these are the already selected edges
            List <Edge> spanningTree = new List <Edge>();

            while (!distances.Empty())
            {
                // get the nearest node that is not yet in the spanning tree.
                int v = distances.DeleteMin();
                // add its connecting edge to the spanning tree.
                spanningTree.Add(connectingEdge[v]);
                // consider all edges from v for the next step of the algorithm.
                foreach (Edge edge in graph.Edges(v))
                {
                    int w = edge.Other(v);
                    // If we still need the node at the other end of that
                    // edge and the edge weight is better than our currently
                    // stored edge, store the new edge.
                    if (distances.Contains(w) && edge.Weight() < distances.GetKey(w))
                    {
                        distances.Change(w, edge.Weight());
                        connectingEdge[w] = edge;
                    }
                }
            }

            return(spanningTree);
        }
        public PriorityQueue(int initialSize, PriorityHeap <TValue> .LessOrEqual leq)
        {
            _leq  = leq;
            _heap = new PriorityHeap <TValue>(initialSize, leq);

            _keys = new TValue[initialSize];

            _size        = 0;
            _max         = initialSize;
            _initialized = false;
        }
        private static IPriorityHeap <byte, string> GivenPriorityHeap()
        {
            var heap = new PriorityHeap <byte, string>();

            heap.Insert(5, "Red");
            heap.Insert(4, "Orange");
            heap.Insert(3, "Yellow");
            heap.Insert(2, "Green");
            heap.Insert(1, "Blue");
            return(heap);
        }
        public void Reset(int initialSize, PriorityHeap <TValue> .LessOrEqual leq)
        {
            _leq = leq;
            _heap.Reset(initialSize, leq);

            if (_keys.Count < initialSize)
            {
                _keys.AddRange(new TValue[initialSize - _keys.Count]);
            }

            _size        = 0;
            _max         = initialSize;
            _initialized = false;
        }
    static void Main(String[] args)
    {
        int n = Convert.ToInt32(Console.ReadLine());

        int[]        a     = new int[n];
        PriorityHeap lows  = new PriorityHeap(n, true);
        PriorityHeap highs = new PriorityHeap(n);

        for (int a_i = 0; a_i < n; a_i++)
        {
            a[a_i] = Convert.ToInt32(Console.ReadLine());
            add(lows, highs, a[a_i]);
            Console.WriteLine("{0:f1}", median(lows, highs));
        }
    }
 static decimal median(PriorityHeap lows, PriorityHeap highs)
 {
     if (lows.Count() == highs.Count())
     {
         return(((decimal)(lows.Peek() + highs.Peek())) / 2);
     }
     else if (lows.Count() > highs.Count())
     {
         return(lows.Peek());
     }
     else
     {
         return(highs.Peek());
     }
 }
Exemple #8
0
        public static List <int> GreedyTour(double[,] D, int v_0)
        {
            int V = D.GetLength(0);
            // erzeuge Tour
            List <int> tour = new List <int>(V + 1);

            var heap = new PriorityHeap(V);

            heap.Insert(v_0, 0);

            //start
            tour.Add(v_0);

            var lastNode = v_0;

            for (int i = 0; i < V - 1; i++)
            {
                int    shortestNode     = -1;
                double shortestDistance = Double.PositiveInfinity;
                for (int j = 0; j < V; j++)
                {
                    if (lastNode == j || heap.Contains(j))
                    {
                        continue;
                    }

                    if (shortestDistance > D[lastNode, j])
                    {
                        shortestNode     = j;
                        shortestDistance = D[lastNode, j];
                    }
                }

                lastNode = shortestNode;
                heap.Insert(shortestNode, shortestDistance);
                tour.Add(shortestNode);
            }

            //end
            tour.Add(v_0);

            return(tour);
        }
Exemple #9
0
 public NodePriorityHeap()
 {
     this.OpenHeap = new PriorityHeap <NodeRecord>();
 }
 public NodePriorityHeap()
 {
     this.OpenHeap = new PriorityHeap<NodeRecord>();
 }
Exemple #11
0
 public Agent()
 {
     _queuedActions = new PriorityHeap <float, IAction <TContext> >();
     _strategies    = new List <IStrategy <TContext> >(DEFAULT_STRATEGY_CAPACITY);
     _locked        = false;
 }
 public LocationPriorityHeap()
 {
     this.OpenHeap = new PriorityHeap <LocationRecord>();
 }