C++ 每个顶点的正向路径
从顶点v开始的向前路径是以汇节点结束的路径。编写一个O(n)算法来检测每个顶点的正向路径数 我能想到的逻辑 我创建了一个包含顶点总数的数组,并将它们初始化为零 就在DFS探索之前,我试着写C++ 每个顶点的正向路径,c++,graph,depth-first-search,C++,Graph,Depth First Search,从顶点v开始的向前路径是以汇节点结束的路径。编写一个O(n)算法来检测每个顶点的正向路径数 我能想到的逻辑 我创建了一个包含顶点总数的数组,并将它们初始化为零 就在DFS探索之前,我试着写 if(v has no outgoing edge) forwardPath[v] += 1 但是,我不确定如何实现if语句 我所做的 #include<iostream> #include<list> using namespace std; // Graph c
if(v has no outgoing edge)
forwardPath[v] += 1
但是,我不确定如何实现if语句
我所做的
#include<iostream>
#include<list>
using namespace std;
// Graph class represents a directed graph
// using adjacency list representation
static int *forwardPath;
class Graph
{
public:
int V; // No. of vertices
// Pointer to an array containing
// adjacency lists
list<int> *adj;
// A recursive function used by DFS
void DFSUtil(int v, bool visited[]);
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// DFS traversal of the vertices
// reachable from v
void DFS(int v);
void printGraph(int V);
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
forwardPath = new int[V];
for(int i=0; i<V; i++){
forwardPath[i] = 0;
}
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i]){
forwardPath[v] += 1;
DFSUtil(*i, visited);
}
}
// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
}
void printForwardPath(){
for(int i=0; i<5; i++){
cout<<forwardPath[i]<< " ";
}
cout<<endl;
}
void Graph:: printGraph(int V){
list<int> :: iterator it;
for(int i=0; i<V; i++){
cout<<i <<"-->";
for(int node:adj[i])
cout<<node<<",";
cout << endl;
}
}
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(2, 3);
g.addEdge(2, 5);
g.addEdge(3, 4);
g.printGraph(6);
cout<<"BELOW IS DFS "<<endl;
cout<<endl;
cout << "Following is Depth First Traversal"
" (starting from vertex 0) \n";
g.DFS(0);
cout<<endl;
cout<<"**********"<<endl;
cout << "Number of forward path\n"
"in the graph \n";
printForwardPath();
return 0;
}
#包括
#包括
使用名称空间std;
//Graph类表示有向图
//使用邻接列表表示法
静态int*forwardPath;
类图
{
公众:
int V;//顶点数
//指向包含
//邻接表
列表*adj;
//DFS使用的递归函数
void DFSUtil(int v,bool已访问[]);
公众:
图(int V);//构造函数
//函数将边添加到图形中
无效补遗(整数v,整数w);
//DFS顶点遍历
//可从v到达
无效DFS(INTV);
无效打印图(INTV);
};
图形::图形(intv)
{
这个->V=V;
adj=新列表[V];
forwardPath=新整数[V];
对于(int i=0;i相关:adj=新列表[V];
是个坏主意。您几乎不想动态分配列表或任何其他库容器。它们的存在在很大程度上是为了消除这种内存管理的需要。自动分配列表
,并让它在您添加和删除项目时处理事情。同样,也不需要转发路径=new int[V];
当std::vector
可用时。将forwardPath作为全局变量也是一个坏主意。如果它是全局变量,则只能安全地拥有一个图形。这可能是从计算路径的函数返回的局部向量。回答问题“如何区分入站边缘和出站边缘?”您将有您的解决方案。bool*visted=new bool[V];
--内存泄漏。您可以使用std::vector visted(V)
;