コード例 #1
0
// The main function that constructs Minimum Spanning Tree (MST)
// using Prim's algorithm
void PrimMST(struct Graph* graph)
{
    int V = graph->V;// Get the number of vertices in graph
    int parent[V];   // Array to store constructed MST
    int key[V];      // Key values used to pick minimum weight edge in cut
 
    // minHeap represents set E
    struct MinHeap* minHeap = createMinHeap(V);
 
    // Initialize min heap with all vertices. Key value of
    // all vertices (except 0th vertex) is initially infinite
    for (int v = 1; v < V; ++v)
    {
        parent[v] = -1;
        key[v] = INT_MAX;
        minHeap->array[v] = newMinHeapNode(v, key[v]);
        minHeap->pos[v] = v;
    }
 
    // Make key value of 0th vertex as 0 so that it
    // is extracted first
    key[0] = 0;
    minHeap->array[0] = newMinHeapNode(0, key[0]);
    minHeap->pos[0]   = 0;
 
    // Initially size of min heap is equal to V
    minHeap->size = V;
 
    // In the followin loop, min heap contains all nodes
    // not yet added to MST.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum key value
        struct MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode->v; // Store the extracted vertex number
 
        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their key values
        struct AdjListNode* pCrawl = graph->array[u].head;
        while (pCrawl != NULL)
        {
            int v = pCrawl->dest;
 
            // If v is not yet included in MST and weight of u-v is
            // less than key value of v, then update key value and
            // parent of v
            if (isInMinHeap(minHeap, v) && pCrawl->weight < key[v])
            {
                key[v] = pCrawl->weight;
                parent[v] = u;
                decreaseKey(minHeap, v, key[v]);
            }
            pCrawl = pCrawl->next;
        }
    }
 
    // print edges of MST
    printArr(parent, V);
}
コード例 #2
0
ファイル: Huffman.cpp プロジェクト: caomw/All-Codes
// Creates a min heap of capacity equal to size and inserts all character of 
// data[] in min heap. Initially size of min heap is equal to capacity
struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size)
{
    struct MinHeap* minHeap = createMinHeap(size);
    for (int i = 0; i < size; ++i)
        minHeap->array[i] = newNode(data[i], freq[i]);
    minHeap->size = size;
    buildMinHeap(minHeap);
    return minHeap;
}
コード例 #3
0
ファイル: Djisktra.cpp プロジェクト: kaustubhkk47/Coding
// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void dijkstra(struct Graph* graph, int src)
{
    int V = graph->V;// Get the number of vertices in graph
    int dist[V];      // dist values used to pick minimum weight edge in cut
 
    // minHeap represents set E
    struct MinHeap* minHeap = createMinHeap(V);
 
    // Initialize min heap with all vertices. dist value of all vertices 
    for (int v = 0; v < V; ++v)
    {
        dist[v] = INT_MAX;
        minHeap->array[v] = newMinHeapNode(v, dist[v]);
        minHeap->pos[v] = v;
    }
 
    // Make dist value of src vertex as 0 so that it is extracted first
    minHeap->array[src] = newMinHeapNode(src, dist[src]);
    minHeap->pos[src]   = src;
    dist[src] = 0;
    decreaseKey(minHeap, src, dist[src]);
 
    // Initially size of min heap is equal to V
    minHeap->size = V;
 
    // In the followin loop, min heap contains all nodes
    // whose shortest distance is not yet finalized.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum distance value
        struct MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode->v; // Store the extracted vertex number
 
        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their distance values
        struct AdjListNode* pCrawl = graph->array[u].head;
        while (pCrawl != NULL)
        {
            int v = pCrawl->dest;
 
            // If shortest distance to v is not finalized yet, and distance to v
            // through u is less than its previously calculated distance
            if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX && 
                                          pCrawl->weight + dist[u] < dist[v])
            {
                dist[v] = dist[u] + pCrawl->weight;
 
                // update distance value in min heap also
                decreaseKey(minHeap, v, dist[v]);
            }
            pCrawl = pCrawl->next;
        }
    }
 
    // print the calculated shortest distances
    printArr(dist, V);
}
コード例 #4
0
ファイル: dijkstra.c プロジェクト: h0tbird/trains-challenge
int dijkstra(PWDG graph, int src, int dst) {

  int V = graph->count;
  int dist[V];

  // Clone node:
  if (src == dst) {
    wdg_clone(graph, src);
    dst = V; V++;
  }

  pMinHeap minHeap = createMinHeap(V);

  for (int v=0; v<V; ++v) {
    dist[v] = INT_MAX;
    minHeap->array[v] = newMinHeapNode(v, dist[v]);
    minHeap->pos[v] = v;
  }

  dist[src] = 0;
  decreaseKey(minHeap, src, dist[src]);
  minHeap->size = V;

  while (!isEmpty(minHeap)) {

    pMinHeapNode minHeapNode = extractMin(minHeap);
    int u = minHeapNode->v;

    if (u == dst) {
      wdg_unclone(graph);
      return dist[u];
    }

    PNODE pCrawl = graph->list[u].head;

    while (pCrawl != NULL) {

      int v = pCrawl->dst;

      if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX && pCrawl->wgt + dist[u] < dist[v]) {
        dist[v] = dist[u] + pCrawl->wgt;
        decreaseKey(minHeap, v, dist[v]);
      }

      pCrawl = pCrawl->next;
    }
  }

  #ifdef DEBUG
  printArr(dist, V, src);
  #endif

  wdg_unclone(graph);
  return -1;
}
コード例 #5
0
MinHeap* createAndBuildMinHeap (char data[], int freq[], int size)
{
	int i;
	MinHeap* minHeap = createMinHeap(size);
	for (i = 0; i < size; ++i)
	{
		minHeap->array[i] = newNode(data[i], freq[i]); //inserting data
	}
	minHeap->size = size;
	minHeapify(minHeap, 0);	
	//buildMinHeap(minHeap); //creating a MinHeap out of given data
	return minHeap;
}
コード例 #6
0
void
dijkstra(graph* g, int src)
{
    int v, V = g->V; // getting the number of indices from the graph.
    int dist[V];
    int i,max = 0;

    minHeap* mHeap = createMinHeap(V);

    for(v = 0; v < V; v++)
    {
        dist[v] = MAXINT;
        mHeap->array[v] = createHeapNode(v, dist[v]);
        mHeap->pos[v] = v;
    }
    mHeap->array[src] = createHeapNode(src, dist[src]);
    mHeap->pos[src]   = src;
    dist[src] = 0;
    decreaseKey(mHeap, src, dist[src]);

    mHeap->size = V;

    while ((mHeap->size!=0))
    {
        minHeapNode* mHeapNode = extractMin(mHeap);
        int u = mHeapNode->v;

        node* pNode = g->array[u].head;
        while (pNode != NULL)
        {
            int v = pNode->d;

            if ((isInMinHeap(mHeap, v) && dist[u] != MAXINT) && ((pNode->w + dist[u]) < dist[v]))
            {
                dist[v] = dist[u] + pNode->w;
                decreaseKey(mHeap, v, dist[v]);
            }
            pNode = pNode->next;
        }
    }

    //print the shortest path from distant vertex
    for(i = 0; i < V; i++)
    {
        if(max < dist[i])
            max = dist[i];
    }

    printf("most distant vertex is %d\n",max);
}
コード例 #7
0
ファイル: heap_sort.cpp プロジェクト: lshi2004/Montevideo
// The main function that takes an array of lists from N machines
// and generates the sorted output
void externalSort( ListNode *array[], int N )
{
    // Create a min heap of size equal to number of machines
    MinHeap* minHeap = createMinHeap( N );
 
    // populate first item from all machines
    populateMinHeap( minHeap, array, N );
 
    while ( !isEmpty( minHeap ) )
    {
        ListNode* temp = extractMin( minHeap );
        printf( "%d ",temp->data );
    }
}
コード例 #8
0
ファイル: huffman.c プロジェクト: qodome/Firmware
// Creates a min heap of capacity equal to size and inserts all character of
// data[] in min heap. Initially size of min heap is equal to capacity
struct MinHeap* createAndBuildMinHeap(uint16 freq[], uint16 size)
{
    struct MinHeap* minHeap = createMinHeap(size);
    if (minHeap == NULL) {
        hError |= (1 << 3);
        return NULL;
    }
    for (uint16 i = 0; i < size; ++i) {
        minHeap->array[i] = newNode((uint8)(i & 0xFF), freq[i]);
        if (minHeap->array[i] == NULL) {
            hError |= (1 << 4);
            return NULL;
        }
    }
    minHeap->size = size;
    buildMinHeap(minHeap);
    return minHeap;
}
コード例 #9
0
int main(){

	int v;
	int i;
	int element;
	int priority;

	scanf ( "%d", &v);
	struct MinHeap * minHeap = createMinHeap(v);

	for ( i = 0; i < v; i++ ) {
		scanf ( "%d", &element);
		scanf ( "%d", &priority);
		insertMinHeap( minHeap, element, priority);
	}
	printHeap ( minHeap );
	printSortedElements ( minHeap );
	
return 0;
}
コード例 #10
0
minheapNodePTR build_Huffman_tree( char data[] , int freq[] , int size)
{
	minheapNodePTR left ,right , top;
	minheapPTR hh = createMinHeap(size);
	/* building minheap */
	int i;
	for(i = 0; i < size; i++)
		hh->array[i] = newNode(data[i],freq[i]);
	hh->size = size;
	
	printf("minheap size = %d\n",hh->size);
	print_min_heap(hh);
	buildminHeap(hh);
	print_min_heap(hh);
		/* building minheap END */

	/* huffman ALgo */

	while ( hh->size != 1 )
	{
		left = extractMin(hh);
		right = extractMin(hh);
		printf("%s\n","DEBUGGING INSIDE BUILD build_Huffman_tree \n");
		printf("left data  :%c\n",left->data);
		printf("right data :%c\n",right->data);
		top = newNode('0' , left->freq + right->freq);
		output1_num_of_nodes_in_tree++;
		top->left = left;
		top->right = right;
		insertminHeap(hh , top);
		print_min_heap(hh);
	}
	return extractMin(hh);
	// returns the root node of the Huffman tree
	// with this we can do Postorder , Inorder traversal and also print the huffman codes
}
コード例 #11
0
ファイル: p.c プロジェクト: Lanchenn/CISC360_PROJECT
// The main function that constructs Minimum Spanning Tree (MST)
// using Prim's algorithm
void PrimMST(Graph* graph)
{
    int MSTWeight = 0;
    int V = graph->V;// Get the number of vertices in graph
    int E = graph->E;// Get the number of edges in graph
    int parent[V];   // Array to store constructed MST
    int key[V];      // Key values used to pick minimum weight edge in cut
 
    // minHeap represents set E
    MinHeap* minHeap = createMinHeap(V);
 
    // Initialize min heap with all vertices. Key value of
    // all vertices (except 0th vertex) is initially infinite

    // print edges of MST
    printf("===============================================\n");
    printf("Following are the edges in the constructed MST\n");

    for (int v = 1; v < V; ++v)
    {
        parent[v] = -1;
        key[v] = INT_MAX;
        minHeap->array[v] = newMinHeapNode(v, key[v]);
        minHeap->pos[v] = v;
    }
 
    // Make key value of 0th vertex as 0 so that it
    // is extracted first
    key[0] = 0;
    minHeap->array[0] = newMinHeapNode(0, key[0]);
    minHeap->pos[0]   = 0;
 
    // Initially size of min heap is equal to V
    minHeap->size = V;
 
    // In the followin loop, min heap contains all nodes
    // not yet added to MST.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum key value
        MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode->v; // Store the extracted vertex number
        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their key values
	int i;
	int* adjList = findAdjList(graph,u);
        int size = findAdjListCount(graph, u, NULL, 0);
	//printf("%d->%d\n",adjList[0],adjList[1]);
        //printf("%d\n",size);
	for (i=1;i<size;i++) 
        {
	    int v = adjList[i];
	    // printf("%d\n",v);   
            // If v is not yet included in MST and weight of u-v is
            // less than key value of v, then update key value and
            // parent of v
            if (isInMinHeap(minHeap, v) && findWeight(graph,u,v) < key[v])
            {
                key[v] = findWeight(graph,u,v);
                parent[v] = u;
                decreaseKey(minHeap, v, key[v]);
	    }
	 }
	 MSTWeight += minHeapNode->key;
    }
    int n;
    for (n=1;n<V;n++) {
       printf("Edge: %d -- %d (Weight: %d)\n",parent[n],n,key[n]);
    }
    printf("Total Weight: %d\n", MSTWeight);
    printf("===============================================\n");
}