void dijkstra(struct Graph *graph, int src)
{
	int v = graph->v, i;
	int pr;

	for(i = 0; i < v; i++)
	{
		pr = 50000;
		insert(i, pr);
	}

	decreaseKey(src, 0);
	while(!isEmptyminHeap())
	{
		extract_min(heap);
		int x = min_vertex, y = min_dist;

		struct adjListNode *vertex = graph->array[x].head;
		while(vertex != NULL)
		{
			int v = vertex->node;
			if(isInMinHeap(v) && y != 50000 && vertex->weight + y < get_priority(v))
			{
				pr = y + vertex->weight;
				decreaseKey(v, pr);
			}
			vertex = vertex->next;
		}
	}
	// printArray(graph);
}
コード例 #2
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);
}
コード例 #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
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);
}
コード例 #6
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");
}