1.图的定义
图(graph)是由一些点(vertex)和这些点之间的连线(edge)所组成的;其中,点通常称为顶点(vertex),而点到点之间的连线通常称之为边或者弧(edge)。通常记为G=(V,E)。
2.图的分类
图通常分为有向图和无向图,而其表示表示方式分为邻接矩阵和邻接链表
4.深度优先搜索(DFS)
图的深度优先算法有点类似于树的前序遍历,首先图中的顶点均未被访问,确定某一顶点,从该顶点出发,依次访问未被访问的邻接点,直到某个邻接点没有未被访问邻接点时,则回溯父节点(此处我们将先被访问的节点当做后被访问节点的父节点,例如对于节点A、B,访问顺序是A ->B,则称A为B的父节点),找到父节点未被访问的子节点;如此类推,直到所有的顶点都被访问到。
5.广度优先搜索(BFS)
从图中的某个顶点出发,访问它所有的未被访问邻接点,然后分别从这些邻接点出发访问它的邻接点。
广度优先搜索的存储方式一般是以队列的方式存储
代码实现:图的深度和广度搜索(邻接表)(无向图)
#include<stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;
#define OK 1
#define OVERFLOW -1
#define ERROR 0
#define TURE 1
#define FALSE 0
//-----图的邻接表存储表示-----
#define MAX_VERTEX_NUM 100 //最大顶点个数
typedef int InfoType,VertexType;
typedef struct ArcNode
{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //该弧相关信息的指针
} ArcNode;
typedef struct VNode
{
VertexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum,arcnum; //图的当前顶点数和弧数
int kind; //图的种类标志
}ALGragh;
//-----队列-----
typedef int QElemType;
typedef int Status;
#define MAXQSIZE 100 //最大队列长度
typedef struct
{
QElemType *base; //初始化的动态分配存储空间
int front; //头指针
int rear; //尾指针
}SqQueue;
//构造一个空队列
Status InitQueue(SqQueue &Q)
{
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!Q.base)exit(OVERFLOW);
Q.front=Q.rear=0;
return 0;
}
Status QueueEmpty(SqQueue &Q)
//若队列为空,则返回TURE,否则返回FALSE
{
if(Q.front!=Q.rear)
return FALSE;
else
return TURE;
}
//插入元素e为Q的新队尾元素
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return OK;
}
//若队列不空,则删除Q的对头元素,用e返回其值,并返回OK,否则返回ERROR
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
return OK;
}
//-----创建无向图-----
int LocateVex_M(ALGragh &G,int v)
{
int i,k=ERROR;
for(i=0;i<G.vexnum;i++)
if(G.vertices[i].data==v)
{k=i;break;}
return k;
}
Status CreateUDG(ALGragh &G)
{
int i,j,k,v1,v2;
ArcNode *s,*t;
cout<<"请输入无向图的顶点数,弧数:"<<endl;
cout<<"vexnum:";cin>>G.vexnum;
cout<<"arcnum:";cin>>G.arcnum;
cout<<endl;
cout<<"请输入各顶点的值:"<<endl;
for(i=0;i<G.vexnum;++i)
{
cin>>G.vertices[i].data;
G.vertices[i].firstarc=NULL;
}
cout<<"请依次输入每条弧依附的两个顶点(形如:1 1):"<<endl;
for(k=0;k<G.arcnum;++k)
{
cin>>v1>>v2;
i=LocateVex_M(G,v1);
j=LocateVex_M(G,v2);
s=(struct ArcNode*)malloc(sizeof(struct ArcNode));
t=(struct ArcNode*)malloc(sizeof(struct ArcNode));
s->adjvex=j;
s->nextarc=G.vertices[i].firstarc;
G.vertices[i].firstarc=s;
t->adjvex=i;
t->nextarc=G.vertices[j].firstarc;
G.vertices[j].firstarc=t;
}
return OK;
}
//-----深度优先搜索-----
bool visited[MAX_VERTEX_NUM];
Status (*VisitFunc)(int v);
void DFSTraverse(ALGragh G)
{
int v;
void DFS(ALGragh G,int v);
//VisitFunc=Visit;
for(v=0;v<G.vexnum;v++) visited[v]=false;
for(v=0;v<G.vexnum;v++) {if(!visited[v]) DFS(G,v);}
}
void DFS(ALGragh G,int v)
{
ArcNode *w;
visited[v]=true; ///VisitFunc(v);
cout<<G.vertices[v].data<<" ";
for(w=G.vertices[v].firstarc;w!=NULL;w=w->nextarc)
{
if(!visited[w->adjvex]) DFS(G,w->adjvex);
}
}
//-----广度优先搜索-----
void BFSTraverse(ALGragh G)
{
int u,v;
ArcNode *w;
SqQueue Q;
for(v=0;v<G.vexnum;v++)
visited[v]=false;
InitQueue(Q);
for(v=0;v<G.vexnum;v++)
if(!visited[v])
{
visited[v]=true; //Visit(v);
cout<<G.vertices[v].data<<" ";
EnQueue(Q,v);
while(!QueueEmpty(Q))
{
DeQueue(Q,u);
for(w=G.vertices[v].firstarc;w!=NULL;w=w->nextarc)
if(!visited[w->adjvex])
{
cout<<G.vertices[w->adjvex].data<<" ";
visited[w->adjvex]=true;
//Visit(w->adjvex);
EnQueue(Q,w->adjvex);
}
}
}
}
int main()
{
ALGragh G;
CreateUDG(G);
cout<<"深度优先搜索:";
DFSTraverse(G);
cout<<endl;
cout<<"广度优先搜索:";
BFSTraverse(G);
cout<<endl;
return 0;
}