标签:
//图的建立的实现->邻结矩阵和邻结表两种表示方法 #include <cstdio> #include <cstdlib> //#define _OJ_ int visit[100]; typedef struct Lnode { int data; //邻结点的位置下标 // int weight; struct Lnode *next; //表由多排的链表组成 } Lnode, *Linklist; typedef struct Fnode { int elem; //每个顶点的信息 是数字或是字符 Linklist firstcell; //构成多个头节点 } Fnode1[100]; typedef struct Graph1 { int nv; int ne; Fnode1 G; //图由顶点数,边数,和邻接表组成 } Graph1, *Graph; typedef struct Edge1 { int v1; int v2; // int weight; //边由两个顶点的值构成 } Edge1, *Edge; Graph creat_graph(int vertex, int edge) //分配边数和节点数并初始化 { int i; Graph g; g = (Graph) malloc (sizeof(Graph1)); g->nv = vertex; g->ne = edge; for(i = 0;i < vertex; i++) { g->G[i].firstcell = NULL; //把每一个头接点赋初值 g->G[i].elem = i; //输入每个节点的信息 } return g; } void inser_edge(Graph g, Edge e) { Linklist L, L1; L = (Linklist) malloc (sizeof(Lnode)); L->data = e->v2; L->next = g->G[e->v1].firstcell; g->G[e->v1].firstcell = L; //无向图的插入两边 每次增加一个节点将其插入在最前面 L1 = (Linklist) malloc (sizeof(Lnode)); L1->data = e->v1; L1->next = g->G[e->v2].firstcell; g->G[e->v2].firstcell = L1; } Graph build_Graph(void) { Graph g; Edge e; int i, j, vertex, edge; scanf("%d %d", &vertex, &edge); g = creat_graph(vertex, edge); if(edge > 0) { e = (Edge) malloc (sizeof(Edge1)); for(i = 0;i < edge; i++) { scanf("%d %d", &e->v1, &e->v2); inser_edge(g, e); } return g; } } void DFS(Graph g, int v) { int i; visit[v] = 1; printf("%d ", g->G[v].elem); while (g->G[v].firstcell->next != NULL) { if(visit[g->G[v].firstcell->data] == 0) DFS(g, g->G[v].firstcell->data); g->G[v].firstcell = g->G[v].firstcell->next; } } void DFS_travers(Graph g) { int i; for(i = 0;i < g->nv; i++) visit[i] = 0; for(i = 0;i < g->nv; i++) if(visit[i] == 0) DFS(g, i); } typedef struct Queue1 { int top; int base; int *data1; } Queue1, *Queue; Queue creat_queue(void) { Queue q; q = (Queue) malloc (sizeof(Queue1)); q->data1 = (int*) malloc (100 * sizeof(int)); q->base = q->top = 0; return q; } int isempty(Queue q) { if(q->base == q->top) return 1; else return 0; } void Enqueue(Queue q, int data) { q->data1[q->top++] = data; } int Dequeue(Queue q) { return q->data1[q->base++]; } void BFS(Graph g, int v) { int i; Queue q; Linklist L; q = creat_queue(); printf("%d ", g->G[v].elem); visit[v] = 1; Enqueue(q, v); while (isempty(q) != 1) { i = Dequeue(q); L = g->G[i].firstcell; while (L) { if(visit[L->data] == 0) { printf("%d ", g->G[L->data].elem); visit[L->data] = 1; Enqueue(q, L->data); } L = L->next; } } } void BFS_travers(Graph g) { int i; for(i = 0;i < g->nv; i++) visit[i] = 0; for(i = 0;i < g->nv; i++) { if(visit[i] == 0) BFS(g, i); } } int main(int argc, char const *argv[]) { #ifndef _OJ_ //ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int i, j; Graph g; g = build_Graph(); // for(i = 0;i < g->nv; i++) { // //printf("%p\n", g->G[i].firstcell); //循环重复的遍历每一条链表 // printf("%d -> ", i); // while (g->G[i].firstcell != NULL) { // printf("%d ",g->G[i].firstcell->data); // g->G[i].firstcell = g->G[i].firstcell->next; // } // printf("\n"); // } DFS_travers(g); // BFS_travers(g); return 0; } /* 8 9 0 1 0 2 1 3 1 4 2 5 2 6 3 7 4 7 5 6 vertex:A→:2→:1 vertex:B→:4→:3→:0 vertex:C→:6→:5→:0 vertex:D→:7→:1 vertex:E→:7→:1 vertex:F→:6→:2 vertex:G→:5→:2 vertex:H→:4→:3 建立无误 BFS: 0 2 1 6 5 4 3 7 DFS: 0 2 6 5 1 4 7 3 */
标签:
原文地址:http://www.cnblogs.com/airfand/p/5020254.html