1. 程式人生 > >資料結構BFS與DFS鄰接表

資料結構BFS與DFS鄰接表

#include<bits/stdc++.h>
#define MVNum 100
#define OK 1
#define ERROR 0
using namespace std;
typedef int Status;
typedef int OtherTnfo;
typedef char VerTexType;
bool visit[MVNum];

typedef struct ArcNode
{
	int adjvex;
	struct ArcNode *nextarc;
	OtherTnfo info;
}ArcNode;

typedef struct VNode
{
	VerTexType data;
	struct ArcNode *firstarc;
}VNode,AdjList[MVNum];

typedef struct
{
	AdjList vertices;
	int vexnum,arcnum;
}ALGraph;

int LocateVex(ALGraph &G,char &v)
{
	int i;
	for (i=0;i<G.vexnum;i++)
	{
		if (G.vertices[i].data==v)
			return i;
	}
	if (i>=G.vexnum) return ERROR;
	else return 0;
}

Status CreateUDG(ALGraph &G)
{
	int i,j;
	char v1,v2;
	ArcNode *p1,*p2;
	printf("輸入圖的頂點數:");
	scanf("%d",&G.vexnum);
	printf("輸入邊的條數:");
	scanf("%d",&G.arcnum);
	printf("輸入各個點的資訊:\n");
	for (int i=0;i<G.vexnum;i++)
	{
		cin >> G.vertices[i].data;
		G.vertices[i].firstarc = NULL;
	}
	printf("\n輸入由兩個頂點構成的邊:\n");
	for (int k=0;k<G.arcnum;k++)
	{
		cin >> v1 >> v2;
		i = LocateVex(G,v1);
		j = LocateVex(G,v2);
		p1 = new ArcNode;
		p1->adjvex = j;
		p1->nextarc = G.vertices[i].firstarc;
		G.vertices[i].firstarc = p1;
		p2 = new ArcNode;
		p2->adjvex = i;
		p2->nextarc = G.vertices[j].firstarc;
		G.vertices[j].firstarc = p2;
	}
	printf("\n圖建立成功!\n");
	return OK;
}

void dfs(ALGraph G,int v)//非遞迴 
{
	stack<int> s;
	visit[v] = 1;
	s.push(v); 
	printf("%c ",G.vertices[v].data);
	ArcNode *p = G.vertices[v].firstarc;
	while (!s.empty())
	{
		while (p)
		{
			if (visit[p->adjvex])
				p = p->nextarc;
			else
			{
				printf("%c ",G.vertices[p->adjvex].data);
				visit[p->adjvex] = 1;
				s.push(p->adjvex);
				p = G.vertices[p->adjvex].firstarc;
			}
		}
		if (!s.empty())
		{
			p = G.vertices[s.top()].firstarc;
			s.pop();
		}
	}
}

void DFS(ALGraph G,int v)//遞迴 
{
	visit[v] = 1;
	printf("%c ",G.vertices[v].data);
	ArcNode *p = G.vertices[v].firstarc;
	while (p)
	{
		int w = p->adjvex;
		if (!visit[w]) DFS(G,w);
		p = p->nextarc;
	}
}

void BFS(ALGraph G,int v)
{
	ArcNode *p;
	queue<int> q;
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
	{
		if (!visit[i])
		{
			visit[i] = 1;
			printf("%c ",G.vertices[i].data);
			q.push(i);
			while (!q.empty())
			{
				i = q.front();
				q.pop();
				p = G.vertices[i].firstarc;
				while (p)
				{
					if (!visit[p->adjvex])
					{
						visit[p->adjvex] = 1;
						printf("%c ",G.vertices[p->adjvex].data);
						q.push(p->adjvex);
					}
					p = p->nextarc;
				}
			}
		}
	}
}

void DFSTravel(ALGraph G)
{
	printf("\nDFS遞迴遍歷:\n");
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i]) 
			DFS(G,i);
	printf("\n");
	printf("\nDFS非遞迴遍歷:\n"); 
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i]) 
			dfs(G,i);
	printf("\n");
}

void BFSTravel(ALGraph G)
{
	printf("\nBFS遍歷:\n");
	memset(visit,0,sizeof(visit));
	for (int i=0;i<G.vexnum;i++)
		if (!visit[i])
			BFS(G,i);
	printf("\n");
}

int main()
{
	ALGraph G;
	CreateUDG(G);
	DFSTravel(G);
	BFSTravel(G);
	return 0;
}