头文件(AMGraph.h)
# ifndef _AMGRAPH_
# define _AMGRAPH_
# include <iostream>
using namespace std;
# define MaxVertexNum 256
typedef char VertexType;
typedef int EdgeType;
typedef struct
{
VertexType vertex[MaxVertexNum];
EdgeType edge[MaxVertexNum][MaxVertexNum];
int vertexNum;
int edgeNum;
}AMGraph, * PAMGraph;
int LocateVertex(PAMGraph g, VertexType x);
PAMGraph CreateGraph(void);
void BFS(PAMGraph g, int i);
void BFSTraversal(PAMGraph g);
# define INFINITY 10000
typedef struct
{
int adjvertex;
int lowcost;
}AuxArray;
void MiniSpanTreePrim(PAMGraph g, int i);
# endif
队列头文件(SeqQueue.h)
# ifndef _SEQQUEUE_
# define _SEQQUEUE_
# include <iostream>
using namespace std;
typedef int DataType;
# define MAXSIZE 256
typedef struct
{
DataType data[MAXSIZE];
int front;
int rear;
}SeqQueue, * PSeqQueue;
PSeqQueue InitSeqQueue(void);
void DestroySeqQueue(PSeqQueue * pQ);
bool EmptySeqQueue(PSeqQueue Q);
bool FullSeqQueue(PSeqQueue Q);
bool InSeqQueue(PSeqQueue Q, DataType x);
bool OutSeqQueue(PSeqQueue Q, DataType * val);
bool FrontSeqQueue(PSeqQueue Q, DataType * val);
# endif
实现文件(AMGraph.cpp)
# include "AMGraph.h"
# include "SeqQueue.h"
bool visited[MaxVertexNum] = { 0 };
int LocateVertex(PAMGraph g, VertexType x)
{
int i = 0;
while ((i < g->vertexNum) && (g->vertex[i] != x))
{
i += 1;
}
if (i >= g->vertexNum)
{
return -1;
}
else
{
return i;
}
}
PAMGraph CreateGraph(void)
{
PAMGraph g = (PAMGraph)malloc(sizeof(AMGraph));
if (NULL != g)
{
//输入顶点数和边数;
cout << "Please input the vertexNum and edgeNum: " << endl;
cin >> g->vertexNum >> g->edgeNum;
//输入顶点值;
for (int i = 0; i < g->vertexNum; i++)
{
cout << "Please input the element value: " << endl;
cin >> g->vertex[i];
}
//输入边节点的值;
for (int i = 0; i < g->vertexNum; i++)
{
for (int j = 0; j < g->vertexNum; j++)
{
if (i == j)
{
g->edge[i][j] = 0;
}
else
{
g->edge[i][j] = INFINITY;
}
}
}
int i = 0;
int j = 0;
for (int k = 0; k < g->edgeNum; k++)
{
cout << "Please input the two index: " << endl;
cin >> i >> j;
int weight = 0;
cout << "Please input weight value: " << endl;
cin >> weight;
g->edge[i][j] = weight;
g->edge[j][i] = weight;//如果是无向图要加上;
}
return g;
}
else
{
cout << "Memory allocate is error! " << endl;
system("pause");
exit(0);
}
}
void BFS(PAMGraph g, int i)
{
PSeqQueue q = InitSeqQueue();
cout << g->vertex[i] << endl;
visited[i] = true;
InSeqQueue(q, i);
while (!EmptySeqQueue(q))
{
OutSeqQueue(q, &i);
for (int j = 0; j < g->vertexNum; j++)
{
if (g->edge[i][j] == 1 && !visited[j])
{
cout << g->vertex[j] << endl;
visited[j] = true;
InSeqQueue(q, j);
}
}
}
}
void BFSTraversal(PAMGraph g)
{
for (int i = 0; i < g->vertexNum; i++)
{
visited[i] = false;
}
for (int i = 0; i < g->vertexNum; i++)
{
if (!visited[i])
{
BFS(g, i);
}
}
}
void MiniSpanTreePrim(PAMGraph g, int u)
{
//定义迭代变量;
int i = 0;
int j = 0;
int k = 0;
//动态生成辅助数组;
AuxArray * array = new AuxArray[g->vertexNum];
//将辅助数组从u开始初始化;
for (i = 0; i < g->vertexNum; i++)
{
array[i].adjvertex = u;
array[i].lowcost = g->edge[u][i];
}
for (i = 0; i < g->vertexNum - 1; i++)
{
//设置象征意义的无穷大;
int v = INFINITY;
//查找最小的边;
for (j = 0; j < g->vertexNum; j++)
{
//当前元素不是U集且小于v;
if ((array[j].lowcost != 0) && (array[j].lowcost < v))
{
v = array[j].lowcost;
k = j;
}
}
//将最小的边置0;即进入U集;
array[k].lowcost = 0;
//更新辅助数组;
for (j = 0; j < g->vertexNum; j++)
{
if (g->edge[k][j] < array[j].lowcost)
{
array[j].adjvertex = k;
array[j].lowcost = g->edge[k][j];
}
}
}
//总权值;
int w = 0;
//统计信息;
for (i = 0; i < g->vertexNum; i++)
{
//跳过开始的元素;
if (i != u)
{
cout << i << "->" << array[i].adjvertex << ", " << g->edge[i][array[i].adjvertex] << endl;
w += g->edge[i][array[i].adjvertex];
}
}
cout << "Total weight = " << w << endl;
}
实现文件(SeqQueue.cpp)
# include "SeqQueue.h"
PSeqQueue InitSeqQueue(void)
{
PSeqQueue Q = (PSeqQueue)malloc(sizeof(SeqQueue));
if (NULL != Q)
{
Q->front = 0;
Q->rear = 0;
return Q;
}
else
{
cout << "Memory allocate is error! " << endl;
system("pause");
exit(0);
}
}
void DestroySeqQueue(PSeqQueue * pQ)
{
PSeqQueue Q = *pQ;
if (NULL != Q)
{
free(Q);
Q = NULL;
}
*pQ = NULL;
return;
}
bool EmptySeqQueue(PSeqQueue Q)
{
if (Q->front == Q->rear)
{
return true;
}
else
{
return false;
}
}
bool FullSeqQueue(PSeqQueue Q)
{
if ((Q->rear + 1) % MAXSIZE == Q->front)
{
return true;
}
else
{
return false;
}
}
bool InSeqQueue(PSeqQueue Q, DataType x)
{
if (FullSeqQueue(Q))
{
return false;
}
else
{
Q->rear = (Q->rear + 1) % MAXSIZE;
Q->data[Q->rear] = x;
return true;
}
}
bool OutSeqQueue(PSeqQueue Q, DataType * val)
{
if (EmptySeqQueue(Q))
{
return false;
}
else
{
Q->front = (Q->front + 1) % MAXSIZE;
*val = Q->data[Q->front];
return true;
}
}
bool FrontSeqQueue(PSeqQueue Q, DataType * val)
{
if (EmptySeqQueue(Q))
{
return false;
}
else
{
*val = Q->data[(Q->front + 1) % MAXSIZE];
return true;
}
}
主函数(Main.cpp)
# include "AMGraph.h"
int main(int argc, char ** argv)
{
PAMGraph g = CreateGraph();
//for (int i = 0; i < g->vertexNum; i++)
//{
// for (int j = 0; j < g->vertexNum; j++)
// {
// cout << g->edge[i][j] << " ";
// }
// cout << endl;
//}
//cout << "----------------------------" << endl;
//BFSTraversal(g);
cout << "----------------------------" << endl;
MiniSpanTreePrim(g, 3);
system("pause");
return 0;
}