Exemple #1
0
void primM(int g[V][V])
{
	int parent[V]; 
	int key[V]; 
	bool mSet[V]; 

	for (int i = 0; i < V; i++)
		key[i] = INT_MAX, mSet[i] = false;

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

	for (int count = 0; count < V-1; count++)
	{

		int u = minKey(key, mSet);

		mSet[u] = true;

		for (int v = 0; v < V; v++)

		if (graph[u][v] && mSet[v] == false && g[u][v] < key[v])
			parent[v] = u, key[v] = g[u][v];
	}

	printM(parent, V, g);
}
Exemple #2
0
// 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);
        }
    }
}
Exemple #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);
}
Exemple #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
}
Exemple #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);
}
// 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);
}
// Function to construct and print DST for a graph represented using adjacency
// matrix representation
void primDST(int graph[V][V])
{
     int parent[V]; // Array to store constructed DST
     int key[V];   // Key values used to pick minimum weight edge in cut
     bool dstSet[V];  // To represent set of vertices not yet included in DST
 
     // Initialize all keys as INFINITE
     for (int i = 0; i < V; i++)
        key[i] = INT_MAX, dstSet[i] = false;
 
     // Always include first 1st vertex in DST.
     key[1] = 0;     // Make key 0 so that this vertex is picked as first vertex
     parent[1] = -1; // First node is always root of DST 
 
     // The DST will have V vertices
     for (int count = 0; count < V-1; count++)
     {
        // Pick thd minimum key vertex from the set of vertices
        // not yet included in DST
        int u = minKey(key, dstSet);
 
        // Add the picked vertex to the DST Set
        dstSet[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 DST
        for (int v = 0; v < V; v++)
 
           // graph[u][v] is non zero only for adjacent vertices of m
           // dstSet[v] is false for vertices not yet included in DST
           // Update the key only if graph[u][v] is smaller than key[v]
          if (graph[u][v] && dstSet[v] == false && graph[u][v] + key[u] < key[v])
             parent[v]  = u, key[v] = graph[u][v] + key[u];
     }
 
     // print the constructed DST
     printDST(parent, V, graph);
}
Exemple #8
0
//use Prim's algorithm or Kruskal algorithm. Copied from 'http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-spanning-tree-mst-2/'
void MST::makeTree() { 
     // Initialize all keys as INFINITE
	bool noNext = false;
     for (int i = 0; i < N; 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 < N-1; count++)
     {
        // Pick thd minimum key vertex from the set of vertices
        // not yet included in MST
        int u = minKey(key, mstSet);
//		cout << u << " - ";	

        // 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 < N; v++){
           // mstSet[v] is false for vertices not yet included in MST
           // Update the key only if adjacentMatrix[u][v] is smaller than key[v]
		//	int g = adjacentMatrix[u][v];
          if (adjacentMatrix[u][v] && mstSet[v] == false && adjacentMatrix[u][v] <  key[v]){
             parent[v]  = u, key[v] = adjacentMatrix[u][v];
		//	cout <<"SOMETHING F*****G HAPPENS IN THE DFS " <<endl;
    	//	cout << key[v] << endl; 
		  }
		//	graph[u][v] = 1;
		}
	}
}