Exemplo n.º 1
0
list Kosaraju(list * Graph, list * Graph_t, int N){
    int i;
    list temp_S;

    for (i=1; i<=N; i++){
        if (G_s[i]==UNVISITED){
            DFS_Visit(Graph, i);
        }
    }

    temp_S = stack_S;
    stack_S = NULL;
    for (i=1; i<=N; i++){
        G_s[i] = UNVISITED;
    }
    Push(-1);

    while (temp_S!=NULL){
        i=temp_S->id;
        temp_S=temp_S->next;
        if (G_s[i]==UNVISITED){
            DFS_Visit(Graph_t, i);
            Push(-1);
        }
    }

    Pop();
    return stack_S;
}
Exemplo n.º 2
0
int main(){

    int i, id1, id2, N, P, scans, group_counter=0, group_size=0, max_group=0, ponto3=0, size, size2, j;
    scans = scanf("%d %d", &N, &P);
    if (scans != 2) exit(0);
    list G[N+1], G_t[N+1], aux=NULL, groups=NULL;
    G_s = calloc (N+1,sizeof(int));

	for (i=1; i<=N; i++){
        G[i] = NULL;
        G_t[i] = NULL;
        G_s[i] = UNVISITED;
    }

    for (i=1; i<=P; i++){
        scans = scanf("%d %d", &id1, &id2);
        if (scans != 2) exit(0);
        G[id1] = New_share(G[id1], id2);
        G_t[id2] = New_share(G_t[id2], id1);
    }

    groups = Kosaraju(G, G_t, N);
    aux = groups;

    while (aux != NULL){
        if (aux->id == -1) {
                group_counter++;
                if (group_size>max_group) max_group = group_size;
                group_size=0;
        }
        else group_size++;
        aux = aux->next;
    }

    for (i=1; i<=group_counter; i++){
        size=1;
        size2=0;
        while (groups->next->id != -1){
            size++;
            groups=groups->next;
        }
        stack_S = NULL;
        for (j=1; j<=N; j++) G_s[j] = UNVISITED;
        DFS_Visit(G, groups->id);
        while (stack_S!=NULL){
            size2++;
            Pop();
        }
        if (size2==size) ponto3++;
        groups=groups->next->next;
    }

    printf("%d\n%d\n%d\n", group_counter, max_group, ponto3);

    return 0;
}
void DFS(int n)
{
    for(int j = 1; j <= n; j++)
    {
        if(color[j] == 0)
        {
            DFS_Visit(n, j);
        }
    }
}
Exemplo n.º 4
0
void DFS_Visit (list * Graph, int id){
    list next = Graph[id];
    G_s[id] = VISITED;

    while (next!=NULL){
        if (G_s[next->id]==UNVISITED) DFS_Visit(Graph, next->id);
        next = next->next;
    }
    Push(id);
    G_s[id] = CLOSED;
}
Exemplo n.º 5
0
void DFS(Graph *G){
	int i;
	for(i=1; i<= G->v; i++)
	{
		(G->arr[i])->color = WHITE;
		(G->arr[i])->p = NIL;
	}
	time = 0;
	for(i=1; i<=G->v; i++)
	{
		printf("\nDFS for %d :", i);
		if((G->arr[i])->color == WHITE)
			DFS_Visit(G, i);
	}
}
Exemplo n.º 6
0
void DFS(struct node *head)
{
	struct node *temp = head;
	struct adj *adjacent;
	struct queueNode *stack;
	while(temp != NULL)
	{
			if(temp->visited == 0) //node is unvisited
		{
			DFS_Visit(head, temp, stack);
		}
		temp = temp->next;
	}

	//Call for DFS of transverse
}
Exemplo n.º 7
0
void DFS_Visit(Vertex vertex) {

	color[vertex] = 1;
	time++;
	d[vertex] = time;

	for (std::list<int>::iterator it = graph[vertex].begin(); it != graph[vertex].end(); it++) {
		if (!color[*it]) {
			color[*it] = 1;
			pred[*it] = vertex;
			DFS_Visit(*it);
			std::cout << *it << ' ';
		}
	}
	getchar();
}
Exemplo n.º 8
0
void DFS(Vertex vertex) {
	
	std::queue <Vertex> Q;

	color.resize(graph.size());
	pred.resize(graph.size());
	d.resize(graph.size());


	for (int i = 0; i < graph.size(); ++i) {
		color[i] = 0; //white
		pred[i] = 0;
	}

	for (int i = 0; i < graph.size(); ++i) {
		if (!color[i]) {
			DFS_Visit(i);
		}
	}
}
Exemplo n.º 9
0
void DFS_Visit(Graph *G, int index){
	AdjList vert = G->arr[index];
	vert.color = GRAY;
	time += 1;
	vert.d = time;
	vertex *temp = vert.head;
	while(temp != NULL)
	{	
		int vert_index = temp->key;  
		
		if ((G->arr[vert_index]).color == WHITE)
		{
			G->arr[vert_index].p = index;
			DFS_Visit(G, temp->key);
		}
		temp = temp -> next;
	}
	vert.color = BLACK;
	time += 1;
	vert.f = time;
}
Exemplo n.º 10
0
void DFS_Visit(struct node *head, struct node*temp, struct queueNode *stack)
{
	time++;
	temp->start = time;
	temp->visited = 1;
	//Go through temp's adjacents
	struct adj *adjacent = temp->adj;
	while(adjacent != NULL)
	{
		if (adjacent->original->visited ==0)
		{
			DFS_Visit(head, adjacent->original, stack);
		}
		adjacent= adjacent->next;
	}
	temp->visited = 2;
	time ++;
	temp->finish = time;
	//Throw onto stack
	push(temp, stack);
}
int main(void)
{
    int n;
    printf("How many vertices you want to take?");
    scanf("%d",&n);
    initialize(n);
    insertEdges(n);
    DFS(n);
    sortVertices(n);
    checker = 1;
    initialize(n);
    reverseDirection(n);
    for(int i = 1; i <= n; i++)
    {
        int v = s_vertices[i];

        if(color[v] == 0)
        {
            c = 0;
            DFS_Visit(n, v);
            for(int j = 1; j <= c; j++)
            {
                if(j == 1)
                {
                    printf("%d ", ver[j]);
                }
                else{

                    printf(", %d",ver[j]);
                }
            }
            printf("\n");
        }
    }


return 0;
}
void DFS_Visit(int n, int u)
{
    color[u] = 1;
    time  += 1;
    d_time[u] = time;
    if(checker == 1)
    {
        ver[++c] = u;
    }
    for(int v = 1; v <= n; v++)
    {
        if(vertices[u][v] != 0 && color[v] == 0)
        {
            DFS_Visit(n, v);
        }
    }
    color[u] = 2;
    time += 1;
    f_time[u] = time;
    if(!checker){
        s_vertices[u] = u;
    }
}