Exemplo n.º 1
0
void dfs::DFS_visit(GraphManipulator * gm, vertex_ < string > *u)
{
    time++;

    u->setInput(time);
    u->setColor("gray");

    list < edge_ < string >> Edges = gm->getEdges((*u));
    list < edge_ < string >>::iterator itEdges;

    for (itEdges = Edges.begin(); itEdges != Edges.end(); itEdges++) {

	vertex_ < string > *vertexAdj =
	    gm->getVertexAttributes((*itEdges).getVertex());

	if (vertexAdj->getColor() == "white") {
	    vertexAdj->setFather(u->getValue());
	    DFS_visit(gm, vertexAdj);
	}

    }
    u->setColor("black");
    time++;
    setOutput_stamp(u);
}
Exemplo n.º 2
0
void DFS(stack<DFS_node*> S, bool first, list<SCC*> *SCCSSEQ){
	int time = 0;
	DFS_node* u;
	SCC * scc_tmp;

	//copy of S stack, in this way we can extract element from S, and G doesn't change
	stack<DFS_node*> G = stack<DFS_node*>(S);

	// if is the second time, we sort the stack
	if(!first){
		S=sort_stack(S);
	}
	// visit every node of the stack if the color is white
	while(!S.empty()){
		u = S.top();
		S.pop();
		if( u->color == 0 ){
			scc_tmp=new SCC;
			//cout << u->argument->getName() << " ";
			DFS_visit(G,u,&time, first, scc_tmp);
			if(!first){
				SCCSSEQ->push_back(scc_tmp);
				//cout<<scc_tmp->set<<endl;
			}
		}
	}
}
Exemplo n.º 3
0
void DFS_visit(Graph* G, int u)
{
	adjNode* traver;
	traver=G->array[u].head;
	time= time+ 1;
	G->array[u].color=GRAY;
	G->array[u].d=time;
	
	int v;
	while(traver!=NULL)
	{
		v=traver->data;
		if(G->array[v].color==WHITE)
		{
			G->array[v].p=u;
			DFS_visit(G,v);
		}
		traver=traver->next;
	}
	G->array[u].color=BLACK;
	time= time +1;
	G->array[u].f=time;



}
Exemplo n.º 4
0
void Graph::DFS_visit(DFSNode * DFS, std::list<DFSNode *> DFSList) {
 DFS->C = GRAY;
 Time++;
 DFS->DTime = Time;
 // for each vector adjacent to dfs
 std::vector<BasicBlock *> Svec = Succ_Vec[DFS->Bb];
 for (std::vector<BasicBlock *>::iterator It = Svec.begin(); It != Svec.end(); ++It) {
  for (std::list<DFSNode *>::iterator I = DFSList.begin(); I != DFSList.end(); ++I) {
    if ((*I)->Bb == *It) {
      if ((*I)->C == BLACK)
        (*I)->T = ENDIF;
      else if ((*I)->C == GRAY){
        DEBUG (errs() << "\nWe are trying to go to a Gray BB\nWe are in: " << DFS->Bb << "Which is " << DFS->T << " We want to go to: " << (*I)->Bb << " Which is " << (*I)->T << "\n");
        if(DFS->T != STARTLOOP) DFS->T = ENDLOOP;
        (*I)->T = STARTLOOP;
      }
      else if ((*I)->C == WHITE) {
        DFS->Vertex.push_back(*I);
        DFS_visit(*I, DFSList);
      }
    }
   }
 }
 DFS->C = BLACK;
 DFS->FTime = ++Time; 
 DEBUG (errs() << "DFS node: " << DFS->Bb << " Dtime: " << DFS->DTime << " Ftime: " << DFS->FTime << " TypeC: " << DFS->T << '\n');
}
Exemplo n.º 5
0
void Graph::DFS() {
    reverse = true;
    t = 0;
    for(int i = 0; i < nodes.size(); i++) {
	if( !visited.at(i) ) {
	    DFS_visit(i);
	};
    };
    visited.assign(node_number, false);
};
Exemplo n.º 6
0
//recursive DFS which can be also used on directed graphs
void DFS_visit(const graphtp & g, vi & visited, int n)
{
    out("DFS_visit n %d\n", n);
    visited[n] = true;
    printf("%d ", n);
    for(vi::const_iterator it = g[n].begin(); it != g[n].end(); ++it) {
        if(visited[*it]) continue;
        DFS_visit(g, visited, *it);
    }
}
/**
 * complexity : O(V + E)
 * @param G Graph
 * @param s source vertex
 */
