130 likes | 182 Views
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. void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v);
E N D
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 void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 1 1 1 1 2 1 1 1 3 4 5 6 1 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 2 2 2 1 2 2 2 2 3 4 5 6 1 2 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 3 3 3 visited 1 2 3 3 3 3 4 5 6 1 2 3 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 4 4 4 visited 1 2 4 4 4 3 4 5 6 1 2 3 4 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 5 5 5 visited visited 1 2 5 5 5 3 4 5 6 1 2 3 4 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 1 2 4 4 3 4 5 6 1 2 3 4 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 5 5 5 1 2 5 5 5 3 4 5 6 1 2 3 4 5 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 5 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 6 6 6 visited visited 1 2 6 6 6 3 4 5 6 1 2 3 4 5 6 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 5 2 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 7 7 7 visited visited visited 1 2 7 7 7 3 4 5 6 1 2 3 4 5 6 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 5 2 6 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 visited visited 1 2 6 5 4 5 4 3 4 5 6 1 2 3 4 5 6 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 5 2 6 Find the node which is not visited
Depth First Search void dfs(int v){ node_pointer w; visited[v] = TURE; printf(“%5d”, v); for(w = graph[v]; w; w = w->link) if(!visited[w->vertex]) dfs(w->vertex); } 0 visited visited 1 2 1 2 3 2 1 3 4 5 6 1 2 3 Input the first vertex into dfs Declare variable to put the graph[v] of Adj-list 7 Markup the node which already visited Print the value of v 0 1 3 7 4 5 2 6 Find the node which is not visited