Pure C Language: Search and travel breadth depth traversal source sharing _c language

Source: Internet
Author: User

Copy Code code as follows:

#include <stdio.h>
typedef int datatype; /* Assume that the type of the linear table element is an integral type * *
#define MAXSIZE 1024/* assumes that the maximum length of the linear table is 1024*/
# define n 100/* Graph maximum number of vertices * *
typedef char Vextype; /* Vertex Data type * *
typedef float ADJTYPE; /* Weight value type * *
typedef struct
{
Vextype Vexs[n]; /* Vertex information array/*
Adjtype Arcs[n][n]; /* Benquan Array * *
int num; /* The actual number of vertices * *
}graph;

/***********************1. Shintou **********************/
void Graphinit (GRAPH *l)
{
l->num=0;
}

/***********************2. To find the knot points **********************/
int Graphvexs (GRAPH *l)
{
return (L->num);
}

/***********************3. Create Diagram **********************/
void Graphcreate (GRAPH *l)
{
int i,j;
Graphinit (L);
printf ("Please enter the number of vertices:");
scanf ("%d", &l->num);
printf ("Please enter the information for each vertex (single symbol):");
for (i=0;i<l->num;i++)
{
Fflush (stdin);
scanf ("%c", &l->vexs[i]);
}
printf ("Please enter the information of the Edge Weights matrix:");
for (i=0;i<l->num;i++)
{
for (j=0;j<l->num;j++)
{
scanf ("%f", &l->arcs[i][j]);
}
}
printf ("The diagram has been created!") ");
}

/***********************4. Output **********************/of graphs
void Graphout (GRAPH L)
{
int i,j;
printf ("\ n graph vertex number is:%d", l.num);
printf ("\ n the information for each vertex of the graph is: \ n");
for (i=0;i<l.num;i++)
printf ("%c", L.vexs[i]);
printf ("\ n the information for the Benquan matrix of the graph is: \ n");
for (i=0;i<l.num;i++)
{
for (j=0;j<l.num;j++)
{
printf ("%6.2f", L.arcs[i][j]);
}
printf ("\ n");
}
printf ("The diagram has been exported!") ");
}

/***********************5. Picture of the depth of travel **********************/
void DFS (GRAPH g,int qidian,int mark[])
From the first Qidian point of departure depth first travel in the graph G can access each vertex
{
int v1;
Mark[qidian]=1;
printf ("%c", G.vexs[qidian]);
for (v1=0;v1<g.num;v1++)
{
if (g.arcs[qidian][v1]!=0&&mark[v1]==0)
DFS (G,v1,mark);
}
}
/***********************6. Picture of the depth of travel **********************/
void Graphdfs (GRAPH g)
Depth first travels through the vertices that can be accessed in the graph G
{
int qidian,v,v1,mark[maxsize];
printf ("Deep travel:");
printf ("\ n Please enter the subscript for the starting point:");
scanf ("%d", &qidian);
for (v=0;v<g.num;v++)
{
mark[v]=0;
}
for (v=qidian;v<g.num+qidian;v++)
{
V1=v%g.num;
if (mark[v1]==0)
DFS (G,v1,mark);
}
}
typedef int DATATYPE; Data type of the queue element
typedef struct
{
DATATYPE Data[maxsize]; Elements of the team
int front,rear; Subscript of the team head element, trailing position of the team tail element
} Seqqueue;
/*****************************************************************************/
void Queueinit (Seqqueue *sq)
Empty (initialize) the sequential loop queue sq
{
sq->front=0;
sq->rear=0;
}
/*****************************************************************************/
int Queueisempty (seqqueue sq)
If the sequential loop queue sq is empty, successfully returns 1, otherwise returns 0
{
if (Sq.rear==sq.front)
return (1);
Else
return (0);
}
/*****************************************************************************/
int Queuefront (seqqueue sq,datatype *e)
Save the team head element of the sequential queue sq to the address E refers to, successfully return 1, failure return 0
{
if (queueisempty (SQ))
{printf (' queue is empty!\n '); return 0;}
Else
{*e=sq.data[(Sq.front)]; return 1;}
}
/*****************************************************************************/
int Queuein (seqqueue *sq,datatype x)
Returns the element x into the queue of SQ, successfully returning 1, failing to return 0
{
if (sq->front== (sq->rear+1)%maxsize)
{
printf ("Queue is full!\n");
return 0;
}
Else
{
sq->data[sq->rear]=x;
Sq->rear= (sq->rear+1)%maxsize;
return (1);
}
}
/*****************************************************************************/
int Queueout (Seqqueue *sq)
The queue sq team first element out of the queue, successfully returned 1, failed to return 0
{
if (Queueisempty (*SQ))
{
printf ("Queue is empty!\n");
return 0;
}
Else
{
Sq->front= (sq->front+1)%maxsize;
return 1;
}
}
/***********************7. The breadth of the chart travels **********************/
void BFS (GRAPH g,int v,int mark[])
From V-range priority travel to each vertex that can be accessed in Fig g
{
int v1,v2;
Seqqueue Q;
Queueinit (&AMP;Q);
Queuein (&AMP;Q,V);
Mark[v]=1;
printf ("%c", G.vexs[v]);
while (Queueisempty (q) ==0)
{
Queuefront (Q,&AMP;V1);
Queueout (&AMP;Q);
for (v2=0;v2<g.num;v2++)
{
if (g.arcs[v1][v2]!=0&&mark[v2]==0)
{
Queuein (&AMP;Q,V2);
Mark[v2]=1;
printf ("%c", G.vexs[v2]);
}
}
}
}
/***********************8. The breadth of the chart travels **********************/
void Graphbfs (GRAPH g)
Depth first travels through the vertices that can be accessed in the graph G
{
int qidian,v,v1,mark[maxsize];
printf ("Breadth Travels:");
printf ("\ n Please enter the subscript for the starting point:");
scanf ("%d", &qidian);
for (v=0;v<g.num;v++)
{
mark[v]=0;
}
for (v=qidian;v<g.num+qidian;v++)
{
V1=v%g.num;
if (mark[v1]==0)
BFS (G,v1,mark);
}
}

/*********************** Main function **********************/

void Main ()
{
GRAPH Tu;
Graphcreate (&TU);
Graphout (TU);
Graphdfs (TU);
Graphbfs (TU);
}

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.