您的位置:首页 > 其它

图的分支生成:对稀疏的无向图,在度数不等于2的节点处,将图“拆散”,输出各条分支,分别用深度优先搜索和广度优先搜索实现。

2014-08-08 10:27 691 查看
图各顶点构造如下:






1.使用C++代码实现BFS如下:

#include<iostream>

#define MAX_VERTEX_NUM 20

using namespace std;

typedef char VertexType;

//我们用邻接表来作图的存储结构

//表结点类型

typedef struct ArcNode{

int adjvex;

struct ArcNode *nextarc;

//int info;

}ArcNode;

//邻接表结点

typedef struct VNode{

VertexType data;

ArcNode *firstarc;

}VNode,AdjList[MAX_VERTEX_NUM];

//图节点

typedef struct{

AdjList vertices;

int vexnum,arcnum;

}ALGraph;

//链队结点的类型

typedef struct Qnode{

int data;

struct Qnode *next;

}Qnode,*QueuePtr;

//链队指针类型

typedef struct{

QueuePtr front;

QueuePtr rear;

}LinkQueue;

int visited[MAX_VERTEX_NUM];

//查找顶点对应的下标值

int LocateVex(ALGraph G,char u)

{

int i;

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

{

if(u==G.vertices[i].data)

return i;

}

if (i==G.vexnum)

{

cout<<"error!"<<endl;

exit(1);

}

return 0;

}

//初始化队列

void InitQueue(LinkQueue &Q)

{

Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode));

if(!Q.front)

exit(1); //存储分配失败

Q.front->next=NULL;

}

//进队

void EnQueue(LinkQueue &Q,int e)

{

QueuePtr p;

p=(QueuePtr)malloc(sizeof(Qnode));

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p;

}

//判断是否为空

int QueueEmpty(LinkQueue &Q)

{

return (Q.front==Q.rear? 1:0);

}

//删除队列元素

void DeQueue(LinkQueue &Q,int &e)

{

QueuePtr p;

if(QueueEmpty(Q))

{

cout<<"\n Queue is free!";

exit(1);

}

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.front->next==NULL)

Q.rear=Q.front;

free(p);

}

void CreateALGraph_adjlist(ALGraph &G)

{

int i,j,k;

char v1,v2;

ArcNode *p;//临接表节点

cout<<"Input 顶点数& 边数:\n";//输入顶点和边数

cin>>G.vexnum;

cin>>G.arcnum;

cout<<"Input 各顶点(以空格隔开各个数据):\n";

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

{

cin>>G.vertices[i].data;//注意点,解说

G.vertices[i].firstarc=NULL;

}

//"r","s","t","u","v","w","x","y"

//{0,1},{0,4},{1,5},{2,3},{2,5},{2,6},{3,6},{3,7},{5,6},{6,7}

cout<<"Input 边(v1,v2)以回车分开各个数据:\n";

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

{

cin>>v1;

cin>>v2;

i=LocateVex(G,v1);

j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G.vertices[i].firstarc; //前插法,即每次都插入到头结点的后面

G.vertices[i].firstarc=p;

cout<<"Next:\n";

}

}

void BFSTraverse(ALGraph &G,int u)

{

LinkQueue Q;

for(int v=0;v<G.vexnum;++v) //之所有节点都未访问

visited[v]=false;

InitQueue(Q);//初始化队列

cout<<"广度遍历结果"<<endl;

for(int v=u,i=0;i<G.vexnum;v=(v+1+G.vexnum)%G.vexnum)

if(!visited[v])//未被访问的

{

EnQueue(Q,v);//未被访问的进队

while(!QueueEmpty(Q))//队列非空

{

int u;

DeQueue(Q,u);//u=队列最早进队的元素

visited[u]=true;//将u置为已被访问

cout<<G.vertices[u].data<<" ";//visit一下,输出节点元素

for(ArcNode *w=G.vertices[u].firstarc;w;w=w->nextarc)//访问节点的邻接表

if(!visited[w->adjvex])//当邻居吧中的元素未被访问时,入队

EnQueue(Q,w->adjvex);

}

}

cout<<endl;

}

