Click here to Skip to main content

Algorithms

    RSS: RSS Feed

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
QuestionHow to Generate Unique Serial Number.. Pinmembershaina22311:12 20 Nov '09  
AnswerRe: How to Generate Unique Serial Number.. PinmemberPaulo Zemek2:16 20 Nov '09  
GeneralRe: How to Generate Unique Serial Number.. Pinmembershaina22313:40 20 Nov '09  
GeneralRe: How to Generate Unique Serial Number.. PinmemberMember 419459312:44 11 Dec '09  
AnswerRe: How to Generate Unique Serial Number.. PinmemberRobin_Roy20:04 2 Dec '09  
GeneralRe: How to Generate Unique Serial Number.. Pinmembersupercat99:04 8 Dec '09  
QuestionWhats the fastest way to search through a binary heap? PinmemberCaptainSeeSharp19:08 18 Nov '09  
I have this binary heap, and I need to get the index of a particular node. I can do that now by iterating through the array one by one until I find it, its too slow. Are there any faster ways to search? Whats the fastest?
 
Here is my code:
 
  class NodeHeap
  {
    private Node[] m_nodeHeap = new Node[1000000];
    private uint m_lastNodeIndex;
 
    public long Count
    {
      get { return m_nodeHeap.LongLength - 1; }
    }
 
    public Node NodeAt(uint index)
    {
      return m_nodeHeap[index];
    }
 
    public void RemoveAt(uint index)
    {
      if (index > m_lastNodeIndex)
      {
        throw new ArgumentOutOfRangeException("uint index");
      }
 
      SwapNode(ref m_nodeHeap[index], ref  m_nodeHeap[m_lastNodeIndex]);
 
      m_nodeHeap[m_lastNodeIndex--] = null;
 
      SiftUp(index);
    }
 
    public uint IndexOf(Node n) //this needs optimized
    {
      for (uint i = 1; i <= m_lastNodeIndex; i++)
      {
        if (m_nodeHeap[i] == n) return i;
      }
 
      return 0;
    }
 
    public void Add(Node n)
    {
      m_nodeHeap[++m_lastNodeIndex] = n;
 
      SiftDown(m_lastNodeIndex);
    }
 
    public void SiftUp(uint index)
    {
      if (index >= m_lastNodeIndex) return;
 
      uint next = index;
 
      next *= 2;
 
      if (next > m_lastNodeIndex) return;
 
      if (m_nodeHeap[next].Cost < m_nodeHeap[index].Cost)
      {
        if (next != m_lastNodeIndex && m_nodeHeap[next + 1].Cost < m_nodeHeap[next].Cost)
        {
          SwapNode(ref m_nodeHeap[++next], ref m_nodeHeap[index]);
        }
        else
        {
          SwapNode(ref m_nodeHeap[next], ref m_nodeHeap[index]);
        }
 
        SiftUp(next);
      }
    }
 
    public void SiftDown(uint index)
    {
      if (index == 1) return;
 
      uint next = index;
 
      next /= 2;
 
      if (m_nodeHeap[next].Cost > m_nodeHeap[index].Cost)
      {
        SwapNode(ref m_nodeHeap[next], ref m_nodeHeap[index]);
        SiftDown(next);
      }
    }
 
    public Node RemoveFirst()
    {
      Node n = m_nodeHeap[1];
      RemoveAt(1);
      return n;
    }
 
    private void SwapNode(ref Node a, ref Node b)
    {
      Node temp = a;
      a = b;
      b = temp;
    }
  }

 

AnswerRe: Whats the fastest way to search through a binary heap? Pinmemberharold aptroot0:06 19 Nov '09  
GeneralRe: Whats the fastest way to search through a binary heap? PinmemberCaptainSeeSharp6:43 19 Nov '09  
GeneralRe: Whats the fastest way to search through a binary heap? Pinmemberharold aptroot7:00 19 Nov '09  
GeneralRe: Whats the fastest way to search through a binary heap? PinmemberCaptainSeeSharp7:07 19 Nov '09  
GeneralRe: Whats the fastest way to search through a binary heap? Pinmemberharold aptroot7:25 19 Nov '09  
GeneralRe: Whats the fastest way to search through a binary heap? PinmemberCaptainSeeSharp8:17 19 Nov '09  
QuestionCrowd detection (people), how to? PinmemberGamma_ace16:53 12 Nov '09  
AnswerRe: Crowd detection (people), how to? Pinmemberkaushik_acharya21:03 5 Nov '11  
QuestionAlgorithm to C# [modified] PinmemberVS newbie8:05 11 Nov '09  
AnswerRe: Algorithm to C# PinmemberRichardM112:29 11 Nov '09  
QuestionCollision Detection with a Quad tree PinmemberSK Genius17:22 5 Nov '09  
AnswerRe: Collision Detection with a Quad tree PinmvpLuc Pattyn17:40 5 Nov '09  
AnswerRe: Collision Detection with a Quad tree Pinmemberely_bob13:05 18 Nov '09  
Questionorder of algorithm Pinmemberkhomeyni21:30 4 Nov '09  
QuestionRe: order of algorithm Pinmemberharold aptroot2:32 5 Nov '09  
AnswerRe: order of algorithm Pinmemberkhomeyni5:45 5 Nov '09  
GeneralRe: order of algorithm Pinmemberharold aptroot6:10 5 Nov '09  
GeneralRe: order of algorithm Pinmemberkhomeyni6:26 5 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.5.120210.1 | Last Updated 14 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid