两顶点的路径长度为k_计算两个顶点之间的所有可能路径
生活随笔
收集整理的這篇文章主要介紹了
两顶点的路径长度为k_计算两个顶点之间的所有可能路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
兩頂點的路徑長度為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 → 42. 0 → 1 → 43. 0 → 3 → 2 → 1 → 44. 0 → 3 → 4Algorithm:
算法:
Here we use a recursive method to detect all the paths of a graph,
在這里,我們使用遞歸方法來檢測圖的所有路徑 ,
We start the recursive function with the starting vertex.
我們從起始頂點開始遞歸函數。
For each node
對于每個節點
C++ implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;//Make a pair between vertex x and vertex y void addedge(list<int>*ls,int x,int y){ls[x].push_back(y);ls[y].push_back(x); }//count the number of paths exists void path_count(list<int>*ls,int s,int d,bool *visit,int &count){visit[s]=true;if(s==d){count++;}else{list<int>::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<int> 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 : "<<count<<endl;return 0; }Output
輸出量
Paths are : 4翻譯自: https://www.includehelp.com/data-structure-tutorial/count-all-the-possible-path-between-two-vertices.aspx
兩頂點的路徑長度為k
總結
以上是生活随笔為你收集整理的两顶点的路径长度为k_计算两个顶点之间的所有可能路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言春考题目,PAT 2017年春考乙
- 下一篇: 8086 寻址方式_8086微处理器的不