Use of DFS Algorithms

Source: Internet
Author: User
• AlgorithmConcept: the DFS algorithm is a recursive process with a rollback process. For an undirected connected graph, after accessing a vertex V0 in the graph, access one of its adjacent vertex V1, then, starting from V1, access the illegally accessed neighboring vertices of V1, and continue until all the access vertices are accessed. Then, return to the last accessed vertex to see if there are any unaccessed vertex. If so, start from this vertex and perform the same access to the above process, if not, go back and perform similar access until all vertices are accessed!
• DFS algorithms are implemented using pseudo-code.

1. If the graph is stored in an adjacent table

DFS (vertex I)

{

Visited [I] = 1;

P is the header pointer of the edge linked list of I.

While (P is not empty)

{

If (another vertex corresponding to I vertex has not been accessed, set to K)

{

The work to be prepared before recursive searchCodeIn these

DFS (k );

Code should be written here during the rollback Process!

}

P = p-> next;

}

2. If the graph is stored in an adjacent matrix

DFS (vertex I)

{

Visited [I] = 1;

For (j = 0; j <n; j ++)

{If (edge [I] [J] <INF & visited [J]! = 1) // If edge links exist between I and K, and J has not been accessed

{DFS (j );

}

}

}

• Application: from Nanyang Institute of Technology OJ platform (very suitable for beginners on the brush questions) attached to the link http://acm.nyist.net/JudgeOnline/problemset.php question is 42nd questions, a stroke problem, this question is mainly used Euler's path problem, use DFS search!

` 1 # Include <cstdio> 2   # Define Maxn 1001 3 # Include <memory. h> 4   Struct Anode 5 { 6       Int To; 7      //  Int adj;    8       Struct Anode * next; 9 }; 10 Anode * list [maxn]; 11   Int Visited [maxn]; 12   Int N, m; 13   Void DFS ( Int Start) 14 { 15 Anode * P; 16 P = list [start];17 Visited [start] = 1 ; 18           While (P) 19 { 20               If (! Visited [p-> to]) 21 { 22 DFS (p-> ); 23 } 24 P = p-> next; 25 } 26   27 } 28   Int Fun2 () 29 { 30       Int I; 31       For (I = 1 ; I <= N; I ++) 32 {33           If (Visited [I] = 0 ) 34           Return   0 ; 35           Break ; 36 } 37       Return   1 ; 38 } 39  Void Fun () 40 { 41       Int Jdnum = 0 ; 42       //  Int start1, start2;    43 Anode * P; 44       Int I; 45       For (I = 1 ; I <= N; I ++) 46 { 47           Int Dnum = 0 ; 48 P = list [I]; 49           While (P) 50 { 51 Dnum ++; 52 P = p-> next;53 } 54           If (Dnum % 2 ! = 0 ) 55 { 56 Jdnum ++; 57 } 58 } 59       If (Fun2 () = 0 )60 Printf ( "  No \ n  " ); 61       Else  62 { 63           If (Jdnum! = 0 & Jdnum! = 2 ) 64 { 65 Printf ("  No \ n  " ); 66 } 67          Else  68 Printf ( "  Yes \ n  " ); 69 } 70   71 }72   73   Int Main () 74 { 75       Int Kase; 76      //  Freopen ("a.txt", "r", stdin );    77 Scanf ( "  % D  " , & Kase );78       While (Kase --) 79 { 80 Scanf ( "  % D  " , & N, & M ); 81 Anode * P1, * P2; 82           Int V1, V2, I; 83 Memset (visited, 0 ,Sizeof (Visited )); 84 Memset (list, 0 , Sizeof (List )); 85           For (I = 0 ; I <m; I ++) 86 { 87 Scanf ( "  % D  " , & V1, & V2 );88 P1 = New Anode; 89 P2 = New Anode; 90 P1-> to = V2; 91 P1-> next = list [V1]; 92 List [V1] = p1; 93 P2-> to = V1; 94 P2-> next = list [V2]; 95 List [V2] = P2; 96 } 97 DFS (V1 ); 98           //  Fun2 ();    99 Fun (); 100 } 101       Return   0 ; 102  103 } 104         `

Specifically, the DFS () function is used to determine whether the graph is connected. The graph of this question is stored in an adjacent table, so that no timeout occurs!

In addition, I will use DFS to answer the 27th questions from Nanyang Institute of Technology.

` 1    2 # Include <stdio. h> 3   Int Grid [ 101 ] [ 101 ]; 4   Int M, N; 5   Int Dir [ 4 ] [ 2 ] = {{- 1 , 0 },{ 1 , 0 },{ 0 , 1 },{ 0 ,- 1 }}; 6   Void DFS ( Int X, Int Y) 7 { 8       Int I, XX, YY; 9 Grid [x] [Y] = 0 ; 10       For (I = 0 ; I < 4 ; I ++) 11 { 12 Xx = x + dir [I] [ 0 ]; 13 YY = Y + dir [I] [ 1 ]; 14           If (Xx < 0 | YY < 0 | X> M | Y> = N) 15           Continue ; 16           If (Grid [XX] [YY] = 1 ) 17 DFS (XX, YY );18 } 19 } 20   Int Main () 21 { 22       Int I, J; 23       Int H, count; 24 Scanf ( "  % D  " , & H );25       While (H --) 26 { 27 Scanf ( "  % D  " , & M, & N ); 28           For (I = 0 ; I <m; I ++) 29 { 30               For (J =0 ; J <n; j ++) 31 Scanf ( "  % D  " , & Grid [I] [J]); 32 } 33 Count = 0 ; 34           For (I = 0 ; I <m; I ++) 35 {36               For (J = 0 ; J <n; j ++) 37 { 38                   If (Grid [I] [J] = 1 ) 39 { 40 DFS (I, j ); 41 Count ++; 42 }43 } 44   45 } 46 Printf ( "  % D \ n  " , Count ); 47 } 48       Return   0 ; 49 } 50          `

This is simply a problem solved by using DFS, which can reflect the concept of DFS!

Related Keywords:

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.