The establishment of "data structure-graph" graph and the algorithm of breadth-first traversal

Source: Internet
Author: User

In this paper, the graph is represented by the Adjacency table method, and the graph is traversed by the breadth-first traversal method.

The following is an example of a diagram:


The code is as follows:

#include <iostream>
using namespace Std;
typedef int VEXTYPE;
typedef struct arcnode{
Vextype data;
struct Arcnode *nextarc;
}arcnode;
typedef struct VEXNODE
{


Vextype data;
Arcnode *firstarc;
}VNODE,ADJLIST[100];
typedef struct GRAPH
{
Adjlist adjlist;
int vexnum;
int arcnum;
}*mgraph,graph;
Mgraph Create_graph ()
{
Mgraph mygraph =new Graph;
int key;
int i;
int m;
int x, y, Z;
printf ("How many vertices are there in figure one?") \ n ");
scanf ("%d", &x);
mygraph->vexnum=x;
printf ("How many sides are there in figure one?") \ n ");
scanf ("%d", &y);
mygraph->arcnum=y;
printf ("Please enter each vertex \ n");
for (i=1;i<=mygraph->vexnum;i++)
{
scanf ("%d", &z);
mygraph->adjlist[i].data=z;
}
for (i=1;i<=mygraph->vexnum;i++)
{
printf ("%d" has no associated vertices? Input 1, no input 0\n ", mygraph->adjlist[i].data);
scanf ("%d", &key);
if (key)
{
printf ("Please enter the vertex associated with%d, end with-123 \ n", mygraph->adjlist[i].data);
scanf ("%d", &m);
Arcnode *p=new Arcnode;
p->data=m;
p->nextarc=null;
mygraph->adjlist[i].firstarc=p;
printf ("Please enter the vertex associated with%d, end with-123 \ n", mygraph->adjlist[i].data);
scanf ("%d", &m);
while (m!=-123)
{
Arcnode *q=new Arcnode;
q->data=m;
p->nextarc=q;
p=q;
printf ("Please enter the vertex associated with%d, end with-123 \ n", mygraph->adjlist[i].data);
scanf ("%d", &m);
}
p->nextarc=null;
}
if (!key)
{
mygraph->adjlist[i].firstarc=null;
}
printf ("Completed%d node \ n", i);
}
printf ("done\n");
return mygraph;
}

void prints (mgraph root)
{
Mgraph Mygraph=root;
int i;
Arcnode *q;
printf ("The adjacency table of this graph is represented as: \ n");
for (i=1;i<=mygraph->vexnum;i++)
{
printf ("%d:", mygraph->adjlist[i].data);
q=mygraph->adjlist[i].firstarc;
if (q)
{
while (Q->nextarc!=null)
{
printf ("%d,", q->data);
q=q->nextarc;
}
printf ("%d", q->data);
}
printf ("\ n");
}
}
void BFS (Mgraph root)//breadth-first traversal
{
Mgraph Mygraph=root;
BOOL *sign=new bool;//Flag This node is not being accessed
int i;
for (i=1;i<=mygraph->vexnum;i++)
Sign[i]=false;
Vnode *Q[100];
Vnode *p;
Arcnode *q;
int rear=-1;
int front=-1;
printf ("Breadth-first traversal:");
for (i=1;i<=mygraph->vexnum;i++)
{
if (!sign[i])
{
printf ("%d", mygraph->adjlist[i].data);
Sign[i]=1;
}
q[++rear]=& (Mygraph->adjlist[i]);
while (Front!=rear)
{
P=q[++front];
if (p->firstarc!=null)
{
q=p->firstarc;
while (Q->nextarc!=null)
{
if (!sign[q->data])
{
printf ("%d", q->data);
q[++rear]=& (Mygraph->adjlist[q->data]);
sign[q->data]=true;
q=q->nextarc;
}
Else
{
q=q->nextarc;
}
}
if (!sign[q->data])
{
printf ("%d", q->data);
q[++rear]=& (Mygraph->adjlist[q->data]);
sign[q->data]=true;
}//access to the last element in each linked list
}
}
}
}
int main (void)
{
Mgraph mygraph;
Mygraph=create_graph ();
BFS (mygraph);
return 0;
}

Results show:






The establishment of "data structure-graph" graph and the algorithm of breadth-first traversal

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.