深度、广度优先遍历(邻接表)

#include
#include
#include
#include
#define MAXVEX 20
typedef char VertexType;
using namespace std;

//边表结点
typedef struct EdgeNode{
	int adjvex;
	struct EdgeNode *next;
}EdgeNode;

//顶点结点
typedef struct VertexNode{
	VertexType data;
	EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];

typedef struct{
	AdjList adjlist;
	int numVertexes, numEdges;
}GraphAdjList; 

//将输入的字符转换为对应的下标 
int get_index(AdjList arr, char m){
	for(int i = 0; i < MAXVEX; i++){
		if(arr[i].data == m)
			return i;
	}
	return -1;
}

//创建图
void Create(GraphAdjList &G){
	int i, j, k;
	EdgeNode *p;
	
	cout << "请输入顶点数和边数:" << endl;
	cin >> G.numVertexes >> G.numEdges;
	
	//输入顶点信息
	cout << "输入顶点信息:" << endl;
	for(i = 0; i < G.numVertexes; i++){
		cin >> G.adjlist[i].data;
		G.adjlist[i].firstedge = NULL;   //初始化为空 
	}
	
	//建立边表
	cout << "输入边(vi,vj)中的下标i,j:" << endl;
	for(k = 0; k < G.numEdges; k++){
		cin >> i >> j;
		p = (EdgeNode *)malloc(sizeof(EdgeNode));
		p->adjvex = j;   //存储弧头 
		p->next = G.adjlist[i].firstedge;  //头插法插入边结点 
		G.adjlist[i].firstedge = p;
		
		//无向图 
		p = p = (EdgeNode *)malloc(sizeof(EdgeNode));
		p->adjvex = i;
		p->next = G.adjlist[j].firstedge;
		G.adjlist[j].firstedge = p;
	} 
}

//打印邻接表 
void print(GraphAdjList &G){
	cout << "邻接表为:" << endl;
	EdgeNode *p;
	for(int i = 0; i < G.numVertexes; i++){
		p = G.adjlist[i].firstedge;
		while(p){
			cout << "<" << G.adjlist[i].data << "," << G.adjlist[p->adjvex].data << ">";
			p = p->next;
		}
		cout << endl;
	}
}

//深度优先遍历 
void dfs(GraphAdjList &G, int v, int* visited){
	visited[v] = 1;
	cout << G.adjlist[v].data;
	EdgeNode *p = G.adjlist[v].firstedge;
	while(p){
		if(!visited[p->adjvex])
			dfs(G, p->adjvex, visited);
		p = p->next;
	}
	return ;
}

void dfsSearch(GraphAdjList &G, char input){
	int visited[MAXVEX] = {0};
	for(int i = 0; i < G.numVertexes; i++){
		visited[i] = 0;
	}
	dfs(G, get_index(G.adjlist, input), visited);
	
	for(int i = 0; i < G.numVertexes; i++){
		if(!visited[i]){
			dfs(G, i, visited);
		}
	}
}

//广度优先遍历 
void bfs(GraphAdjList &G, int v, int* visited){
	int u, w;
	EdgeNode *p;
	queue<int> Q;
	cout << G.adjlist[v].data;
	
	visited[v] = 1;
	Q.push(v);
	while(!Q.empty()){  //如果队列不为空 
		u = Q.front();  //取出队头元素赋值给u 
		Q.pop();
		p = G.adjlist[u].firstedge;
		while(p){  //一次检查u的所有邻接点 
			w = p->adjvex;
			if(!visited[w]){
				cout << G.adjlist[w].data;
				visited[w] = 1;
				Q.push(w);
			}
			p = p->next;
		}
	}
} 

void bfsSearch(GraphAdjList &G, char input){
	int visited[MAXVEX] = {0};
	for(int i = 0; i < G.numVertexes; i++){
		visited[i] = 0;
	}
	
	bfs(G, get_index(G.adjlist, input), visited);
	
	for(int i = 0; i < G.numVertexes; i++){
		if(!visited[i]){
			bfs(G, i, visited);
		}
	}
}

/*
A B C D E
0 1 
0 3 
1 4
1 2
2 3
2 4
*/

int main(){
	GraphAdjList G;
	Create(G);
	print(G);
	cout << "输入深度搜索的起始点:" << endl;
	char input;
	cin >> input;
	dfsSearch(G, input);
	
	cout << endl << "输入广度搜索的起始点:" << endl;
	cin >> input;
	bfsSearch(G, input);
	
} 

连通图案例

深度、广度优先遍历(邻接表)_第1张图片

非连通图案例

深度、广度优先遍历(邻接表)_第2张图片

你可能感兴趣的:(算法)