void DFS(IGraph &G, IGraph::vertex &s){

	int n = G.numVertices();
	int *color = new int[n];
	int *d = new int[n];
	int *f = new int[n];
	IGraph::vertex **pi = new IGraph::vertex*[n];
	
	for (IGraph::vertex_iterator v = G.begin(); v != G.end(); ++v)
	{
		color[v->id] = WHITE;
		pi[v->id] = NULL;
		d[v->id] = 0;
		f[v->id] = 0;
	}

	int dtime = 0;

	DFS_visit(G, s, color, d, f, pi, dtime);
	for (IGraph::vertex_iterator v = G.begin(); v != G.end(); ++v)
	{
		if(color[v->id] == WHITE){
			DFS_visit(G, *v, color, d, f, pi, dtime);
		}
	}

	cout << "S : " << s.id << endl;
	for (int i = 0; i < n; ++i)
	{
		if(pi[i] != NULL)
			cout << pi[i]->id << "(" << d[i] << "/" << f[i] <<  ") ";
		else
			cout << "nil" << "(" << d[i] << "/" << f[i] <<  ") ";
	}
	cout << endl;

	delete [] color;
	delete [] d;
	delete [] f;
	delete [] pi;
}
Exemplo n.º 8
0
void DFS_recursive(const graphtp & g)
{
    out("DFS_recursive g.size %d\n", g.size());
    vi visited(g.size());
    assert(visited.size() == g.size() && "Vector not same size");

    for(int n = 0; n < g.size(); n++)
    {
        if(visited[n]) continue;
        DFS_visit(g, visited, n);
    }
}
Exemplo n.º 9
0
void DFS(node graph[])
{
    int i;
    for(i=0;i<VERTICES;i++)
    {
        if(graph[i].visited == 0)
        {
            DFS_visit(graph,i);
        }

    }
}
Exemplo n.º 10
0
void dfs::mainLoop(GraphManipulator * gm)
{

    for (itVertexs = Vertexs.begin(); itVertexs != Vertexs.end();
	 itVertexs++) {

	if ((*itVertexs)->getColor() == "white") {
	    DFS_visit(gm, (*itVertexs));
	}

    }

}
Exemplo n.º 11
0
void Graph::DFS2() {
    reverse = false;
    int node_idx;
    int SCC_number = 0;
    for(int i = (node_number-1); i >= 0; i--) {
	node_idx = times_to_node[i];
	if( !visited.at(node_idx) ) {
	    current_scc_size = 0;
	    DFS_visit(node_idx);
	    std::cout << "size of SCC " << SCC_number << ": " << current_scc_size << std::endl;
	    SCC_number++;
	};
    };
};
Exemplo n.º 12
0
void DFS_visit(node graph[],int start_vertex)
{
    int i,j;
    node* temp = graph[start_vertex].next;
    while(temp!=0)
    {
        if(graph[temp->name-1].visited==0)
        {
            graph[temp->name-1].visited = count++;
            DFS_visit(graph,temp->name-1);
        }
        temp = temp->next;
    }
}
Exemplo n.º 13
0
void DFS(Graph *G)
{
	int i;
	for(i=1;i<=G->V;++i)
	{
		G->array[i].color=WHITE;
		G->array[i].p=NIL;
	}
	time=0;
	for(i=1;i<=G->V;++i)
	{
		if(G->array[i].color==WHITE)
			DFS_visit(G,i);
	}
}
Exemplo n.º 14
0
static void DFS(struct graph_s *graph) {
    int i;

    for (i = 0; i < graph->num_vertices; ++i) {
        graph->vertices[i]->color = WHITE;
        graph->vertices[i]->predecessor = NULL;
    }

    dfs_time = 0;

    for (i = 0; i < graph->num_vertices; ++i) {
        if (graph->vertices[i]->color == WHITE) {
            DFS_visit(graph->vertices[i]);
        }
    }
}
Exemplo n.º 15
0
static void DFS_visit(headNode *listHead, headNode *head, int *visited)
{
	visited[head->key] = 1;
	adjNode *node = head->neighbor;
	while(node != NULL)
	{
		if(!visited[node->key])
		{
			printf("%d -> %d ", head->key, node->key);
				
			headNode *temp = findNode(listHead, node->key);
			if(temp != NULL)
				DFS_visit(listHead, temp, visited);
		}
		node = node->next;
	}
}
Exemplo n.º 16
0
void DFS(headNode *listHead)
{
	int size = graphSize(listHead) + 1;

	int *visited = (int*)malloc(sizeof(int)*(size));
	int i = 0;
	for(i = 0; i < size; i++)
		visited[i] = 0;

	headNode *head = listHead;
	while(head!= NULL)
	{
		if(!visited[head->key])
			DFS_visit(listHead, head, visited);
		head= head->next;
	}
	printf("\n");
}
Exemplo n.º 17
0
void DFS_visit(int i, INFO_OF_NODE info, ADJACENT_LIST graph, int *time)

