In-depth graph Traversal

Source: Internet
Author: User


# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <string. h>
Int visited [10];/* access flag array */
Typedef struct ArcCell {
Int adj;/* vertex link type. 1 indicates adjacent, 0 indicates not adjacent */
} ArcCell, ** AdjMatrix;/* adjacent matrix */
Typedef struct type {
Char data [3];/* vertex value */
Struct type * next;/* next pointer to the vertex */
} VertexType;
Typedef struct {
VertexType * vexs;/* vertex vector */
AdjMatrix arcs;/* adjacent matrix */
Int vexnum, arcnum;/* Number of vertices and edges of the graph */
} MGraph;
Void InitGraph (MGraph * G)/* Initial graph */
{Int I, nu, mu;
Printf ("Number of input vertices and number of (edge) arcs :");
Scanf ("% d", & nu, & mu );
G-> arcs = (ArcCell **) malloc (nu * sizeof (ArcCell *));
For (I = 0; I <nu; I ++)/* allocate the adjacent matrix space */
G-> arcs [I] = (ArcCell *) malloc (nu * sizeof (ArcCell ));
G-> vexs = (VertexType *) malloc (nu * sizeof (VertexType);/* allocate vertex space */
G-> vexnum = nu; G-> arcnum = mu;/* Number of vertices and edges */
}
Void InsertGraph (MGraph * G, int I, VertexType e)
{If (I <0 | I> G-> vexnum) return;
G-> vexs [I]. next = e. next;
Strcpy (G-> vexs [I]. data, e. data );
}
Int Locate (MGraph G, VertexType v1)/* determine the position of v1 in the graph vertex */
{Int I;
For (I = 0; I <G. vexnum; I ++)
If (strcmp (v1.data, G. vexs [I]. data) = 0) return I;
Return-1;
}
Void CreateUND (MGraph * G)/* uses an array (Adjacent matrix) and an adjacent table to represent an undirected graph */
{Int I, j, k, p [10], d;
VertexType v1, v2, * q1, * q2, * q;
For (I = 0; I <10; I ++) p [I] = 0;
For (I = 0; I <G-> vexnum; ++ I)/* Initial adjacent table */
{For (j = 0; j <G-> vexnum; ++ j) G-> arcs [I] [j]. adj = 0 ;}
For (k = 0; k <G-> arcnum; ++ k)
{Printf ("n input two vertex values relative to the % d (edge) arc: n", k + 1 );
D = 0;
Scanf ("% s", v1.data, v2.data);/* enter two adjacent vertex values */
I = Locate (* G, v1); j = Locate (* G, v2);/* use I and j to determine their locations */
G-> arcs [I] [j]. adj = 1;
G-> arcs [j] [I]. adj = G-> arcs [I] [j]. adj;
Q1 = (VertexType *) malloc (sizeof (VertexType);/* allocate space for q1 and q2 */
Q2 = (VertexType *) malloc (sizeof (VertexType ));
Strcpy (q1-> data, G-> vexs [I]. data );
Strcpy (q2-> data, G-> vexs [j]. data );
Q1-> next = q2-> next = NULL;
If (p [I] = 0) {G-> vexs [I]. next = q2; p [I] ++ ;}
Else {q = & (G-> vexs [I]);
While (d! = P [I]) {d ++; q = q-> next

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.