算法导论22(基本的图算法)
来源:互联网 发布:在淘宝买摩托车可靠吗 编辑:程序博客网 时间:2024/04/30 07:13
1. 广度优先搜索(BFS)
#include<iostream> #include<queue>using namespace std;const int N=100;bool visited[N];int d[N],pred[N];struct Node{ int v; Node *next; Node(int x):v(x),next(0){}};struct Graph { int VNum,ENum; Node *Adj[N]; }; //无向图,图的顶点的编号为0..VNum-1void createGraph(Graph &G) { cin>>G.VNum>>G.ENum; for(int i=0;i<G.VNum;++i)G.Adj[i]=0; for(int i=0;i<G.ENum;++i) { int u,v; cin>>u>>v; Node *p=new Node(v); p->next=G.Adj[u]; G.Adj[u]=p; p=new Node(u); p->next=G.Adj[v]; G.Adj[v]=p; } } void BFS(Graph G,int s){ for(int i=0;i<G.VNum;++i) { visited[i]=false; d[i]=INT_MAX; pred[i]=-1; } visited[s]=true; d[s]=0; queue<int> q; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(Node *p=G.Adj[u];p;p=p->next) { int v=p->v; if(!visited[v]) { visited[v]=true; d[v]=d[u]+1; pred[v]=u; q.push(v); } } }}void printPath(Graph G,int s,int v){ if(v==s)cout<<s<<' '; else { printPath(G,s,pred[v]); cout<<v<<' '; }}//输入//8 10//0 1//0 4//1 5//2 3//2 5//2 6//3 6//3 7//5 6//6 7////输出//1 0//1 //1 5 2//1 5 2 3//1 0 4//1 5//1 5 6//1 5 6 7int main() { Graph G; createGraph(G); int s=1; BFS(G,s); for(int i=0;i<G.VNum;++i) { printPath(G,s,i); cout<<endl; } return 0; }
2. 深度优先搜索(DFS)
#include<iostream>using namespace std;const int N=100; bool visited[N];int d[N],pred[N],f[N],time; struct Node { int v; Node *next; Node(int x):v(x),next(0){} };struct Graph { int VNum,ENum; Node *Adj[N]; }; //有向图,图的顶点的编号为0..VNum-1void createGraph(Graph &G) { cin>>G.VNum>>G.ENum; for(int i=0;i<G.VNum;++i)G.Adj[i]=0; for(int i=0;i<G.ENum;++i) { int u,v; cin>>u>>v; Node *p=new Node(v); p->next=G.Adj[u]; G.Adj[u]=p; } } void DFSVisit(Graph G,int u) { d[u]=++time; visited[u]=true; for(Node *p=G.Adj[u];p;p=p->next) { int v=p->v; if(!visited[v]) { pred[v]=u; DFSVisit(G,v); } } f[u]=++time; } void DFS(Graph G) { for(int i=0;i<G.VNum;++i) { visited[i]=false; pred[i]=-1; } time=0; for(int i=0;i<G.VNum;++i) { if(!visited[i])DFSVisit(G,i); } } //输入//6 8//0 1//0 3//1 4//2 4//2 5//3 1//4 3//5 5////输出//1 8//3 6//9 12//2 7//4 5//10 11int main() { Graph G; createGraph(G); DFS(G); for(int i=0;i<G.VNum;++i) { cout<<d[i]<<' '<<f[i]<<endl; } return 0; }
3. 拓扑排序
#include<iostream>#include<vector>#include<stack>using namespace std;struct Node{ int v,w; Node *next; Node(int x,int y):v(x),w(y),next(0){}};struct Graph{ int VNum,ENum; vector<Node *> Adj;};void createGraph(Graph &G){ cin>>G.VNum<<G.ENum; for(int i=0;i<G.VNum;++i)Adj.push_back(0); for(int i=0;i<G.ENum;++i) { int u,v,w; cin>>u>>v>>w; Node *p=new Node(v,w); p->next=G.Adj[u]; G.Adj[u]=p; }}vector<int> GetIndegree(Graph G){ vector<int> indegree(G.VNum,0); for(int i=0;i<G.VNum;++i) { for(Node *p=G.Adj[i];p;p=p->next) { int v=p->v; ++indegree[v]; } } return indegree;}vector<int> TopoSort(Graph G){ vector<int> res; vector<int> indegree=GetIndegree(G); stack<int> S; for(int i=0;i<G.VNum;++i) { if(indegree[i]==0)S.push(i); } while(!S.empty()) { int u=S.top(); S.pop(); res.push_back(u); for(Node *p=G.Adj[u];p;p=p->next) { int v=p->v; if(--indegree[v]==0)S.push(v); } } return res;}int main(){ Graph G; createGraph(G); vector<int> res=TopoSort(G); for(int i=0;i<res.size();++i)cout<<res[i]<<' '; cout<<endl; return 0;}
0 0
- 算法导论22(基本的图算法)
- 算法导论读书笔记(22)基本的图算法
- 算法导论 ch22 图的基本算法
- [算法导论]图的基本算法
- 算法导论之图的基本算法
- 算法导论代码 第22章 图的基本算法
- 算法导论 | 第22章基本的图算法
- 算法导论22章基本的图算法 思考题总结
- 算法导论笔记:22基本图算法
- 广度优先搜索(算法导论第22章-基本的图算法)
- 算法导论-第22章-基本的图算法-22.2 广度优先搜索(BFS)
- 算法导论-第22章-基本的图算法-22.3 深度优先搜索(DFS)
- 算法导论 第22章 图的基本算法(一)
- 算法导论 第22章 图的基本算法(二) 深度优先搜索
- 算法导论 第22章 图的基本算法(三) 拓扑排序
- 算法导论 第22章 图的基本算法(四) 强连通分支
- [算法导论]22:基本图结构
- 《算法导论》学习笔记-图的基本算法
- Android常用adb命令
- Sort List
- C#中的扩展方法
- OOA/OOD/OOP
- 开通400电话需要提交的资料
- 算法导论22(基本的图算法)
- Oracle DDL,DML,DCL,TCL 基础概念
- iOS懒加载
- Android Intent的几种用法
- 文件编码格式
- Android App框架设计之编写基类BaseActivity
- 无约束最优化方法——牛顿法、拟牛顿法、BFGS、LBFGS
- Android中 记住密码(SharedPreferences)
- 设计模式-Factory