2009-12-21 20 views
5

मैं इस तरह एक अंतरफलक के साथ एक प्राथमिकता कतार के लिए देख रहा हूँ:सी # प्राथमिकता कतार

class PriorityQueue<T> 
{ 
    public void Enqueue(T item, int priority) 
    { 
    } 

    public T Dequeue() 
    { 
    } 
} 

सभी कार्यान्वयन मैंने देखा है मान लेते हैं कि item एक IComparable है, लेकिन मैं इस दृष्टिकोण पसंद नहीं है; जब मैं इसे कतार पर दबा रहा हूं तो मैं प्राथमिकता निर्दिष्ट करना चाहता हूं।

यदि तैयार किए गए कार्यान्वयन मौजूद नहीं हैं, तो इसे स्वयं करने के लिए सबसे अच्छा तरीका क्या है? मुझे किस अंतर्निहित डेटा संरचना का उपयोग करना चाहिए? कुछ प्रकार के आत्म-संतुलन पेड़, या क्या? एक मानक सी # नेट संरचना अच्छी होगी।

+0

क्या आप इसे एकाधिक धागे से कॉल करने जा रहे हैं? – sohum

+0

@sohum: नहीं ... मेरा प्रोग्राम * थ्रेड किया गया है, लेकिन केवल एक थ्रेड को इसकी पहुंच की आवश्यकता होगी। – mpen

+0

आईसीओपरपेबल का समर्थन करने के लिए तुरंत ध्यान देने योग्य कारण यह है कि यदि आप प्राथमिकता 2 के साथ कतार में दो आइटम धक्का देते हैं, तो आपको अभी भी आइटम की तुलना करने और दो प्राथमिकताओं को दो प्राथमिकताओं को संसाधित करने का आदेश तय करने की आवश्यकता है। । । तो आखिरकार आपको तुलनीय होने की आवश्यकता है। तो अपने इंटरफेस के साथ ऐसा कैसे करें ... ठीक है आपको नीचे कुछ अच्छे सुझाव मिल गए हैं। –

उत्तर

11

यदि आप किसी मौजूदा प्राथमिकता कतार IComparable के आधार पर कार्यान्वयन है, तो आप आसानी से उपयोग कर सकते हैं कि संरचना की जरूरत है निर्माण करने के लिए:

public class CustomPriorityQueue<T> // where T need NOT be IComparable 
{ 
    private class PriorityQueueItem : IComparable<PriorityQueueItem> 
    { 
    private readonly T _item; 
    private readonly int _priority: 

    // obvious constructor, CompareTo implementation and Item accessor 
    } 

    // the existing PQ implementation where the item *does* need to be IComparable 
    private readonly PriorityQueue<PriorityQueueItem> _inner = new PriorityQueue<PriorityQueueItem>(); 

    public void Enqueue(T item, int priority) 
    { 
    _inner.Enqueue(new PriorityQueueItem(item, priority)); 
    } 

    public T Dequeue() 
    { 
    return _inner.Dequeue().Item; 
    } 
} 
0

ऐसा लगता है कि आप प्रत्येक प्राथमिकता के लिए एक पंक्ति के एक दृश्य के साथ अपना खुद का रोल कर सकते हैं। शब्दकोश और बस इसे उपयुक्त में जोड़ें।

+2

इस पॉप के लिए भयानक प्रदर्शन है, जैसा कि आपके पास है पहली गैर खाली कतार खोजने के लिए। – Yuliy

+0

@Yuliy: मैंने इसे पहले भी सोचा था, लेकिन अगर हम कतार बंद कर देते हैं तो वे खाली हो जाते हैं, यह वास्तव में कोई मुद्दा नहीं है? – mpen

+0

आप प्राथमिकता को प्रबंधित करने के लिए कहीं भी भुगतान करने जा रहे हैं। यदि आपके पास एक लिंक की गई सूची है, तो आपको वर्तमान प्राथमिकता के "अंत" को खोजने के लिए इसे सम्मिलित करना होगा। प्राथमिकता प्रबंधन मुक्त नहीं है। –

