广度优先搜索

45 篇文章 0 订阅
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;
struct node                       /* 图顶点结构定义     */
{
   int vertex;                    /* 顶点数据信息       */
   struct node *nextnode;         /* 指下一顶点的指标   */
};
typedef struct node *graph;       /* 图形的结构新型态   */
struct node head[9];              /* 图形顶点数组       */
int visited[9];                   /* 遍历标记数组       */
queue<int> bqueue;
/********************根据已有的信息建立邻接表********************/
void creategraph(int node[20][2],int num)/*num指的是图的边数*/
{
   graph newnode;                 /*指向新节点的指针定义*/
   graph ptr;
   int from;                      /* 边的起点          */
   int to;                        /* 边的终点          */
   int i;
   for ( i = 0; i < num; i++ )    /* 读取边线信息,插入邻接表*/
   {
      from = node[i][0];         /*    边线的起点            */
      to = node[i][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;    /* 插入节点        */
   }
}

/**********************  图的深度优先搜寻法********************/
void bfs(node head[9],int current){/*非递归深度优先遍历算法*/
	node* p;
	node* stack[9];
	int top=-1,vertex,nextvertex;
	printf("%d\n",current);
	visited[current] = 1;
	bqueue.push(current);
	while(!bqueue.empty()){
		vertex = bqueue.front();
		bqueue.pop();
		p = head[vertex].nextnode;
		while(p != NULL){
			nextvertex = p->vertex;
			if(visited[nextvertex] == 0){
				printf("%d\n",nextvertex);
				visited[nextvertex] = 1;
				bqueue.push(nextvertex);
			}
			p = p->nextnode;
		}
	}
}
/****************************** 主程序******************************/
int main()
{
   graph ptr;
   int node[20][2] = { {1, 2}, {2, 1},  /* 边线数组     */
                       {1, 3}, {3, 1},
                       {1, 4}, {4, 1},
                       {2, 5}, {5, 2},
                       {2, 6}, {6, 2},
                       {3, 7}, {7, 3},
                       {4, 7}, {4, 4},
                       {5, 8}, {8, 5},
                       {6, 7}, {7, 6},
                       {7, 8}, {8, 7} };
   int i;
   //clrscr();
   for ( i = 1; i <= 8; i++ )      /*   顶点数组初始化  */
   {
      head[i].vertex = i;         /*    设定顶点值      */
      head[i].nextnode = NULL;    /*       指针为空     */
      visited[i] = 0;             /* 设定遍历初始标志   */
   }
   creategraph(node,20);          /*    建立邻接表      */
   printf("Content of the gragh's ADlist is:\n");
   for ( i = 1; i <= 8; i++ )
   {
      printf("vertex%d ->",head[i].vertex); /* 顶点值    */
      ptr = head[i].nextnode;             /* 顶点位置   */
      while ( ptr != NULL )       /* 遍历至链表尾       */
      {
         printf(" %d ",ptr->vertex);  /* 印出顶点内容   */
         ptr = ptr->nextnode;         /* 下一个顶点     */
      }
      printf("\n");               /*   换行             */
   }
   printf("\nThe end of the dfs are:\n");
   bfs(head,1);                        /* 打印输出遍历过程   */
   printf("\n");    
   printf("\nThe end of the dfs are:\n");              /* 换行               */
  // getch();
}

如果存在s到达v的路径(可以多条),广度优先搜索总能找到一条s到v的最短路径.

广度优先搜索在最坏的情况下,时间正比于V+E.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值