1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <stdio.h>
typedef enum {false, true} bool; #define MaxVertexNum 10 #define INFINITY 65535 typedef int Vertex; typedef int WeightType;
typedef struct GNode *PtrToGNode; struct GNode{ int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; }; typedef PtrToGNode MGraph; bool Visited[MaxVertexNum];
MGraph CreateGraph();
void Visit( Vertex V ) { printf(" %d", V); }
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) );
int main() { MGraph G; Vertex V;
G = CreateGraph(); scanf("%d", &V); printf("DFS from %d:", V); DFS(G, V, Visit);
return 0; }
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex)) { Visited[V]=true; Visit(V); for(int i=0;i<Graph->Nv;i++) { if(!Visited[i]&&Graph->G[V][i]<INFINITY) { DFS(Graph,i,Visit); } } }
|