6

आप सुरक्षा जाँच और क्या नहीं जोड़ सकते हैं, लेकिन यहाँ एक बहुत ही सरल SortedList का उपयोग कर दिया गया है:

class PriorityQueue<T> { 
    SortedList<Pair<int>, T> _list; 
    int count; 

    public PriorityQueue() { 
     _list = new SortedList<Pair<int>, T>(new PairComparer<int>()); 
    } 

    public void Enqueue(T item, int priority) { 
     _list.Add(new Pair<int>(priority, count), item); 
     count++; 
    } 

    public T Dequeue() { 
     T item = _list[_list.Keys[0]]; 
     _list.RemoveAt(0); 
     return item; 
    } 
} 

मैं यह सोचते हैं रहा है कि priority के छोटे मूल्यों (उच्च प्राथमिकता आइटम के अनुरूप इस संशोधित करने के लिए आसान है)।

यदि एकाधिक धागे कतार तक पहुंच जाएंगे तो आपको लॉकिंग तंत्र भी जोड़ना होगा। यह आसान है, लेकिन अगर आपको मार्गदर्शन की आवश्यकता है तो मुझे बताएं।

SortedList आंतरिक रूप से एक बाइनरी पेड़ के रूप में लागू किया गया है।

उपर्युक्त कार्यान्वयन निम्नलिखित सहायक वर्गों की आवश्यकता है। यह पता लास वी। कार्ल्सन की टिप्पणी है कि एक ही प्राथमिकता वाले आइटम को SortedList का उपयोग करके निष्पक्ष कार्यान्वयन का उपयोग करके जोड़ा नहीं जा सकता है।

class Pair<T> { 
    public T First { get; private set; } 
    public T Second { get; private set; } 

    public Pair(T first, T second) { 
     First = first; 
     Second = second; 
    } 

    public override int GetHashCode() { 
     return First.GetHashCode()^Second.GetHashCode(); 
    } 

    public override bool Equals(object other) { 
     Pair<T> pair = other as Pair<T>; 
     if (pair == null) { 
      return false; 
     } 
     return (this.First.Equals(pair.First) && this.Second.Equals(pair.Second)); 
    } 
} 

class PairComparer<T> : IComparer<Pair<T>> where T : IComparable { 
    public int Compare(Pair<T> x, Pair<T> y) { 
     if (x.First.CompareTo(y.First) < 0) { 
      return -1; 
     } 
     else if (x.First.CompareTo(y.First) > 0) { 
      return 1; 
     } 
     else { 
      return x.Second.CompareTo(y.Second); 
     } 
    } 
} 
+3

सॉर्टेडलिस्ट के साथ समस्या यह है कि यह डुप्लिकेट प्राथमिकताओं की अनुमति नहीं देता है, इसलिए यह आपको अद्वितीय प्राथमिकताओं को सुनिश्चित करने के लिए मजबूर करता है। –

+1

मुझे और अधिक समझ में आता है कि * उच्च * संख्या * उच्च * प्राथमिकता से मेल खाते हैं। – mpen

+1

आसानी से तय किया गया है, अगर प्राथमिकता सूची पर्याप्त है तो केवल प्राथमिकता मानों को अस्वीकार करें। –

5

आप मौजूदा कार्यान्वयन में से एक के चारों ओर एक आवरण लिख सकता है कि

using System; 

class PriorityQueueThatYouDontLike<T> where T: IComparable<T> 
{ 
    public void Enqueue(T item) { throw new NotImplementedException(); } 
    public T Dequeue() { throw new NotImplementedException(); } 
} 

class PriorityQueue<T> 
{ 
    class ItemWithPriority : IComparable<ItemWithPriority> 
    { 
     public ItemWithPriority(T t, int priority) 
     { 
      Item = t; 
      Priority = priority; 
     } 

