广度优先搜索算法c语言,图形的广度优先搜寻法

/* ======================================== */

/*    图形的广度优先搜寻法                  */

/* ======================================== */

#include 

#define MAXQUEUE 10               /* 伫列的最大容量       */

struct node                       /* 图形顶点结构宣告     */

{

int vertex;                    /* 顶点资料             */

struct node *nextnode;         /* 指下一顶点的指标     */

};

typedef struct node *graph;       /* 图形的结构新型态     */

struct node head[9];              /* 图形顶点结构数组     */

int visited[9];                   /* 遍历记录数组         */

int queue[MAXQUEUE];              /* 伫列的数组宣告       */

int front = -1;                   /* 伫列的前端           */

int rear = -1;                    /* 伫列的后端           */

/* ---------------------------------------- */

/*  建立图形                                */

/* ---------------------------------------- */

void creategraph(int *node,int num)

{

graph newnode;                 /* 新顶点指标           */

graph ptr;

int from;                      /* 边线的起点           */

int to;                        /* 边线的终点           */

int i;

for ( i = 0; i 

{

from = node[i*2];           /* 边线的起点           */

to = node[i*2+1];           /* 边线的终点           */

/* 建立新顶点记忆体 */

newnode = ( graph ) malloc(sizeof(struct node));

newnode->vertex = to;       /* 建立顶点内容         */

newnode->nextnode = NULL;   /* 设定指标初值         */

ptr = &(head[from]);        /* 顶点位置             */

while ( ptr->nextnode != NULL ) /* 遍历至链表尾     */

ptr = ptr->nextnode;         /* 下一个顶点       */

ptr->nextnode = newnode;        /* 插入结尾         */

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
广度优先遍历(BFS)是一种用于图的遍历算法,用于遍历或搜索图中的节点。下面是一个使用C语言实现广度优先遍历的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义图的最大节点数量 #define MAX_NODES 100 // 定义图的邻接矩阵表示 int graph[MAX_NODES][MAX_NODES]; // 定义队列结构 typedef struct { int data[MAX_NODES]; int front; int rear; } Queue; // 初始化队列 void initializeQueue(Queue* queue) { queue->front = -1; queue->rear = -1; } // 判断队列是否为空 int isQueueEmpty(Queue* queue) { return queue->front == -1; } // 入队操作 void enqueue(Queue* queue, int item) { if (queue->rear == MAX_NODES - 1) { printf("Queue is full\n"); } else { if (queue->front == -1) { queue->front = 0; } queue->rear++; queue->data[queue->rear] = item; } } // 出队操作 int dequeue(Queue* queue) { int item; if (isQueueEmpty(queue)) { printf("Queue is empty\n"); return -1; } else { item = queue->data[queue->front]; if (queue->front >= queue->rear) { queue->front = -1; queue->rear = -1; } else { queue->front++; } return item; } } // 广度优先遍历函数 void breadthFirstSearch(int startNode, int numNodes) { Queue queue; int visited[MAX_NODES] = {0}; // 记录节点是否被访问过 initializeQueue(&queue); visited[startNode] = 1; // 标记起始节点为已访问 enqueue(&queue, startNode); while (!isQueueEmpty(&queue)) { int currentNode = dequeue(&queue); printf("%d ", currentNode); // 遍历当前节点的邻居节点 for (int i = 0; i < numNodes; i++) { if (graph[currentNode][i] == 1 && !visited[i]) { enqueue(&queue, i); visited[i] = 1; } } } } int main() { int numNodes, startNode; printf("Enter the number of nodes: "); scanf("%d", &numNodes); printf("Enter the adjacency matrix:\n"); for (int i = 0; i < numNodes; i++) { for (int j = 0; j < numNodes; j++) { scanf("%d", &graph[i][j]); } } printf("Enter the starting node: "); scanf("%d", &startNode); printf("BFS traversal: "); breadthFirstSearch(startNode, numNodes); return 0; } ``` 这是一个简单的广度优先遍历算法C语言实现。它使用邻接矩阵来表示图,通过队列来辅助实现广度优先遍历。你可以根据需要修改或扩展代码。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值