DFS---Depth first search algorithm

Source: Internet
Author: User

Depth First Search




Principle or to see "DSAA", here focus on the implementation strategy.



If this data structure is not familiar with the figure, this BFS is generally uncertain ...

The following are the adjacency table implementations and adjacency matrix implementations of the non-direction graphs respectively

http://blog.csdn.net/cinmyheart/article/details/41381845

http://blog.csdn.net/cinmyheart/article/details/41370465


-----------------------------------------------------------


/************************************************************code File:dfs.hcode Writer:eofcode Date: 2014.11.22e-mail: [Email protected]code description:this file is a header file for out test program. We abstract The data structure-Graph here. And we alsodeclare some useful API to construct out naive graph:) ******************************************************* /#ifndef _dfs_h_#define _dfs_h_#include <stdio.h> #include <stdlib.h> #define CONNECTED 1#define Disconnected 0#define SUCCESS 0#define failed-1#define un_visited 0#define visited 1#define ENTRY_POINT 3struct visit Ed{int array[0];}; struct Vertex{int value;struct vertex* next;struct vertex* end;}; struct Graph{int num_vertex;int num_edge;struct vertex adjacent[0];}; void Dfs (struct graph* p_graph,struct vertex* p_vertex,struct visited* p_visited); struct graph* init_graph (int vertex, int edge); void Release_graph (struct graph* p_graph); int Add_edge (struct graph* p_graph,char from_v,char to_v); int PRint_graph (struct graph* p_graph); #endif 


Key to the implementation of DFS

This function I designed to accept three parameters (pointers), P_graph point to the graph, P_vertex point to the node, p_visited points to the tag "array" (dynamically allocated structure)

/*********************************************************code writer:eofcode Date:2014.11.24code File: Dfs.ce-mail:[email protected]code description:this function @dfs () is a implementation of "Depth first search" which is BA sed on recursion.**********************************************************/#include "dfs.h" void Dfs (struct graph* p _graph,struct vertex* p_vertex,struct visited* p_visited) {if (!p_vertex) {return;} P_visited->array[p_vertex->value] = visited;printf ("%d->", P_vertex->value); for (P_vertex = p_vertex- >next;!   ! P_vertex;   P_vertex = P_vertex->next) {if (p_visited->array[p_vertex->value] = = un_visited) {dfs (p_graph,& (p_graph- >adjacent[p_vertex->value]), p_visited);}} printf ("\ n");}

Since it is a depth-first search, there is a "global tag" for data that has already been retrieved (in this case the same dynamic memory-allocated memory area).

So we can make the test using the circle chart.

Say

Node link is a circle: 1->2->3->4->1, this, the end-to-end loop.

From any node, you can stop and retrieve all the nodes.


Our testing procedures:

/****************************************************************code File:test_graph.ccode Writer:eofcode Date:  2014.11.22e-mail: [Email protected]code description:here, we use this program to call some API which would construct A adt--graph and test it.*****************************************************************/#include "dfs.h" int main ( {struct graph* p_graph = NULL; file* fp = fopen ("./text.txt", "r+"), if (!FP) {printf ("fopen () failed!\n"); return 0;} int ret = 0;int vertex = 0;int Edge = 0;int From_v = 0;int To_v = 0;fscanf (FP, "%d", &vertex); FSCANF (FP, "%d", &amp Edge);p _graph = Init_graph (vertex,edge); int temp = 0;for (temp = 0;temp < edge;temp++) {/***i think it ' s necessary to CH Eck The returned value** of scanf () Family.*/ret = fscanf (FP, "%d%d", &from_v,&to_v); if (ret! = 2) {break;} Add_edge (P_GRAPH,FROM_V,TO_V);} struct visited* p_visited = (struct visited*) malloc (sizeof (struct visited) +sizeof (int) * (P_graph->num_vertex)); for (temp = 0;temp < p_graph->num_vertex;temp++) {P_visited->array[temp] = un_visited;} Print_graph (p_graph);/***here, we start to Dfs.*/dfs (p_graph,& (P_graph->adjacent[entry_point]), p_visited); Release_graph (p_graph); free (p_visited);p _visited = Null;fclose (FP); return 0;}


Test text data text.txt:

551 22 33) 44 1

Test results:



In the same way, we can test with other graphs:

Test text:

8-node, 12-Edge graph

8123 63 11 41 24 34 64 74 57 62 42 55 7

Test results:






DFS---Depth first search algorithm

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.