用BFS找最短路,并打印路径

转自:http://lingyibin.javaeye.com/blog/849828

 

我想大部分人都用Floyd或者Dijstra算法,甚至dfs算过最短路吧。

其实BFS也可以计算最短路。(补充:本文只针对无权图,有权图很难用BFS)

当我们用BFS找端对端最短路时,从出发点开始,第一次遍历到终点时过的那条路径就是最短的路径。(读者可以思考一下为什么!)

下面就用邻接表来实现一下BFS最短路,并把路径打出来。

 

Cpp代码
  1. #include<iostream>   
  2. #include<fstream>   
  3. #include<queue>   
  4. using namespace std;   
  5.   
  6. const int MAX = 50;   
  7.   
  8. struct link   
  9. {   
  10.  int data;   
  11.  link *next;   
  12. };   
  13. struct Node   
  14. {   
  15.  int v;     //顶点相关的信息   
  16.  link *next;   
  17. };   
  18. struct Graph   
  19. {   
  20.   Node node[MAX+1]; //所有的结点   
  21.   int nodeCnt; //用来记录图中结点的数目,这里假设用的是连通图。假设不连通,稍微改一下本程序就行了,具体操作留给读者思考!   
  22. };   
  23.   
  24. int visited[MAX+1]; //标志数组   
  25. int pa[MAX+1];   
  26.   
  27. /* 读取文件中的数据,构造一个邻接表来表示图 */    
  28. Graph readGraph()   
  29. {   
  30.     ifstream cin("c://graph.txt");   
  31.   
  32.     //表的初始化   
  33.     Graph graph;   
  34.     int i ;   
  35.     for(i = 1; i < MAX; i ++)   
  36.     {   
  37.         graph.node[i].v = i;   
  38.         graph.node[i].next = NULL;   
  39.     }   
  40.   
  41.     int n1 = 0,n2 = 0;   
  42.     link *s;   
  43.     graph.nodeCnt = 1;//把结点数初始化为1   
  44.     while(cin>>n1>>n2)   
  45.     {   
  46.         if(graph.nodeCnt < n1) graph.nodeCnt=n1;   
  47.         if(graph.nodeCnt < n2) graph.nodeCnt=n2;   
  48.         s = new link;   
  49.         s->data = n2;   
  50.         s->next=graph.node[n1].next;   
  51.         graph.node[n1].next=s; //从尾部插入   
  52.         //delete(s);   
  53.   
  54.         s=new link;   
  55.         s->data = n1;   
  56.         s->next=graph.node[n2].next;   
  57.         graph.node[n2].next=s; //反过来也赋值,说明本程序测试的是无向图,如果是有向图的话读者应该知道怎么改了吧!   
  58.         //delete(s);   
  59.     }   
  60.     return graph;   
  61. }   
  62.   
  63. /* 打印邻接表 */    
  64. void printGraph(Graph graph)   
  65. {   
  66.   link *p;   
  67.   for(int i = 1; i <= graph.nodeCnt; i ++)   
  68.   {   
  69.       cout<<graph.node[i].v<<" -- ";   
  70.   
  71.       p=graph.node[i].next;   
  72.       while(p!=NULL)   
  73.       {   
  74.           cout<<p->data;   
  75.           p=p->next;   
  76.           if(p != NULL)    
  77.               cout<<",";   
  78.       }   
  79.       cout<<endl;   
  80.   }   
  81. }   
  82.   
  83. /* 用BFS求最短路径 */    
  84. void shortestPath(Graph graph, int s, int d)   
  85. {   
  86.     cout<<"从"<<s<<"到"<<d<<"的bfs最短路求解过程如下:"<<endl;   
  87.     queue<int> que ;   
  88.     link * p = NULL;   
  89.     //int cnt = 0;   
  90.     int parents = s;   
  91.     memset(visited,0,sizeof(visited));   
  92.     memset(pa,0,sizeof(pa));   
  93.     visited[s] = 1;   
  94.     pa[s] = -1;   
  95.     que.push(s);   
  96.     while(!que.empty()){   
  97.         p = graph.node[que.front()].next;   
  98.         parents = que.front();   
  99.         que.pop();   
  100.         //cnt ++;   
  101.         while(p != NULL)   
  102.         {   
  103.             if(!visited[p->data])   
  104.             {   
  105.                 visited[p->data] = 1;   
  106.                 pa[p->data] = parents;   
  107.                 cout<<"访问:"<<p->data<<endl;   
  108.                 if(p->data == d) //找到了目标结点   
  109.                 {   
  110.                     //cout<<"在第"<<cnt<<"层找到目标结点!(出发点算第一层)"<<endl;   
  111.                     break;   
  112.                 }   
  113.                 que.push(p->data);   
  114.             }   
  115.             p = p->next;   
  116.         }   
  117.     }   
  118.     cout<<"路径如下:"<<endl;   
  119.     parents = d;   
  120.     //cout<<parents<<" <- ";   
  121.     while(pa[parents] != -1)   
  122.     {   
  123.         cout<<parents<<" <- ";   
  124.         parents = pa[parents];   
  125.     }   
  126.     cout<<parents<<endl;   
  127. }   
  128.   
  129. int main()   
  130. {   
  131.     int s,d;   
  132.     Graph graph = readGraph();   
  133.     printGraph(graph);   
  134.     while(true)   
  135.     {   
  136.         cout<<"请输入起点和终点:"<<endl;   
  137.         cin>>s>>d;   
  138.         shortestPath(graph, s , d);   
  139.     }   
  140.     return 0;   
  141. }  
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;

