DFS recursively implements the traversal of graphs, and the difference between adding return and not adding return in functions is that DFS recursively implements the traversal of graphs.

  c++, question

Directly on the code to explain the problem:
(return refers to the one in the void DFS( int x) function)


#include<stdio.h>
 #include<string.h>
 #include<stdlib.h>
 
 int check[11] = {0};
 int Graph[11][11];
 int N;
 
 void DFS( int x){
 int i,j;
 check[x] = 1;
 printf("%d ",x);
 
 for( i = 0;   i < N;  i++){
 if( Graph[x][i] && !  check[i]){
 return DFS(i);  //With or without return refers to this return, without return is DFS(i);
 }
 }
 }
 
 int main(){
 
 int E;
 int i,j;
 int v,w, index;
 scanf("%d %d",&N, &E);
 memset(Graph, 0, N*N);
 
 /* Read Edge */
 for( i = 0 ;   i < E;  i++){
 scanf("%d %d",&v, &w);
 Graph[w][v] =Graph[v][w]=  1;
 }
 
 /* DFS */
 for( i = 0;   i < N;  i++ ){
 if( !  check[i] ){
 printf("{ ");
 DFS(i);
 printf("}");
 printf("\n");
 }
 }
 return 0;
 }

test data

Enter sample:

8 6
0 7
0 1
2 0
4 1
2 4
3 5
Output sample:

{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }

Test Case Results with return

clipboard.png

Test Case Results Without return

clipboard.png

Obviously, it is right not to add.
May I ask why?
What is the difference between plus and minus?

Traversal graph. . .
I<N you set
Add it and execute it once.
Not adding until i==N