     public T Item {get; private set;} 
     public int Priority {get; private set;} 

     public int CompareTo(ItemWithPriority other) 
     { 
      return Priority.CompareTo(other.Priority); 
     } 
    } 

    PriorityQueueThatYouDontLike<ItemWithPriority> q = new PriorityQueueThatYouDontLike<ItemWithPriority>(); 

    public void Enqueue(T item, int priority) 
    { 
     q.Enqueue(new ItemWithPriority(item, priority)); 
    } 

    public T Dequeue() 
    { 
     return q.Dequeue().Item; 
    } 
} 

यह itowlson के सुझाव के रूप में ही है: अपनी पसंद से इंटरफ़ेस संशोधित करता है। मैंने अभी लिखने में अधिक समय लगाया क्योंकि मैंने अधिक तरीकों को भर दिया था। : -एस

+0

अभी भी 'प्राथमिकता QueueThatYouDontLike' का अच्छा कार्यान्वयन खोजने का प्रयास कर रहा है।यहां तक ​​कि जो लोग IComparables का उपयोग करते हैं वे बहुत अच्छे लगते नहीं हैं। – mpen

+0

एक ढेर काम करेगा? –

+0

मेरे पास यहां एक सामान्य ढेर है: http://vkarlsen.serveftp.com:81/websvn/listing.php?repname=LVK&path=/LVK_3_5/trunk/LVK.Core/Collections/, उपयोगकर्ता नाम और पासवर्ड दोनों 'अतिथि' (बिना उद्धरण)। ढेर अभी भी "समस्या" का शिकार हो जाता है जिसका आप उल्लेख करते हैं कि आप आईसीओपरपेबल की आवश्यकता रखते हैं, लेकिन आप मार्कविथ प्राइरिटी क्लास का उपयोग कर सकते हैं जिसे मार्क ने पोस्ट किया है। –

4

यहां एक बहुत ही हल्का हल्का कार्यान्वयन है जिसमें पुश और पॉप दोनों के लिए ओ (लॉग (एन)) प्रदर्शन है। यह का उपयोग किसी सूची < टी > के शीर्ष पर बनाया गया है।

/// <summary>Implements a priority queue of T, where T has an ordering.</summary> 
/// Elements may be added to the queue in any order, but when we pull 
/// elements out of the queue, they will be returned in 'ascending' order. 
/// Adding new elements into the queue may be done at any time, so this is 
/// useful to implement a dynamically growing and shrinking queue. Both adding 
/// an element and removing the first element are log(N) operations. 
/// 
/// The queue is implemented using a priority-heap data structure. For more 
/// details on this elegant and simple data structure see "Programming Pearls" 
/// in our library. The tree is implemented atop a list, where 2N and 2N+1 are 
/// the child nodes of node N. The tree is balanced and left-aligned so there 
/// are no 'holes' in this list. 
/// <typeparam name="T">Type T, should implement IComparable[T];</typeparam> 
public class PriorityQueue<T> where T : IComparable<T> { 
    /// <summary>Clear all the elements from the priority queue</summary> 
    public void Clear() { 
     mA.Clear(); 
    } 

    /// <summary>Add an element to the priority queue - O(log(n)) time operation.</summary> 
    /// <param name="item">The item to be added to the queue</param> 
    public void Add (T item) { 
     // We add the item to the end of the list (at the bottom of the 
     // tree). Then, the heap-property could be violated between this element 
     // and it's parent. If this is the case, we swap this element with the 
     // parent (a safe operation to do since the element is known to be less 
     // than it's parent). Now the element move one level up the tree. We repeat 
     // this test with the element and it's new parent. The element, if lesser 
     // than everybody else in the tree will eventually bubble all the way up 
     // to the root of the tree (or the head of the list). It is easy to see 
     // this will take log(N) time, since we are working with a balanced binary 
     // tree. 
     int n = mA.Count; mA.Add (item); 
     while (n != 0) { 
     int p = n/2; // This is the 'parent' of this item 
     if (mA[n].CompareTo (mA[p]) >= 0) break; // Item >= parent 
     T tmp = mA[n]; mA[n] = mA[p]; mA[p] = tmp; // Swap item and parent 
     n = p;   // And continue 
     } 
    } 

