当前位置: 首页>>代码示例>>C++>>正文


C++ Digraph::nextOut方法代码示例

本文整理汇总了C++中Digraph::nextOut方法的典型用法代码示例。如果您正苦于以下问题:C++ Digraph::nextOut方法的具体用法?C++ Digraph::nextOut怎么用?C++ Digraph::nextOut使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Digraph的用法示例。


在下文中一共展示了Digraph::nextOut方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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");
}
开发者ID:caiograg,项目名称:grafalgo,代码行数:26,代码来源:toposort.cpp

示例2: 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());
}
开发者ID:caiograg,项目名称:grafalgo,代码行数:30,代码来源:toposort.cpp

示例3: 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);
}
开发者ID:caiograg,项目名称:grafalgo,代码行数:21,代码来源:toposort.cpp


注:本文中的Digraph::nextOut方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。