Data structure-understanding and implementation of depth-first traversal (DFS) and breadth-first traversal (BFS) of adjacency matrix

Data structure-the adjacency matrix of the graph implements
depth-first traversal
**First step:** Construct an access array with the same size as the number of vertices. Initialization is recorded as 0, meaning that the
second step has not been accessed from the first vertex or The last vertex is the starting point to determine whether the vertex has passed, and then perform the DFS function. Finally, follow each vertex in turn (use the access array to determine whether it has passed). The
third step is to construct the DFS function: The
DFS function starts from a certain vertex according to whether the edge is Existence is to judge the second edge and then record the other vertex of its edge as 1 (walked)

Preliminary preparation

typedef int Boolean;//深度遍历的数组为布尔类型 用来标记
Boolean visited[MAXVER];//深度遍历的访问数组

Operative part

/*邻接矩阵的深度遍历算法   访问数组初始化,对每个顶点都可以走一遍  前提是没有走过的*/

Status DFS(MGhaph &G, int i)
{
    
    
	int j;
	visited[i] = true;
	cout << "此时访问的顶点为: "<< endl;
	cout << G.vexs[i]<< endl;
	for (j = 0; j < G.numver; j++)
	{
    
    
		if (G.arc[j][i] != INF && !visited[j])//递归之处   从i为开始 又转到从j的0到顶点数都寻找一遍
			DFS(G,j);
	}
	return 0;
}

Status DFSTraverse(MGhaph &G)
{
    
    
	int i;
	for (i = 0; i < G.numver; i++)//访问数组都记为0  意为 顶点没有被访问过
	{
    
    
		visited[i] = false;
	}

	for (i = 0; i < G.numver; i++)
	{
    
    
		if (!visited[i])//所有顶点都可以访问(前提是:之前没有被访问过) 且从该顶点开始循环
		{
    
    
			cout << "从" << i << "开始访问" << endl;
			DFS(G, i);
		}
		
	}
	return 0;
}

/*————————深度优先遍历————————*/


Thinking : Depth-first traversal can walk every vertex without repeating it. You can do some operations on the vertex (of course it can be repeated but the complexity is higher). It belongs to the search algorithm.


Breadth first traversal involves a queue

First define a queue queue.h

#include<iostream>
//#include <iomanip>
using namespace std;
typedef char elementtype;
typedef int Status;

typedef struct QNode
{
    
    
	char data;
	struct QNode *next;
}QNode, *Queueptr;

typedef struct Queue
{
    
    
	QNode *head;
	QNode *tail;
}Queue;

//初始化队列
Status InitQueue(Queue &Q)
{
    
    
	Q.head = Q.tail = (Queueptr)malloc(sizeof(QNode));
	Q.head->next = NULL;
	Q.tail->next = NULL;
	return 0;
}
//头指针和尾指针只有一个
//插入元素
Status Enqueue(Queue &Q, elementtype x)
{
    
    
	Queueptr p = (Queueptr)malloc(sizeof(QNode));//创建一个结点

	p->data = x;
	p->next = NULL;
	Q.tail->next = p;
	Q.tail = p;
	return 0;
}

Status Dequeue(Queue &Q, elementtype &y)
{
    
    
	if (Q.head == Q.tail)
	{
    
    
		return NULL;

	}

	Queueptr q;
	q = Q.head->next;
	y = q->data;

	Q.head->next = q->next;
	if (Q.tail == q)
	{
    
    
		Q.tail = Q.head;
	}
	free(q);
	return 0;
}

Status QueueEmpty(Queue &Q)
{
    
    
	if (Q.head != NULL || Q.tail != NULL)
	{
    
    
		return 1;
	}
	else
		return 0;
}



Then add this to the data structure-the adjacency matrix implementation of the graph,
and #include "queue.h" at the same time.

/*————————实现广度优先遍历————————*/

void BFS(MGhaph &G)
{
    
    
	char x;
	int i, j;
	Queue Q;
	for (i = 0; i < G.numver; i++)
	{
    
    
		visited[i] = false;//标记
	}
	InitQueue(Q);//初始化队列

	for (i = 0; i < G.numver; i++)
	{
    
    
		if (!visited[i])//如果从未访问过
		{
    
    
			visited[i] = true;//标记 走过的顶点 并输出,并将此顶点存入队列
			cout << "此时访问的顶点为: " << endl;
			cout << G.vexs[i] << endl;
			Enqueue(Q, G.vexs[i]);//将此顶点进入队列


			while (!QueueEmpty(Q))//若此时队列不为空时  进入   空时弹出
			{
    
    
				Dequeue(Q,x);//把队列里的一个元素弹出

				for (j = 0; j < G.numver; j++)
				{
    
    
					if (G.arc[j][i] != INF && !visited[j])//寻找和i所在的元素有弧的元素
					{
    
    
						visited[j] = true;//标记走过
						cout << "从" << j << "开始访问" << endl;
						cout <<G.vexs[j] << endl;
						Enqueue(Q, G.vexs[j]);//将有联系的顶点元素入队列
					}
				}
			}
		}
	}
}

Generalized first traversal:
1. Construct a queue to store vertex elements as an intermediate process
2. Construct an access array to mark the vertices that have been visited
3. Set up a for loop to store the vertices visited each time in the queue and
then enter the while Loop (to judge whether the queue is empty) enter the loop body when it is not empty

Pop up an element in the team

Enter the for loop to find the vertex of the edge
related to the vertex. After finding the vertex of the related edge, store it in the queue and mark the visited

After the for loop is over, it is still in the while loop. At this time, clear the queue to jump out
of the queue in the big loop. Return to the initialization.

Guess you like

Origin blog.csdn.net/weixin_46096297/article/details/113435033