图的简单实现

// GraphMatrix.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <IOSTREAM>
#include <CSTDIO>
#include <CSTDLIB>
#include <CSTRING>

using namespace std;


// data
#define  MaxNum 20						// 图最大顶点
#define	 MaxValue 65535					// 最大值

typedef struct{
	char Vertex[MaxNum];				// 保存顶点信息
	int GType;							// 图的类型 0 无向 1 有向
	int VertexNum;						// 顶点的数量
	int EdgeNum;						// 边的数量
	int EdgeWeight[MaxNum][MaxNum];		// 保存边的权	
	int isTrav[MaxNum];					// 遍历标识
}GraphMatrix;							// 定义邻接矩阵图结构


// Create 
void CreateGragh(GraphMatrix *GM)
{
	int i,j,k;
	int weight;
	char EstartV, EendV;

	cout<<"enter graph each Vertex Data: "<<endl;
	for (i = 0; i < GM->VertexNum; i++)				// 输入顶点数
	{
		fflush(stdin);
	//	getchar();
		cout<<"NUM: "<<i+1<<"Vertex"<<endl;				
		cin>>GM->Vertex[i];							// 保存顶点于数组中
	}
	cout<<"enter each Vertex Edge and weight"<<endl;
	for (k = 0; k < GM->EdgeNum; k++)
	{
		fflush(stdin);
	//	getchar();
		cout<<"NUM "<<k + 1 <<"Edge"<<endl;
		cin>>EstartV;
		cin>>EendV;
		cin>>weight;
		
		for (i = 0; EstartV != GM->Vertex[i]; i++);	// 在已有的顶点中查找始点 没有找到的情况
		for (j = 0; EendV != GM->Vertex[j]; j++);	// 在已有的顶点中查找终点

		GM->EdgeWeight[i][j] = weight;

		if(GM->GType == 0)							// 无向图
		{
			GM->EdgeWeight[j][i] = weight;			// 在对角位置保存权值				
		}
	}
}

// Clear
void ClearGraph(GraphMatrix *GM)
{
	int i, j;
	for (i =0; i < GM->VertexNum; i++)
	{
		for (j = 0; j < GM->EdgeNum; j++)
		{
			GM->EdgeWeight[i][j] = MaxValue;		// 设置矩阵中各元素的值为MaxValue
		}
	}
}

// show
void OutGraph(GraphMatrix *GM)
{
	int i, j;
	for (j = 0; j < GM->VertexNum; j++)
	{
		cout<<"Vertex Data:"<<GM->Vertex[j]<<endl;   // 输出顶点信息
	}
	cout<<endl;
	for (i = 0; i < GM->VertexNum; i++)
	{
		cout<<GM->Vertex[i]<<endl;
		for (j = 0; j < GM->VertexNum; j++)
		{
			if (GM->EdgeWeight[i][j] == MaxValue)
			{
				cout<<"\tZ";							// 无穷大
			}
			else
			{
				cout<<"\t"<<GM->EdgeWeight[i][j];		 // 输出边的权值
			}
		}
		cout<<endl;
	}
}

// Full
void DeepTraOne(GraphMatrix *GM, int n)
{
	int i;
	GM->isTrav[n] = 1;
	cout<<GM->Vertex[n]<<endl;

	for (i = 0; i < GM->VertexNum; i++)
	{
		if(GM->EdgeWeight[n][i] != MaxValue && !GM->isTrav[n])
		{
			DeepTraOne(GM, i);									// 递归进行遍历
		}
	}
}

void DeepTraGraph(GraphMatrix *GM)
{
	int i;
	for (i = 0; i < GM->VertexNum; i++)
	{
		GM->isTrav[i] = 0;
	}
	cout<<"deep Tra!"<<endl;
	for (i = 0; i < GM->VertexNum; i++)
	{
		if(!GM->isTrav[i])
		{
			DeepTraOne(GM, i);
		}
	}
	cout<<endl;
}


int main(int argc, char* argv[])
{
	GraphMatrix GM;

	cout<<"Graph Type: \
		0 is Not Vector Graph; \
		1 is Vector Graph"<<endl;
	cin>>GM.GType;
	cout<<"Graph Vertex Num: "<<endl;
	cin>>GM.VertexNum;
	cout<<"Graph Edge Num: "<<endl;
	cin>>GM.EdgeNum;
	ClearGraph(&GM);
	CreateGragh(&GM);
	cout<<"GraphVertex Data Show: "<<endl;
	OutGraph(&GM);
	DeepTraGraph(&GM);

	return 0;
}




Graph Type:             0 is Not Vector Graph;          1 is Vector Graph
0
Graph Vertex Num:
4
Graph Edge Num:
6
enter graph each Vertex Data:
NUM: 1Vertex
A
NUM: 2Vertex
B
NUM: 3Vertex
C
NUM: 4Vertex
D
enter each Vertex Edge and weight
NUM 1Edge
A B 12
NUM 2Edge
A C 11
NUM 3Edge
A D 14
NUM 4Edge
B C 15
NUM 5Edge
B D 17
NUM 6Edge
C D 20
GraphVertex Data Show:
Vertex Data:A
Vertex Data:B
Vertex Data:C
Vertex Data:D

A
        Z       12      11      14
B
        12      Z       15      17
C
        11      15      Z       20
D
        14      17      20      Z
deep Tra!
A
B
C
D


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值