C language implementation of depth and breadth traversal of 03 adjacency matrices

Source: Internet
Author: User

#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int STATUS;/* Status is the type of function whose value is the function result status code, such as OK, etc. */
typedef int Boolean; /* Boolean is a Boolean type with a value of TRUE or FALSE */

typedef char Vertextype; /* The vertex type should be user defined */
typedef int EDGETYPE; /* The weight type on the edge should be defined by the user */

#define MAXSIZE 9/* Storage space Initial allocation */
#define MAXEDGE 15
#define Maxvex 9
#define INFINITY 65535

typedef struct
{
Vertextype Vexs[maxvex]; /* Vertex table */
Edgetype arc[maxvex][maxvex];/* adjacency Matrix, can be considered as edge table */
int numvertexes, numedges; /* Current number of vertices and sides in the graph */
}mgraph;

/* Queue structure and function used ********************************** */

/* Sequential storage structure for cyclic queues */
typedef struct
{
int data[maxsize];
int front; /* Head pointer */
int rear;/* tail pointer, if the queue is not empty, point to the next position of the tail element of the queue */
}queue;

/* Initialize an empty queue Q */
Status initqueue (Queue *q)
{
q->front=0;
q->rear=0;
return OK;
}

/* Returns True if Queue Q is an empty queue, otherwise false */
Status queueempty (Queue Q)
{
if (q.front==q.rear)/* Queue empty flag */
return TRUE;
Else
return FALSE;
}

/* If the queue is not full, insert element e is Q new team tail element */
Status EnQueue (Queue *q,int e)
{
if ((q->rear+1)%maxsize = = Q->front)/* Queue full judgment */
return ERROR;
q->data[q->rear]=e;/* Assign the element e to the tail of the team */
Q->rear= (q->rear+1)%maxsize;/* rear pointer moves back one position, */
/* If the end goes to the head of the array */
return OK;
}

/* If the queue is not empty, delete the Q Squadron header element and return its value with E */
Status DeQueue (Queue *q,int *e)
{
if (Q->front = = q->rear)/* Queue empty judgment */
return ERROR;
*e=q->data[q->front];/* assign the team head element to E */
Q->front= (q->front+1)%maxsize;/* front pointer moves back one position, */
/* If the end goes to the head of the array */
return OK;
}
/* ****************************************************** */


void Createmgraph (Mgraph *g)
{
int I, J;

g->numedges=15;
g->numvertexes=9;

/* Read the vertex information and create the vertex table */
g->vexs[0]= ' A ';
g->vexs[1]= ' B ';
g->vexs[2]= ' C ';
g->vexs[3]= ' D ';
g->vexs[4]= ' E ';
g->vexs[5]= ' F ';
g->vexs[6]= ' G ';
g->vexs[7]= ' H ';
g->vexs[8]= ' I ';


for (i = 0; i < g->numvertexes; i++)/* Initialization diagram */
{
for (j = 0; J < g->numvertexes; J + +)
{
g->arc[i][j]=0;
}
}

g->arc[0][1]=1;
g->arc[0][5]=1;

g->arc[1][2]=1;
g->arc[1][8]=1;
g->arc[1][6]=1;

g->arc[2][3]=1;
g->arc[2][8]=1;

g->arc[3][4]=1;
g->arc[3][7]=1;
g->arc[3][6]=1;
g->arc[3][8]=1;

g->arc[4][5]=1;
g->arc[4][7]=1;

g->arc[5][6]=1;

g->arc[6][7]=1;


for (i = 0; i < g->numvertexes; i++)
{
for (j = i; J < g->numvertexes; J + +)
{
G->arc[j][i] =g->arc[i][j];
}
}

}

Boolean Visited[maxvex]; /* Array of Access flags */

/* Depth-first recursive algorithm for adjacency matrix */
void DFS (mgraph G, int i)
{
Int J;
Visited[i] = TRUE;
printf ("%c", g.vexs[i]);/* print vertices or other actions */
for (j = 0; J < G.numvertexes; J + +)
if (g.arc[i][j] = = 1 &&!visited[j])
DFS (G, j);/* Recursive call to adjacency vertex for access */
}

/* Depth traversal operation of adjacency matrix */
void Dfstraverse (mgraph G)
{
int i;
for (i = 0; i < g.numvertexes; i++)
Visited[i] = FALSE; /* Initial all vertex states are not visited status */
for (i = 0; i < g.numvertexes; i++)
if (!visited[i])/* Dfs is invoked on an unreachable vertex, if connected graph, only once */
D FS (G, i);
}

/* Breadth traversal algorithm for adjacency Matrix */
void Bfstraverse (Mgraph G)
{
int I, J;
Queue Q;
for (i = 0; i < g.numvertexes; i++)
Visited[i] = FALSE;
Initqueue (&Q);/* Initializes an auxiliary queue */
for (i = 0; i < g.numvertexes; i++)/* cycles through each vertex */
{
if (!visited[i])/* If you have not accessed it * *
{
visited[i]=true;/* set current vertex access over */
printf ("%c", g.vexs[i]);/* print vertices or other actions */
EnQueue (&q,i);/* Put this vertex in the queue */
while (! Queueempty (Q))/* If the current queue is not empty */
{
DeQueue (&q,&i);/* Set the team to the element out of the queue, assign the value to I */
for (j=0;j<g.numvertexes;j++)
{
/* Determine if the other vertices have edges and have not been visited with the current vertex */
if (g.arc[i][j] = = 1 &&!visited[j])
{
visited[j]=true;/* marks the found vertex as visited */
printf ("%c", G.vexs[j]);/* Print vertex */
EnQueue (&Q,J);/* will find this vertex into the queue */
}
}
}
}
}
}


int main (void)
{
Mgraph G;
Createmgraph (&G);
printf ("\ n Depth traversal:");
Dfstraverse (G);
printf ("\ n breadth traversal:");
Bfstraverse (G);
return 0;
}

03 C language implementation of depth and breadth traversal of adjacency matrices

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.