Блог пользователя nchn27

Автор nchn27, история, 7 лет назад, По-английски

In use, multiset seems more versatile than priority queue due to the fact that you can remove things other than the first element. In C++, when would priority_queue be used instead of multiset?

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

Priority_queue is faster, but usually for competitive programming using multiset is better since it is more convenient and for problems that arent very tight it makes no difference.

  • »
    »
    7 лет назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится -66 Проголосовать: не нравится

    How is Priority_queue faster than a multiset?

    Wont Deletion through an iterator take constant time in a multiset where as pop takes log(n) time in priority queue

    • »
      »
      »
      7 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +20 Проголосовать: не нравится

      I don't know the implementations in C++ nor have I benchmarked the structures, but I would definitely expect priority queue to be faster, maybe even a lot faster. Why would you expect otherwise?

      The functions of a priority queue are a subset of the functions of a multiset, if the latter is faster then the former is useless.

      You can implement a priority queue through a heap structure, while for a multiset you need a balanced binary tree structure, which is significantly heavier.

      • »
        »
        »
        »
        7 лет назад, скрыть # ^ |
         
        Проголосовать: нравится -56 Проголосовать: не нравится

        Agreed, but Wont Deletion through an iterator take constant time in a multiset where as pop takes log(n) time in priority queue

        • »
          »
          »
          »
          »
          7 лет назад, скрыть # ^ |
           
          Проголосовать: нравится +16 Проголосовать: не нравится

          It may be so, but that's not a great way to look at it. If you add $$$N$$$ elements and then remove them all, both priority queue and multiset will be $$$O(N log N)$$$ (and in fact no structure is faster than that, because you'd break the sorting complexity lower bound). Even if multiset is quicker in the deletion part, it likely pays a price for that during the addition part.

          It may be interesting to properly compare them, but I'd be quite surprised if multiset turns out to be faster, as my experience has showed that set/map in C++ are incredibly heavy.

        • »
          »
          »
          »
          »
          7 лет назад, скрыть # ^ |
          ← Rev. 2  
          Проголосовать: нравится +8 Проголосовать: не нравится

          As far as I know, multiset is implemented as a balanced binary tree as Enchom mentioned. Since it should be 'balanced' after insertion and deletion, it has to do some 're-balancing' (algorithms can vary by implementation, but generally rotating trees). Unless it does that job, there might be some possbile occasions which a sequence of insertion and deletion makes multiset's internal implementation tree unbalanced and makes operation after that very slow (up to O(n))

          To sum up, priority queue is a heap structure, which takes $$$O(\log n)$$$ time to delete top element. Multiset is a balanced binary search tree, which takes up to $$$O(\log n)$$$ time to delete anything and then assuring balance. Latter can be a lot slower (bigger constant factor).

          Currently priority queue is somewhere 1.5x to 2x faster than multiset with compiler optimizations. (Depends on if you turned -O2 on, and bunch of other factors, including your compiler version and OS) There are some benchmarks in stackoverflow, which I haven't throughly read yet.

          Edit : my friend pointed out that my numbers are wrong.

          • »
            »
            »
            »
            »
            »
            7 лет назад, скрыть # ^ |
            ← Rev. 2  
            Проголосовать: нравится +24 Проголосовать: не нравится

            According to this reference, he is actually right that deletion is amortized constant. I ran a few quick tests and indeed multiset deletion seems faster than priority queue deletion.

            However, as I suspected and mentioned in my other comment, this is at the cost of a very slow addition in multiset compared to priority queue. Since you can't delete more elements than you add, I can't imagine a scenario where you'd prefer multiset only due to the faster deletion.

            • »
              »
              »
              »
              »
              »
              »
              7 лет назад, скрыть # ^ |
               
              Проголосовать: нравится +1 Проголосовать: не нравится

              Oh, I misread the question and considered erasing by value. I was talking about wrong thing then. Thanks for pointing it out.

            • »
              »
              »
              »
              »
              »
              »
              7 лет назад, скрыть # ^ |
               
              Проголосовать: нравится +10 Проголосовать: не нравится

              Right...

              Maybe as gratus907 said, Addition in Multiset would probably be slower due to ReBalancing Algorithms (Rotating Tree).

              Thanks once Again!

            • »
              »
              »
              »
              »
              »
              »
              7 лет назад, скрыть # ^ |
               
              Проголосовать: нравится -29 Проголосовать: не нравится

              Amortised isn't worst case, that's still $$$O(\log)$$$.

              • »
                »
                »
                »
                »
                »
                »
                »
                7 лет назад, скрыть # ^ |
                 
                Проголосовать: нравится 0 Проголосовать: не нравится

                Worst case may be o(log) but the average time, ie the amortized time is constant.

                Check out Enchom comment on this blog. He ran few tests, and found out that deletion in a multiset is faster than popping from a priority queue

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  7 лет назад, скрыть # ^ |
                   
                  Проголосовать: нравится -7 Проголосовать: не нравится

                  Yes, I read the comments before posting, unsurprisingly. It's just that when you don't mention that you're talking about amortised complexity, the assumption is that it's worst case.

                  There are implementation tricks for faster insert/delete operations for a lot of structures, for example deletion flags on nodes and lazy rebalancing. That's why I'm more interested in a test where insert/delete is mixed with searching, but that comparison is tough because heap doesn't have searching.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 лет назад, скрыть # ^ |
                   
                  Проголосовать: нравится -10 Проголосовать: не нравится

                  "It's just that when you don't mention that you're talking about amortised complexity, the assumption is that it's worst case."

                  This is a widely misunderstood point about amortization, but it is actually completely independent from the concept of "worst case" or "average case". Classical analysis only considers complexity on a per-operation basis, whereas amortized complexity is about analyzing on a per-algorithm basis. There is a nice rigorous definition regarding potential functions here that shows that if you sum up the amortized complexity over a sequence of operations, it will necessarily be an upper bound for the standard complexity summed over that sequence of operations (note that "worst case" is never referenced in this definition).

                  I also don't think it's necessarily correct that people default to not talking about amortized analysis. I actually can't think of a scenario where the amortized complexity is better than non-amortized complexity and we don't default to amortized (std::vector::push_back, for example). Curious to hear if you have contrary examples, though. It's quite late for me so my recall could be bad.

              • »
                »
                »
                »
                »
                »
                »
                »
                7 лет назад, скрыть # ^ |
                 
                Проголосовать: нравится 0 Проголосовать: не нравится

                What's your point? Why would we care specifically about worst case complexity in this discussion? It is worst case $$$O(logN)$$$ to delete one element, but $$$O(1)$$$ amortized. This means if you were to delete all $$$N$$$ elements it'd be $$$O(N)$$$.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  7 лет назад, скрыть # ^ |
                   
                  Проголосовать: нравится -35 Проголосовать: не нравится

                  Why would we care specifically about worst case complexity in this discussion?

                  Because that's normal?

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  7 лет назад, скрыть # ^ |
                   
                  Проголосовать: нравится 0 Проголосовать: не нравится

                  Still don't get your point. Is your claim that when mixed with other operations, deletion will no longer be amortized $$$O(1)$$$? If that's true that's interesting, but I don't think it's easy to tell without knowing the specific implementation.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  7 лет назад, скрыть # ^ |
                   
                  Проголосовать: нравится 0 Проголосовать: не нравится

                  Not a claim, just one hypothesis. I'm wondering about the real implementation and what implementations there could be.

        • »
          »
          »
          »
          »
          7 лет назад, скрыть # ^ |
           
          Проголосовать: нравится -22 Проголосовать: не нравится

          NO. Deletion of anything in a balanced binary tree requires rebalancing, which is $$$O(\log)$$$. Whether it's through an iterator is irrelevant.

      • »
        »
        »
        »
        7 лет назад, скрыть # ^ |
         
        Проголосовать: нравится +4 Проголосовать: не нравится

        I think pq is faster since it is cache friendly. pq's are internally stored in a contiguous memory(like vector), While multiset's are not.

    • »
      »
      »
      7 лет назад, скрыть # ^ |
      ← Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      UPD : Deleted

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -53 Проголосовать: не нравится

The comments on this blog made me re-realize how much I still have to learn to become a Master. XD

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -92 Проголосовать: не нравится

Multisets and priority ques are used only on Div1 E-F problems and top onsite competitions like ICPC WF, and are not needed to score well in CF rounds.

Personally I have never seen a reasonable problem that would require priority que during my CP career, and believe me, I consider myself experienced competitive programmer.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

i dont use multiset nor i use qriority queue ,i use set<pair<int,int>> where i store value in pair.first and index in pair.second automatically it becomes mutiset + it supports more functions like searching a specific element after a particular index etc.. :)

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Also priority queue construct in O(n)