The prime algorithm to find the minimum spanning tree (adjacency matrix)

Source: Internet
Author: User

/*
Name:prime algorithm to find the minimum spanning tree (adjacency matrix)
Copyright:
Author: A clever clumsy
DATE:25/11/14 13:38

Description:

The general algorithm and the minimum heap optimization algorithm for the minimum spanning tree (adjacency matrix) of the prime algorithm are implemented.

*/
#include <stdio.h>
#include <stdlib.h>


#define MAX 2000//MAX Vertex count
#define INFINITY 999999//Infinity


typedef struct minheap{
int num;//store vertex numbers
int W; The distance from the storage vertex to the smallest spanning tree
} minheap; Minimum heap structure


int Map[max][max] = {0};//adjacency matrix storage diagram Information


void Creatgraph (int m, int n);//create adjacency matrix diagram
void creatgraph_2 (int m, int n);//Create adjacency Matrix graph (random graph)
void Printgraph (int n);//Output graph
void Prime (int n, int v0),//prime algorithm for minimum Spanning tree (original version)
void Prime_minheap (int n, int v0),//prime algorithm to find the minimum spanning tree (priority queue version)
void Buildminheap (Minheap que[], int n);
void Minheapsiftdown (Minheap que[], int n, int pos);
void Minheapsiftup (Minheap que[], int n, int pos);
void ChangeKey (Minheap que[], int pos, int weight)//Change the key value of the POS element to weight
int Searchkey (minheap que[], int pos, int weight),//Find the element subscript with the key value K in the minimum heap, return 1 (non-recursive) if not found
int Extractmin (minheap que[]);//delete and return the element with the smallest keyword in the smallest heap


int main ()
{
int I, j, M, n, v0 = 0;

printf ("Please enter the number of vertices:");
scanf ("%d", &n);
printf ("\ n Please enter the number of sides:");
scanf ("%d", &m);

Creatgraph_2 (M, n);//create adjacency matrix diagram
// Printgraph (n);//output diagram
Prime (n, v0);//prime algorithm for minimum Spanning tree (original version)
Prime_minheap (n, v0);//prime algorithm to find the minimum spanning tree (priority queue version)


return 0;
}


void Creatgraph (int m, int n)//create adjacency matrix diagram
{
int I, J, A, B, C;

for (i=0; i<n; i++)//Initialize vertex data
{
for (j=0; j<n; j + +)
{
MAP[I][J] = (i = = j)? 0:infinity;
}
}
printf ("\ n Please enter the edge information in a b C format: \ n");
for (i=0; i<m; i++)
{
scanf ("%d%d%d", &a,&b,&c);
MAP[A][B] = map[b][a] = C;
}
}


void creatgraph_2 (int m, int n)//create adjacency Matrix graph (random graph)
{
int I, J, A, B, C;

for (i=0; i<n; i++)//Initialize vertex data
{
for (j=0; j<n; j + +)
{
MAP[I][J] = (i = = j)? 0:infinity;
}
}

for (I=1; i<n; i++)//Make sure it is connected diagram
{
Map[i][0] = Map[0][i] = rand ()% 100 + 1;
}
M-= n-1;
while (M > 0)
{
for (i=0; i<n; i++)
{
for (j=i+1; j<n; j + +)
{
if (rand ()%10 = = 0)//has a 10% probability of appearing side
{
if (map[j][i] = = INFINITY)
{
MAP[I][J] = Map[j][i] = rand ()% 100 + 1;
m--;
if (M = = 0)
Return
}
}
}
}
}
}


void Printgraph (int n)//output diagram
{
int I, J;

for (i=0; i<n; i++)
{
printf ("g[%d] =%d:", I, I);
for (j=0; j<n; j + +)
{
if (map[i][j]! = 0 && map[i][j]! = INFINITY)
printf ("<%d,%d> =%d", I, J, Map[i][j]);
}
printf ("\ n");
}
printf ("\ n");
}


void Prime (int n, int v0)//prime algorithm for minimum Spanning tree (original version)
{
int Book[max] = {0}; Marks whether the vertex is already in the path
int Dic[max] = {0}; The distance from the storage vertex to the smallest spanning tree
int Adj[max] = {0}; The number of adjacency points that store vertices in the smallest spanning tree tree
int min, I, J, K;

for (i=0; i<n; i++)//initialization work
{
Dic[i] = Map[v0][i];
Adj[i] = V0;
}
Book[v0] = 1;

for (I=1; i<n; i++)//Each trip determines a new vertex, total n-1 times
{
min = INFINITY;
K = V0;
for (j=0; j<n; j + +)//Find the closest vertex to the smallest spanning tree K
{
if (book[j] = = 0 && dic[j] < min)
{
min = Dic[j];
K = J;
}
}

Book[k] = 1;printf ("<%d,%d> =%d", Adj[k], K, dic[k]);
for (j=0; j<n; j + +)//update the dic[] value of the adjacency point with the vertex K
{
if (book[j] = = 0 && dic[j] > Map[k][j])
{
DIC[J] = Map[k][j];
ADJ[J] = k;
}
}
}

min = 0;
for (i=0; i<n; i++)//Output The adjacency point and the length of the edge of each vertex in the smallest spanning tree
{
// printf ("<%d,%d> =%d\n", Adj[i], I, dic[i]);
Min + = Dic[i];
}
printf ("The total length of the minimum spanning tree (weights) is%d\n", min);
}


