算法与数据结构实验三-图

该程序分别实现了图的邻接矩阵和邻接表表示,用户可以交互式输入顶点和边信息。对于邻接矩阵,程序能输出矩阵和各顶点的度;对于无向图,边是双向的。邻接表实现中,程序支持无向图的深度优先搜索遍历,输出顶点序列。
摘要由CSDN通过智能技术生成

//算法与数据结构实验-  by 022116213-CS

/*1.设计一个程序,采用交互方式建立一个网的邻接矩阵表示,并且:

     (1)分行输出该邻接矩阵;

    (2)求出各顶点的度并输出。*/

#include <iostream>

#include<malloc.h>

using namespace std;

#define MAXVEX 100//最大顶点数

typedef char VertexType;//顶点类型

typedef int EdgeType;//边的权值

typedef struct

{

VertexType vexs[MAXVEX];//顶点表

EdgeType edges[MAXVEX][MAXVEX];//邻接矩阵

int n, e;//顶点数和边数

}MGraph;

MGraph CreateMGraph(int pd)//建立邻接矩阵

{

MGraph G;

int i, j, k, n;

cout << "请输入顶点数和边数:";

cin >> G.n >> G.e;

cout << "请输入顶点信息:";

for (i = 0; i < G.n; i++)

cin >> G.vexs[i];

for (i = 0; i < G.n; i++)

for (j = 0; j < G.n; j++)

G.edges[i][j] = 0;//初始化邻接矩阵

cout << "请输入每条边对应的两个顶点的序号及权重:\n例:0 1 2 表示标注的第0个顶点和第1个顶点之间有边且权重为2 (注意序号从0开始)\n";

for (n = 0; n < G.e; n++)

{

cin >> i >> j >> k;//输入边的信息

G.edges[i][j] = k;

if (pd == 0)//无向图,边是双向的

G.edges[j][i] = k;

}

return G;

}

void DisplayMGraph(MGraph G, int pd)//分行输出

{

for (int i = 0; i < G.n; i++)//第i个顶点(行)

{

cout << "第" << i + 1 << "行:";

for (int j = 0; j < G.n; j++)//第j列

if (pd == 0 && G.edges[i][j] == 0)

cout << "∞" << "\t";

else

cout << G.edges[i][j] << "\t";

cout << "\n";

}

}

int OutDegree(MGraph G, int i)//(出)度(求第i个顶点表中的结点数)

{

int degree = 0;

for (int j = 0; j < G.n; j++)

if (G.edges[i][j] != 0)

degree++;

return degree;

}

int InDegree(MGraph G, int i)//入度

{

int degree = 0;

for (int j = 0; j < G.n; j++)

if (G.edges[j][i] != 0)

degree++;

return degree;

}

void PrintOut(MGraph G, int pd)//度

{

int all;

if (pd == 0)//无向图

for (int i = 0; i < G.n; i++)

cout << "第" << i << "个顶点" << G.vexs[i] << "的度是" << OutDegree(G, i) << endl;

else//有向图

for (int i = 0; i < G.n; i++)

{

cout << "第" << i << "个顶点" << G.vexs[i] << "的出度是" << OutDegree(G, i) << ",入度是" << InDegree(G, i) << endl;

all = OutDegree(G, i) + InDegree(G, i);

cout << "第" << i << "个顶点" << G.vexs[i] << "的度是" << all << endl;

}

}

int main()

{

//判断是有向图还是无向图//

cout << "如果是无向图,请输入0;如果是有向图,请输入1:";

int pd;

cin >> pd;

if (pd != 0 && pd != 1)

{

cout << "输入有误,请退出重新输入0或1。";

return 0;

}

MGraph G = CreateMGraph(pd);

cout << "\n分行输出该邻接矩阵为:\n";

DisplayMGraph(G, pd);

PrintOut(G, pd);

system("pause");

return 0;

}

//算法与数据结构实验-  by 022116213-CS

/*2.设计一个程序,采用交互方式建立一个无向图的邻接表表示,并输出该图的深度优先搜索遍历得到的顶点序列。*/

#include <iostream>

using namespace std;

#define MaxVertexNum 100// 最大顶点数为100

#define VertexType char//顶点域为字符型

int visited[MaxVertexNum];//标记结点是否被访问过

typedef struct enode//边表中的结点

{

int adjvex;//边表顶点域

struct enode* next;//指针域

}EdgeNode;

typedef   struct vnode//顶点表

{

VertexType vertex;//顶点域

EdgeNode* firstedge;//边表头指针

}VertexNode;

typedef struct//邻接表

{

VertexNode vexs[MaxVertexNum];//节点表

int n, e;//顶点数和边数

}ALGraph;

void InsertNode(ALGraph& G, int i, int j)//在边表中插入结点

{

EdgeNode* s;

s = (EdgeNode*)malloc(sizeof(EdgeNode));//生成新边表结点s

s->adjvex = j;//邻接点序号为j

s->next = G.vexs[i].firstedge;

G.vexs[i].firstedge = s;//将新边表结点s插入到顶点Vi的边表头部

}

ALGraph CreateALGraph()//建立邻边表

{

ALGraph G;

int i, j;

cout << "请输入顶点数和边数:\n";

cin >> G.n >> G.e;

cout << "请输入顶点信息:\n";

for (i = 0; i < G.n; i++)//建立有n个顶点的顶点表

{   cin >> G.vexs[i].vertex;//输入顶点

G.vexs[i].firstedge = NULL;//顶点的边表头指针设为空 }

cout << "请输入边的信息(输入格式为:i (空格) j ):\n";

for (int k = 0; k < G.e; k++)//建立邻接表

{   cin >> i >> j;

InsertNode(G, i, j);

InsertNode(G, j, i);

}return G;}

void DFSAL(ALGraph G, int i) //以Vi为出发点对图G搜索

{   EdgeNode* p;

cout << G.vexs[i].vertex << "  ";//访问顶点Vi

visited[i] = 1;//标记Vi已访问

p = G.vexs[i].firstedge;//取Vi边表的头指针

while (p)//依次搜索Vi的邻接点Vj

{   if (visited[p->adjvex] == 0)//若Vj尚未访问,则以Vj为出发点继续搜索

DFSAL(G, p->adjvex);

p = p->next;//找Vi的下一个邻接点 }}

void DFSTraverseAL(ALGraph G)

{

int  i;

for (i = 0; i < G.n; i++)

visited[i] = 0;//初始化

for (i = 0; i < G.n; i++)

if (visited[i] == 0)

DFSAL(G, i);//Vi未访问过,从Vi开始搜索

}

int main()

{   ALGraph G = CreateALGraph();

cout << "该图的深度优先搜索遍历得到的顶点序列为:";

DFSTraverseAL(G);

system("pause");

return 0;   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dr.木公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值