const int MAX = 50;

struct link
{
 int data;
 link *next;
};
struct Node
{
 int v;     //顶点相关的信息
 link *next;
};
struct Graph
{
  Node node[MAX+1]; //所有的结点
  int nodeCnt; //用来记录图中结点的数目,这里假设用的是连通图。假设不连通,稍微改一下本程序就行了,具体操作留给读者思考!
};

int visited[MAX+1]; //标志数组
int pa[MAX+1];

/* 读取文件中的数据,构造一个邻接表来表示图 */ 
Graph readGraph()
{
	ifstream cin("c://graph.txt");

	//表的初始化
	Graph graph;
	int i ;
	for(i = 1; i < MAX; i ++)
	{
		graph.node[i].v = i;
		graph.node[i].next = NULL;
	}

	int n1 = 0,n2 = 0;
	link *s;
	graph.nodeCnt = 1;//把结点数初始化为1
	while(cin>>n1>>n2)
	{
		if(graph.nodeCnt < n1) graph.nodeCnt=n1;
		if(graph.nodeCnt < n2) graph.nodeCnt=n2;
		s = new link;
		s->data = n2;
		s->next=graph.node[n1].next;
		graph.node[n1].next=s; //从尾部插入
		//delete(s);

		s=new link;
		s->data = n1;
		s->next=graph.node[n2].next;
		graph.node[n2].next=s; //反过来也赋值,说明本程序测试的是无向图,如果是有向图的话读者应该知道怎么改了吧!
		//delete(s);
	}
	return graph;
}

/* 打印邻接表 */ 
void printGraph(Graph graph)
{
  link *p;
  for(int i = 1; i <= graph.nodeCnt; i ++)
  {
	  cout<<graph.node[i].v<<" -- ";

	  p=graph.node[i].next;
	  while(p!=NULL)
	  {
		  cout<<p->data;
		  p=p->next;
		  if(p != NULL) 
			  cout<<",";
	  }
	  cout<<endl;
  }
}

/* 用BFS求最短路径 */ 
void shortestPath(Graph graph, int s, int d)
{
	cout<<"从"<<s<<"到"<<d<<"的bfs最短路求解过程如下:"<<endl;
	queue<int> que ;
	link * p = NULL;
	//int cnt = 0;
	int parents = s;
	memset(visited,0,sizeof(visited));
	memset(pa,0,sizeof(pa));
	visited[s] = 1;
	pa[s] = -1;
	que.push(s);
	while(!que.empty()){
		p = graph.node[que.front()].next;
		parents = que.front();
		que.pop();
		//cnt ++;
		while(p != NULL)
		{
			if(!visited[p->data])
			{
				visited[p->data] = 1;
				pa[p->data] = parents;
				cout<<"访问:"<<p->data<<endl;
				if(p->data == d) //找到了目标结点
				{
					//cout<<"在第"<<cnt<<"层找到目标结点!(出发点算第一层)"<<endl;
					break;
				}
				que.push(p->data);
			}
			p = p->next;
		}
	}
	cout<<"路径如下:"<<endl;
	parents = d;
	//cout<<parents<<" <- ";
	while(pa[parents] != -1)
	{
		cout<<parents<<" <- ";
		parents = pa[parents];
	}
	cout<<parents<<endl;
}

int main()
{
	int s,d;
	Graph graph = readGraph();
	printGraph(graph);
	while(true)
	{
		cout<<"请输入起点和终点:"<<endl;
		cin>>s>>d;
		shortestPath(graph, s , d);
	}
	return 0;
}

 

 

输入文件graph.txt里数据格式如下:

 

Txt代码
  1. 1 2    
  2. 1 3    
  3. 1 4    
  4. 2 4  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值