图的一系列算法(待续)

2014-11-23 22:13:31 · 作者: · 浏览: 4
#include
using namespace std;

#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
typedef enum {DG, DN, UDG, UDN} GraphKind;

//邻接矩阵
typedef struct ArcCell
{
	int key;    //对于无权图,用1或0表示相邻否,对带权图,则为权值类型
} ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];   //这种定义数组的方式一定要记住

typedef struct {
   int vexs[MAX_VERTEX_NUM];
   AdjMatrix arcs;
   int vexnum,arcnum; //图的当前定点数和弧边数
   GraphKind kind;
}MGraph;

//邻接表
typedef struct ArcNode
{
     int adjvex;
	 struct ArcNode *nextarc;
}ArcNode;

typedef struct VNode
{
	int key;
	ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX_NUM];

typedef struct {
	AdjList vertices;
	int vexnum,arcnum;
	GraphKind kind;
} ALGraph;

void CreateUD(MGraph &G)      //用邻接矩阵无向图
{  
   int i,j;
   cout<<"Input vexnum:"<>G.vexnum;
   cout<<"Input arcnum:"<>G.arcnum;
   for(  i=0; i>v1>>v2;
	  G.arcs[v1][v2].key=1;
	  G.arcs[v2][v1].key = G.arcs[v1][v2].key;  //有向图和无向图的区别
   }
   cout<<"Graph:"<
>G.vexnum; cout<<"Input arcnum:"<>G.arcnum; for( i=0; i>v1>>v2; if( v1==v2 ) { --k; cout<<"two index are same, renew input:"<adjvex=v2; node->nextarc=NULL; if(G.vertices[v1].firstarc==NULL) { G.vertices[v1].firstarc=node; } else { ArcNode *next=G.vertices[v1].firstarc; while(next->nextarc) { next=next->nextarc; } next->nextarc=node; } } for( int j=0; jadjvex<<" "; next=next->nextarc; } cout<