Beispiel #1
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);
        }
        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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public PQHandle Insert(TValue value)
        {
            if (_initialized)
            {
                return _heap.Insert(value);
            }

            int curr = _size;
            if (++_size >= _max)
            {
                _max <<= 1;
                Array.Resize(ref _keys, _max);
            }

            _keys[curr] = value;
            return new PQHandle { _handle = -(curr + 1) };
        }
Beispiel #5
0
        public PQHandle Insert(TValue value)
        {
            if (_initialized)
            {
                return(_heap.Insert(value));
            }

            int curr = _size;

            if (++_size >= _max)
            {
                _max <<= 1;
                _keys.AddRange(new TValue[_max - _keys.Count]);
            }

            _keys[curr] = value;
            return(new PQHandle {
                _handle = -(curr + 1)
            });
        }
    public PQHandle Insert(TValue value)
    {
        switch (_initialized)
        {
        case true:
            return(_heap.Insert(value));
        }

        int curr = _size;

        if (++_size >= _max)
        {
            _max <<= 1;
            Array.Resize(ref _keys, _max);
        }

        _keys[curr] = value;
        return(new PQHandle {
            _handle = -(curr + 1)
        });
    }