Depth-first traversal and breadth-first traversal of graphs stored in the adjacent table, and adjacent breadth-first Traversal

Source: Internet
Author: User

Depth-first traversal and breadth-first traversal of graphs stored in the adjacent table, and adjacent breadth-first Traversal

1. depth-first traversal is a traversal policy for connected graphs. The basic idea is as follows:

Set x to the currently accessed vertex. After marking x, select an undetected edge (x, y) starting from x ). If vertex y is found to have been accessed, re-select another side that has not been detected from x. Otherwise, the edge (x, y) will arrive at y that has not been accessed, access y and mark it as accessed. Then, start searching from y until all paths from y are searched, that is, after accessing all vertices that can be reached from y, and then select an undetected edge from x. The above process ends until all edges starting from x have been detected.

Example:

 

1. Starting from 0, first find the correlated vertex of 0 32. Starting from 3, find 1; Starting from 1, there is no correlated vertex. 3. Return to 3, start from 3, and find 2; start from 2, and no associated vertex. 4. Return to 4 and start from 4. Find 1. Because 1 has been accessed, It is not accessible. So the last order is, 4. 2. breadth-first traversal is a traversal policy for connected graphs. The basic idea is as follows:

1. Start from a vertex V0 and access this vertex;

2. Starting from V0, access the neighboring contacts W1, W2 ,..., Wk; then, sequentially from W1, W2 ,..., Access the neighboring points that are not accessed from Wk;

3. Repeat Step 2 until all vertices are accessed.

 

Example: 1. starting from 0, first find the correlated vertex 3, 42 of 0. starting from 3, finding 1, 2, and 4, finding 1, but 1 has been traversed, so ignore. 3. Starting from 1, there is no correlated vertex; Starting from 2, there is no correlated vertex. So the final order is, 2 3. the following uses an undirected graph as an example to achieve depth-first traversal and breadth-first traversal: The implementation code is as follows:
/** Depth-first traversal and breadth-first traversal of the adjacent table **/# include <stdio. h> # include <stdlib. h> # define MaxVex 255 # define TRUE 1 # define FALSE 0 typedef char VertexType; // vertex type typedef int Bool; Bool visited [MaxVex]; // Global Array, record the node access status in the figure. typedef struct EdgeNode {// edge table node int adjvex; // The subscript struct EdgeNode * next of the adjacent vertex in the vertex array; // link the domain to the next adjacent vertex} EdgeNode; typedef struct VertexNode {// header node VertexType data; // vertex information EdgeNode * firstedge; // edge header pointer (pointing to the first object attached to this VertexNode, AdjList [MaxVex]; // vertex array (struct array) typedef struct Graph {AdjList adjList; int numVertexes, numEdges; // number of nodes and number of edges in the Graph} Graph, * GraphAdjList;/** queue definition and related operations (This cyclic queue will be used for breadth traversal) **/typedef struct LoopQueue {int data [MaxVex]; int front, rear;} LoopQueue, * Queue; // Queue structure void initQueue (Queue & Q) {Q-> front = Q-> rear = 0;} Bool QueueEmpty (Queue & Q) {if (Q-> front = Q-> rear) {return TRUE ;} else {return FALSE;} Bool QueueFull (Queue & Q) {if (Q-> rear + 1) % MaxVex = Q-> front) {return TRUE ;} else {return FALSE ;}}/*** insert element at the end of the Team */void EnQueue (Queue & Q, int e) {if (! QueueFull (Q) {Q-> data [Q-> rear] = e; Q-> rear = (Q-> rear + 1) % MaxVex ;}} /*** Delete element in the Queue header */void DeQueue (Queue & Q, int * e) {if (! QueueEmpty (Q) {* e = Q-> data [Q-> front]; Q-> front = (Q-> front + 1) % MaxVex ;}} /*************************************** * ********* // **** create the graph's adjacent table structure (this example uses an undirected graph) */void CreateALGraph (GraphAdjList & G) {int I, j, k; if (G = NULL) {G = (GraphAdjList) malloc (sizeof (Graph ));} printf ("Number of knots and number of edges in the input graph:"); scanf ("% d", & G-> numVertexes, & G-> numEdges ); fflush (stdin); printf ("=============================\ n "); printf ("input data of each vertex: \ N "); for (I = 0; I <G-> numVertexes; ++ I) {printf (" vertex % d: ", I + 1 ); scanf ("% c", & (G-> adjList [I]. data); G-> adjList [I]. firstedge = NULL; fflush (stdin );} printf ("===============================\ n "); for (k = 0; k <G-> numEdges; ++ k) {printf ("vertex number on the input (vi, vj :"); scanf ("% d", & I, & j); EdgeNode * ptrEdgeNode = (EdgeNode *) malloc (sizeof (EdgeNode); ptrEdgeNode-> adjvex = j; ptrEdgeNode-> next = G-> adjList [I]. firstedge; G-> adjList [I]. firstedge = ptrEdgeNode; ptrEdgeNode = (EdgeNode *) malloc (sizeof (EdgeNode); ptrEdgeNode-> adjvex = I; ptrEdgeNode-> next = G-> adjList [j. firstedge; G-> adjList [j]. firstedge = ptrEdgeNode;} void DFS (GraphAdjList & G, int I) {visited [I] = TRUE; printf ("% c", G-> adjList [I]. data); EdgeNode * p = G-> adjList [I]. firstedge; while (p) {if (! Visited [p-> adjvex]) {DFS (G, p-> adjvex); // recursive depth traversal} p = p-> next ;}} /*** depth-first traversal */void DFSTraverse (GraphAdjList & G) {int I; for (I = 0; I <G-> numVertexes; ++ I) {visited [I] = FALSE; // The element value of the initial access array visited is false} for (I = 0; I <G-> numVertexes; ++ I) {if (! Visited [I]) {// The node has not accessed DFS (G, I);}/*** graph's breadth first traversal */void BFSTraverse (GraphAdjList & G) {int I; Queue Q = (Queue) malloc (sizeof (LoopQueue); for (I = 0; I <G-> numVertexes; ++ I) {visited [I] = FALSE;} initQueue (Q); for (I = 0; I <G-> numVertexes; ++ I) {if (! Visited [I]) {visited [I] = TRUE; printf ("% c", G-> adjList [I]. data); EnQueue (Q, I); while (! QueueEmpty (Q) {DeQueue (Q, & I); EdgeNode * p = G-> adjList [I]. firstedge; while (p) {if (! Visited [p-> adjvex]) {visited [p-> adjvex] = TRUE; printf ("% c", G-> adjList [p-> adjvex]. data); EnQueue (Q, p-> adjvex);} p = p-> next ;}}}/ *** program entry */int main () {GraphAdjList G = NULL; CreateALGraph (G); printf ("\ n:"); DFSTraverse (G ); printf ("\ n graph's breadth first traversal:"); BFSTraverse (G); printf ("\ n"); return 0 ;}

 

Running result:

Related Article

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.