{
    NODE node_p = graph[i]; 

    int u = node_p->data; 

    while(u != -1) {
    
        if(info[u].color == WHITE) {
            info[u].color = GRAY; 
            info[u].detect_time = ++(*time); 
            info[u].parent = i; 

            printf("-->%d", u);     
        
            DFS_visit(u, info, graph, time); 

            printf("==back==>%d", i); 
        }
        else if(info[u].color == GRAY) {
            printf("--B-->%d %d", u, i); 
        }
        else if(info[u].detect_time < info[i].detect_time) {
            printf("--C-->%d %d", u, i);         
        }
        else {
            printf("--F-->%d %d", u, i); 
        }
        node_p = node_p->next; 
        u = node_p->data; 
        
    }

    // BLACK color is just used for checking whether an edge is cross edge or forward edge. 
    info[i].color = BLACK; 
    info[i].finish_time = ++(*time);
    TOPOLOGY_RESULT t = (TOPOLOGY_RESULT)malloc(sizeof(struct topology_result));

    t->vertex = i; 
    t->next = result;
    result = t; 
}
Exemplo n.º 18
0
void Graph::DFS()
{
 DEBUG (errs() << "\n\n\n\n\n Initializing DFS nodes \n\n\n\n\n\n");
 // for(std::map<BasicBlock *, std::vector<BasicBlock *> >::iterator it = succ_vec.begin(); it != succ_vec.end(); ++it){
 // add all BasicBlock nodes to the list to create a list of dfsnodes
 for (std::list<BasicBlock *>::iterator It = BBList.begin(); It != BBList.end(); ++It){
  DFSNode * DFSN = new DFSNode;
  DFSN->Bb = *It;
  DFSN->C = WHITE;
  DFSN->T = UNKNOWN;
  DFSList.push_back(DFSN);
  DEBUG (errs() << "added " << DFSN->Bb << " to the list\n");
 }
 Time = 0;
 for (std::list<DFSNode *>::iterator It = DFSList.begin(); It != DFSList.end(); ++It){
 DFSNode *P = *It;
 if (P->C == WHITE) 
  DFS_visit(P, DFSList);
 }
}
void DFS_visit(IGraph &G, IGraph::vertex &v, int color[], int d[], int f[], IGraph::vertex **pi, int &dtime){

	dtime++;
	color[v.id] = GRAY;
	d[v.id] = dtime;
	
	cout << v.id << endl;	

	for (IGraph::vertex::iterator e = v.begin(); e != v.end(); e++){
		IGraph::vertex *u = e->second;
		if(color[u->id] == WHITE){	
			pi[u->id] = &v;
			DFS_visit(G, *u, color, d, f, pi, dtime);
		}
	}

	dtime++;
	color[v.id] = BLACK;
	f[v.id] = dtime;
}
Exemplo n.º 20
0
static void DFS_visit(struct vertex_s *u) {
    int j;
    struct listnode_s *n;
    struct vertex_s *v;

    u->color = GRAY;
    u->dt = ++dfs_time;

    for (n = u->adj; n; n = n->next) {
        v = n->v;
        if (v->color == WHITE) {
            v->predecessor = u;
            DFS_visit(v);
        }
    }

    u->color = BLACK;
    u->ft = ++dfs_time;

    push_vertex(&toplist, u);
}
Exemplo n.º 21
0
void DFS(ADJACENT_LIST graph, int vertex_num)
{
    INFO_OF_NODE info; 
    int i; 


    for(i = 1; i <= vertex_num; ++i) {
        info[i].color = WHITE; 
        info[i].finish_time=info[i].detect_time = 0; 
        info[i].parent = -1;
    }

    int time = 0; 
    for(i = 1; i <= vertex_num; ++i) {
        if(info[i].color == WHITE) {
            printf("%d", i);
            info[i].detect_time = ++time; 
            info[i].color = GRAY; 
            DFS_visit(i, info, graph, &time); 
        }

        // change to a new line to print another dfs tree..
        printf("\n"); 
    }
    
    // print detect/finish time.
    for(i = 1; i <= vertex_num; ++i) {
        printf("%d: %d/%d\n", i, info[i].detect_time, info[i].finish_time); 

    }

    // topology sort. 

    while(result != NULL) {
        printf("%d ", result->vertex); 
        result = result->next; 
    }
    

}
Exemplo n.º 22
0
/**
 * @brief 				visit in the depth first search
 * @param u				node which we are visiting
 * @param first			true if is the first time which we are using DFS in the SCCSSEQ method
 * @retval SCC			return the SCC computed
 */
