PTA lists connected sets --- preservation of symmetric arrays

Ideas

This question is to test the storage and traversal (BFS, DFS) operations of the graph

The graph is saved with adjacency matrix and adjacency list, and in this question I use the adjacency matrix, there are also two ways to save the adjacency matrix:

  1. Define a two-dimensional array to save all the edges of each point. For an undirected graph, each edge must save two edges
  2. Defining a one-dimensional array. For undirected graphs, saving only the lower triangular matrix of the two-dimensional array in Method 1 (stored once for each edge) can save a lot of space compared to the first one.
  3. For the second method,The subscript of the edge between two nodes in this one-dimensional array isInsert picture description here

error code

void DFS(int* G, int* visit, int N, int V) {
    
    
	visit[V] = 1;
	printf("%d ", V);
	//错误地方
	for (int i = V; i < N; i++) {
    
     //遍历每个临界点 对某个点的临界点来说,要按下三角矩阵遍历列
		if (G[i * (i + 1) / 2 + V] == 1 && visit[i] != 1) {
    
    
			DFS(G, visit, N, i);
		}
	}
}

void BFS(int* G, int* visit, int N, int V) {
    
    
	visit[V] = 1;
	printf("%d ", V);
	Enquee(V);
	while(!IsEmpty()) {
    
    
		V = Dequee();	//以下遍历V的临接点
		//错误地方
		for (int i = V; i < N; i++) {
    
     //遍历每个临接点 对某个点的临接点来说,要按下三角矩阵遍历列
			if (G[i * (i + 1) / 2 + V] == 1 && visit[i] != 1) {
    
    
				visit[i] = 1;
				printf("%d ", i);
				Enquee(i);
			}
		}
	}
}


Interpretation error

Very strange, why is the error code still sent out?
That's because in the error code, the second way to save space is used in the saving method of my graph. What if you use the second way? ? ?
Let's look at a case:

10	7
1	4
1	3
1	5
4	2
6	7
6	9
7	8

The problem in this case is the edge 4 2. If you draw the picture of this case (I drew it too ugly and I didn't draw it), you will find that there are edges between node 1 and node 4, and node 4 and Node 2 has an edge.

  • in other words:
    When we traverse, we should first traverse to node 1, then traverse to node 4, and finally traverse to node 2
  • howeverIn the second saving method, when we traverse the adjacency matrix of a node, we traverse the column corresponding to the lower triangular matrix to find the value of 1 (for example, traverse the adjacency point of v3, starting from v3 instead of v0). This leads to So when we traverse the neighboring nodes of node 4, we start from node 4, and we didn’t find node 2.

Correct code

The first method used to save the graph-save it with a two-dimensional matrix, so that when looking for the adjacency matrix of a node, you can simply traverse the corresponding column, because it is two-dimensional, so each traversal is from v0 The beginning

note

When using a two-dimensional array to pass parameters, the formal parameter will use the array pointer int(* G)[10]

#include <stdio.h>
#include <stdlib.h>

void DFS(int(* G)[10], int* visit, int N, int V) {
    
    
	visit[V] = 1;
	printf("%d ", V);
	for (int i = 0; i < N; i++) {
    
     //遍历每个临界点 对某个点的临界点来说,要按下三角矩阵遍历列
		if (G[i][V] == 1 && visit[i] != 1) {
    
    
			DFS(G, visit, N, i);
		}
	}
}

struct quee {
    
    
	int q[10];
	int front = 0;
	int rear = 0;
}Pq;

void Enquee(int V) {
    
    
	Pq.q[Pq.rear++] = V;
}

int Dequee() {
    
    
	return Pq.q[Pq.front++];
}

int IsEmpty() {
    
    
	if (Pq.front == Pq.rear)
		return 1;
	else
		return 0;
}

void BFS(int(* G)[10], int* visit, int N, int V) {
    
    
	visit[V] = 1;
	printf("%d ", V);
	Enquee(V);
	while(!IsEmpty()) {
    
    
		V = Dequee();	//以下遍历V的临接点
		for (int i = 0; i < N; i++) {
    
     //遍历每个临接点 对某个点的临接点来说,要按下三角矩阵遍历列
			if (G[i][V] == 1 && visit[i] != 1) {
    
    
				visit[i] = 1;
				printf("%d ", i);
				Enquee(i);
			}
		}
	}
}

int main() {
    
    
	int N, E;
	int a1, a2;
	scanf("%d %d", &N, &E);
	int G[10][10];
	for (int i = 0; i < E; i++) {
    
    
		scanf("%d %d", &a1, &a2);
		G[a1][a2] = 1;
		G[a2][a1] = 1;
	}
	//for (int i = 0; i < 44; i++) {
    
    
	//	printf("%d ", G[i]);
	//}
	int visit[10];
	for (int i = 0; i < N; i++) {
    
    
		visit[i] = 0;
	}
	for (int i = 0; i < N; i++) {
    
    
		if (visit[i] == 0) {
    
    
			printf("{ ");
			DFS(G, visit, N, i);
			printf("}\n");
		}
	}
	for (int i = 0; i < N; i++) {
    
    
		visit[i] = 0;
	}
	for (int i = 0; i < N; i++) {
    
    
		if (visit[i] == 0) {
    
    
			printf("{ ");
			BFS(G, visit, N, i);
			printf("}\n");
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43779658/article/details/105182993