#include <stdio.h>
#include <stdlib.h>
#define VSIZE 7 /* change the number of vertices fix here*/
/**
A Test Program Of DFS(Depth First Search)
refer <to Introduction To Algorithm >
* Math Frog@SHFU
* jp021@hotmail.com
* 2003.5.2
* Dfs is a very important searching technique ,
In fact if we ignore the computation ability of
the computer,DFS is always useful to solve all
the searching problem.
* Now we make DFS just on graph traversing.The
problem is given one start vertice in graph,reach
all the vertice in graph.This procedure is very
similar to preorder tree traversing.
* In our algorithm there are three state for
the vertice,WHITE,GRAY,BLACK.When the vertex is
WHITE,it tells us that the vertice is not discovered
yet.And when the vertice is GRAY,it tells us that the
vertex is just discovered.If black,the vertice is
visited.These three states are very important in
graph algorithm.
* When coding,we represent the graph using adjacent
matrix,of course using adjacent link list is just
the same.Because adjacent matrix is easy to code and
more clear to understand.And the graph is direct,
because direct graph is used much more widely in
practice then undirect graph.
**/
/* global var */
int graph[VSIZE][VSIZE]; /* the graph's adjacent matrix */
int vSet[VSIZE]; /* set of the vertice */
enum state {WHITE,GRAY,BLACK}; /* the three state */
enum state vSetState[VSIZE]; /* state of every vertex */
/* function declaration */
void inputGraph(); /* get graph */
void dispGraph(); /* display graph */
void DFS(); /* the overall function to traverse graph */
void DFS_visit(int);/* to reach the vertex that can be reach from given vetex */
/** To be simple input graph means you
just input the graph's adjacent matrix.
*/
void inputGraph()
{
int i,j;
printf("Input the adjacent matrix of the graph \n");
printf("___________________________\n");
for(i = 0;i < VSIZE;i++)
for(j = 0;j < VSIZE;j++)
scanf("%d",&graph[i][j]);
for(i = 0;i < VSIZE;i++)
vSet[i] = i + 1;/* be default the vertice are 1,2...VSIZE */
}
void dispGraph()
{
int i,j;
printf("___________________________\n");
printf("The vertice of this graph are \n");
for(i = 0;i < VSIZE;i++)
printf("%d ",vSet[i]);
printf("\n___________________________\n");
printf("\nThe adjacent matrix of this graph is \n");
for(i = 0;i < VSIZE;i++){
for(j = 0;j < VSIZE;j++)
printf("%d ",graph[i][j]);
printf("\n");
}
}
void DFS()
{
int i;
for(i = 0;i < VSIZE;i++)
vSetState[i] = WHITE;
/* for every vertex in vSet */
for(i = 0;i < VSIZE;i++)
DFS_visit(vSet[i]);
}