    /// <summary>Returns the number of elements in the queue.</summary> 
    public int Count { 
     get { return mA.Count; } 
    } 

    /// <summary>Returns true if the queue is empty.</summary> 
    /// Trying to call Peek() or Next() on an empty queue will throw an exception. 
    /// Check using Empty first before calling these methods. 
    public bool Empty { 
     get { return mA.Count == 0; } 
    } 

    /// <summary>Allows you to look at the first element waiting in the queue, without removing it.</summary> 
    /// This element will be the one that will be returned if you subsequently call Next(). 
    public T Peek() { 
     return mA[0]; 
    } 

    /// <summary>Removes and returns the first element from the queue (least element)</summary> 
    /// <returns>The first element in the queue, in ascending order.</returns> 
    public T Next() { 
     // The element to return is of course the first element in the array, 
     // or the root of the tree. However, this will leave a 'hole' there. We 
     // fill up this hole with the last element from the array. This will 
     // break the heap property. So we bubble the element downwards by swapping 
     // it with it's lower child until it reaches it's correct level. The lower 
     // child (one of the orignal elements with index 1 or 2) will now be at the 
     // head of the queue (root of the tree). 
     T val = mA[0]; 
     int nMax = mA.Count - 1; 
     mA[0] = mA[nMax]; mA.RemoveAt (nMax); // Move the last element to the top 

     int p = 0; 
     while (true) { 
     // c is the child we want to swap with. If there 
     // is no child at all, then the heap is balanced 
     int c = p * 2; if (c >= nMax) break; 

     // If the second child is smaller than the first, that's the one 
     // we want to swap with this parent. 
     if (c + 1 < nMax && mA[c + 1].CompareTo (mA[c]) < 0) c++; 
     // If the parent is already smaller than this smaller child, then 
     // we are done 
     if (mA[p].CompareTo (mA[c]) <= 0) break; 

     // Othewise, swap parent and child, and follow down the parent 
     T tmp = mA[p]; mA[p] = mA[c]; mA[c] = tmp; 
     p = c; 
     } 
     return val; 
    } 

    /// <summary>The List we use for implementation.</summary> 
    List<T> mA = new List<T>(); 
} 
+0

उन्होंने कहा कि वह नहीं चाहते थे कि 'टी' को 'आईसीओपरपेबल' होना चाहिए। – jason

+1

उन्होंने लिखा "यहां तक ​​कि जो लोग IComparable का उपयोग करते हैं वे बहुत अच्छे लगते नहीं हैं" और मैं इसका जवाब दे रहा था। मुख्य बिंदु यह है कि प्राथमिकता कतार को पूरी तरह से क्रमबद्ध सूची की आवश्यकता नहीं होती है; हमें यह जानने की जरूरत है कि किसी भी समय सूची में पहला तत्व कौन सा है। मैं जिस ढेर डेटा संरचना का उपयोग कर रहा हूं वह पूरी तरह से क्रमबद्ध नहीं है, लेकिन लॉग (एन) को कुशलतापूर्वक कार्यान्वित करने और निकालने के लिए पर्याप्त सॉर्टिंग बनाए रखता है। जाहिर है, यह एक पूर्ण उड़ा हुआ बाइनरी पेड़ की तुलना में बहुत हल्का है, और समग्र प्रदर्शन समान होगा। – Tarydon

+1

