无向图深度优先遍历和广度优先遍历

#include<iostream> using namespace std; typedef struct _Node//存放边 { int vertex1; int vertex2; struct _Node * next; }Node; typedef struct _Node2//存放和顶点i关联的顶点,建立邻接表时用到 { int vertex; struct _Node2 * next; }Node2; typedef struct _Node1//存放某个顶点i,建立邻接表时用到 { int vertex; int degree; Node2 * connected; }Node1; typedef struct _Queue//队列,BFS用到 { Node1 node1; struct _Queue * next; }Queue; Node * head,* rear;//存放边的链表 int N;//顶点个数 Node1 * a;//邻接表 int i,j,k; bool * visited;//访问标记 Queue *start,*end;//队列 bool * visited1;//队列元素访问标记 void initiallist();//初始化存放边的链表 void input();//把边保存到链表 Node1 * createtable();//创建邻接表 void DFS();//深度优先遍历 void visitchain(Node1 node1);//访问邻接表中的某条链,被DFS()调用 void BFS();//广度优先遍历 void inqueue(Node1 node1);//进队 Queue * outqueue();//出队 bool queueisempty();//判断队列是否空 void del(); int main() { initiallist(); input(); a=createtable(); DFS(); BFS(); del(); return 0; } void initiallist() { head=rear=new Node; head->vertex1=0; head->vertex2=0; head->next=NULL; } void input() { cout<<“有几个顶点?”<<endl; cin>>N; cout<<“输入图中所有边:”<<endl; int x,y; while(1) { cin>>x; if(x==0) { break; } cin>>y; rear->next=new Node; rear=rear->next; rear->vertex1=x; rear->vertex2=y; rear->next=NULL; ++(head->vertex1); } } Node1 * createtable() { Node1 * b=new Node1[N]; for(i=0;i<N;++i)//初始化 { b[i].vertex=0; b[i].degree=0; b[i].connected=NULL; } Node * p=head->next; while(p!=NULL)//遍历链表 { if(b[p->vertex1-1].vertex==p->vertex1)//找到,就把vertex2接到b[p->vertex1-1]后面,前插 { Node2 * save=b[p->vertex1-1].connected; b[p->vertex1-1].connected=new Node2; b[p->vertex1-1].connected->next=save; b[p->vertex1-1].connected->vertex=p->vertex2; ++(b[p->vertex1-1].degree); } else//没找到,在b[p->vertex1-1]中新建一项vertex1,并把vertex2接到后面 { b[p->vertex1-1].vertex=p->vertex1; b[p->vertex1-1].connected=new Node2; ++(b[p->vertex1-1].degree); b[p->vertex1-1].connected->vertex=p->vertex2; b[p->vertex1-1].connected->next=NULL; } if(b[p->vertex2-1].vertex==p->vertex2)//找到,就把vertex1接到b[p->vertex2-1]后面,前插 { Node2 * save=b[p->vertex2-1].connected; b[p->vertex2-1].connected=new Node2; b[p->vertex2-1].connected->next=save; b[p->vertex2-1].connected->vertex=p->vertex1; ++(b[p->vertex2-1].degree); } else//没找到,在b[p->vertex2-1]中新建一项vertex2,并把vertex1接到后面 { b[p->vertex2-1].vertex=p->vertex2; b[p->vertex2-1].connected=new Node2; ++(b[p->vertex2-1].degree); b[p->vertex2-1].connected->vertex=p->vertex1; b[p->vertex2-1].connected->next=NULL; } p=p->next; } return b; } void DFS() { visited=new bool[N]; for(i=0;i<N;++i) { visited[i]=false; } cout<<“深度优先遍历:”; visitchain(a[0]); cout<<endl; } void visitchain(Node1 node1) { cout<<node1.vertex<<” “; visited[node1.vertex-1]=true; Node2 * ptrnode2=node1.connected; while(ptrnode2!=NULL) { if(visited[ptrnode2->vertex-1]==false) { visitchain(a[ptrnode2->vertex-1]); } ptrnode2=ptrnode2->next; } } void BFS() { visited1=new bool[N]; for(i=0;i<N;++i) { visited1[i]=false; } start=end=new Queue; start->node1.vertex=0; start->node1.degree=0; start->node1.connected=NULL; start->next=NULL; inqueue(a[0]); visited1[0]=true; cout<<“广度优先遍历:”; while(!queueisempty()) { Queue * queue=outqueue(); Node2 * ptr2node2=a[queue->node1.vertex-1].connected; delete queue; while(ptr2node2!=NULL) { if(visited1[ptr2node2->vertex-1]==false) { inqueue(a[ptr2node2->vertex-1]); visited1[ptr2node2->vertex-1]=true; } ptr2node2=ptr2node2->next; } } delete start; cout<<endl; } void inqueue(Node1 node1) { end->next=new Queue; end=end->next; end->node1.vertex=node1.vertex; end->node1.degree=node1.degree; end->node1.connected=NULL; end->next=NULL; } Queue * outqueue() { if(start->next==end) { Queue * save=end; end=start; cout<<save->node1.vertex<<” “; return save; } else { Queue * save=start->next; start->next=start->next->next; cout<<save->node1.vertex<<” “; return save; } } bool queueisempty() { if(start==end) { return true; } else { return false; } } void del() { while(head!=NULL)//删除链表 { Node * save=head; head=head->next; delete save; } rear=NULL; cout<<“邻接表:”<<endl; for(i=0;i<N;++i)//删除邻接表 { cout<<a[i].vertex<<“->”; while(a[i].connected!=NULL) { Node2 * d; d=a[i].connected; a[i].connected=a[i].connected->next; cout<<d->vertex<<” “; delete d; } cout<<endl; } delete a; delete visited;//删除访问标记 delete visited1;//删除访问标记 }

运行结果:

有几个顶点?
8
输入图中所有边:
1 2
1 3
2 4
2 5
4 5
5 6
3 7
3 8
7 8
0
深度优先遍历:1 3 8 7 2 5 6 4
广度优先遍历:1 3 2 8 7 5 4 6
邻接表:
1->3 2
2->5 4 1
3->8 7 1
4->5 2
5->6 4 2
6->5
7->8 3
8->7 3
Press any key to continue

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/msdn5168986/article/details/5927761
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