void DominatorTree::squash(int v) { if (ANCESTOR(ANCESTOR(v)) >= 0) { squash(ANCESTOR(v)); if (SEMI(LABEL(ANCESTOR(v))) < SEMI(LABEL(v))) LABEL(v) = LABEL(ANCESTOR(v)); ANCESTOR(v) = ANCESTOR(ANCESTOR(v)); } }
void DominatorTree::buildDFS(Graph::Node *node) { SEMI(node->tag) = node->tag; for (Graph::EdgeIterator ei = node->outgoing(); !ei.end(); ei.next()) { if (SEMI(ei.getNode()->tag) < 0) { buildDFS(ei.getNode()); PARENT(ei.getNode()->tag) = node->tag; } } }
void DominatorTree::debugPrint() { for (int i = 0; i < count; ++i) { INFO("SEMI(%i) = %i\n", i, SEMI(i)); INFO("ANCESTOR(%i) = %i\n", i, ANCESTOR(i)); INFO("PARENT(%i) = %i\n", i, PARENT(i)); INFO("LABEL(%i) = %i\n", i, LABEL(i)); INFO("DOM(%i) = %i\n", i, DOM(i)); } }
void DominatorTree::build() { DLList *bucket = new DLList[count]; Node *nv, *nw; int p, u, v, w; buildDFS(cfg->getRoot()); for (w = count - 1; w >= 1; --w) { nw = vert[w]; assert(nw->tag == w); for (Graph::EdgeIterator ei = nw->incident(); !ei.end(); ei.next()) { nv = ei.getNode(); v = nv->tag; u = eval(v); if (SEMI(u) < SEMI(w)) SEMI(w) = SEMI(u); } p = PARENT(w); bucket[SEMI(w)].insert(nw); link(p, w); for (DLList::Iterator it = bucket[p].iterator(); !it.end(); it.erase()) { v = reinterpret_cast<Node *>(it.get())->tag; u = eval(v); DOM(v) = (SEMI(u) < SEMI(v)) ? u : p; } } for (w = 1; w < count; ++w) { if (DOM(w) != SEMI(w)) DOM(w) = DOM(DOM(w)); } DOM(0) = 0; insert(&BasicBlock::get(cfg->getRoot())->dom); do { p = 0; for (v = 1; v < count; ++v) { nw = &BasicBlock::get(vert[DOM(v)])->dom;; nv = &BasicBlock::get(vert[v])->dom; if (nw->getGraph() && !nv->getGraph()) { ++p; nw->attach(nv, Graph::Edge::TREE); } } } while (p); delete[] bucket; }
DominatorTree::DominatorTree(Graph *cfgraph) : cfg(cfgraph), count(cfg->getSize()) { int i = 0; vert = new Node * [count]; data = new int[5 * count]; for (IteratorRef it = cfg->iteratorDFS(true); !it->end(); it->next(), ++i) { vert[i] = reinterpret_cast<Node *>(it->get()); vert[i]->tag = i; LABEL(i) = i; SEMI(i) = ANCESTOR(i) = -1; } assert(i == count); build(); delete[] vert; delete[] data; }
int main() { igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t adj, V; igraph_vector_t groups; igraph_eigen_which_t which; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&adj, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_get_adjacency(&g, &adj, IGRAPH_GET_ADJACENCY_BOTH, /*eids=*/ 0); which.pos=IGRAPH_EIGEN_LM; which.howmany=1; igraph_eigen_matrix_symmetric(&adj, /*sparsemat=*/ 0, /*fun=*/ 0, igraph_vcount(&g), /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_SYMMETRIC, &L, &R, \ &Lsparse, &Rsparse, /*p=*/ 0, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_SYMMETRIC, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_vector_destroy(&groups); igraph_matrix_destroy(&V); igraph_matrix_destroy(&adj); igraph_destroy(&g); return 0; }
int main() { int nodes=10; igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t V; igraph_matrix_complex_t V2; igraph_sparsemat_t laplacian; igraph_vector_t groups; igraph_eigen_which_t which; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_matrix_complex_init(&V2, 0, 0); igraph_vector_init(&groups, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_sparsemat_init(&laplacian, nodes, nodes, igraph_ecount(&g)*2); igraph_rng_seed(igraph_rng_default(), 42); igraph_laplacian(&g, /*res=*/ 0, /*sparseres=*/ &laplacian, /*normalized=*/ 0, /*weights=*/ 0); which.pos=IGRAPH_EIGEN_LM; which.howmany=1; igraph_eigen_matrix(/*matrix=*/ 0, &laplacian, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_LAPLACIAN, &L, &R, \ &Lsparse, &Rsparse, /*p=*/ 0, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ igraph_sparsemat_destroy(&Lsparse); \ igraph_sparsemat_destroy(&Rsparse); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_OPTIMUM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV_KM, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 2, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_INTERV, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_LAPLACIAN, IGRAPH_SCG_EXACT, /*p=*/ 0, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_matrix_destroy(&L); igraph_matrix_destroy(&R); igraph_matrix_destroy(&V); igraph_matrix_complex_destroy(&V2); igraph_vector_destroy(&groups); igraph_sparsemat_destroy(&laplacian); igraph_destroy(&g); return 0; }
int main() { int nodes=10; igraph_t g; igraph_matrix_t L, R; igraph_sparsemat_t Lsparse, Rsparse; igraph_matrix_t V, V3; igraph_matrix_complex_t V2; igraph_sparsemat_t stochastic, stochasticT; igraph_vector_t groups; igraph_eigen_which_t which; igraph_vector_t p, selcol; igraph_matrix_init(&L, 0, 0); igraph_matrix_init(&R, 0, 0); igraph_matrix_init(&V, 0, 0); igraph_matrix_init(&V3, 0, 0); igraph_vector_init(&groups, 0); igraph_vector_init(&selcol, 1); igraph_rng_seed(igraph_rng_default(), 42); igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED); igraph_sparsemat_init(&stochastic, nodes, nodes, igraph_ecount(&g)*2); igraph_matrix_complex_init(&V2, 0, 0); igraph_vector_init(&p, 0); igraph_rng_seed(igraph_rng_default(), 42); igraph_get_stochastic_sparsemat(&g, &stochastic, /*column-wise=*/ 0); igraph_sparsemat_transpose(&stochastic, &stochasticT, /*values=*/ 1); which.pos=IGRAPH_EIGEN_LR; which.howmany=1; igraph_eigen_matrix(/*matrix=*/ 0, &stochasticT, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V); /* `p' is always the eigenvector corresponding to the 1-eigenvalue */ igraph_matrix_get_col(&V, &p, 0); which.howmany=3; igraph_eigen_matrix(/*matrix=*/ 0, &stochastic, /*fun=*/ 0, 10, /*extra=*/ 0, /*algorithm=*/ IGRAPH_EIGEN_LAPACK, &which, /*options=*/ 0, /*storage=*/ 0, /*values=*/ 0, &V2); igraph_matrix_complex_real(&V2, &V3); VECTOR(selcol)[0]=2; igraph_matrix_select_cols(&V3, &V, &selcol); #define SEMI() \ do { \ igraph_scg_semiprojectors(&groups, IGRAPH_SCG_STOCHASTIC, &L, &R, \ &Lsparse, &Rsparse, &p, \ IGRAPH_SCG_NORM_ROW); \ } while(0) #define PRINTRES() \ do { \ printf("----------------------\n"); \ igraph_matrix_print(&L); \ printf("---\n"); \ igraph_matrix_print(&R); \ printf("---\n"); \ } while (0) /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_OPTIMUM, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_INTERV_KM, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*intervals=*/ 3, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_INTERV, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_scg_grouping(&V, &groups, /*(ignored) intervals=*/ 0, /*intervals_vector=*/ 0, IGRAPH_SCG_STOCHASTIC, IGRAPH_SCG_EXACT, &p, /*maxiter=*/ 10000); SEMI(); PRINTRES(); /* -------------- */ igraph_vector_destroy(&p); igraph_vector_destroy(&selcol); igraph_vector_destroy(&groups); igraph_matrix_destroy(&L); igraph_matrix_destroy(&R); igraph_matrix_destroy(&V); igraph_matrix_destroy(&V3); igraph_matrix_complex_destroy(&V2); igraph_sparsemat_destroy(&stochasticT); igraph_sparsemat_destroy(&stochastic); igraph_destroy(&g); #ifdef __APPLE__ return 0; #else return 77; #endif }