博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
两顶点的路径长度为k_计算两个顶点之间的所有可能路径
阅读量:2530 次
发布时间:2019-05-11

本文共 1865 字,大约阅读时间需要 6 分钟。

两顶点的路径长度为k

What to Learn?

学什么?

How to count all possible paths between two vertices?

如何计算两个顶点之间的所有可能路径?

In the graph there are many alternative paths from vertex 0 to vertex 4

在图中,有许多从顶点0到顶点4的替代路径。

1.	0 → 1 → 2 → 3 → 4    2.	0 → 1 → 4    3.	0 → 3 → 2 → 1 → 4    4.	0 → 3 → 4
Graph 7 Example

Algorithm:

算法:

Here we use a recursive method to detect all the paths of a graph,

在这里,我们使用递归方法来检测图的所有路径

  1. We start the recursive function with the starting vertex.

    我们从起始顶点开始递归函数。

  2. For each node

    对于每个节点

    1. Whenever we visited one vertex we mark it and go for all its unvisited adjacent nodes.
    2. If we found the destination vertex then count the number else we go for recursive call.
Check(current node){        visit[curr_node]=true;        if(curr_node == destination){            count++;        }        else{            for( all the adjacent vertices ){                if(not visited yet) then                    check(adjacent vertex);            }        }    }

C++ implementation:

C ++实现:

#include 
using namespace std;//Make a pair between vertex x and vertex yvoid addedge(list
*ls,int x,int y){
ls[x].push_back(y); ls[y].push_back(x);}//count the number of paths existsvoid path_count(list
*ls,int s,int d,bool *visit,int &count){
visit[s]=true; if(s==d){
count++; } else{
list
::iterator it; for(it=ls[s].begin();it!=ls[s].end();it++){
if(!visit[*it]){
path_count(ls,*it,d,visit,count); } } } visit[s]=false;}int main(){
list
ls[6]; addedge(ls,0,1); addedge(ls,2,3); addedge(ls,3,4); addedge(ls,4,5); addedge(ls,1,2); addedge(ls,1,4); addedge(ls,3,0); bool visit[6]; for(int i=0;i<6;i++){
visit[i]=false; } int count=0; path_count(ls,0,4,visit,count); cout<<"Paths are : "<
<

Output

输出量

Paths are : 4

翻译自:

两顶点的路径长度为k

转载地址:http://xwxzd.baihongyu.com/

你可能感兴趣的文章
iOS开发网络篇—XML数据的解析
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
我需要一个足够大的桌子
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
linux awk命令详解
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>