DFS遍历代码(邻接矩阵法)
#include <stdio.h>
#include <stdlib.h>
typedef struct Graph{
char* vertex;
int **edge;
int numvertex;
}*graph;
graph inigraph(char* str, int*arr, int num){
graph g = (graph)malloc(sizeof(struct Graph));
g->numvertex = num;
g->vertex = (char*)malloc(sizeof(char) * num);
// 二维数组的创建:先设置二维指针(int**)的指向,再设置一维指针(int*)的指向
g->edge = (int**)malloc(sizeof(int*) * num);
for(int m = 0; m < num; m++){
g->edge[m] = (int*)malloc(sizeof(int) * num);
}
for (int i = 0; i < num; i++){
g->vertex[i] = str[i];
}
for(int j = 0; j < num; j++){
for(int k = 0; k < num; k++){
g->edge[j][k] = *(arr + j * num + k);
//g->edge[j][k] = arr[j * num + k];也可以
}
}
return g;
}
//DFS遍历
void DFS(graph g, int* visited){
for (int m = 0; m < g->numvertex; m++){
for(int n = 0; n < g->numvertex; n++){
if(g->edge[m][n] == 1 && visited[n] == 0){
printf("%c\t",g->vertex[n]);
visited[n] = 1;
DFS(g, visited);
}
}
}
}
int* createvisited(int length){
int *arr = (int*)malloc(sizeof(int) * length);
for (int p = 0; p < length; p++){
arr[p] = 0;
}
return arr;
}
int main(){
int array[][5] = {
{0,1,1,0,1},
{1,0,0,1,0},
{1,0,0,1,1},
{0,1,1,0,1},
{1,0,1,1,0}
};
int *visited = createvisited(5);
graph G = inigraph("ABCDE", array[0], 5);//(int*)array传进去也可以
DFS(G, visited);
return 0;
}
BFS遍历代码
邻接表法
邻接表大致结构
BFS邻接矩阵法遍历
visited[x]数组注意事项
今后在使用队列加visited[x]数组时,最好是第一次访问到元素时(就是入队时)就在visited[x]数组内做标记,同时打印输出元素。
#include <stdio.h>
#include <stdlib.h>
typedef struct Graph{
char* vertex;
int **edge;
int numvertex;
}*graph;
graph inigraph(char* str, int*arr, int num){
graph g = (graph)malloc(sizeof(struct Graph));
g->numvertex = num;
g->vertex = (char*)malloc(sizeof(char) * num);
// 二维数组的创建
g->edge = (int**)malloc(sizeof(int*) * num);
for(int m = 0; m < num; m++){
g->edge[m] = (int*)malloc(sizeof(int) * num);
}
for (int i = 0; i < num; i++){
g->vertex[i] = str[i];
}
for(int j = 0; j < num; j++){
for(int k = 0; k < num; k++){
g->edge[j][k] = *(arr + j * num + k);
//g->edge[j][k] = arr[j * num + k];
}
}
return g;
}
int* createvisited(int length){
int *arr = (int*)malloc(sizeof(int) * length);
for (int p = 0; p < length; p++){
arr[p] = 0;
}
return arr;
}
//BFS
void BFS(graph g, int* visited){
int ch[g->numvertex + 1];
int front = 0,rear = 0;
ch[rear] = 0; // 入队,入队的是字母的索引
visited[0] = 1;
rear = (rear + 1) % (g->numvertex + 1);
while(rear != front){ // 队列不为空
//出队
int index = ch[front];
front = (front + 1) % (g->numvertex + 1);
printf("%c\t", g->vertex[index]);
for (int x = 0; x < g->numvertex; x++){
//入队
if(g->edge[index][x] == 1 && visited[x] != 1){
ch[rear] = x ;
visited[x] = 1;
rear = (rear + 1) % (g->numvertex + 1);
}
}
}
}
int main(){
int array[][5] = {
{0,1,1,0,1},
{1,0,0,1,0},
{0,0,0,1,0},
{0,1,1,0,1},
{1,0,0,1,0}
};
int *visit = createvisited(5);
graph G = inigraph("ABCDE", array[0], 5);
BFS(G, visit);
return 0;
}
两种遍历方法比较
- 稀疏图:边很少,相对于顶点数量比较小
- 稠密图:边较多,接近于 V^2
二维数组传入函数的方法
int TwoDimArraySum(int *pr, int row, int col)
{
int i, j;
int result = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
//下面两种寻址方式都行(把二维数组变为一维数组)
result += pr[i*col + j];
// result += *(pr + i*col + j);
}
}
return result;
}
int main(){
result = TwoDimArraySum(twoDimArray[0], ROW, COL);
//result = TwoDimArraySum((int*)twoDimArray, ROW, COL);也可以
printf("Sum函数结果:%d\n", result);
return 0;
}
如上图,将二维数组传入到函数内,最适合的方式为传int*类型的指针,然后在函数内寻找地址就可以获取或修改二维数组内的数。