Пример #1
0
 T value(size_type index) const {
     FFASSERT( index >= 0 && index < (size_type)d_.size() );
     if (0 == index)
         return d_[0];
     T prob = d_[index];
     size_type index_parent = parent_index(index);
     --index;
     while (index_parent != index) {
         prob = fun_rev_(prob, d_[index]);
         index = parent_index(index);
     }
     return prob;
 }
Пример #2
0
void heapify(std::vector<int>* array) {
  for (auto itr = array->begin();
       itr != array->end();
       ++itr) {
    auto index = itr - array->begin();
    auto parent = parent_index(index);
    while (index != parent
           && (*array)[parent] < (*array)[index]) {
      std::swap((*array)[parent], (*array)[index]);
      index = parent;
      parent = parent_index(index);
    }
  }
}
    /// Dijkstraµ¥Ô´×î¶Ì·¾¶Ëã·¨
    void testDijkstra()
    {
        cout << "Dijkstra×î¶Ì·¾¶" << endl;
        //Éú³ÉP367Ò³µÄͼ24-6
        vector<char> v;
        v.push_back( 's' );
        v.push_back( 't' );
        v.push_back( 'x' );
        v.push_back( 'z' );
        v.push_back( 'y' );
        GraphicsViaAdjacencyList<char> g( v, Digraph );
        g.Link2Vertex( 0, 1, 10 );
        g.Link2Vertex( 0, 4, 5 );
        g.Link2Vertex( 1, 2, 1 );
        g.Link2Vertex( 1, 4, 2 );
        g.Link2Vertex( 2, 3, 4 );
        g.Link2Vertex( 3, 2, 6 );
        g.Link2Vertex( 3, 0, 7 );
        g.Link2Vertex( 4, 1, 3 );
        g.Link2Vertex( 4, 2, 9 );
        g.Link2Vertex( 4, 3, 2 );

        int start_index = 0;
        vector<int> d( g.GetVertex().size() );
        vector<int> parent_index( g.GetVertex().size() );
        Dijkstra( g, start_index, d, parent_index );


        for ( size_t i = 0; i < g.GetVertex().size(); ++i )
        {
            cout << g.GetVertex()[i] << " | " << d[i] << endl;
        }
    }
Пример #4
0
 void p_queue::push(const value_type& entry, size_type priority)
 {
     if(capacity <= used)
     {
         size_type newCapacity = 1 + (capacity * 1.25);
         resize(newCapacity);
     }
     ItemType newItem;
     newItem.data = entry;
     newItem.priority = priority;
     size_type newItemIndex = used;
     heap[newItemIndex] = newItem;
     ++used;
     while(newItemIndex != 0)
     {
         size_type parentIndex = parent_index(newItemIndex);
         if(priority <= heap[parentIndex].priority)
         {
             return;
         }
         else
         {
             swap_with_parent(newItemIndex);
             newItemIndex = parentIndex;
         }
     }
 }
Пример #5
0
 T accumulate(size_type i) const {
     FFASSERT( i >= 0 && i < (size_type)d_.size() );
     T ret = d_[0];
     while (i > 0) {
         ret = fun_(ret, d_[i]);
         i = parent_index(i);
     }
     return ret;
 }
Пример #6
0
   p_queue::size_type
   p_queue::parent_priority(size_type i) const
   // Pre:  (i > 0) && (i < used)
   // Post: The priority of "the parent of the item at heap[i]" has
   //       been returned.
   {
	  assert(i > 0 && i < used);
	   
      return heap[parent_index(i)].priority; 
   }
Пример #7
0
static status siftup(heap * const p_H,
                     size_t const index)
{
    if (index == 0)
        return OK;

    {
        size_t parent;
        if (parent_index(index, &parent) == ERROR)
            return ERROR;

        if ((*(p_H -> p_cmp_f))(p_H -> base[index], p_H -> base[parent]) >= 0)
            return OK;

        swap(p_H, index, parent);
        return siftup(p_H, parent);
    }
}
Пример #8
0
void MPILock::initialize_sides() {
  sides.reserve(tree_rank + 2);
  sides.push_back(index);
  sides.push_back(index);
  sides[SIDE_INDEX_PARENT] = parent_index();

  for (unsigned child_num = 1, child_index = tree_rank * index + child_num;
      (child_num <= tree_rank) && (child_index < size);
      (child_num += 1), (child_index += 1) ) {
    sides.push_back(child_index);
  }
  if (DEBUG) {
    printf("#%u: sides(%lu) ", index, sides.size());
    printf("PARENT: % 3d,\t", sides[SIDE_INDEX_PARENT]);
    printf("SELF: % 3d", sides[SIDE_INDEX_SELF]);
    if (sides.size() > SIDE_INDEX_FIRST_CHILD) {
      printf(",\t{% 2d", sides[SIDE_INDEX_FIRST_CHILD]);
      for (unsigned i = SIDE_INDEX_FIRST_CHILD + 1; i < sides.size(); ++i) printf(", % 2d", sides[i]);
      printf(" }");
    }
    printf("\n");
  }
}