コード例 #1
0
ファイル: Program.cs プロジェクト: IvayloIV/Algorithms
        private static int GetMinBudget(int budget)
        {
            int currentBudget = 0;

            while (priorityQueue.Count > 0)
            {
                var current = priorityQueue.First();
                priorityQueue.Remove(current);

                if (currentBudget + current.Value > budget)
                {
                    break;
                }

                if ((connectedCables.Contains(current.Start) && !connectedCables.Contains(current.End) ||
                     connectedCables.Contains(current.Start) && !connectedCables.Contains(current.End)))
                {
                    currentBudget += current.Value;
                    var unmark = current.Start;

                    if (!connectedCables.Contains(current.End))
                    {
                        unmark = current.End;
                    }

                    connectedCables.Add(unmark);
                    AddChildToQueue(unmark);
                }
            }

            return(currentBudget);
        }
コード例 #2
0
ファイル: Loss.cs プロジェクト: omaremadeldin/fastText.Net
        private void FindKBest(
            int k,
            float threshold,
            Predictions predictions,
            float[] output)
        {
            var heap = new OrderedBag <Tuple <float, int> >(
                predictions,
                new Comparison <Tuple <float, int> >((l, r) =>
            {
                var b = l.Item1 > r.Item1;
                return(b ? 1 : 0);
            }));

            for (int i = 0; i < output.Length; i++)
            {
                if (output[i] < threshold)
                {
                    continue;
                }

                if (heap.Count == k && StdLog(output[i]) < heap.First().Item1)
                {
                    continue;
                }

                heap.Add(Tuple.Create(StdLog(output[i]), i));

                if (heap.Count > k)
                {
                    heap.RemoveFirst();
                }
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: PavelAndonov00/Softuni
        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}");
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: IvayloIV/Algorithms
        private static void FindMostReliablePath(int startPoint, int endPoint)
        {
            priorityQueue = new OrderedBag <int>(
                Comparer <int> .Create((a, b) => bestPathToNode[b].CompareTo(bestPathToNode[a])));
            bestPathToNode = new double[graph.Length];
            prev           = new int[graph.Length];
            HashSet <int> visited = new HashSet <int>();

            bestPathToNode[startPoint] = 1;
            priorityQueue.Add(startPoint);
            visited.Add(startPoint);

            for (int i = 0; i < prev.Length; i++)
            {
                prev[i] = i;
            }

            while (priorityQueue.Count > 0)
            {
                int current = priorityQueue.First();
                priorityQueue.Remove(current);

                foreach (var child in graph[current])
                {
                    double currentBestPath = bestPathToNode[child.End];

                    double newBestPath = bestPathToNode[child.Start] * child.WeightByPercentage;
                    if (currentBestPath < newBestPath)
                    {
                        bestPathToNode[child.End] = newBestPath;
                        prev[child.End]           = child.Start;
                    }

                    if (!visited.Contains(child.End))
                    {
                        priorityQueue.Add(child.End);
                        visited.Add(child.End);
                    }
                }
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: aquaruiz/SoftUni-1
        private static void Prim(int startingNode)
        {
            spanningTree.Add(startingNode);
            var priorityQueue = new OrderedBag <Edge>(Comparer <Edge> .Create((f, s) => f.Weight - s.Weight));

            priorityQueue.AddMany(nodeToEdges[startingNode]);

            while (priorityQueue.Count != 0)
            {
                var minEdge = priorityQueue.First();
                priorityQueue.Remove(minEdge);

                var nonTreeNode = -1;

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

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

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

                spanningTree.Add(nonTreeNode);
                Console.WriteLine($"{minEdge.First} - {minEdge.Second}");
                priorityQueue.AddMany(nodeToEdges[nonTreeNode]);
            }
        }
コード例 #6
0
 public NodeModelModel Peek()
 {
     return(priorityQueue.First());
 }