离散作业之无环图两点间路径数目
来源:互联网 发布:软件研发中心简介 编辑:程序博客网 时间:2024/06/05 17:14
又是离散作业 ,无环图两点间路径数目,不指出具体路径,不判断图是否有环
Write a program that takes as input a directed acyclic graph G = (V, E) and two
vertices s and t, and returns the number of paths from s to t in G.
思路:通过DFS求拓扑排序,利用拓扑排序的有序性,求两点间的所有路径数
void Graph::initDFS(){
initBFS();
for(int i=0;i<nodeCounts;i++){
graNodes[i].fin=INF;
}
}
bool Graph::DFS(){
initDFS();
int time=0;
for(int i=0;i<nodeCounts;i++){
if(graNodes[i].color==WHITE)
DFSVisit(&graNodes[i],&time);
}
return true;
}
void Graph::DFSVisit(Node* uNode, int* time){
uNode->color=GRAY;
(*time)++;
uNode->level=(*time);
for (vector<Node*>::iterator ivl = (uNode->adjList).begin(); ivl != (uNode->adjList).end(); ivl++) {
if((*ivl)->color==WHITE){
(*ivl)->pre=uNode->nodeID;
DFSVisit(*ivl,time);
}
}
uNode->color=BLACK;
(*time)++;
uNode->fin=(*time);
//cout<<"*"<<uNode->nodeID<<"*"<<uNode->level<<"*"<<uNode->fin<<endl;
}
int Graph::Paths(int s,int t){
if(s>=nodeCounts || t>=nodeCounts){
cout<<"输入的节点不在图中"<<endl;
return 0;
}
DFS();
int sT=graNodes[s].fin;
//cout<<sT<<endl;
int tT=graNodes[t].fin;
//cout<<tT<<endl;
if(sT<=tT){
cout<<"终点比起点先结束"<<endl;
return 0;
}
for(int i=0;i<nodeCounts;i++){
graNodes[i].justPaths=JUST;
}
graNodes[s].justPaths=1;
deque<Node*> blocks;
blocks.clear();
blocks.push_back(&graNodes[s]);
Node* tempNode;
int time;
int num=sT-tT;
int* dequeNode=new int[num];
for(int i=0;i<num;i++)
dequeNode[i]=INF;
for(int i=0;i<nodeCounts;i++){
time=graNodes[i].fin-tT;
if(time<num && time>0){
dequeNode[time]=graNodes[i].nodeID;
}
}
for(int i=num-1;i>0;i--){
int id=dequeNode[i];
if(id!=-1){
blocks.push_back(&graNodes[id]);
}
}
while(!blocks.empty()){
tempNode=blocks.front();
//cout<<"xjt"<<tempNode->nodeID<<endl;
time=tempNode->fin;
blocks.pop_front();
for (vector<Node*>::iterator ivl = (tempNode->adjList).begin(); ivl != (tempNode->adjList).end(); ivl++) {
if((((*ivl)->fin)<time) && (((*ivl)->fin)>=tT)){
((*ivl)->justPaths)+=tempNode->justPaths;
}
//cout<<(*ivl)->nodeID<<"***"<<(*ivl)->justPaths<<endl;
}
}
return graNodes[t].justPaths;
}
- 离散作业之无环图两点间路径数目
- 求任意两点路径数目+floyed+uva125
- 有向无环图两点之间的路径数目(算法导论22.4-2)
- 离散作业之真值表
- 两点间方格路径总数
- 离散作业之goodguy and badguy
- 打印两点间的所有路径
- 深度优先搜索,两点间所有路径
- 两点间所有路径的遍历算法
- 平面坐标两点间路径问题
- 任意两点间的最短路径
- 矩阵乘法两点间路径问题
- 有向有环图两点间路径问题
- 软件测试作业 图中任意两点的最短路径
- dijkstra算法--求两点之的最短路径
- 图算法之求两点之间的所有路径(java)
- 寻找路径数目
- 两点间多条最短路径
- 一个简单的空间配置器: allocator
- 监听程序无法分发客户机连接
- Evolution分类管理邮件
- Apache POI 实现Excel文件单元格合并、冻结和文件导出
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
- 离散作业之无环图两点间路径数目
- 关于TelephonyManager类所有方法及说明: RequestLocationUpdates
- mina2.0的优化
- 使用smarty缓存控制
- 抢购倒计时,理论是同步的,网速越快,误差就越小
- 使用ruby来操作excel文件 Win32ole
- Shell 脚本专家指南 学习笔记1
- c++指针知识
- Amazon或将于明年推出KindlePhone智能手机