int BFS_selected(ALGraph &G)//寻找图中节点度不为的顶点

{

for(int v=0;v<G.vexnum;++v)

{

for(ArcNode *w=G.vertices[v].firstarc;w;w=w->nextarc)//访问节点的邻接表

{

int i=0;

ArcNode *ww=w;

while(ww)

{

i++;

ww=ww->nextarc;

}

if(i==3)//度数为的顶点

{

return i;

break;

}

else

{

continue;

}

}

}

}

int main()

{

ALGraph G;

CreateALGraph_adjlist(G);

cout<<endl;

int u=BFS_selected(G);

BFSTraverse(G,u);

system("pause");

}

[b]2.使用C++代码实现DFS如下:[/b]

#define MAX_VERTEX_NUM 10

#include<iostream>

using namespace std;

typedef char VertexType;

typedef struct ArcNode{

int adjvex;

struct ArcNode *nextarc;

int info;

}ArcNode; //表结点类型

typedef struct VNode{

VertexType data;

ArcNode *firstarc;

}VNode,AdjList[MAX_VERTEX_NUM]; //头结点

typedef struct{

AdjList vertices; //邻接表

int vexnum,arcnum;

}ALGraph;

int visited[MAX_VERTEX_NUM];

//定位u

int LocateVex(ALGraph G,char u)

{

int i;

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

{

if(u==G.vertices[i].data)

return i;

}

if (i==G.vexnum)

{

cout<<"Error u!\n";

exit(1);

}

return 0;

}

//建立图的邻接表

void CreateALGraph_adjlist(ALGraph &G)

{

int i,j,k;

char v1,v2;

ArcNode *p;

//输入顶点个数和边数

cout<<"Input vexnum & arcnum:\n";

cin>>G.vexnum;//顶点

cin>>G.arcnum;//边

cout<<"Input Vertices:\n";//输入各顶点

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

{

cin>>G.vertices[i].data;

G.vertices[i].firstarc=NULL;

}

cout<<"Input 各边(v1,v2)以回车分开各个数据:\n";

//"r","s","t","u","v","w","x","y"

// //{0,1},{0,4},{1,5},{2,3},{2,5},{2,6},{3,6},{3,7},{5,6},{6,7}

for (k=0;k<G.arcnum;k++)//建立个顶点对应的邻接表

{

cin>>v1;

cin>>v2;

i=LocateVex(G,v1);

j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

//p->info = w;//带权值的图

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

cout<<"NEXT:"<<endl;

}

}

//从第v个顶点出发DFS

void DFS(ALGraph &G, int v)

{

ArcNode *p;

cout<<G.vertices[v].data<<" ";

visited[v]=1;//输出节点,并将节点置为已访问

p=G.vertices[v].firstarc;

while(p)//p不为空时

{

if (!visited[p->adjvex])//递归遍历所有未被访问的邻接点

DFS(G,p->adjvex);

p=p->nextarc;

}

}

int DFS_selected(ALGraph &G)//寻找图中节点度不为2的顶点

{

for(int v=0;v<G.vexnum;++v)

{

for(ArcNode *w=G.vertices[v].firstarc;w;w=w->nextarc)//访问节点的邻接表

{

int i=0;

ArcNode *ww=w;

while(ww)

{

i++;

ww=ww->nextarc;

}

if(i==3)//度数为3的顶点

{

return i;

break;

}

else

{

continue;

}

}

}

}

void DFSTraverse(ALGraph &G)

{

for (int v=0;v<G.vexnum;++v)

visited[v]=0;

cout<<"深度遍历结果:"<<endl;

//(int v=u,i=0;i<G.vexnum;v=(v+1+G.vexnum)%G.vexnum)

int u=DFS_selected(G);

for (int v=u,i=0;i<G.vexnum;v=(v+1+G.vexnum)%G.vexnum)

if (!visited[v])

DFS(G,v);

cout<<endl;

}

