您的位置:首页 > 其它

有向图的DFS和BFS算法实现

2009-06-13 21:45 393 查看
/*OL_graph.c无向图的DFS和BFS遍历。
*该代码是对教科书中算法的实现
*由于水平所限,代码雍肿在所难免,欢迎批评指正,欢迎讨论切搓
* by: double; version: 0.1; date: 2009-06-13
*/

#include "/usr/c/head.h"

#ifndef MAX_INT
#define MAX_INT 0x7fffffff
#endif

#ifndef MAX
#define MAX 20
#endif

typedef char info_type;

typedef char vertex_type;

typedef struct arc_type {
int mark;
int tailvex, headvex;
struct arc_type * hlink, * tlink;
info_type * info;
}arc_type;

typedef struct vex_type {
vertex_type data;
arc_type * first_in, * first_out;
}vex_type;

typedef struct OL_graph {
vex_type vexs[MAX];
int vexnum, arcnum;
}OL_graph;

typedef struct queue_node {
int data;
struct queue_node * next;
}queue_node, * queue_ptr;

typedef struct {
queue_ptr front;
queue_ptr rear;
}link_queue;

OL_graph G;
int flag[MAX];
link_queue Q;

int locate_vex(OL_graph G, vertex_type v) {
int i;
for (i = 0; i < G.vexnum; i++)
if (G.vexs[i].data == v)
return i;
return -1;
}

status init_graph(OL_graph * G) {
int temp, i, p1, p2;
vertex_type v1, v2;
arc_type * p;
printf("Input the vexnum:");
if (scanf("%d", &G->vexnum) != EOF)
while ((temp = getchar()) != '/n' && temp != EOF) {;}
for (i = 0; i < G->vexnum; i++) {
printf("No. %d:", i + 1);
if (scanf("%c", &G->vexs[i].data) != EOF)
while ((temp = getchar()) != '/n' && temp != EOF) {;}
G->vexs[i].first_in = G->vexs[i].first_out = NULL;
}
printf("Input the arcnum:");
if (scanf("%d", &G->arcnum) != EOF)
while ((temp = getchar()) != '/n' && temp != EOF) {;}
for (i = 0; i < G->arcnum; i++) {
printf("No.%d v1:", i + 1);
if (scanf("%c", &v1) != EOF)
while ((temp = getchar()) != '/n' && temp != EOF) {;}
printf("No.%d v2:", i + 1);
if (scanf("%c", &v2) != EOF)
while ((temp = getchar()) != '/n' && temp != EOF) {;}
p1 = locate_vex(* G, v1);
if (p1 == -1) {
printf("v1 = %c is not exist!/n", v1);
exit(ERROR);
}
p2 = locate_vex(* G, v2);
if (p2 == -1) {
printf("v2 = %c is not exist!/n", v2);
exit(ERROR);
}
p = (arc_type *)malloc(sizeof(arc_type));
if (!p) exit(OVERFLOW);
p->tailvex = p1;
p->headvex = p2;
p->hlink = G->vexs[p2].first_in;
p->tlink = G->vexs[p1].first_out;
p->info = NULL;
G->vexs[p2].first_in = p;
G->vexs[p1].first_out = p;
}

return OK;
}

status visit(int v) {
printf("%c/t", G.vexs[v].data);
return OK;
}

int first_adj(int v) {
arc_type * p;
p = G.vexs[v].first_out;
if (p)
return p->headvex;
return -1;
}

int next_adj(int v, int w) {
arc_type * p, * q;
p = G.vexs[v].first_out;
while (p) {
if (p->tailvex + p->headvex == v + w) {
q = p->tlink;
if (q)
return q->headvex == w ?
q->tailvex : q->headvex;
}
p = p->tlink;
}
return -1;
}

status DFS(int v) {
int w;
flag[v] = TRUE;
visit(v);
for (w = first_adj(v); w >= 0; w = next_adj(v, w))
if (flag[w] == FALSE)
DFS(w);

return OK;
}

status DFS_graph(OL_graph G) {
int i;
for (i = 0; i < G.vexnum; i++)
flag[i] = FALSE;
for (i = 0; i < G.vexnum; i++)
if (flag[i] == FALSE)
DFS(i);

return OK;
}

status init_queue(link_queue * Q) {
Q->front = Q->rear = (queue_ptr)malloc(sizeof(queue_node));
if (!Q->front) exit(OVERFLOW);
Q->front->next = NULL;

return OK;
}

status en_queue(link_queue * Q, int e) {
queue_ptr p;
p = (queue_ptr)malloc(sizeof(queue_node));
if (!p) exit(OVERFLOW);
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;

return OK;
}

status de_queue(link_queue * Q, int * e) {
queue_ptr p;
if (Q->front == Q->rear) {
printf("Queue empty!/n");
exit(ERROR);
}
p = Q->front->next;
* e = p->data;
Q->front->next = p->next;
if (p == Q->rear)
Q->rear = Q->front;
free(p);

return OK;
}

int queue_empty(link_queue Q) {
if (Q.front == Q.rear)
return 1;
return 0;
}

status BFS_graph(OL_graph G) {
int i, e, w;
init_queue(&Q);
for (i = 0; i < G.vexnum; i++)
flag[i] = FALSE;
for (i = 0; i < G.vexnum; i++)
if (flag[i] == FALSE) {
flag[i] = TRUE;
visit(i);
en_queue(&Q, i);
while (!queue_empty(Q)) {
de_queue(&Q, &e);
for (w = first_adj(e); w >= 0;
w = next_adj(e, w))
if (flag[w] == FALSE) {
flag[w] = TRUE;
visit(w);
en_queue(&Q, w);
}
}
}

return OK;
}

int main(void) {
init_graph(&G);
printf("graph initilized OK!/n");
printf("DFS result is:/n");
DFS_graph(G);
printf("/n");
printf("BFS result is:/n");
BFS_graph(G);
printf("/n");

return 0;
}


注释见上篇算法实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: