//'order_buf': record the bfs-order for each vertex. void DGRAPH::sort_in_bfs_order(SVECTOR<UINT> & order_buf, GRAPH & domtree, VERTEX * root) { LIST<VERTEX*> worklst; worklst.append_tail(root); UINT order = 1; while (worklst.get_elem_count() > 0) { VERTEX * sv = worklst.remove_head(); order_buf.set(VERTEX_id(sv), order); order++; EDGE_C * el = VERTEX_out_list(sv); while (el != NULL) { VERTEX * to = EDGE_to(EC_edge(el)); worklst.append_tail(to); el = EC_next(el); } } }
/* 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; }