स्पष्टीकरण के लिए: यह प्राथमिकता Queue एक सादे सूची से अधिक स्थान का उपयोग नहीं करता है और लॉग (एन) प्रदर्शन को सम्मिलित और हटा देता है। – Tarydon

2

इस तरह के बारे में इतना भयानक क्या होगा?

class PriorityQueue<TItem, TPriority> where TPriority : IComparable 
{ 
    private SortedList<TPriority, Queue<TItem>> pq = new SortedList<TPriority, Queue<TItem>>(); 
    public int Count { get; private set; } 

    public void Enqueue(TItem item, TPriority priority) 
    { 
     ++Count; 
     if (!pq.ContainsKey(priority)) pq[priority] = new Queue<TItem>(); 
     pq[priority].Enqueue(item); 
    } 

    public TItem Dequeue() 
    { 
     --Count; 
     var queue = pq.ElementAt(0).Value; 
     if (queue.Count == 1) pq.RemoveAt(0); 
     return queue.Dequeue(); 
    } 
} 

class PriorityQueue<TItem> : PriorityQueue<TItem, int> { } 
+1

साफ दिखता है। एकता - किसी कारण से- 'ElementAt()' प्रदान नहीं करता है। इसलिए मैंने वहां 'मूल्य [0]' का उपयोग किया। – noio

1

मुझे लगता है कि आपके सवाल का विशेष रूप से एक गैर IComparable आधारित कार्यान्वयन के लिए पूछता है, लेकिन मैं Visual Studio Magazine से हाल ही में एक लेख का कहना चाहते हैं।

http://visualstudiomagazine.com/articles/2012/11/01/priority-queues-with-c.aspx

@ itowlson के साथ यह लेख एक पूरा जवाब दे सकते हैं।

3

यह मेरे highly optimized C# priority-queue द्वारा उपयोग किया जाने वाला सटीक इंटरफ़ेस है।

यह विशेष रूप से पथदर्शी अनुप्रयोगों (ए *, आदि) के लिए विकसित किया गया था, लेकिन किसी अन्य एप्लिकेशन के लिए भी पूरी तरह से काम करना चाहिए।

एकमात्र संभावित मुद्दा यह है कि, पूर्ण अधिकतम प्रदर्शन को निचोड़ने के लिए, कार्यान्वयन को PriorityQueueNode का विस्तार करने के लिए लगाए गए मानों की आवश्यकता होती है।

public class User : PriorityQueueNode 
{ 
    public string Name { get; private set; } 
    public User(string name) 
    { 
     Name = name; 
    } 
} 

... 

HeapPriorityQueue<User> priorityQueue = new HeapPriorityQueue<User>(MAX_USERS_IN_QUEUE); 
priorityQueue.Enqueue(new User("Jason"), 1); 
priorityQueue.Enqueue(new User("Joseph"), 10); 

//Because it's a min-priority queue, the following line will return "Jason" 
User user = priorityQueue.Dequeue(); 
1

एक छोटी सी देर लेकिन मैं इसे यहाँ संदर्भ

https://github.com/ERufian/Algs4-CSharp

कुंजी-मान-जोड़ी प्राथमिकता कतारों Algs4 में लागू किया जाता है के लिए जोड़ देंगे/IndexMaxPQ.cs, Algs4/IndexMinPQ.cs और Algs4/IndexPQDictionary.cs

नोट्स:

  1. प्राथमिकताएं IComparable नहीं कर रहे हैं ' एस, एक आईसीओएमपीयर को कन्स्ट्रक्टर
  2. ऑब्जेक्ट और इसकी प्राथमिकता को लागू करने के बजाय निर्दिष्ट किया जा सकता है, जो जुड़ा हुआ है वह एक सूचकांक और इसकी प्राथमिकता है (और, मूल प्रश्न के लिए, एक अलग सूची या टी [] की आवश्यकता होगी उस सूचकांक को अपेक्षित परिणाम में परिवर्तित करें)
संबंधित मुद्दे