void DFS_visit(stack<DFS_node*> S, DFS_node* u, int* time, bool first, SCC *tmp_scc){
	SCC tmp_scc2;
	(*time)++;
	u->d = *time;
	u->color = 1;//gray

	DFS_node * temp;

	// if is the first time we consider the attacked nodes, otherwise we consider the attackers ( way to compute G trasposed )
	SetArguments * adj;
	if(first){
		adj = u->argument->get_attacks();
	}
	else{
		adj = u->argument->get_attackers();
	}

	// if the color is white call DFS_visit, compute SCC by union 
	for(SetArgumentsIterator it = adj->begin(); it != adj->end(); it++ ){
		temp = get_DFS_node(S, **it );
		//cout << (temp->argument->getName())<<" sub"<<endl;
		if( temp->color == 0 ){
			temp->p = u;
			DFS_visit(S,temp,time,first,&tmp_scc2);
			if(!first){
				tmp_scc->set.setunion(&(tmp_scc2.set),&(tmp_scc->set));
			}
		}
	}

	u->color = 2;

	if (!first){
		tmp_scc->set.add_Argument(u->argument);
	}

	(*time)++;
	u->f = *time;
	//cout << u->argument->getName() <<" : "<< u->f << endl;
}
Exemplo n.º 23
0
void Graph::DFS_visit(int i) {
    if ( !reverse ) { current_scc_size++; };
    visited.at(i) = true;
    std::vector<int>::iterator it,itstart,itend;
    if ( reverse ) {
	itstart = nodes.at(i).redges.begin();
	itend = nodes.at(i).redges.end();
    } else {
	itstart = nodes.at(i).edges.begin();
	itend = nodes.at(i).edges.end();
    };
    for( it = itstart; it != itend; ++it ) { 
	if( !visited.at(*it) ) {
	    DFS_visit(*it);
	};
    };
    if ( reverse ) {
	times.at(i) = t;
	times_to_node[t] = i;
	t++;
    };
};
/* Print the BFS tree from a particular vertex. */
void graph_using_AL::print_dfs_tree(string start_vertex, ofstream& fout) {
    /* Check if 'fout' is good. */
    assert(fout.good());

    /* Find the start vertex. */
    map<string, set<string> >::iterator v_itr = find_vertex(start_vertex);
    if(v_itr == data.end()) {
        /* Vertex not found. */
        cerr << "Start vertex not found." << endl;
        return;
    }

    map<string, string>  parent;  /* Parent of node.		  */
    map<string,    int>  d_time;  /* Discovery time of node.  */
    map<string,    int>  f_time;  /* Finish time of node.     */

    char                 c_temp[100];

    /* Do the DFS traversal. */
    DFS_visit(parent, d_time, f_time, start_vertex);

    map<string, set<string> >::iterator map_itr;
    set<string>::const_iterator set_itr;

    string graph_type = ((d_type == undirected_graph) ? "graph " : "digraph ");
    string arrow      = ((d_type == undirected_graph) ? " -- "   : " -> "   );

    map<string, string> d_time_string;
    map<string, string> f_time_string;

    for(map<string, int>::iterator d_itr = d_time.begin(); d_itr != d_time.end(); d_itr++) {
        _itoa_s(d_itr->second, c_temp, 10);
        d_time_string.insert(pair<string, string>(d_itr->first,string(c_temp)));
    }

    for(map<string, int>::iterator f_itr = f_time.begin(); f_itr != f_time.end(); f_itr++) {
        _itoa_s(f_itr->second, c_temp, 10);
        f_time_string.insert(pair<string, string>(f_itr->first, string(c_temp)));
    }

    fout << graph_type << name << "{" << endl;
    graph_using_AL g_temp = *this;

    for(map<string, string>::iterator p_itr = parent.begin(); p_itr != parent.end(); p_itr++) {
        if(p_itr->second.begin() == p_itr->second.end()) continue;
        fout << "\t\"" << p_itr->second  << "(" << d_time_string[p_itr->second]
             << ", " << f_time_string[p_itr->second] << ")" << "\""
             << arrow
             << "\"" << p_itr->first << "(" << d_time_string[p_itr->first]
             << ", " << f_time_string[p_itr->first] << ")";
        fout << "\" [color=red ";
        if(w_type == weighted_graph) {
            /* It is a weighted graph. So print the weight too (as the label of the edge
             * along with the color.
             */
            fout << " label = " << edge_weights.find(pair<string, string>(p_itr->second, p_itr->first))->second;
        }


        fout << "];\n";

        /* Delete the corresponding reverse-edge in the undirected graph. */
        if((d_type == undirected_graph) && (p_itr->second != p_itr->first)) {
            set<string>::iterator itr_temp = g_temp.data.find(p_itr->first)->second.find(p_itr->second);
            if(debug_flag) clog << "Deleting " << p_itr->first << " --> " << p_itr->second << endl;
            g_temp.data.find(p_itr->first)->second.erase(itr_temp);
        }
    }

    if(debug_flag) g_temp.print();

    for(map<string, string>::iterator p_itr = parent.begin(); p_itr != parent.end(); p_itr++) {
        if(p_itr->second.begin() == p_itr->second.end()) continue;
        if(p_itr->second != p_itr->first) {
            if(debug_flag) clog << "Deleting " << p_itr->second << " --> " << p_itr->first << endl;
            set<string>::iterator itr_temp = g_temp.data.find(p_itr->second)->second.find(p_itr->first);
            g_temp.data.find(p_itr->second)->second.erase(itr_temp);
        }
    }

    if(debug_flag) g_temp.print();

    for(map_itr = g_temp.data.begin(); map_itr != g_temp.data.end(); map_itr ++) {
        if(map_itr->second.begin() == map_itr->second.end()) {
            fout << "\t\"" << map_itr->first  << "(" << d_time_string[map_itr->first]
                 << ", " << f_time_string[map_itr->first] << ")" << "\"" << ";\n";
        }
        else {
            for(set_itr = map_itr->second.begin(); set_itr != map_itr->second.end(); set_itr ++) {
                fout << "\t\"" << map_itr->first  << "(" << d_time_string[map_itr->first]
                     << ", " << f_time_string[map_itr->first] << ")" << "\""
                     << arrow
                     << "\"" << *set_itr << "(" << d_time_string[*set_itr]
                     << ", " << f_time_string[*set_itr] << ")\" ";

                if(w_type == weighted_graph) {
                    /* It is a weighted graph. So print the weight too (as the label of the edge along with the color.	  */
                    fout << "[label = " << edge_weights.find(pair<string, string>(map_itr->first, *set_itr))->second
                         << "]";
                }
                fout << endl;

                if((map_itr->first != *set_itr) && (d_type == undirected_graph)) {
                    if(debug_flag) clog << "Deleting " << *set_itr << " --> " << map_itr->first << endl;
                    set<string>::iterator itr_temp = g_temp.data.find(*set_itr)->second.find(map_itr->first);
                    g_temp.data.find(*set_itr)->second.erase(itr_temp);
                }
            }
        }
    }
    fout << "}" << endl;

    return;
}