Exemple #1
0
/*
Sort graph vertices in topological order. 
'vex_vec': record nodes with topological sort.
NOTE: current graph will be empty at function return.
	If one need to keep the graph unchanged, clone graph
	as a tmpgraph and operate on the tmpgraph.
	e.g: GRAPH org; 
		And org must be unchanged, 
		GRAPH tmp(org);
		tmp.sort_in_toplog_order(...)	
*/
bool GRAPH::sort_in_toplog_order(OUT SVECTOR<UINT> & vex_vec, bool is_topdown)
{
	IS_TRUE(m_pool != NULL, ("Graph still not yet initialize."));
	if (get_vertex_num() == 0) {
		return true;
	}
	LIST<VERTEX*> vlst;
	UINT pos = 0;
	vex_vec.clean();
	vex_vec.grow(get_vertex_num());
	while (this->get_vertex_num() != 0) {
		vlst.clean();
		VERTEX * v;
		INT c;
		for (v = this->get_first_vertex(c);
			 v != NULL; v = this->get_next_vertex(c)) {
			if (is_topdown) {
				if (VERTEX_in_list(v) == NULL) {
					vlst.append_tail(v);
				}
			} else if (VERTEX_out_list(v) == NULL) {
				vlst.append_tail(v);				
			}
		}
		if (vlst.get_elem_count() == 0 && this->get_vertex_num() != 0) {
			IS_TRUE(0, ("exist cycle in graph"));
			return false;
		}
		for (v = vlst.get_head(); v != NULL; v = vlst.get_next()) {
			vex_vec.set(pos, VERTEX_id(v));
			pos++;
			this->remove_vertex(v);
		}
	}
	return true;
}