深度优先遍历(Depth-First Traversa Undirected Graph)
Posted fanlifeli31
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深度优先遍历(Depth-First Traversa Undirected Graph)相关的知识,希望对你有一定的参考价值。
#inlcude<stdio.h>
#define V_SIZE 8
#define E_SIZE 10
typedef struct
{
char vertex[V_SIZE];
int adjacency_matrix[V_SIZE][V_SIZE];
}Un_Graph;
int visited(V_SIZE);
int Create_G(Un_Graph *G);
int DFS(Un_Graph *G, int i);
int Output(Un_Graph *G);
int main(void)
{
int i = 0;
Un_Graph G;
Create_G(&G);
Output(&G);
while(i < V_SIZE)
{
visited[i] = 0;
i++;
}
DFS(&G, 0);
getchar();
return 0;
}
int Create(Un_Graph *G)
{
int i = 0, j = 0, k = 0;
printf("Please enter vertex:");
for(i = 0; i < V_SIZE; i++)
{
scanf("%c", &G->vertex[i]);
getchar();
}
for(j = 0; j < V_SIZE; j++)
for(i = 0; i < V_SIZE; i++)
G->adjacency_matrix[j][i] = 0;
printf("Please enter the edge infomation: ");
for(i = 0; i < E_SIZE; i++)
{
scanf("%d,%d", &k, &j); getchar();
G->adjacency_matrix[k - 1][j - 1] = 1;
G->adjacency_matrix[j - 1][k - 1] = 1;
}
return 0;
}
int DFS(Un_Graph *G, int i)
{
int j = 0;
printf("%c ", G->data[i]);
for(j = 0; j < V_SIZE; j++)
if(G->adjacency_matrix[i][j] != 0 && visited[j] != 1)
DFS(G, j);
return 0;
}
int Output(Un_Graph *G)
{
int i = 0, j = 0;
for(i = 0; i < V_SIZE; i++)
{
for(j = 0; j < V_SIZE; j++)
printf("%d ", G->adjacency_matrix[i][j]);
printf(" ");
}
return 0;
}
以上是关于深度优先遍历(Depth-First Traversa Undirected Graph)的主要内容,如果未能解决你的问题,请参考以下文章
算法漫游指北(第十四篇):二叉树的深度优先遍历前序遍历(递归方式/非递归方式)中序遍历(递归方式/非递归方式)后序遍历(递归方式/非递归方式)