int main()

{

ALGraph G;//建图

CreateALGraph_adjlist(G);

DFSTraverse(G);//深度优先遍历

system("pause");

}

DFS:

#define MAX_VERTEX_NUM 10

#include<iostream>

using namespace std;

typedef char VertexType;

typedef struct ArcNode{

int adjvex;

struct ArcNode *nextarc;

int info;

}ArcNode; //表结点类型

typedef struct VNode{

VertexType data;

ArcNode *firstarc;

}VNode,AdjList[MAX_VERTEX_NUM]; //头结点

typedef struct{

AdjList vertices; //邻接表

int vexnum,arcnum;

}ALGraph;

int visited[MAX_VERTEX_NUM];

//定位u

int LocateVex(ALGraph G,char u)

{

int i;

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

{

if(u==G.vertices[i].data)

return i;

}

if (i==G.vexnum)

{

cout<<"Error u!\n";

exit(1);

}

return 0;

}

//建立图的邻接表

void CreateALGraph_adjlist(ALGraph &G)

{

int i,j,k;

char v1,v2;

ArcNode *p;

//输入顶点个数和边数

cout<<"Input vexnum & arcnum:\n";

cin>>G.vexnum;//顶点

cin>>G.arcnum;//边

cout<<"Input Vertices:\n";//输入各顶点

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

{

cin>>G.vertices[i].data;

G.vertices[i].firstarc=NULL;

}

cout<<"Input 各边(v1,v2)以回车分开各个数据:\n";

//"r","s","t","u","v","w","x","y"

////{0,1},{0,4},{1,5},{2,3},{2,5},{2,6},{3,6},{3,7},{5,6},{6,7}

for (k=0;k<G.arcnum;k++)//建立个顶点对应的邻接表

{

cin>>v1;

cin>>v2;

i=LocateVex(G,v1);

j=LocateVex(G,v2);

p=(ArcNode*)malloc(sizeof(ArcNode));

p->adjvex=j;

//p->info = w;//带权值的图

p->nextarc=G.vertices[i].firstarc;

G.vertices[i].firstarc=p;

cout<<"NEXT:"<<endl;

}

}

//从第v个顶点出发DFS

void DFS(ALGraph &G, int v)

{

ArcNode *p;

cout<<G.vertices[v].data<<" ";

visited[v]=1;//输出节点,并将节点置为已访问

p=G.vertices[v].firstarc;

while(p)//p不为空时

{

if (!visited[p->adjvex])//递归遍历所有未被访问的邻接点

DFS(G,p->adjvex);

p=p->nextarc;

}

}

int BFS_selected(ALGraph &G)//寻找图中节点度不为的顶点

{

for(int v=0;v<G.vexnum;++v)

{

for(ArcNode *w=G.vertices[v].firstarc;w;w=w->nextarc)//访问节点的邻接表

{

int i=0;

ArcNode *ww=w;

while(ww)

{

i++;

ww=ww->nextarc;

}

if(i==3)//度数为的顶点

{

return i;

break;

}

else

{

continue;

}

}

}

}

void DFSTraverse(ALGraph &G)

{

for (int v=0;v<G.vexnum;++v)

visited[v]=0;

cout<<"深度遍历结果:"<<endl;

//(int v=u,i=0;i<G.vexnum;v=(v+1+G.vexnum)%G.vexnum)

int u=BFS_selected(G);

for (int v=u,i=0;i<G.vexnum;v=(v+1+G.vexnum)%G.vexnum)

if (!visited[v])

DFS(G,v);

cout<<endl;

}

int main()

{

ALGraph G;//建图

CreateALGraph_adjlist(G);

DFSTraverse(G);//深度优先遍历

system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: