Click here to Skip to main content
15,926,596 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: Advice about choice of algorithm Pin
Alan Balkany1-Dec-09 4:17
Alan Balkany1-Dec-09 4:17 
QuestionHow to Generate Unique Serial Number.. Pin
shaina223120-Nov-09 0:12
shaina223120-Nov-09 0:12 
AnswerRe: How to Generate Unique Serial Number.. Pin
Paulo Zemek20-Nov-09 1:16
Paulo Zemek20-Nov-09 1:16 
GeneralRe: How to Generate Unique Serial Number.. Pin
shaina223120-Nov-09 2:40
shaina223120-Nov-09 2:40 
GeneralRe: How to Generate Unique Serial Number.. Pin
Member 419459311-Dec-09 11:44
Member 419459311-Dec-09 11:44 
AnswerRe: How to Generate Unique Serial Number.. Pin
Robin_Roy2-Dec-09 19:04
Robin_Roy2-Dec-09 19:04 
GeneralRe: How to Generate Unique Serial Number.. Pin
supercat98-Dec-09 8:04
supercat98-Dec-09 8:04 
QuestionWhats the fastest way to search through a binary heap? Pin
CaptainSeeSharp18-Nov-09 18:08
CaptainSeeSharp18-Nov-09 18:08 
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? Pin
harold aptroot18-Nov-09 23:06
harold aptroot18-Nov-09 23:06 
GeneralRe: Whats the fastest way to search through a binary heap? Pin
CaptainSeeSharp19-Nov-09 5:43
CaptainSeeSharp19-Nov-09 5:43 
GeneralRe: Whats the fastest way to search through a binary heap? Pin
harold aptroot19-Nov-09 6:00
harold aptroot19-Nov-09 6:00 
GeneralRe: Whats the fastest way to search through a binary heap? Pin
CaptainSeeSharp19-Nov-09 6:07
CaptainSeeSharp19-Nov-09 6:07 
GeneralRe: Whats the fastest way to search through a binary heap? Pin
harold aptroot19-Nov-09 6:25
harold aptroot19-Nov-09 6:25 
GeneralRe: Whats the fastest way to search through a binary heap? Pin
CaptainSeeSharp19-Nov-09 7:17
CaptainSeeSharp19-Nov-09 7:17 
QuestionCrowd detection (people), how to? Pin
Gamma_ace12-Nov-09 15:53
Gamma_ace12-Nov-09 15:53 
AnswerRe: Crowd detection (people), how to? Pin
kaushik_acharya5-Nov-11 20:03
kaushik_acharya5-Nov-11 20:03 
QuestionAlgorithm to C# [modified] Pin
VS newbie 11-Nov-09 7:05
VS newbie 11-Nov-09 7:05 
AnswerRe: Algorithm to C# Pin
RichardM111-Nov-09 11:29
RichardM111-Nov-09 11:29 
QuestionCollision Detection with a Quad tree Pin
Anthony Mushrow5-Nov-09 16:22
professionalAnthony Mushrow5-Nov-09 16:22 
AnswerRe: Collision Detection with a Quad tree Pin
Luc Pattyn5-Nov-09 16:40
sitebuilderLuc Pattyn5-Nov-09 16:40 
AnswerRe: Collision Detection with a Quad tree Pin
ely_bob18-Nov-09 12:05
professionalely_bob18-Nov-09 12:05 
Questionorder of algorithm Pin
khomeyni4-Nov-09 20:30
khomeyni4-Nov-09 20:30 
QuestionRe: order of algorithm Pin
harold aptroot5-Nov-09 1:32
harold aptroot5-Nov-09 1:32 
AnswerRe: order of algorithm Pin
khomeyni5-Nov-09 4:45
khomeyni5-Nov-09 4:45 
GeneralRe: order of algorithm Pin
harold aptroot5-Nov-09 5:10
harold aptroot5-Nov-09 5:10 

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

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