Esempio n. 1
0
        private static Dictionary <int, int> Prim(int startNode)
        {
            // node -> depth
            var tree = new Dictionary <int, int>
            {
                { startNode, 1 }
            };

            var queue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((f, s) => f.Distance - s.Distance));

            queue.AddMany(graph[startNode]);

            while (queue.Count > 0)
            {
                var edge = queue.RemoveFirst();

                var nonTreeNode = GetNonTreeNode(tree, edge);

                if (nonTreeNode == -1)
                {
                    continue;
                }

                var treeNode = GetTreeNode(tree, edge);

                // node -> depth
                tree.Add(nonTreeNode, tree[treeNode] + 1);

                queue.AddMany(graph[nonTreeNode]);
            }

            return(tree);
        }
        private static void PrimAlgorithm(int startNode)
        {
            priorityQueue.AddMany(edges.Where(e => e.to == startNode || e.@from == startNode));
            while (priorityQueue.Count != 0)
            {
                Edge   currentEdge = priorityQueue.RemoveFirst();
                string current     = $"{currentEdge.@from} {currentEdge.to} {currentEdge.weight}";
                if (!visited[currentEdge.to] || !visited[currentEdge.@from])
                {
                    if (currentDamage[currentEdge.@from] != 0)
                    {
                        currentDamage[currentEdge.to] = currentDamage[currentEdge.@from] / 2;
                    }
                    else if (currentDamage[currentEdge.to] != 0)
                    {
                        currentDamage[currentEdge.@from] = currentDamage[currentEdge.to] / 2;
                    }
                    var destination = visited[currentEdge.@from] ? currentEdge.to : currentEdge.@from;
                    visited[currentEdge.to]    = true;
                    visited[currentEdge.@from] = true;

                    priorityQueue.AddMany(edges.Where(e => e.to == destination || e.@from == destination));
                }
            }

            for (int i = 0; i < nodesCount; i++)
            {
                damage[i] += currentDamage[i];
            }
        }
Esempio n. 3
0
        private static void Prim(int node)
        {
            spanningTreeNodes.Add(node);
            var priorityQueue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            priorityQueue.AddMany(edges.Where(e => e.First == node || e.Second == node));
            while (priorityQueue.Count > 0)
            {
                var minEdge = priorityQueue.First();
                priorityQueue.Remove(minEdge);
                var firstNode  = minEdge.First;
                var secondNode = minEdge.Second;

                int nonTreeNode = -1;
                if (spanningTreeNodes.Contains(firstNode) && !spanningTreeNodes.Contains(secondNode))
                {
                    nonTreeNode = secondNode;
                }
                else if (!spanningTreeNodes.Contains(firstNode) && spanningTreeNodes.Contains(secondNode))
                {
                    nonTreeNode = firstNode;
                }

                if (nonTreeNode != -1)
                {
                    spanningTreeNodes.Add(nonTreeNode);
                    priorityQueue.AddMany(edges.Where(e => e.First == nonTreeNode || e.Second == nonTreeNode));
                    Console.WriteLine($"{firstNode} - {secondNode}");
                }
            }
        }
