Traversing C language data structures and algorithms of FIG ---

A depth-first search (DFS)

1. The concept and realization of the principle

Here Insert Picture Description
A first from the beginning, in the absence of duplicate vertices encountered, always want to take the right-hand side, then to B. The general principle is to the right of the C, D, E, F was found back in A, has been marked as the A gone, so returning F., The right to go to a second number of G, find B, D has gone down when continued, so to H, H at this time found that the channel has gone through, but the node has not been traversed, so returns to H at the beginning of G, E returning to have gone through, returning to D, I found not to go, visit I, and then continue to return to the vertex A.

Depth-first traversal is actually a recursive process

How to avoid repeat visits?
An auxiliary array for each vertex flag is accessed.

2. The depth-first traversal of the implementation code

1. If the graph adjacency matrix

bool visited[MAXWEX]; //标志访问过的数组
// i 为待访问结点的下标
void DFS(MGraph G, int i)
{
	int j;
	visited[i] = true;
	printf("%c",G.vesx[i]);
	for (j = 0; j < G.numv; j++)
	{
		if (G.arc[i][j] == 1 && visited[j] == false)
		{
			DFS(G,j);  //对为访问的邻接顶点递归调用
		}
	}
}

//邻接矩阵的深度优先遍历
void DFSTraverse(MGraph G)
{
	int i;
	//初始化所有结点为未访问状态
	for (i = 0; i < G.numv; i++)
	{
		visited[i] = false;
	}

	for (i = 0; i < G.numv; i++)
	{
		//对未访问过的顶点调用DFS,若是连通图,只调用一次
		if (visited[i] == false)
		{
			DFS(G,i);
		}
	}
}

2. If the graph adjacency list

bool visited[MAXWEX]; //标志访问过的数组
// i 为待访问结点的下标
void DFS(GraphAdjList *G, int i)
{
	EdgeNode* p;
	visited[i] == true;
	printf("%c",G->adjList[i].data);
	p = G->adjList[i].firstedge;
	while (p)
	{
		if (visited[p->adjvex] == false)
		{
			DFS(G,p->adjvex);
		}
		p = p->next;
	}
}

//邻接矩阵的深度优先遍历
void DFSTraverse(GraphAdjList *G)
{
	int i;
	//初始化所有结点为未访问状态
	for (i = 0; i < G->numv; i++)
	{
		visited[i] = false;
	}

	for (i = 0; i < G->numv; i++)
	{
		//对未访问过的顶点调用DFS,若是连通图,只调用一次
		if (visited[i] == false)
		{
			DFS(G, i);
		}
	}
}

II. Breadth-first search (BFS)

1. The concept and realization of the principle

Similar tree traversal sequence

Method: From the graph a node, first of all neighboring access points of the sequence node, and then accessed in the order in which the vertices are not accessed once all visits vertices into contact with their adjacent, repeating this process

2. The breadth-first traversal implementation code

/* 循环队列的顺序存储结构 */
typedef struct
{
	int data[MAXSIZE];
	int front;    	/* 头指针 */
	int rear;		/* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}Queue;

/* 初始化一个空队列Q */
Status InitQueue(Queue *Q)
{
	Q->front=0;
	Q->rear=0;
	return  OK;
}

/* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
Status QueueEmpty(Queue Q)
{ 
	if(Q.front==Q.rear) /* 队列空的标志 */
		return TRUE;
	else
		return FALSE;
}

/* 若队列未满,则插入元素e为Q新的队尾元素 */
Status EnQueue(Queue *Q,int e)
{
	if ((Q->rear+1)%MAXSIZE == Q->front)	/* 队列满的判断 */
		return ERROR;
	Q->data[Q->rear]=e;			/* 将元素e赋值给队尾 */
	Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
								/* 若到最后则转到数组头部 */
	return  OK;
}

/* 若队列不空,则删除Q中队头元素,用e返回其值 */
Status DeQueue(Queue *Q,int *e)
{
	if (Q->front == Q->rear)			/* 队列空的判断 */
		return ERROR;
	*e=Q->data[Q->front];				/* 将队头元素赋值给e */
	Q->front=(Q->front+1)%MAXSIZE;	/* front指针向后移一位置, */
									/* 若到最后则转到数组头部 */
	return  OK;
}

1. If the graph adjacency matrix

/* 邻接矩阵的广度遍历算法 */
void BFSTraverse(MGraph G)
{
	int i, j;
	Queue Q;
	for(i = 0; i < G.numv; i++)
       	visited[i] = FALSE;
    InitQueue(&Q);		/* 初始化一辅助用的队列 */
    for(i = 0; i < G.numv; i++)  /* 对每一个顶点做循环 */
    {
		if (!visited[i])	/* 若是未访问过就处理 */
		{
			visited[i]=TRUE;		/* 设置当前顶点访问过 */
			printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
			EnQueue(&Q,i);		/* 将此顶点入队列 */
			while(!QueueEmpty(Q))	/* 若当前队列不为空 */
			{
				DeQueue(&Q,&i);	/* 将队对元素出队列,赋值给i */
				for(j=0;j<G.numv;j++) 
				{ 
					/* 判断其它顶点若与当前顶点存在边且未访问过  */
					if(G.arc[i][j] == 1 && !visited[j]) 
					{ 
 						visited[j]=TRUE;			/* 将找到的此顶点标记为已访问 */
						printf("%c ", G.vexs[j]);	/* 打印顶点 */
						EnQueue(&Q,j);				/* 将找到的此顶点入队列  */
					} 
				} 
			}
		}
	}
}

2. If the graph adjacency list

/* 邻接表的广度遍历算法 */
void BFSTraverse(GraphAdjList *G)
{
	int i;
    EdgeNode *p;
	Queue Q;
	for(i = 0; i < G->numv; i++)
       	visited[i] = FALSE;
    InitQueue(&Q);
   	for(i = 0; i < G->numv; i++)
   	{
		if (!visited[i])
		{
			visited[i]=TRUE;
			printf("%c ",G->adjList[i].data);/* 打印顶点,也可以其它操作 */
			EnQueue(&Q,i);
			while(!QueueEmpty(Q))
			{
				DeQueue(&Q,&i);
				p = G->adjList[i].firstedge;	/* 找到当前顶点的边表链表头指针 */
				while(p)
				{
					if(!visited[p->adjvex])	/* 若此顶点未被访问 */
 					{
 						visited[p->adjvex]=TRUE;
						printf("%c ",G->adjList[p->adjvex].data);
						EnQueue(&Q,p->adjvex);	/* 将此顶点入队列 */
					}
					p = p->next;	/* 指针指向下一个邻接点 */
				}
			}
		}
	}
}
Published 37 original articles · won praise 30 · views 1103

Guess you like

Origin blog.csdn.net/myjess/article/details/104575741