码迷,mamicode.com
首页 > 其他好文 > 详细

深度优先遍历(Depth-First Traversa Undirected Graph)

时间:2018-06-21 23:50:24      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:深度优先遍历   site   fine   遍历   优先   scanf   enter   char   AC   

#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:\n");

  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("\n");

  }

 

  return 0;

}

 

深度优先遍历(Depth-First Traversa Undirected Graph)

标签:深度优先遍历   site   fine   遍历   优先   scanf   enter   char   AC   

原文地址:https://www.cnblogs.com/fanlifeli31/p/9211228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!