`
jaychang
  • 浏览: 715603 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

普利姆算法求连通图最小生成树

阅读更多
/***************************************                                           
  title:普利姆算法求连通图最小生成树
  author: jay chang 
  date:   2009.7.12 
****************************************/
#include<iostream>

using namespace std;

#define MAX 9999
#define MAXSIZE 99
#define FALSE 0
#define TRUE  1

typedef char VertexData;

typedef int AdjType;

typedef struct VertexNode
{
	VertexData vertexData;
}VertexNode;

typedef struct ArcNode
{
	AdjType adj;					//弧的权值
}ArcNode;

typedef struct AdjMatrix
{
	VertexNode vertexNodes[MAXSIZE+1];
	ArcNode arcNodes[MAXSIZE+1][MAXSIZE+1];
	int vertexNum,arcNum;
}AdjMatrix;

typedef struct ClosedgeNode
{
	VertexData vertexData;
	AdjType adj;
}ClosegdeNode,Closedge[MAXSIZE];


/**********************************************************************************
  Function Name:LocateGraph
  Return Type:	int
  Function Description: 查询在图中是否存在结点信息为vertexData的结点,存在返回结
  点位置,否则返回FALSE。
**********************************************************************************/
int LocateGraph(AdjMatrix* g,VertexData vertexData)
{
	int iIndex;
	for(iIndex=1;iIndex<=g->vertexNum;iIndex++)
	{
		if(vertexData==g->vertexNodes[iIndex].vertexData)
			return iIndex;
	}
	return FALSE;
}


/**********************************************************************************
  Function Name:CreateAdjMatrix
  Return Type:	void
  Function Description: 创建连通图
**********************************************************************************/
void CreateAdjMatrix(AdjMatrix* g)
{	
	cout<<"******************************************************************\n";
	cout<<"		普里姆算法求连通图最短路径\n";
	cout<<"******************************************************************\n";
	cout<<"输入连通图的顶点数,弧数:\n";
	cin>>g->vertexNum>>g->arcNum;
	cout<<"输入顶点信息"<<endl;
	int iCount,start,end,adj;char arcStart,arcEnd;
	for(int iIndex=1;iIndex<=g->vertexNum;iIndex++)
	{
		for(int jIndex=1;jIndex<=g->vertexNum;jIndex++)
		{
			g->arcNodes[iIndex][jIndex].adj=MAX;
		}
	}
	for(iCount=1;iCount<=g->vertexNum;iCount++)
	{	
		cout<<"结点"<<iCount<<"的信息"<<endl;
		cin>>g->vertexNodes[iCount].vertexData;
	}
	for(iCount=1;iCount<=g->arcNum;iCount++)
	{
		cout<<"输入第"<<iCount<<"条弧的起始,终点,弧的权值"<<endl;
		cin>>arcStart>>arcEnd>>adj;
		start=LocateGraph(g,arcStart);end=LocateGraph(g,arcEnd);
		g->arcNodes[start][end].adj=adj;
		g->arcNodes[end][start].adj=adj;
	}
}



/**********************************************************************************
  Function Name:Minimun
  Return Type:	int
  Function Description: 求U,V-U集合中的,两个顶点间权值最小的边
**********************************************************************************/
int Minimum(Closedge closedge,AdjMatrix* g)
{
	int iCount,flag=FALSE,min=MAX;
	for(iCount=1;iCount<=g->vertexNum;iCount++)
	{   
		if(closedge[iCount].adj!=0&&closedge[iCount].adj<min)
		{	
			min=closedge[iCount].adj;
		}
	}
	for(iCount=1;iCount<=g->vertexNum;iCount++)
	{
		if(closedge[iCount].adj!=0&&closedge[iCount].adj==min)
		{	
			flag=iCount;
			break;
		}
	}
	return flag;
}

/**********************************************************************************
  Function Name:Print
  Return Type:	void
  Function Description: 打印边
**********************************************************************************/
inline void Print(AdjMatrix* g,VertexData u0,VertexData v0)
{
	int start,end;
	start=LocateGraph(g,u0);end=LocateGraph(g,v0);
	cout<<u0<<"到"<<v0<<"的权值为"<<g->arcNodes[start][end].adj<<endl;
}

/**********************************************************************************
  Function Name:Prime
  Return Type:	void
  Function Description: 普里姆算法求连通图的最小生成树
**********************************************************************************/
void Prime(AdjMatrix* g,Closedge closedge,VertexData vertexData)
{	
	int iIndex,first;VertexData u0,v0;
	first=LocateGraph(g,vertexData);			//取结点vertexData顶点的位置
	closedge[first].adj=0;						//将结点vertexData并入U集合
	for(iIndex=1;iIndex<=g->vertexNum;iIndex++) //初始化closedge
	{	
		if(iIndex!=first)
		{
			closedge[iIndex].vertexData=vertexData;
			closedge[iIndex].adj=g->arcNodes[first][iIndex].adj;
		}
	}
	for(iIndex=1;iIndex<=g->vertexNum-1;iIndex++)
	{
		int k0=Minimum(closedge,g),varI;
		u0=closedge[k0].vertexData;
		v0=g->vertexNodes[k0].vertexData;
		Print(g,u0,v0);
		varI=LocateGraph(g,v0);
		closedge[varI].adj=0;		//将结点v0并入U集合
		for(int jIndex=1;jIndex<=g->vertexNum;jIndex++)
		{
			if(g->arcNodes[varI][jIndex].adj<closedge[jIndex].adj)
			{
				closedge[jIndex].adj=g->arcNodes[varI][jIndex].adj;
				closedge[jIndex].vertexData=v0;
			}
		}
	}
}

int main()
{	
	AdjMatrix* g=(AdjMatrix*)malloc(sizeof(AdjMatrix));
	Closedge closedge;
	CreateAdjMatrix(g);
	Prime(g,closedge,g->vertexNodes[1].vertexData);
	return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics