Depth-first traversal of undirected graphs in Data Structures

Source: Internet
Author: User
The following figure is an example. Only the source code is provided, regardless of the algorithm concept.

# Include <stdio. h> # include <cstring> # include <iostream> # define max_vertex_num 20 + 3 // maximum number of vertices // # define true 1 // # define false 0 using namespace STD; typedef char vertex_type; // vertex type typedef struct node {int vertex_num; // Number of vertices int edge_num; // Number of edges vertex_type vertex [max_vertex_num]; // vertex information int matrix [max_vertex_num] [max_vertex_num]; // bool visited [max_vertex_num]; // undigraph for Deep Search}; // undirected graph void init _ Undigraph (undigraph * g) // initialization graph {G-> vertex_num = G-> edge_num = 0; memset (G-> vertex, 0, sizeof (G-> vertex); memset (G-> matrix, 0, sizeof (G-> matrix); memset (G-> visited, 0, sizeof (G-> visited); // It should be used to initialize the logical array} void add_vertex (undigraph * g) // Add the vertex {printf ("Enter the number of vertices \ n"); CIN> G-> vertex_num; // scanf ("% d ", & G-> vertex_num); For (INT I = 0; I <G-> vertex_num; ++ I) CIN> G-> vertex [I]; // scanf ("% C", & G-> vertex [I]);} int loca Te_vertex (undigraph * g, vertex_type who) // locate a vertex {for (INT Index = 0; index <G-> vertex_num; ++ index) if (G-> vertex [Index] = who) Return Index; // return cannot find return-1; // Meaning} void add_edge (undigraph * g) // Add edge {printf ("Enter the number of edge \ n"); CIN> G-> edge_num; // scanf ("% d ", & G-> edge_num); For (INT I = 0; I <G-> edge_num; ++ I) {vertex_type S1, S2; // The vertex CIN> S1> S2 at both ends of the edge; // scanf ("% C", & S1, & S2); int index_1 = loca Te_vertex (G, S1); // locate the subscript int index_2 = locate_vertex (G, S2) where the two vertices are located ); // same as G-> matrix [index_1] [index_2] = G-> matrix [index_2] [index_1] = 1; // The matrix is symmetric} void create_undigraph (undigraph * g) // constructs an undirected graph {add_vertex (g); // adds the vertex add_edge (g ); // Add edge} void DFS (undigraph * g, int I) {cout <G-> vertex [I]; // printf ("% C ", & G-> vertex [I]); G-> visited [I] = true; For (Int J = 0; j <G-> vertex_num; ++ J) if (G-> matrix [I] [J] = 1 &&! G-> visited [J]) DFS (G, J);} void dfs_traverse (undigraph * g) {for (INT I = 0; I <G-> vertex_num; ++ I) if (! G-> visited [I]) DFS (G, I);} int main (void) {undigraph g; init_undigraph (& G ); // initialize undirected graph create_undigraph (& G); // construct an undirected graph dfs_traverse (& G); // returns 0 for depth-first traversal ;}

Depth-first traversal of undirected graphs in Data Structures

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.