Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 为了找到所有拓扑类型的DAG,需要私下定义索引向量吗?_Oop_C++11_Graph - Fatal编程技术网

Oop 为了找到所有拓扑类型的DAG,需要私下定义索引向量吗?

Oop 为了找到所有拓扑类型的DAG,需要私下定义索引向量吗?,oop,c++11,graph,Oop,C++11,Graph,在类的私有空间中定义indegree向量的重要性是什么?它可以在alltopologicalSort()函数中定义 class Graph { int V; // No. of vertices // Pointer to an array containing adjacency list list<int> *adj; // Vector to store indegree of vertices vector<int> indegree;

在类的私有空间中定义
indegree
向量的重要性是什么?它可以在alltopologicalSort()函数中定义

class Graph 
{ 
int V;    // No. of vertices 

// Pointer to an array containing adjacency list 
list<int> *adj; 

// Vector to store indegree of vertices 
vector<int> indegree; 

// A function used by alltopologicalSort 
void alltopologicalSortUtil(vector<int>& res, 
                            bool visited[]); 

public: 
Graph(int V);   // Constructor 

// function to add an edge to graph 
void addEdge(int v, int w); 

// Prints all Topological Sorts 
void alltopologicalSort(); 
}; 
使用indegree时,请在此解释加法函数在递减indegree中的作用

void Graph::alltopologicalSortUtil(vector<int>& res, 
                               bool visited[]) 
{ 
// To indicate whether all topological are found 
// or not 
bool flag = false;  

for (int i = 0; i < V; i++) 
{ 
    //  If indegree is 0 and not yet visited then 
    //  only choose that vertex 
    if (indegree[i] == 0 && !visited[i]) 
    { 
        //  reducing indegree of adjacent vertices 
        list<int>:: iterator j; 
        for (j = adj[i].begin(); j != adj[i].end(); j++) 
            indegree[*j]--; 

        //  including in result 
        res.push_back(i); 
        visited[i] = true; 
        alltopologicalSortUtil(res, visited); 

        // resetting visited, res and indegree for 
        // backtracking 
        visited[i] = false; 
        res.erase(res.end() - 1); 
        for (j = adj[i].begin(); j != adj[i].end(); j++) 
            indegree[*j]++; 

        flag = true; 
      } 
     }
 }   
void图::alltopologicalSortUtil(矢量和分辨率,
布尔访问[])
{ 
//指示是否找到所有拓扑结构
//还是不
布尔标志=假;
对于(int i=0;i
这是找到所有拓扑类型的有向无环图的完整代码的链接

我从上面与古普塔和卡娅的讨论中得到了答案

Indegree可以在某个函数中定义,然后作为引用传递给alltopologicalSort()函数。但是,在课堂上定义它会使它更容易处理


由于封装规则,类的数据成员总是保持私有的

我认为答案可能是“您不需要,但代码作者选择了”。@kaya3它不可能在alltopologicalSort()函数中定义,因为它在其他方法中使用,如
addEdge()
方法。@Gupta显然,如果未在类中定义它,则该类的
addEdge
方法将无法保持其状态,而另一个方法将必须这样做。这是一个微不足道的重构,不是一个不可逾越的障碍。@kaya3所以,问题的答案不是“你不需要,但代码的作者选择了”。答案是“您应该在类级别定义indegree,因为它被其他方法共享。”@Gupta有两种不同的设计方法。问题是为什么一个而不是另一个。如果你想假设这个问题仅仅是由OP犯了一个小错误引起的,那就继续吧,但我认为没有理由这样假设。无论哪种方式,“它不可能在alltopologicalSort()中定义”都是错误的;如果它是以另一种方式设计的,它本来是可以的。
void Graph::alltopologicalSortUtil(vector<int>& res, 
                               bool visited[]) 
{ 
// To indicate whether all topological are found 
// or not 
bool flag = false;  

for (int i = 0; i < V; i++) 
{ 
    //  If indegree is 0 and not yet visited then 
    //  only choose that vertex 
    if (indegree[i] == 0 && !visited[i]) 
    { 
        //  reducing indegree of adjacent vertices 
        list<int>:: iterator j; 
        for (j = adj[i].begin(); j != adj[i].end(); j++) 
            indegree[*j]--; 

        //  including in result 
        res.push_back(i); 
        visited[i] = true; 
        alltopologicalSortUtil(res, visited); 

        // resetting visited, res and indegree for 
        // backtracking 
        visited[i] = false; 
        res.erase(res.end() - 1); 
        for (j = adj[i].begin(); j != adj[i].end(); j++) 
            indegree[*j]++; 

        flag = true; 
      } 
     }
 }