Пример #1
0
int 
main()
{
	int key, ch;
	H.hsize = 0;
	while (1) {
		printf("1.Insert\n2.Display Min Heap\n3.Pop out Minimum element\n4.Exit\n");
		scanf("%d", &ch);
		switch (ch) {
			case 1 :
				printf("Enter integer to be inserted \n");
				scanf("%d", &key);
				heapInsert(key);
				break;

			case 2 :
				displayHeap();
				break;	

			case 3 :
				delMin();
				break;

			case 4 :
				return (0);
		}
	}
}
void AStar() {
	int i, cx, cy;
	L = 1, insVal(L, 0, sx, sy);
	dx2 = sx-ex, dy2 = sy-ey;
	while(L > 0) {
		cx = Heap[1].x, cy = Heap[1].y;
		if(cx == ex && cy == ey)	break;
		delMin();
		Expand(cx+1, cy, cx, cy);
		Expand(cx-1, cy, cx, cy);
		Expand(cx, cy+1, cx, cy);
		Expand(cx, cy-1, cx, cy);
	}
	printf("%d\n", map[ex][ey]);
}
Пример #3
0
float Prim(queue* Q, vertex* Graph)
{
	//Take seed to be the first vertex in heap array. Change its distance from the tree to be 0.
	decKey(Graph, 0, 0);
	
	// initialize the weight of the MST so far to 0.
	float weight=0;

	// while the heap isn't empty, delete the minimum element and update the
	// remaining vertices distances from the working tree S
	while (Q->last >= 0)
	{
		// printHeap(Q,Graph);
		// printf("%i", Q->last);
		vertex min = delMin(Q, Graph);
		// printf("\ndelMin happening. returns %f\n", sqrt(min.distFromS));
		// printHeap(Q,Graph);
		weight += sqrt(min.distFromS);
		// printf("\nweight:%f\n\n", weight);

		// grab the ptr to the adjacent verticies
		AdjListNode* adjVerts = min.adjacentVertices;

		// update verticies in adjVerts
		while(adjVerts!=NULL)
		{	
			// distance from min
			float e = adjVerts->edgeLength;
			// index of the neighbor
			int ind = adjVerts->self;
			if(e < Graph[ind].distFromS)
			{
				decKey(Graph,ind,e);
			}
			adjVerts = adjVerts->next;
			buildHeap(Q, Graph);
		}
	}
	return weight;
}