Пример #1
0
 void buildHeap(vector<int>& nums, int k)
 {
     int x = k/2-1;
     for(int i = x;i>=0;--i)
     {
         modifyHeap(nums,i,k);
     }
 }
Пример #2
0
    int findKthLargest(vector<int>& nums, int k) {
        int size ;
        if((size=nums.size()) == 0) return 0;
        if(size == 1)return nums[0];
        buildHeap(nums,k);
        for(int i = k;i<size;++i)
        {
            if(nums[i]>nums[0])
            {
                swap(nums[i],nums[0]);
                modifyHeap(nums,0,k);
            }

        }

        return nums[0];
    }
Пример #3
0
float getShortestPathUtil(graph* gr, heap* h, float vert[],int destination)
{

	edge_st* temp_ed;
	int temp_id;
	int temp_dest;
	float temp_len;
	graphArrnode* head;

	while(h->size)
	{
		temp_ed = getMinHeap(h);
		temp_id = temp_ed->source;
#ifdef DEBUG
		printf("getShortestPathUtil: HeapSource: %d\n",temp_id);
#endif
		head = gr->ArrList[temp_id].head;
		while(head)
		{
			temp_dest = head->dest;
#ifdef DEBUG
			printf("getSHortestPathUtil: Link Source %d\n",temp_dest);
#endif
			temp_len = head->len+temp_ed->length;
			modifyHeap(h,temp_dest,temp_len);
			head = head->next;
		}
		vert[temp_id] = temp_ed->length;

		deleteMinHeap(h);
		if(temp_id == destination)
		{
			return vert[temp_id];
		}

	}
	return vert[destination];


}
Пример #4
0
    void modifyHeap(vector<int>& nums, int index, int k)
    {
        int lc = 2*index+1;
        int rc = lc+1;
        if(lc>=k) return;

        int minv = nums[index];
        int minindex = index;
        if(lc<k && nums[lc]<minv)
        {
            minv = nums[lc];
            minindex = lc;
        }
        if(rc<k && nums[rc]<minv)
        {
            minv = nums[rc];
            minindex = rc;
        }
        if(minindex!=index)
        {
            swap(nums[minindex],nums[index]);
            modifyHeap(nums,minindex,k);
        }
    }