邻接矩阵实现Dijkstra算法以及BFS与DFS算法
//============================================================================
// Name : MatrixUDG.cpp
// Author : fffff
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include<iostream>
#include<cstdlib>
#include<queue>
#define maxsize 100
#define far 10000
using namespace std;
class MatrixUDG{
public:
char mVerxs[maxsize];
int mVerNum;
int mEdgeNum;
public:
int mMatrix[maxsize][maxsize];
MatrixUDG();
MatrixUDG(char vexs[],int vlen,char edge[][],int elen);
~MatrixUDG();
void print();
int getPosition(char ch);
char getChar(int place);
char readChar();
};
MatrixUDG::MatrixUDG(char vexs[],int vlen,char edge[][],int elen){
this->mVerNum = vlen;
this->mEdgeNum = elen;
for(int i=;i<mVerNum;i++)
for(int j=;j<mVerNum;j++)
mMatrix[i][j] = far;
for(int i=;i<mVerNum;i++){
mVerxs[i] = vexs[i];
}
for(int i=;i<mEdgeNum;i++){
int start = getPosition(edge[i][]);
int end = getPosition(edge[i][]);
int weight;
cout<<"input weight of ("<<edge[i][]<<","<<edge[i][]<<") : ";
cin>>weight;
mMatrix[start][end]=weight;
mMatrix[end][start]=weight;
}
};
MatrixUDG::MatrixUDG(){
cout<<"input num of verx and edge:";
cin>>mVerNum>>mEdgeNum;
cout<<"input verxs:";
for(int i=;i<mVerNum;i++){
mVerxs[i] = readChar();
}
for(int i=;i<mEdgeNum;i++){
cout<<"edge("<<i<<") : ";
char startChar = readChar();
char endChar = readChar();
int start = getPosition(startChar);
int end = getPosition(endChar);
int weight;
cout<<"input weight of ("<<startChar<<","<<endChar<<") : ";
cin>>weight;
mMatrix[start][end]=weight;
mMatrix[end][start]=weight;
}
};
MatrixUDG::~MatrixUDG(){ };
void MatrixUDG::print(){
for(int i=;i<mVerNum;i++){
for(int j=;j<mVerNum;j++){
cout<<mMatrix[i][j]<<" ";
}
cout<<endl;
}
}
int MatrixUDG::getPosition(char ch){
for(int i=;i<mVerNum;i++){
if(mVerxs[i]==ch)
return i;
}
return -;
};
char MatrixUDG::getChar(int place){
return mVerxs[place];
};
char MatrixUDG::readChar(){
char cha;
cin>>cha;
while(!((cha>='a'&&cha<='z')||(cha>='A'&&cha<='Z')))
cin>>cha;
return cha;
};
void Dijkstra(int n,int v,int *dist,int *pre,int weight[][maxsize]){
/*
* 第一步:初始化s[n],dist,pre
*/
bool s[n];
for(int i=;i<n;i++){
dist[i] = weight[v][i];
s[i] = false;
if(dist[i]==far)
pre[i] = -;
else
pre[i] = v;
}
s[v] = true;
dist[v] = ;
/*
* 第二步:寻找出dist最小的顶点加入到s中
*/
for(int i=;i<n;i++){
int temp = far;
int u = v;
for(int j=;j<n;j++){
if(!s[j]&&dist[j]<temp){
temp = dist[j];
u = j;
}
}
s[u] = true;
/*
* 第三步:更新dist
*/
for(int j=;j<n;j++){
if(!s[j]&&weight[u][j]<far){
int newdist = dist[u]+weight[u][j];
if(newdist<dist[j]){
dist[j] = newdist;
pre[j] = u;
}
}
}
} }
void printTrace(MatrixUDG*PG,int *pre,int v,int u){
int start = v;
int end = u;
char cha;
while(end!=start){
cha = PG->getChar(end);
cout<<cha<<"<--";
end = pre[end];
}
cout<<PG->getChar(start)<<endl; }
bool visited[maxsize];
void Dfsk(MatrixUDG *PG,int k){
visited[k] = true;
cout<<PG->getChar(k)<<" ";
for(int i=;i<PG->mVerNum;i++)
if(PG->mMatrix[k][i]!=far&&visited[i]==false)
Dfsk(PG,i);
}
void Dfs(MatrixUDG *PG){
for(int i=;i<PG->mVerNum;i++)
if(!visited[i])
Dfsk(PG,i);
cout<<endl;
}
void Bfs(MatrixUDG *PG){
queue<int>que;
que.push();
int place;
while(!que.empty()){
place = que.front();
que.pop();
visited[place] = true;
cout<<PG->getChar(place)<<" ";
for(int i=;i<PG->mVerNum;i++){
if(PG->mMatrix[place][i]!=far&&visited[i]==false){
que.push(i);
/*
* 开始时掉了visited[i] = true;导致出错
* 因为有可能在队列中的顶点还没有出来,在后面再一次的进入队列
* 因此必须在进入队列后将其设为已访问来防止再次进入而导致的重复访问
*/
visited[i] = true;
}
}
}
}
int main(){
char verx[] = {'A','B','C','D','E','F','G'};
char edge[][] = {
{'A','C'},
{'A','E'},
{'B','D'},
{'B','E'},
{'B','G'},
{'C','F'},
{'D','F'},
{'E','G'},
{'F','G'}
};
int vlen = sizeof(verx)/sizeof(verx[]);
int elen = sizeof(edge)/sizeof(edge[]);
MatrixUDG *PG = new MatrixUDG(verx,vlen,edge,elen);
int dist[vlen];
int pre[vlen];
int startVex =;
Dijkstra(vlen,startVex,dist,pre,PG->mMatrix);
PG->print();
printTrace(PG,pre,,);
for(int i=;i<maxsize;i++){
visited[i] = false;
}
Dfs(PG);
for(int i=;i<maxsize;i++){
visited[i] = false;
}
Bfs(PG);
return ; }
邻接矩阵实现Dijkstra算法以及BFS与DFS算法的更多相关文章
- BFS和DFS算法
昨晚刚昨晚华为笔试题,用到了BFS和DFS,可惜自己学艺不精,忘记了实现原理,现在借用大佬写的内容给自己做个提高 转自:https://www.jianshu.com/p/70952b51f0c8 图 ...
- 15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法
算法分析和具体步骤解说直接写在代码注释上了 TvT 没时间了等下还要去洗衣服 就先不赘述了 有不明白的欢迎留言交流!(估计是没人看的了) 直接上代码: #include<stdio.h> ...
- 图的基本算法(BFS和DFS)
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
- 图算法(一)——基本图算法(BFS,DFS及其应用)(2)
2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和 ...
- 图算法(一)——基本图算法(BFS,DFS及其应用)(1)
1)BFS 广度优先搜索:给定源节点s,生成广度优先搜索树广度优先搜索树中从节点s到节点v的简单路径对应的就是s到v的最短路径(边数最少的路径)广度优先:将已发现节点与未发现节点之间的边界(灰色节点) ...
- BFS与DFS算法解析
1)前言 和树的遍历类似,图的遍历也是从图中某点出发,然后按照某种方法对图种所有顶点进行访问,且仅访问一次. 但是图的遍历相对树的遍历更为复杂,因为图中任意顶点都能与其他顶点相邻,所以在图的遍历中必须 ...
- BFS/DFS算法介绍与实现(转)
广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...
- 算法录 之 BFS和DFS
说一下BFS和DFS,这是个比较重要的概念,是很多很多算法的基础. 不过在说这个之前需要先说一下图和树,当然这里的图不是自拍的图片了,树也不是能结苹果的树了.这里要说的是图论和数学里面的概念. 以上概 ...
- 图结构练习——判断给定图是否存在合法拓扑序列(dfs算法(第一个代码),邻接矩阵(前两个代码),邻接表(第三个代码))
sdut 2140 图结构练习——判断给定图是否存在合法拓扑序列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 给定一个有向图 ...
随机推荐
- 织梦dedecms调用栏目的SEO标题、描述、关键字的方法
调用SEO标题:<title>{dede:field.title/}_{dede:field.seotitle /}-{dede:global.cfg_webname/}</titl ...
- Asp.net默认配置下,Session莫名丢失的原因及解决
Asp.net默认配置下,Session莫名丢失的原因及解决 我们平时写的asp.net程序,里面要用到Session来保存一些跨页面的数据.但是Session会经常无故丢失,上网查查,也没找到原因. ...
- Loadrunner:error -86401 Failed to connceted xxx.xxx.xxx.xxx:25问题解决
[转自:http://www.51testing.com/html/00/130600-3578408.html]windows 2003上安装的LoadRunner11做为负载机在SMTP协议压测时 ...
- vim中查找指定字符串
0x01 自当前光标位置向上搜索 /pattern Enter (pattern表示要搜索的字符串) 0x02 自当前光标位置向下搜索 ?pattern Enter 0x03 n继续从同 ...
- 使用Spring开发第一个HelloWorld应用
http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...
- 搭建高性能计算环境(六)、应用软件的安装之lammps
1,上传需要的软件包lammps-stable.tar.gz. 2,解压缩并进入安装目录 tar xvf lammps-stable.tar.gz cd lammps-30Oct14 3,如果需要re ...
- Oracle一些常用的查询命令总结(持续更新)
更新于:2015年1月28日 17:08:13 -------------------------表空间 --------------------------------------- ----- 查 ...
- phpQuery采集微信公众号文章乱码
终于找到解决方案了,这是一个值得庆祝的事情.... 原来是因为微信在源码中加入了防采集代码<!--headTrap<body></body><head>< ...
- Ajax的get请求向服务器请求数据五步骤?
如下: ①创建ajax对象 ②建立http请求 ③发送http请求 ④设置ajax对象状态改变的回调函数 ⑤判断ajax状态是否等于4,做相应的业务逻辑
- Android VideoView简单播放视频
给Android VideoView一个文件目录,就可以直接播放智能设备中的视频文件,现在以播放事先用手机拍好并重命名的视频文件test.mp4为例.(1) 需要在布局文件中写一个ViedoView: ...