Exemplo n.º 1
0
void vtkPolyDataSingleSourceShortestPath::ShortestPath(int startv, int endv)
{
	int i, u, v;
	
	InitSingleSource(startv);
	
	HeapInsert(startv);
	this->f->SetValue(startv, 1);
	
	int stop = 0;
	while ((u = HeapExtractMin()) >= 0 && !stop)
	{
		// u is now in s since the shortest path to u is determined
		this->s->SetValue(u, 1);
		// remove u from the front set
		this->f->SetValue(u, 0);
		
		if (u == endv && StopWhenEndReached)
			stop = 1;
		
		// Update all vertices v adjacent to u
		for (i = 0; i < Adj[u]->GetNumberOfIds(); i++)
		{
			v = Adj[u]->GetId(i);
			
			// s is the set of vertices with determined shortest path...do not use them again
			if (!this->s->GetValue(v))
			{
				// Only relax edges where the end is not in s and edge is in the front set
				double w = EdgeCost(this->GetInput(), u, v);
				
				if (this->f->GetValue(v))
				{
					Relax(u, v, w);
				}
				// add edge v to front set
				else
				{
					this->f->SetValue(v, 1);
					this->d->SetValue(v, this->d->GetValue(u) + w);
					
					// Set Predecessor of v to be u
					this->pre->SetValue(v, u);
					
					HeapInsert(v);
				}
			}
		}
	}
}
Exemplo n.º 2
0
int main () {

    Data_t a [10] = {4,2,7,1,9,4,6,2,8,0};
    struct BHeap *heap = HeapBuild(a, 10);
//    struct BHeap *heap = HeapNew(10);
    HeapPrint(heap);
    printf("%d\n", HeapExtractMin(heap));
    HeapPrint (heap);
    HeapInsert (heap, 1);
    HeapPrint(heap);
    HeapDestroy(heap);
    return 0;

}