Ejemplo n.º 1
0
// Prim algorithm 
void Prim(Graph G, int s){
	//perform Prims's algorithm on G starting with vertex s

	int j, heap[MaxVertices + 1], heapLoc[MaxVertices + 1];
	G->vertex[s].cost = 0;
	for (j = 1; j <= G->numV; j++) heap[j] = heapLoc[j] = j;
	heap[1] = s; heap[s] = 1; heapLoc[s] = 1; heapLoc[1] = s;
	int heapSize = G->numV;
	while (heapSize > 0)
	{
		int u = heap[1];
		if (G->vertex[u].cost == Infinity) break;
		G->vertex[u].colour = Black;
		//reorganize heap after removing top item
		siftDown(G, heap[heapSize], heap, 1, heapSize - 1, heapLoc);
		GEdgePtr p = G->vertex[u].firstEdge;
		while (p != NULL)
		{
			if (G->vertex[p->child].colour == White && p->weight < G->vertex[p->child].cost)
			{
				G->vertex[p->child].cost = p->weight;
				G->vertex[p->child].parent = u;
				siftUp(G, heap, heapLoc[p->child], heapLoc);
			}
			p = p->nextEdge;
		}
		--heapSize;
	}// end while
	printMST(G);
}// end Prim's
Ejemplo n.º 2
0
Archivo: homework2.c Proyecto: 2khc/UCL
// a function to construct and print MST for a graph represented using adjacency
// matrix representation
void primMST(int graph[V][V])
{
    int parent[V]; // array to store constructed MST
    int key[V];    // key values used to pick minimum weight edge in cut
    bool mstSet[V] // to represent set of vertices not yet included in MST

    // initialise all keys as infinite
    for (int count = 0; count < V - 1; count++)
    {
        // pick the minimum key vertex from the set of vertices
        // not yet included in MST
        int u = minKey(key, mstSet);

        // add the picked vertex to the MST set
        mstSet[u] = true;

        // update the key value and parent index of the adjacent vertices of
        // the picked vertex. Consider only those vertices which are not yet
        // included in the MST
        for (int v = 0; v < V; v++)
        {
            // graph[u][v] is non-zero only for adjacent vertices of m
            // mstSet[v] is false for vertices not yet included in MST
            // Update the key only if graph[u][v] is smaller than key[v]
            if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
            {
                parent[v] = u;
                key[v] = graph[u][v];
            }
            // print the constructed MST
            printMST(parent, V, graph);
        }
    }
}
Ejemplo n.º 3
0
//--------------------------------------------------------------------------------------------------
// Function to construct and print MST for a graph represented using adjacency matrix representation
void primMST(int graph[V][V])
{
  int parent[V];                                                   // Array to store constructed MST
  int key[V];                                  // Key values used to pick minimum weight edge in cut
  bool visited[V];                            // To represent set of vertices not yet included in MST
  for (int i = 0; i < V; i++)                                     // Initialize all keys as INFINITE
    key[i] = INT_MAX, visited[i] = false;
  // Always include first 1st vertex in MST.
  key[0] = 0;                            // Make key 0 so that this vertex is picked as first vertex
  parent[0] = -1;                                                // First node is always root of MST 
  for (int count = 0; count < V-1; count++)                          // The MST will have V vertices
  {
    // Pick the minimum key vertex from the set of vertices not yet included in MST
    int min_vertex = minKey(key, visited);
    visited[min_vertex] = true;                               // Add the picked vertex to the MST Set
    // Update key value and parent index of the adjacent vertices of the picked vertex. Consider 
    // only those vertices which are not yet included in MST
    for (int v = 0; v < V; v++)
       // graph[min_vertex][v] is non zero only for adjacent vertices of m visited[v] is false for 
       // vertices not yet included in MST Update the key only if graph[min_vertex][v] is smaller 
       // than key[v]
      if (graph[min_vertex][v] && visited[v] == false && graph[min_vertex][v] <  key[v])
         parent[v]  = min_vertex, key[v] = graph[min_vertex][v];
  }
  // print the constructed MST
  printMST(parent, V, graph);
}
Ejemplo n.º 4
0
//http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-spanning-tree-mst-2/
void Cristof::primMST(int *tour[], int n){

    graph = tour;

    int currMst[n];     //holds MST
    int key[n];         //holds current key
    bool setMst[n];     //set of vertices not in MST yet

    //initialize all keys to infinity and bool in set to false
    for(int v = 0; v < n; v++){
        key[v] = std::numeric_limits<int>::max();
        setMst[v] = false;
    }

    key[0] = 0;         //first vertex
    currMst[0] = -1;    //root of MST is first node

    for (int num = 0; num < n - 1; num++){
        //pick lowest key vertex not yet in the MST
        int u = minKey(key, setMst);
        //add to chosen in set
        setMst[u] = true;
        //check adjacent vertices that have not been picked
        //and add to key
        for (int v = 0; v < n; v++){
            if (graph[u][v] && setMst[v] == false && graph[u][v] < key[v]){
                currMst[v] = u;
                key[v] = graph[u][v];
            }
        }
    }
    for (int v1 = 0; v1 < n; v1++){
        int v2 = currMst[v1];
        //if not root
        if(v2 != -1){
            //key each value to it's adjacent edge
            mst[v1].push_back(v2);
            mst[v2].push_back(v1);
        }
    }
    cout << endl;
    printMST(currMst, n, graph);
    cout << endl;
    //move mst to mst matrix and build adjacency list
}
Ejemplo n.º 5
0
void primMST(int graph[V][V])
{
     int parent[V],i,j,v,max,key[V];

     for (i = 0; i < V; i++)
        key[i] = max;

     key[0] = 0;
     parent[0] = -1;

     for (j = 0; j < V-1; j++)
     {
        int u = minKey(key);
        for (v = 0; v < V; v++)
          if (graph[u][v] && graph[u][v] <  key[v])
             parent[v]  = u, key[v] = graph[u][v];
     }
     printMST(parent, V, graph);
}
Ejemplo n.º 6
0
// Function to construct and print MST for a graph represented using a matrix
// representation
void primMST(int graph[V][V])
{
  int  parent[V];  // Array to store constructed MST
  int  key[V];     // Key values used to pick minimum weight edge in the cut
  bool mstSet[V];  // To represent set of vertices not yet included in MST

  // Initialize all keys as INFINITE
  for (int i = 0; i < V; ++i)
  {
    key[i] = INT_MAX, mstSet[i] = false;
  }

  // Always include first 1st vertex in MST
  key[0] = 0;      // Make key 0 so that this vertex is picked as first vertex
  parent[0] = -1;  // First node is always root of MST

  // The MST will have V vertices
  for (int count = 0; count < V-1; ++count)
  {
    // Pick the minimum key vertex from the set of vertices not yet
    // included in MST
    int u = minKey(key, mstSet);

    // Add the picked vertex to the MST set
    mstSet[u] = true;

    // Update key value and parent index of the adjacent vertices of
    // the picked vertex. Consider only those vertices which are not yet
    // included in MST
    for (int v = 0; v < V; ++v)
    {
      // graph[u][v] is non zero only adjacent vertices
      if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
      {
        parent[v] = u, key[v] = graph[u][v];
      }
    }
  }

  // Print the constructed MST
  printMST(parent, V, graph);
}
Ejemplo n.º 7
0
// int totalDistance(RST* root);
// Must check for errors in instance, if so output error to console.
// If option output is given, output file to a text
// If option output is not given output to screen
int main(int argc, char** argv){
    char* filename = NULL;
    char** lines;
    Plane plane;
    char* options = NULL;
    bool correctFile = true;
    int** MST = NULL;
    int* degrees;
    //initiallize rand() with current time
    srand(time(NULL));

    // Cheching for arguments, if they are greater than or equal to two, assume
    // their are options and a filename being passed in trying to be passed in.
    if(argc >= 2){
        filename = getFilename(argv, argc);
        options = getOption(argv, argc);
    }

    // Grab Data
    if (filename == NULL){
        plane = getParameters();
        plane.instance_size = plane.NUM_PT;
    } else {
        lines = readFile(filename);

        // Check to see if a file was succesffully parsed
        if(lines == NULL){
            printf("File not found\n");
            printf("Exiting...\n");
            return -1;
        }
        plane.generation = getGeneration(filename);
        plane = getFileParameters(lines);
        correctFile = checkFile(plane);
        free(lines);
    }

    // Ensure instances is of the correct size;
    if(!correctFile){
        printf("File is corrupt, the instance file does not match specification\n");
        return -2;
    }


    if(filename != NULL){
    // If we opened up a file, the Plane instance is all ready generated for us.
        printPlane(plane, filename, options);
        if(plane.instance_size > 1){
            MST = prims(plane);
            printMST(MST,plane.instance_size, filename, options);

            degrees = nodeDegree(MST, plane.instance_size-1);
        } else {
            printf("Can not generate MST, insufficient nodes \n");
        }
    } else {
        while(plane.generation < plane.total_gen){
    // If not we need to generate instances for the number of planes required
            plane.instance = genInstance(plane.NUM_PT, plane.MAX_X, plane.MAX_Y);
            printPlane(plane, NULL, options);
            filename = genFilename(plane.NUM_PT, plane.generation);
            if(plane.instance_size > 1){
                MST = prims(plane);
                printMST(MST,plane.instance_size, filename, options);
                degrees = nodeDegree(MST, plane.instance_size-1);
            } else {
                printf("Can not generate MST, insufficient nodes \n");
            }
            plane.generation++;
        }
    }

    RST* root;
    int rootIndex = findRoot(MST, plane.instance_size-1);
    printf("Root index %i\n", rootIndex);
    root = buildNode(NULL, rootIndex, NULL, 0, 0 );
    buildTree(root, plane.instance, MST, plane.instance_size-1);

    // Need to create code to free plane.instance and and sub arrays of instance
    printList(root,0);
    //printf("Overlap is %i\n", maxOverlap(root));
    //printf("Distance is %i\n", totalDistance(root));
    freeMST(MST, plane.instance_size-1);
    freePlane(&plane);
    freeRST(root);


    // need to free MST
    return 0;
}