本文整理汇总了C++中Digraph::n方法的典型用法代码示例。如果您正苦于以下问题:C++ Digraph::n方法的具体用法?C++ Digraph::n怎么用?C++ Digraph::n使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Digraph
的用法示例。
在下文中一共展示了Digraph::n方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: toposort
void toposort(Digraph& dg, int *pos, int *vert) {
// Compute a topological ordering of dg. On return, pos[u]
// is the position of vertex u in the ordering and vert[i]
// is the vertex in the i-th position in the ordering.
int i; vertex u,v; edge e;
UiList q(dg.n());
int *nin = new int[dg.n()+1];
// Let nin[u]=in-degree of u and put nodes u with nin[u]=0 on q
for (u = 1; u <= dg.n(); u++) {
nin[u] = 0;
for (e = dg.firstIn(u); e != 0; e=dg.nextIn(u,e)) {
nin[u]++;
}
if (nin[u] == 0) q.addLast(u);
}
i = 0;
while (!q.empty()) { // q contains nodes u with nin[u] == 0
u = q.first(); q.removeFirst(); pos[u] = ++i; vert[i] = u;
for (e = dg.firstOut(u); e != 0; e = dg.nextOut(u,e)) {
v = dg.head(e);
if ((--(nin[v])) == 0) q.addLast(v);
}
}
if (i < dg.n()) fatal("toposort: graph has cycle");
}
示例2: dfs
void KosarajuSCC::dfs(Digraph& dg, int v) {
marked[v] = true;
id[v] = count;
for (int w = dg.first(v); w < dg.n(); w = dg.next(v, w)) {
if (!marked[w])
dfs(dg, w);
}
}
示例3: toposort
/** Compute a topological ordering of dg.
* @param dg is a reference to an acyclic digraph
* @param vlist is a List object in which result is returned;
* on return, the vertices in vlist are in topogical order
*/
bool toposort(const Digraph& dg, List& vlist) {
List q(dg.n());
int *nin = new int[dg.n()+1];
if (vlist.n() < dg.n()) vlist.resize(dg.n());
else vlist.clear();
// Let nin[u]=in-degree of u and put nodes u with nin[u]=0 on q
for (vertex u = 1; u <= dg.n(); u++) {
nin[u] = 0;
for (edge e = dg.firstIn(u); e != 0; e = dg.nextIn(u,e)) {
nin[u]++;
}
if (nin[u] == 0) q.addLast(u);
}
int i = 0;
while (!q.empty()) { // q contains nodes u with nin[u] == 0
vertex u = q.first(); q.removeFirst(); vlist.addLast(u); i++;
for (edge e = dg.firstOut(u); e != 0; e = dg.nextOut(u,e)) {
vertex v = dg.head(e);
if ((--(nin[v])) == 0) q.addLast(v);
}
}
return (i == dg.n());
}
示例4: DepthFirstOrder
KosarajuSCC::KosarajuSCC(Digraph& dg) {
marked = new bool[dg.n()];
for (int i = 0; i < dg.n(); i++)
marked[i] = false;
id = new int[dg.n()];
count = 0;
Digraph* dgR = dg.reverse();
DepthFirstOrder* dforder = new DepthFirstOrder(*dgR);
stack<int>& reverseOrder = dforder->reversePostOrder();
delete dgR;
while (!reverseOrder.empty()) {
int v = reverseOrder.top();
reverseOrder.pop();
if (!marked[v]) {
dfs(dg, v);
count++;
}
}
delete dforder;
}
示例5: inOrder
main() {
int i; vertex u,v; edge e; string s;
Digraph dg; dg.read(cin);
int *pos = new int[dg.n()+1];
vertex *vert = new vertex[dg.n()+1];
toposort(dg,pos,vert);
Digraph inOrder(dg.n(),dg.m());
cout << "# ";
for (i = 1; i <= dg.n(); i++) {
u = vert[i];
cout << Util::node2string(u,dg.n(),s) << "->";
cout << Util::node2string(pos[u],dg.n(),s) << " ";
if ((i%10) == 0) cout << "\n# ";
for (e = dg.firstOut(u); e != 0; e=dg.nextOut(u,e)) {
v = dg.head(e);
inOrder.join(pos[u],pos[v]);
}
}
inOrder.sortAdjLists();
cout << endl << inOrder.toString(s);
}