本文整理汇总了C++中Digraph::adj方法的典型用法代码示例。如果您正苦于以下问题:C++ Digraph::adj方法的具体用法?C++ Digraph::adj怎么用?C++ Digraph::adj使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Digraph
的用法示例。
在下文中一共展示了Digraph::adj方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bfs
/* @brief breadth first search to compute bfs tree(forest)
* @param g digraph
* @param sources vertices to start
*
* @note may be not the shortest path
*/
void BFSDirectedPaths::bfs(const Digraph &g, const vector<int> &sources){
queue<int> q;
int sz = sources.size();
for(int i=0; i<sz; ++i){
int v = sources[i];
marked[v] = true;
distTo[v] = 0;
edgeTo[v] = v;
q.push(v);
}
while( !q.empty() ){
int v = q.front();
list<int>::const_iterator it;
for(it = g.adj(v).begin(); it != g.adj(v).end(); ++it){
int w = *it;
if( !marked[w] ){
marked[w] = true;
distTo[w] = distTo[v] + 1;
edgeTo[w] = v;
q.push(w);
}
}
//deque
q.pop();
}
}
示例2: dfs
/* @brief depth first search to detect cycle
*
*/
void DirectedCycle::dfs(const Digraph &dg, int v){
marked[v] = true;
onStack[v] = true;
list<int>::const_iterator it;
for( it = (dg.adj(v)).begin(); it != dg.adj(v).end(); ++it){
//there exists a cycle
if( hasCycle() ){
return ;
}
if( !marked[*it] ){
edgeTo[*it] = v;
dfs(dg, *it);
}else{
//cycle detected
if( onStack[*it] ){
for(int u = v; u != *it; u = edgeTo[u] ){
cyc.push_front(u);
}
cyc.push_front(*it);
cyc.push_front(v);
}
}
}
//v has been searched
onStack[v] = false;
}
示例3: dfs
void dfs(const Digraph &graph, size_t v)
{
on_stack[v] = true;
marked[v] = true;
vector<size_t> vec = graph.adj(v);
for (size_t i = 0; i < vec.size(); i++)
{
if (this->hasCycle())
return;
else if (!marked[vec[i]])
{
edgeto[vec[i]] = v;
dfs(graph, vec[i]);
}
else if (on_stack[vec[i]])
{
for (size_t x = v; x != vec[i]; x = edgeto[x])
sta.push(x);
sta.push(vec[i]);
sta.push(v);
}
}
on_stack[v] = false;
}