Esempio n. 4
0
        private static void Prim()
        {
            var queue = new OrderedBag <Edge>
                            (Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            foreach (var node in forest)
            {
                queue.AddMany(graph[node]);
            }
            while (queue.Count > 0)
            {
                var edge = queue.RemoveFirst();
                if (leftBudget - edge.Weight < 0)
                {
                    break;
                }

                var nonTreeNode = FindNonTreeNode
                                      (edge.First, edge.Second);
                if (nonTreeNode != -1)
                {
                    forest.Add(nonTreeNode);
                    leftBudget -= edge.Weight;
                    queue.AddMany(graph[nonTreeNode]);
                }
            }
        }
Esempio n. 5
0
        public static void Prim(int startNode, int damage)
        {
            var queue = new OrderedBag <Edge>();

            HashSet <int>          spanningTree = new HashSet <int>();
            Dictionary <int, long> currentDmgs  = new Dictionary <int, long>();

            currentDmgs[startNode] = damage;
            damages[startNode]    += damage;
            spanningTree.Add(startNode);
            queue.AddMany(graph[startNode]);

            while (queue.Count > 0)
            {
                var minEdge = queue.GetFirst();
                queue.RemoveFirst();

                int firstNode  = minEdge.StartNode;
                int secondNode = minEdge.EndNode;

                int treeNode    = -1;
                int nonTreeNode = -1;

                if (spanningTree.Contains(firstNode) &&
                    !spanningTree.Contains(secondNode))
                {
                    treeNode    = firstNode;
                    nonTreeNode = secondNode;
                }
                else if (spanningTree.Contains(secondNode) &&
                         !spanningTree.Contains(firstNode))
                {
                    treeNode    = secondNode;
                    nonTreeNode = firstNode;
                }
                else if (nonTreeNode == -1)
                {
                    continue;
                }

                spanningTree.Add(nonTreeNode);
                if (!currentDmgs.ContainsKey(nonTreeNode))
                {
                    currentDmgs[nonTreeNode] = 0;
                }
                currentDmgs[nonTreeNode] = currentDmgs[treeNode] / 2;
                damages[nonTreeNode]    += currentDmgs[nonTreeNode];
                queue.AddMany(graph[nonTreeNode]);
                //if (currentDmgs[nonTreeNode] == 0)
                //{
                //    break;
                //}
            }
        }
Esempio n. 6
0
        static void Prim(int startingNode)
        {
            visitedNodes.Add(startingNode);

            var priorityQueue =
                new OrderedBag <Edge>(Comparer <Edge> .Create((first, second) => first.Weight - second.Weight));

            //get child edges of starting node
            var startingNodeChildEdges = childEdges[startingNode];

            //add child edges to priority queue
            priorityQueue.AddMany(startingNodeChildEdges);

            //while priority queue is not empty
            while (priorityQueue.Count > 0)
            {
                //get min edge by weight
                var minEdge = priorityQueue.GetFirst();
                priorityQueue.Remove(minEdge);

                //check if minEdge will cause cycle
                //to not cause a cycle one node must be in tree, the other not
                var first       = minEdge.FirstNode;
                var second      = minEdge.SecondNode;
                var nonTreeNode = -1;

                if (visitedNodes.Contains(first) && !visitedNodes.Contains(second))
                {
                    nonTreeNode = second;
                }

                if (visitedNodes.Contains(second) && !visitedNodes.Contains(first))
                {
                    nonTreeNode = first;
                }

                if (nonTreeNode == -1)
                {
                    continue;                    //both nodes are in visited -> cause cycle
                }
                //add edge to spanning tree
                spanningTree.Add(minEdge);
                //print edge
                Console.WriteLine($"{minEdge.FirstNode} - {minEdge.SecondNode}");

                visitedNodes.Add(nonTreeNode);

                //enqueue all child nodes of nonTreeNode to priority queue
                priorityQueue.AddMany(childEdges[nonTreeNode]);
            }
        }
Esempio n. 7
0
        static void Prim()
        {
            //initialize priority queue and its comparer with sorted rules
            var queue = new OrderedBag <Edge>(Comparer <Edge> .Create((a, b) => a.Cost - b.Cost));

            //all alredy connected nodes -> add their childs in priority queue
            queue.AddMany(spanningTree.SelectMany(x => graph[x]));

            //while priority queue is not empty
            while (queue.Count > 0)
            {
                //get smallest edge by cost
                var min = queue.RemoveFirst();

                //check if min node connects tree node with non tree node
                var nonTreeNode = -1;
                if (spanningTree.Contains(min.First) && !spanningTree.Contains(min.Second)) //first case
                {
                    nonTreeNode = min.Second;
                }
                if (spanningTree.Contains(min.Second) && !spanningTree.Contains(min.First)) //second case
                {
                    nonTreeNode = min.First;
                }

                //no need of this edge because if connects two tree nodes(already connected) or two non tree nodes (non node of this edge is not connected to existing network)
                if (nonTreeNode == -1)
                {
                    continue;
                }

                //check if cost of new edge to be connected fit in budget
                if (budget >= min.Cost)
                {
                    budget     -= min.Cost;
                    usedBudget += min.Cost;
                }
                else
                {
                    break;
                }

                //add nonTree node to already existing network
                spanningTree.Add(nonTreeNode);

                //add its child edges to priority queue
                queue.AddMany(graph[nonTreeNode]);
            }
        }
Esempio n. 8
0
        public IEnemy[] GetOrderedByHealth()
        {
            var newLegion = new OrderedBag <IEnemy>(CompareElements);

            newLegion.AddMany(this.legion);
            return(newLegion.ToArray());
        }
Esempio n. 9
0
        private static Dictionary <int, int> GetStriked(int startNode)
        {
            var striked = new Dictionary <int, int>();

            striked.Add(startNode, 1);

            var queue = new OrderedBag <Edge>(graph[startNode],
                                              Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            while (queue.Count > 0)
            {
                var edge        = queue.RemoveFirst();
                var nonTreeNode = GetNonTreeNode(edge.First, edge.Second, striked);
                if (nonTreeNode != -1)
                {
                    var treeNode = -1;
                    if (nonTreeNode == edge.First)
                    {
                        treeNode = edge.Second;
                    }
                    else
                    {
                        treeNode = edge.First;
                    }
                    int depth = striked[treeNode];
                    striked.Add(nonTreeNode, depth + 1);
                    queue.AddMany(graph[nonTreeNode]);
                    queue = new OrderedBag <Edge>(queue,
                                                  Comparer <Edge>
                                                  .Create((f, s) => f.Weight - s.Weight));
                }
            }
            return(striked);
        }
Esempio n. 10
0
        private static void Prim(int node)
        {
            forest.Add(node);

            var queue = new OrderedBag <Edge>(
                edgesByNode[node],
                Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            while (queue.Count > 0)
            {
                var edge = queue.RemoveFirst();

                var nonTreeNode = GetNonTreeNode(edge.First, edge.Second);

                if (nonTreeNode == -1)
                {
                    continue;
                }

                Console.WriteLine($"{edge.First} - {edge.Second}");

                forest.Add(nonTreeNode);
                queue.AddMany(edgesByNode[nonTreeNode]);
            }
        }
Esempio n. 11
0
        private static List <Edge> Prim(int node)
        {
            List <Edge> spanningTree = new List <Edge>();

            spanningTreeNodes.Add(node);
            OrderedBag <Edge> prioriryQueue = new OrderedBag <Edge>(nodesByEdges[node]);

            while (prioriryQueue.Count > 0)
            {
                Edge edge = prioriryQueue.GetFirst();
                prioriryQueue.RemoveFirst();
                int nextNode = -1;

                if (spanningTreeNodes.Contains(edge.StartNode) && !spanningTreeNodes.Contains(edge.EndNode))
                {
                    nextNode = edge.EndNode;
                }

                if (spanningTreeNodes.Contains(edge.EndNode) && !spanningTreeNodes.Contains(edge.StartNode))
                {
                    nextNode = edge.StartNode;
                }

                if (nextNode == -1)
                {
                    continue;
                }

                spanningTree.Add(edge);
                spanningTreeNodes.Add(nextNode);
                prioriryQueue.AddMany(nodesByEdges[nextNode]);
            }

            return(spanningTree);
        }
Esempio n. 12
0
        private static void Prim()
        {
            var queue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((f, s) => f.Cost - s.Cost));

            queue.AddMany(spanningTree.SelectMany(s => graph[s]));

            while (queue.Count != 0)
            {
                var min = queue.RemoveFirst();

                var nonTreeNode = -1;

                if (spanningTree.Contains(min.First) &&
                    !spanningTree.Contains(min.Second))
                {
                    nonTreeNode = min.Second;
                }

                if (spanningTree.Contains(min.Second) &&
                    !spanningTree.Contains(min.First))
                {
                    nonTreeNode = min.First;
                }

                if (nonTreeNode == -1)
                {
                    continue;
                }

                if (totalBudget >= min.Cost)
                {
                    totalBudget -= min.Cost;
                    usedBudget  += min.Cost;
                }
                else
                {
                    break;
                }

                spanningTree.Add(nonTreeNode);
                queue.AddMany(graph[nonTreeNode]);
            }
        }
Esempio n. 13
0
        static void Prim(int startingNode)
        {
            spanningTree.Add(startingNode);

            var prioriryQueue = new OrderedBag <Edge>(Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            prioriryQueue.AddMany(nodeToEdges[startingNode]);

            while (prioriryQueue.Count != 0)
            {
                var minEdge = prioriryQueue.GetFirst();
                prioriryQueue.Remove(minEdge);

                var firstNode  = minEdge.First;
                var secondNode = minEdge.Second;

                var nonTreeNode = -1;

                if (spanningTree.Contains(firstNode) &&
                    !spanningTree.Contains(secondNode))
                {
                    nonTreeNode = secondNode;
                }

                if (spanningTree.Contains(secondNode) &&
                    !spanningTree.Contains(firstNode))
                {
                    nonTreeNode = firstNode;
                }

                if (nonTreeNode == -1)
                {
                    continue;
                }

                spanningTree.Add(nonTreeNode);

                Console.WriteLine($"{minEdge.First} - {minEdge.Second}");

                prioriryQueue.AddMany(nodeToEdges[nonTreeNode]);
            }
        }
        public IEnumerable <Product> FindByPriceRange(double from, double to)
        {
            OrderedBag <Product> ob = new OrderedBag <Product>();

            foreach (var bag in byPrice.Range(from, true, to, true))
            {
                ob.AddMany(bag.Value);
            }

            return(ob);
        }
Esempio n. 15
0
        private static void Prim()
        {
            var queue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((a, b) => a.Cost - b.Cost));

            queue.AddMany(spanningTree.SelectMany(n => graph[n])); // starting nodes existing network

            while (queue.Any())
            {
                var minEdge     = queue.RemoveFirst();
                var nonTreeNode = -1;

                if (spanningTree.Contains(minEdge.First) &&
                    !spanningTree.Contains(minEdge.Second))
                {
                    nonTreeNode = minEdge.Second;
                }

                if (!spanningTree.Contains(minEdge.First) &&
                    spanningTree.Contains(minEdge.Second))
                {
                    nonTreeNode = minEdge.First;
                }

                if (nonTreeNode == -1)
                {
                    continue;
                }

                if (availableBudget < minEdge.Cost)
                {
                    break;
                }

                spanningTree.Add(nonTreeNode);
                queue.AddMany(graph[nonTreeNode]);

                availableBudget -= minEdge.Cost;
                usedBudget      += minEdge.Cost;
            }
        }
Esempio n. 16
0
        private static void Prim()
        {
            var queue = new OrderedBag <Edge>(Comparer <Edge> .Create((a, b) => a.Cost - b.Cost));

            queue.AddMany(connectedNodes.SelectMany(cn => graph[cn]));

            while (queue.Count > 0)
            {
                Edge minCostEdge = queue.RemoveFirst();
                int  first       = minCostEdge.First;
                int  second      = minCostEdge.Second;
                int  cost        = minCostEdge.Cost;

                int notThreeNode = -1;

                if (connectedNodes.Contains(first) && !connectedNodes.Contains(second))
                {
                    notThreeNode = second;
                }

                if (connectedNodes.Contains(second) && !connectedNodes.Contains(first))
                {
                    notThreeNode = first;
                }

                if (notThreeNode == -1)
                {
                    continue;
                }

                if (currentBudget < cost)
                {
                    break;
                }

                currentBudget -= cost;

                connectedNodes.Add(notThreeNode);
                queue.AddMany(graph[notThreeNode]);
            }
        }
Esempio n. 17
0
        private static void Prim(int startingNode)
        {
            visited.Add(startingNode);

            var priorityQueue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            priorityQueue.AddMany(graph[startingNode]);

            while (priorityQueue.Any())
            {
                var minEdge = priorityQueue.GetFirst();
                priorityQueue.Remove(minEdge);

                // Connect a non-tree node to the spanning tree, avoiding a cycle
                var nonTreeNode = -1;

                if (visited.Contains(minEdge.Start) &&
                    !visited.Contains(minEdge.End))
                {
                    nonTreeNode = minEdge.End;
                }

                if (!visited.Contains(minEdge.Start) &&
                    visited.Contains(minEdge.End))
                {
                    nonTreeNode = minEdge.Start;
                }

                if (nonTreeNode == -1)
                {
                    continue;
                }

                // Connect non-tree node & spanning tree
                minSpanningTree.Add(minEdge);

                visited.Add(nonTreeNode);
                priorityQueue.AddMany(graph[nonTreeNode]);
            }
        }
        public static void Main()
        {
            var bag = new OrderedBag<Product>();

            var products = ProductsGenerator.Generate(500000);

            bag.AddMany(products);

            PrintProductsInRange(20, 1000, bag);

            MakeSearches(10000, 1, 5000, bag);
        }
        public static void Main()
        {
            var bag = new OrderedBag <Product>();

            var products = ProductsGenerator.Generate(500000);

            bag.AddMany(products);

            PrintProductsInRange(20, 1000, bag);

            MakeSearches(10000, 1, 5000, bag);
        }
 private static void PrimAlgorithm()
 {
     while (visited.Count(e => e == false) != 0)
     {
         int currentNode = Array.FindIndex(visited, w => w == false);
         queue.AddMany(edges.Where(e => e.first == currentNode || e.second == currentNode));
         while (queue.Count != 0)
         {
             Edge currentEdge = queue.RemoveFirst();
             if (!visited[currentEdge.first] || !visited[currentEdge.second])
             {
                 MSTResult.Add(new Edge {
                     first = currentEdge.first, second = currentEdge.second, weight = currentEdge.weight
                 });
                 visited[currentEdge.first]  = true;
                 visited[currentEdge.second] = true;
                 currentNode = currentNode == currentEdge.first ? currentEdge.second : currentEdge.second;
                 queue.AddMany(edges.Where(e => e.first == currentNode || e.second == currentNode));
             }
         }
     }
 }
        public OrderedBag <Product> FindByPriceRange(double from, double to)
        {
            var result = new OrderedBag <Product>();

            var found = this.Prices.FindAll(x => x.Key >= from && x.Key <= to);

            foreach (var price in found)
            {
                result.AddMany(price.Value);
            }

            return(result);
        }
Esempio n. 22
0
        private static int Prim(int budget)
        {
            var used  = 0;
            var queue = new OrderedBag <Edge>(Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            foreach (var node in _spanningTree)
            {
                queue.AddMany(_graph[node]);
            }
            while (queue.Count > 0)
            {
                var edge        = queue.RemoveFirst();
                var nonTreeNode = -1;
                if (_spanningTree.Contains(edge.First) && !_spanningTree.Contains(edge.Second))
                {
                    nonTreeNode = edge.Second;
                }
                if (!_spanningTree.Contains(edge.First) && _spanningTree.Contains(edge.Second))
                {
                    nonTreeNode = edge.First;
                }

                if (nonTreeNode == -1)
                {
                    continue;
                }
                if (edge.Weight > budget)
                {
                    break;
                }

                used   += edge.Weight;
                budget -= edge.Weight;
                _spanningTree.Add(nonTreeNode);
                queue.AddMany(_graph[nonTreeNode]);
            }
            return(used);
        }
Esempio n. 23
0
        public static void IncreaseNetwork()
        {
            var queue = new OrderedBag <Edge>();

            queue.AddMany(spanningTree.SelectMany(x => graph[x]));

            while (queue.Count > 0 && currentBudget > 0)
            {
                var minEdge = queue.GetFirst();
                queue.RemoveFirst();

                int firstNode  = minEdge.StartNode;
                int secondNode = minEdge.EndNode;

                int nonTreeNode = -1;

                if (spanningTree.Contains(firstNode) &&
                    !spanningTree.Contains(secondNode) &&
                    currentBudget - minEdge.Cost >= 0)
                {
                    nonTreeNode = secondNode;
                }
                else if (spanningTree.Contains(secondNode) &&
                         !spanningTree.Contains(firstNode) &&
                         currentBudget - minEdge.Cost >= 0)
                {
                    nonTreeNode = firstNode;
                }
                else if (nonTreeNode == -1)
                {
                    continue;
                }

                spanningTree.Add(nonTreeNode);
                queue.AddMany(graph[nonTreeNode]);
                currentBudget -= minEdge.Cost;
            }
        }
Esempio n. 24
0
        private static int Prim(int budget)
        {
            var usedBudget = 0;

            var queue = new OrderedBag <Edge>(
                Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            foreach (var node in spanningTree)
            {
                queue.AddMany(graph[node]);
            }

            while (queue.Count > 0)
            {
                var edge = queue.RemoveFirst();

                var nonTreeNode = GetNonTreeNode(edge.First, edge.Second);

                if (nonTreeNode == -1)
                {
                    continue;
                }

                if (edge.Weight > budget)
                {
                    break;
                }

                usedBudget += edge.Weight;
                budget     -= edge.Weight;

                spanningTree.Add(nonTreeNode);
                queue.AddMany(graph[nonTreeNode]);
            }

            return(usedBudget);
        }
Esempio n. 25
0
        public void EnsureUtcTimes()
        {
            // a bug in developer version caused wrong sorting of laps in some cases
            // this code handles the issue (which should be really rare)
            var utcLaps = new List <Lap>();

            foreach (var lap in this)
            {
                lap.Time = lap.Time.ToUniversalTime();
                utcLaps.Add(lap);
            }
            Clear();

            laps.AddMany(utcLaps);
        }
Esempio n. 26
0
        private static void Prim(int nhood)
        {
            visited[nhood] = true;
            var priorQueue = new OrderedBag <Edge>();

            priorQueue.AddMany(nhoods[nhood]);

            while (priorQueue.Count > 0)
            {
                var currEdge = priorQueue.RemoveFirst();
                int source;
                int dest;

                if (visited[currEdge.FirstNhood] && !visited[currEdge.SecondNhood])
                {
                    source = currEdge.FirstNhood;
                    dest   = currEdge.SecondNhood;
                }
                else if (!visited[currEdge.FirstNhood] && visited[currEdge.SecondNhood])
                {
                    source = currEdge.SecondNhood;
                    dest   = currEdge.FirstNhood;
                }
                else
                {
                    continue;
                }

                visited[dest] = true;

                msf[source].Add(dest);
                msf[dest].Add(source);

                priorQueue.AddMany(nhoods[dest]);
            }
        }
        public static List<Edge<int>> GetMinimalspanningTreeUsingPrim(List<Edge<int>> graph, int start)
        {
            var result = new List<Edge<int>>();
            var priority = new OrderedBag<Edge<int>>();
            var visited = new HashSet<int>();
            visited.Add(start);
            foreach (var edge in graph)
            {
                if (edge.Start == start || edge.End == start)
                {
                    priority.Add(edge);
                }
            }

            while (priority.Count > 0)
            {
                var current = priority.RemoveFirst();
                if (!(visited.Contains(current.Start) && visited.Contains(current.End)))
                {
                    result.Add(current);
                }

                if (visited.Contains(current.Start) && !visited.Contains(current.End))
                {
                    priority.AddMany(graph.Where(x => x.Start == current.End || x.End == current.End));
                    visited.Add(current.End);
                }
                else if (!visited.Contains(current.Start) && visited.Contains(current.End))
                {
                    priority.AddMany(graph.Where(x => x.Start == current.Start || x.End == current.Start));
                    visited.Add(current.Start);
                }
            }

            return result;
        }
Esempio n. 28
0
        private static int Prim(int budget)
        {
            var costedBudget = 0;
            var queue        = new OrderedBag <Node>
                                   (Comparer <Node> .Create((f, s) => f.Weight - s.Weight));

            foreach (var edge in spanningTree)
            {
                queue.AddMany(graph[edge]);
            }

            while (queue.Count > 0)
            {
                var curNod = queue.RemoveFirst();

                var actualNode = GetActualNode(curNod);

                if (actualNode == -1)
                {
                    continue;
                }

                if (budget < curNod.Weight)
                {
                    break;
                }

                budget       -= curNod.Weight;
                costedBudget += curNod.Weight;

                spanningTree.Add(actualNode);
                queue.AddMany(graph[actualNode]);
            }

            return(costedBudget);
        }
Esempio n. 29
0
        public void FindProductsByPriceRange(double fromPrice, double toPrice)
        {
            OrderedBag <Product> products = new OrderedBag <Product>();

            foreach (var product in this.byPrice)
            {
                if (product.Key >= fromPrice && product.Key <= toPrice)
                {
                    products.AddMany(product.Value);
                }
            }
            if (products.Count == 0)
            {
                Console.WriteLine("No products found");
                return;
            }
            Print(products);
        }
    /// <summary>
    /// Finds a product by price range
    /// </summary>
    /// <param name="lowerBoundary">lower price (inclusive)</param>
    /// <param name="upperBoundary">higher price (inclusive)</param>
    /// <returns>OrderedBag with results or null if no results</returns>
    public OrderedBag <Product> FindByPriceRange(decimal lowerBoundary, decimal upperBoundary)
    {
        var resultsRange = this.byPrice.Range(lowerBoundary, true, upperBoundary, true);

        if (resultsRange.Count == 0)
        {
            return(null);
        }

        OrderedBag <Product> resultsByPrice = new OrderedBag <Product>();

        foreach (var kvp in resultsRange)
        {
            resultsByPrice.AddMany(kvp.Value);
        }

        return(resultsByPrice);
    }
    public IEnumerable <Product> FindProductsByPriceRange(decimal startPrice, decimal endPrice)
    {
        var result = new OrderedBag <Product>();

        this.byPrice
        .Keys
        .Where(price => startPrice <= price && price <= endPrice)
        .ToList()
        .ForEach(price => result.AddMany(this.byPrice[price]));

        //// Worse performance Ordered Dictionary
        //this.byPrice
        //    .Range(startPrice, true, endPrice, true)
        //    .Values
        //    .ToList()
        //    .ForEach(productList => result.AddMany(productList));

        return(result);
    }
Esempio n. 32
0
        private static void Prim(int node)
        {
            forest.Add(node);
            var priorityQueue = new OrderedBag <Edge>
                                    (graph[node],
                                    Comparer <Edge> .Create((x, y) => x.Weight - y.Weight));

            while (priorityQueue.Count > 0)
            {
                var edge        = priorityQueue.RemoveFirst();
                var nonTreeNode = GetNonTreeNode(edge.First, edge.Second);

                if (nonTreeNode != -1)
                {
                    Console.WriteLine($"{edge.First} - {edge.Second}");
                    forest.Add(nonTreeNode);
                    priorityQueue.AddMany(graph[nonTreeNode]);
                }
            }
        }
        static void Main(string[] args)
        {
            var products = new OrderedBag<Product>();
            products.AddMany(
                new Product[] 
                {
                    new Product("Grabage", 24),
                    new Product("Grabage 2", 22),
                    new Product("Grabage 3", 290),
                    new Product("Grabage 4", 29)
                });
            var result = products.Range(
                new Product("", 20), true, 
                new Product("", 30), true);

            foreach (var item in result)
            {
                Console.WriteLine(item.ProductName);
            }

        }
Esempio n. 34
0
    private static int Dijkstra(Node[] graphMatrix, int hospital)
    {
        OrderedBag<Connection> streets = new OrderedBag<Connection>();

        Node currentNode = graphMatrix[hospital];
        currentNode.TempDistance = 0;
        currentNode.PermanentDistance = 0;
        currentNode.Visited = true;
        streets.AddMany(currentNode.ChildsList);

        while (streets.Count != 0) // streets.Count == num of streets
        {
            foreach (Connection child in currentNode.ChildsList)
            {
                if (graphMatrix[child.Node].TempDistance >
                    child.Value + currentNode.PermanentDistance)
                {
                    graphMatrix[child.Node].TempDistance =
                        child.Value + currentNode.PermanentDistance;
                }
            }

            int nextNode = streets.RemoveFirst().Node;
            if (graphMatrix[nextNode].Visited == false)
            {
                currentNode = graphMatrix[nextNode];
                streets.AddMany(currentNode.ChildsList);
                currentNode.PermanentDistance = currentNode.TempDistance; currentNode.Visited = true;
            }
        }

        int minDistance = 0;
        for (int i = 1; i < graphMatrix.Length; i++ )
        {
            if (!hospitals.Contains(graphMatrix[i].ID))
            {
                minDistance += graphMatrix[i].PermanentDistance;
            }
        }

        return minDistance;
    }