void Prime_minheap (int n, int v0)//prime algorithm to find the minimum spanning tree (priority queue version)
{
int Book[max] = {0}; Mark whether the city is already in the path
int Dic[max] = {0}; The distance from the storage vertex to the smallest spanning tree
int Adj[max] = {0}; The number of adjacency points that store vertices in the smallest spanning tree tree
Minheap que[max+1];//minimum heap used to store the vertex ordinal and the distance to the minimum spanning tree
int min, I, J, K, Pos, K;

Que[0].num = n; Stores the number of vertices in the smallest heap
for (i=0; i<n; i++)//initialization work
{
Dic[i] = Map[v0][i];
Adj[i] = V0;
Que[i+1].num = i;
QUE[I+1].W = Dic[i];
}
Book[v0] = 1;
Buildminheap (que, n);//Construct a minimum heap
Extractmin (que);//delete vertex v0


for (I=1; i<n; i++)//Each trip determines a new vertex, total n-1 times
{
K = Extractmin (que);//delete and return the element with the smallest keyword in the smallest heap
Book[k] = 1; printf ("<%d,%d> =%d", Adj[k], K, dic[k]);
for (j=0; j<n; j + +)//update the dic[] value of the adjacency point with vertex k while updating the minimum heap
{
if (book[j] = = 0 && dic[j] > Map[k][j])
{
pos = Searchkey (que, J, Dic[j]);
DIC[J] = Map[k][j];
ADJ[J] = k;
ChangeKey (que, POS, dic[j]);//Change the value of the first POS element to weight
}
}
}

min = 0;
for (i=0; i<n; i++)//Output The adjacency point and the length of the edge of each vertex in the smallest spanning tree
{
// printf ("<%d,%d> =%d\n", Adj[i], I, dic[i]);
Min + = Dic[i];
}
printf ("The total length of the minimum spanning tree (weights) is%d\n", min);
}


int Extractmin (minheap que[])//delete and return the element with the smallest keyword in the smallest heap
{
int pos = Que[1].num;

QUE[1] = que[que[0].num--];
Minheapsiftdown (que, que[0].num, 1);

return POS;
}


int Searchkey (minheap que[], int pos, int weight)//Find the element subscript in the minimum heap with the key value K, return 1 (non-recursive) if not found
{
int Stack[max] = {0};
int i = 1, top =-1;

while ((I <= que[0].num && que[i].w <= weight) | | Top >= 0)//similar to first-order traversal binary Tree Search method
{
if (i <= que[0].num && que[i].w <= weight)
{
if (QUE[I].W = = Weight && Que[i].num = = pos)//weights and vertex numbers must correspond
return i;
Stack[++top] = i;//the node into the stack.
I *= 2; Search for left child
}
Else
{
i = stack[top--] * 2 + 1; Node back stack and search for right child
}
}

return-1;
}


void ChangeKey (Minheap que[], int pos, int weight)//Change the key value of the POS element to weight
{
if (weight < QUE[POS].W)//keyword value is smaller, adjust the minimum heap up
{
QUE[POS].W = weight;
Minheapsiftup (que, que[0].num, POS);
}
else if (weight > QUE[POS].W)//keyword value is larger and the minimum heap is adjusted downward
{
QUE[POS].W = weight;
Minheapsiftdown (que, que[0].num, POS);
}
}


void Minheapsiftdown (Minheap que[], int n, int pos)//Downward adjustment node
{
Minheap temp = Que[pos];
int child = pos * 2; Pointing to the left child

while (Child <= N)
{
if (Child < n && que[child].w > QUE[CHILD+1].W)//have right children, and the right child is smaller, locate its right child
Child + = 1;

if (QUE[CHILD].W < TEMP.W)//Make sure parents are smaller than children by moving the child node value up
{
Que[pos] = Que[child];
pos = child;
Child = pos * 2;
}
Else
Break
}
Que[pos] = temp; Adjust temp downward to the appropriate location
}


void Minheapsiftup (Minheap que[], int n, int pos)//Adjust nodes up
{
Minheap temp = Que[pos];
int parent = POS/2; Point to parent node

if (pos > N)//element subscript that does not meet the criteria
Return

while (Parent > 0)
{
if (QUE[PARENT].W > TEMP.W)//Make sure parents are smaller than children by moving the parent node values down
{
Que[pos] = que[parent];
pos = parent;
parent = POS/2;
}
Else
Break
}
Que[pos] = temp; Adjust temp upward to the appropriate location
}




void Buildminheap (Minheap que[], int n)//constructs a minimum heap
{
int i;

for (I=N/2; i>0; i--)
{
Minheapsiftdown (que, n, i);
}
}

The prime algorithm to find the minimum spanning tree (adjacency matrix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.