DFS stack Implementation C code

Source: Internet
Author: User

Here the code is written in a casual way, using the array subscript as an index, and the node is represented by 0 to n-1.

Similar to the idea of recursive invocation (the feeling is that the call stack inside the recursive call is replaced with a simple stack array). Here for the element of the top of the stack, if it is white, then the global time variable plus one, and put it adjacent to the list of white nodes all into the stack, and if the stack top element is already gray, it becomes black, time global variable plus one, and the stack, if the top element of the stack is black, directly out of the stack. (Black is required here because the nodes may be repeated into the stack.)

Time calculation: Each loop has either a black node out of the stack, or a time variable plus one, so the run time is θ (v+e), because all the nodes in the stack will not exceed the total size of the adjacency list.

Analyze the assignment of two timestamps and parent nodes:

1) V.D is the first time to explore this node, that is, to turn it from white to Gray when the V.D is assigned.

2) V.F is the node where all the nodes are explored, that is, the V.F is assigned when it turns from gray to black.

3) V.pi is assigned when the stack is entered.

PS: On-line search, found that there is a method is a node into the stack break, and to deal with this node. There is not much difference in nature, I use a larger stack space here, and that method expects longer.

#include <stdio.h>#defineN 100typedefstructVertex {intcolor; intPi; intD; intF; intIndex// Subscript}vertex;typedefstructnode{intIndex//an array subscript for the corresponding node, equivalent to the pointer that stores the node    structNode *Next;} Node;typedefstructlist{Node*Head;} List;intStack[n];Static intCount =0;intTime=0;voidDfs_v (List *adj, Vertex *v,intNintu);voidDFS (List *adj,vertex *v,intN) {     for(inti =0; I < n; ++i) {V[i].color=0; V[i].pi= -1; }     for(inti =0; I < n; ++i) {        if(V[i].color = =0) Dfs_v (adj,v, n, i); }}voidDfs_v (List *adj, Vertex *v,intNintu) {Stack[count++] =u;  while(count) {intv = stack[count-1]; if(V[v].color = =0) {V[v].color=1; ++Time ; V[V].D=Time ; Node*tmp =Adj[v].head;  while(TMP) {if(V[tmp->index].color = =0) {Stack[count+ +] = tmp->index; V[tmp->index].pi =v; } tmp= tmp->Next; }        }        Else if(v[v].color==1) {V[v].color=2; ++Time ; V[V].F=Time ; --count; }        Else--count; }}voidPrint (Vertex *v,intN) {     for(inti =0; I <n; i++) {printf ("%d%d%d\n", V[I].D, V[i].f,v[i].pi); }}voidPrint_a (List *adj,intN) {     for(inti =0; I < n; ++i) {Node*tmp =Adj[i].head;  while(TMP) {printf ("%d", tmp->index); TMP= tmp->Next; } printf ("\ n"); }}intMain () {Vertex v[6];  for(inti =0; I <6; ++i) V[i].index=i; List adj[6] = {0,0,0,0,0,0 }; /*While (1) {int u, v;        cin >> u >> v;        if (U < 0) break;        Node tmp = {v, adj[u].head};    Adj[u].head = &tmp; }    */Node N1= {2,0}, N2 = {1, &n1}, N11 = {4,&N2}; adj[0].head = &N11; Node N3= {0,0}, N4 = {4, &n3}, N5 = {3, &N4}; adj[1].head = &N5; Node N6= {0,0}, N7 = {5, &N6}; adj[2].head = &N7; Node N8= {1,0 }; adj[3].head = &N8; Node N9= {1,0 }; adj[4].head = &N9; Node N10= {2,0 }; adj[5].head = &N10; Print_a (ADJ,6); DFS (ADJ, V,6); Print (V,6);}

DFS stack Implementation C code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.