Animation

Depth First Search
Instructor : Prof. Jyh-Shing Roger Jang
Designer:Shao-Huan Wang
The ideas are reference to the textbook “Fundamentals of Data Structures in C “ .
Depth First Search
0
1
3
2
4
5
7
0
6
void dfs(int v){ 1
node_pointer w; 1
visited[v] = TURE; 1
printf(“%5d”, v); 1
for(w = graph[v]; w; w = w->link) 1
if(!visited[w->vertex]) 1
dfs(w->vertex); 1
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
4
5
7
01
6
void dfs(int v){ 2
node_pointer w; 2
visited[v] = TURE; 2
printf(“%5d”, v); 2
for(w = graph[v]; w; w = w->link) 2
if(!visited[w->vertex]) 2
dfs(w->vertex); 1 2
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
visited
1
3
4
5
7
013
2
6
void dfs(int v){ 3
node_pointer w; 3
visited[v] = TURE; 3
printf(“%5d”, v); 3
for(w = graph[v]; w; w = w->link) 3
if(!visited[w->vertex]) 3
dfs(w->vertex); 1 2 3
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
visited
4
5
7
0137
6
void dfs(int v){ 4
node_pointer w; 4
visited[v] = TURE; 4
printf(“%5d”, v); 4
for(w = graph[v]; w; w = w->link) 4
if(!visited[w->vertex]) 4
dfs(w->vertex); 1 2 3 4
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
4
5
7
01374
2
visited
visited
6
void dfs(int v){ 5
node_pointer w; 5
visited[v] = TURE; 5
printf(“%5d”, v); 5
for(w = graph[v]; w; w = w->link) 5
if(!visited[w->vertex]) 5
dfs(w->vertex); 1 2 3 4
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
4
5
7
01374
6
void dfs(int v){
node_pointer w;
visited[v] = TURE;
printf(“%5d”, v);
for(w = graph[v]; w; w = w->link) 4
if(!visited[w->vertex]) 4
dfs(w->vertex); 1 2 3 4
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
4
5
7
013745
6
void dfs(int v){ 5
node_pointer w; 5
visited[v] = TURE; 5
printf(“%5d”, v); 5
for(w = graph[v]; w; w = w->link) 5
if(!visited[w->vertex]) 5
dfs(w->vertex); 1 2 3 4 5
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
4
5
7
0 1 3 7 4 52
void dfs(int v){ 6
node_pointer w; 6
visited[v] = TURE; 6
2
printf(“%5d”, v); 6
for(w = graph[v]; w; w = w->link) 6
if(!visited[w->vertex]) 6
dfs(w->vertex); 1 2 3 4 5
visited
6
}
visited
6
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
4
5
7
0 1 3 7 4 52 6
void dfs(int v){ 7
node_pointer w; 7
visited[v] = TURE; 7
visited
2
printf(“%5d”, v); 7
for(w = graph[v]; w; w = w->link) 7
if(!visited[w->vertex]) 7
dfs(w->vertex); 1 2 3 4 5
visited
6
}
visited
6
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
4
5
7
0 1 3 7 4 52 6
visited
6
void dfs(int v){
node_pointer w;
visited[v] = TURE;
printf(“%5d”, v);
for(w = graph[v]; w; w = w->link) 6
4
5
if(!visited[w->vertex]) 4
5
visited dfs(w->vertex); 1 2 3 4 5
}
6
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited
Depth First Search
0
1
3
2
4
visited
5
7
0 1 3 7 4 52 6
void dfs(int v){
node_pointer w;
visited[v] = TURE;
visited
printf(“%5d”, v);
for(w = graph[v]; w; w = w->link) 1
2
3
if(!visited[w->vertex]) 1
2
dfs(w->vertex); 1 2 3
6
}
Input the first vertex into dfs
Declare variable to put the graph[v] of Adj-list
Markup the node which already visited
Print the value of v
Find the node which is not visited