目录
7039:试编写算法,请从键盘输入数据,(1)建立一个有向图的邻接表存储。
7040:在有向图的邻接表存储的基础上,试设计算法计算各顶点的度,并依次输出入度、出度、度
前言:
我们学校的图的输入方式是:先输入两个整数(m,n)(分别表示待创建的图顶点数和边数),之后是m个顶点的信息,再之后是n 条边。
输出格式见示例:
输入: 6 6 1 2 3 4 5 6 1 2 1 3 2 4 3 4 4 5 3 6
1:有向图无向图的创建和输出
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX_VERTEXT_NUM 20
typedef struct ArcNode{//弧的结构
int adjvex;//该弧所指向的顶点位置
struct ArcNode *nextarc;//指向下一条弧
}ArcNode;
typedef struct VNode{//顶点结构
char data;//顶点信息
ArcNode *firstarc;//指向第一条依附该顶点的弧
}VNode,AdjList[MAX_VERTEXT_NUM];//数组结构
typedef struct{
AdjList vertices;//定义一个数组存放顶点信息
int vexnum,arcnum;//顶点数量和弧的数量
//int kind;//图的种类有四种有向图有向网无向图无向网
}ALGraph;//图的邻接表存储
//邻接矩阵存储
typedef struct ArcCell{
int adj;//如果是有向无向图那么存的是0 1 邻不邻接如果是有向网无向网那么他存的是权
//InfoType *info与弧相关信息的指针暂时用不到
}AdjMatrix[MAX_VERTEXT_NUM][MAX_VERTEXT_NUM];//存的是一个邻接矩阵
typedef struct{
char vexs[MAX_VERTEXT_NUM];//顶点信息
AdjMatrix arcs;//邻接矩阵边的信息
int vexnum,arcnum;
}MGraph;//图的邻接矩阵存储;
void CreateUDG(ALGraph &G){//无向图的建立用邻接表
cin>>G.vexnum>>G.arcnum;//输入顶点数量和弧的数量
for(int i=1;i<=G.vexnum;i++){
cin>>G.vertices[i].data;//顶点值输完了
G.vertices[i].firstarc=NULL;//把这个首先的弧指针置空
}
for(int i=1;i<=G.arcnum;i++){
int x,y;cin>>x>>y;
ArcNode * p1= new ArcNode;
ArcNode * p2= new ArcNode;
p1->adjvex=y;//该弧所指向的顶点位置
p1->nextarc=G.vertices[x].firstarc;//指向下一条弧
G.vertices[x].firstarc=p1;
p2->adjvex=x;//该弧所指向的顶点位置
p2->nextarc=G.vertices[y].firstarc;
G.vertices[y].firstarc=p2;
}
}
void CreatUDA(ALGraph &G){//有向图;
cin>>G.vexnum>>G.arcnum;
for(int i=1;i<=G.vexnum;i++){
cin>>G.vertices[i].data;
G.vertices[i].firstarc=NULL;
}
for(int i=1;i<=G.arcnum;i++){
int x,y;cin>>x>>y;//<x,y>弧尾弧头
ArcNode *p1=new ArcNode;
p1->adjvex=y;//该弧所指向的顶点位置
p1->nextarc=G.vertices[x].firstarc;//将头指针的值赋给他的下一位
G.vertices[x].firstarc=p1;//将头指针付给他
}
}
void print(ALGraph G)
{
for(int i=1;i<=G.vexnum;i++){
ArcNode *p=G.vertices[i].firstarc;
cout<<G.vertices[i].data<<':';
while(p!=NULL){
cout<<p->adjvex<<" ";//所指向的弧顶点信息
p=p->nextarc;
}
cout<<endl;
}
}
int main()
{
ALGraph G;
CreatUDA(G);
print(G);
}
7043:无向图广度优先遍历
- memset用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为' '或'\0';
单个字符串数组以及数组的话,比较直接 char a[100];memset(a,'\0',sizeof a);
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>
using namespace std;
#define MAX_VERTEXT_NUM 20
#define Maxn 100
typedef struct ArcNode
{ //弧的结构
int adjvex; //该弧所指向的顶点位置
struct ArcNode *nextarc; //指向下一条弧
typedef struct VNode
{ //顶点结构
char data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的弧
} VNode, AdjList[MAX_VERTEXT_NUM]; //数组结构
AdjList vertices; //定义一个数组存放顶点信息
int vexnum, arcnum; //顶点数量和弧的数量
//int kind;//图的种类有四种有向图有向网无向图无向网
} ALGraph; //图的邻接表存储
void CreateUDG(ALGraph &G)
{ //无向图的建立用邻接表
cin >> G.vexnum >> G.arcnum; //输入顶点数量和弧的数量
for (int i = 1; i <= G.vexnum; i++)
{
cin >> G.vertices[i].data; //顶点值输完了
G.vertices[i].firstarc = NULL; //把这个首先的弧指针置空
}
for (int i = 1; i <= G.arcnum; i++)
{
int x, y;
cin >> x >> y;
ArcNode *p1 = new ArcNode;
ArcNode *p2 = new ArcNode;
p1->adjvex = y; //该弧所指向的顶点位置
p1->nextarc = G.vertices[x].firstarc; //指向下一条弧
G.vertices[x].firstarc = p1;
p2->adjvex = x; //该弧所指向的顶点位置
p2->nextarc = G.vertices[y].firstarc;
G.vertices[y].firstarc = p2;
}
}
void BFS(ALGraph G)
{
queue<int> Q;
bool vis[Maxn]; //判别数组是不是已经走过
memset(vis, 0, sizeof(vis));
Q.push(1);
cout << "v1" << " ";
vis[1] = true;
while (!Q.empty())
{
int i = Q.front();
Q.pop(); //弹出对头
ArcNode *p = G.vertices[i].firstarc;
while (p)
{
if (!vis[p->adjvex])
{
cout << "v" << p->adjvex << " "; //他只输出他的弧顶点的值
vis[p->adjvex] = true;
Q.push(p->adjvex);
}
p = p->nextarc;
}
}
}
int main()
{
ALGraph G;
CreateUDG(G);
BFS(G);
}
7039:试编写算法,请从键盘输入数据,(1)建立一个有向图的邻接表存储。
(2)输出该邻接表。(3)输出深度优先遍历序列
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>
using namespace std;
#define MAX_VERTEXT_NUM 20
#define Maxn 100
typedef struct ArcNode
{ //弧的结构
int adjvex; //该弧所指向的顶点位置
struct ArcNode *nextarc; //指向下一条弧
typedef struct VNode
{ //顶点结构
char data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的弧
} VNode, AdjList[MAX_VERTEXT_NUM]; //数组结构
AdjList vertices; //定义一个数组存放顶点信息
int vexnum, arcnum; //顶点数量和弧的数量
//int kind;//图的种类有四种有向图有向网无向图无向网
} ALGraph;
void CreatUDA(ALGraph &G)
{ //有向图;
cin >> G.vexnum >> G.arcnum;
for (int i = 1; i <= G.vexnum; i++)
{
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
for (int i = 1; i <= G.arcnum; i++)
{
int x, y;
cin >> x >> y; //<x,y>弧尾弧头
ArcNode *p1 = new ArcNode;
p1->adjvex = y; //该弧所指向的顶点位置
p1->nextarc = G.vertices[x].firstarc; //将头指针的值赋给他的下一位
G.vertices[x].firstarc = p1; //将头指针付给他
}
}
bool vis[Maxn];//定义全局要不递归会出错
void DFS(ALGraph G,int u)//有向图DFS//递归的深度遍历算法
{
ArcNode *p=G.vertices[u].firstarc;
vis[u]=true;
cout<<u<<" ";
while(p){
if(!vis[p->adjvex]){//当他没有被遍历到的时候继续深度遍历它用递归
DFS(G,p->adjvex);
}
p=p->nextarc;
}
}
void print(ALGraph G)
{
for (int i = 1; i <= G.vexnum; i++)
{
ArcNode *p = G.vertices[i].firstarc;
cout << G.vertices[i].data << ':';
while (p != NULL)
{
cout << p->adjvex << " "; //所指向的弧顶点信息
p = p->nextarc;
}
cout << endl;
}
}
int main()
{
ALGraph G;
CreatUDA(G);
print(G);
DFS(G,1);
}
7040:在有向图的邻接表存储的基础上,试设计算法计算各顶点的度,并依次输出入度、出度、度
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>
using namespace std;
#define MAX_VERTEXT_NUM 20
#define Maxn 100
typedef struct ArcNode
{ //弧的结构
int adjvex; //该弧所指向的顶点位置
struct ArcNode *nextarc; //指向下一条弧
typedef struct VNode
{ //顶点结构
char data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的弧
} VNode, AdjList[MAX_VERTEXT_NUM]; //数组结构
AdjList vertices; //定义一个数组存放顶点信息
int vexnum, arcnum; //顶点数量和弧的数量
//int kind;//图的种类有四种有向图有向网无向图无向网
} ALGraph;
void CreatUDA(ALGraph &G)
{ //有向图;
cin >> G.vexnum >> G.arcnum;
for (int i = 1; i <= G.vexnum; i++)
{
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
for (int i = 1; i <= G.arcnum; i++)
{
int x, y;
cin >> x >> y; //<x,y>弧尾弧头
ArcNode *p1 = new ArcNode;
p1->adjvex = y; //该弧所指向的顶点位置
p1->nextarc = G.vertices[x].firstarc; //将头指针的值赋给他的下一位
G.vertices[x].firstarc = p1; //将头指针付给他
}
}
int out[Maxn],in[Maxn];//定义两个全局变量
void Get_info(ALGraph G)
{
for(int i=1;i<=G.vexnum;i++)
{ ArcNode *p=G.vertices[i].firstarc;
while(p){
out[i]++;//既然有弧顶点则证明弧尾有出度
in[p->adjvex]++;//弧头的位置;则弧头的位置是入度
p=p->nextarc;
}
}
}
int main()
{
ALGraph G;
CreatUDA(G);
Get_info(G);
for(int i=1;i<=G.vexnum;i++){
cout<<G.vertices[i].data<<":";//顶点信息
cout<<in[i]<<" "<<out[i]<<" "<<in[i]+out[i]<<endl;//存储顶点的位置
}
}
后续会在更复习的要去复习其他考试了
7042:图的非递归深度DFS遍历:
#include <iostream>
#include <algorithm>
#include <queue>
#include<cstring>
#include<stack>
using namespace std;
#define MAXN 105
typedef struct ArcNode{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *firstarc;
}VNode,AdjList[MAXN];
typedef struct{
AdjList Vertices;
int vexnum,arcnum;
}ALGraph;
void CreatUDG(ALGraph &G)
{
cin>>G.vexnum>>G.arcnum;
for(int i=1;i<=G.vexnum;i++){
cin>>G.Vertices[i].data;
G.Vertices[i].firstarc=NULL;
}
for(int i=1;i<=G.arcnum;i++)
{ int x,y;
cin>>x;cin>>y;
ArcNode *p1=new ArcNode;
ArcNode *p2=new ArcNode;
p1->adjvex=y;
p1->nextarc=G.Vertices[x].firstarc;
G.Vertices[x].firstarc=p1;
p2->adjvex=x;
p2->nextarc=G.Vertices[y].firstarc;
G.Vertices[y].firstarc=p2;
}
}
int vin[100];
void DFS(ALGraph G,int u)
{
stack<int>S;
S.push(u);
vin[u]=1;
cout<<"v"<<G.Vertices[u].data<<" ";//压入栈中第一个元素
while(!S.empty()){
int i=S.top();
ArcNode *p=G.Vertices[i].firstarc;//找到他的弧的头指针
while(p&&vin[p->adjvex]==1){
p=p->nextarc;
}
while(p&&vin[p->adjvex]==0){
cout<<"v"<<G.Vertices[p->adjvex].data<<" ";//若没有遍历过则输出且将数组变为1
vin[p->adjvex]=1;
S.push(p->adjvex);
p=G.Vertices[p->adjvex].firstarc;//将P变为弧顶点指针的顶点指针从而进行深度遍历
}
if(!p){
S.pop();
}
}
}
int main()
{
ALGraph G;
CreatUDG(G);
DFS(G,1);
}