Breadth-first traversal of graph graphs

Source: Internet
Author: User

Breadth-first traversal is a traversal strategy for connected graphs. The basic ideas are as follows:

1, starting from a vertex V0 in the graph, and accessing this vertex;

2, from V0, access to the V0 of the w1,w2,...,wk of the neighboring points; then, from the W1,w2,..., Wk to access their inaccessible neighboring points;

3. Repeat step 2 until all the vertices have been accessed.


For example, in the following image:
1. Starting with 0, we first find the associated vertex of 0 3,4 2. From 3, find 1, 2, 4, 1, but 1 have been traversed, so ignore. 3. Starting from 1, there are no associated vertices; starting from 2, there are no associated vertices. So the final order is 0,3,4,1,2 C language implementation as follows: (using adjacency matrix storage)

#include <stdio.h> #include <malloc.h> #define VERTEXNUM 5//Queue element typedef struct Qelement{int value
        ;
        struct qelement* pre;
struct qelement* next;

}st_qelement;
The front and back end of the queue, the last element into the queue is rear, the first out-of-queue element is front st_qelement* front = NULL;

st_qelement* rear = NULL;
void Creategraph (int (*edge) [vertexnum], int start, int end);
void Displaygraph (int (*edge) [Vertexnum]);
void BFT (int (*edge) [vertexnum],int* Vertexstatusarr);
void Bftcore (int (*edge) [Vertexnum],int i,int* Vertexstatusarr);
void putqueue (int vertex);
int* Getqueue ();

void Putrelatedinqueue (int (*edge) [vertexnum], int vertex); int main (void) {//dynamically creates a two-dimensional array that holds the edge of int (*edge) [vertexnum] = (int (*) [vertexnum]) malloc (sizeof (int) *vertexnum*vertex
        NUM);
        int i,j;
                for (i=0;i<vertexnum;i++) {for (j=0;j<vertexnum;j++) {edge[i][j] = 0; }}//Hold vertex traversal state, 0: Not traversed, 1: traversed int* Vertexstatusarr = (int*) malloc (sizeof (int) *vertexnum);
        for (i=0;i<vertexnum;i++) {vertexstatusarr[i] = 0;
        } printf ("After init:\n");
		Displaygraph (Edge);
        Create Figure creategraph (edge,0,3);
        Creategraph (edge,0,4);
        Creategraph (edge,3,1);
        Creategraph (edge,3,2);

        Creategraph (edge,4,1);
        printf ("After create:\n");
		
		Displaygraph (Edge);

        Breadth-first traversal of BFT (Edge,vertexstatusarr);
        Free (edge);
return 0;

}//create diagram void creategraph (int (*edge) [vertexnum], int start, int end) {Edge[start][end] = 1;}
        Print stored figure void displaygraph (int (*edge) [vertexnum]) {int i,j; for (i=0;i<vertexnum;i++) {for (j=0;j<vertexnum;j++) {printf ("%d", Edge[i][j])
                ;
        } printf ("\ n");
        }}//breadth-first traversal of void BFT (int (*edge) [Vertexnum], int* Vertexstatusarr) {printf ("Start BFT graph:\n");
        int i; for (i=0;i<vertexnum;i++){Bftcore (Edge,i,vertexstatusarr);
} printf ("\ n");
        } void Bftcore (int (*edge) [Vertexnum],int i,int* Vertexstatusarr) {putqueue (i);
        int* qevalue = NULL; while ((Qevalue = Getqueue ()) = NULL) {if (vertexstatusarr[*qevalue] = = 0) {printf (
                        "%d", *qevalue);
                        Vertexstatusarr[*qevalue] = 1;
                Putrelatedinqueue (Edge, *qevalue);
                } free (Qevalue);
        Qevalue = NULL;
        }}//Put the element in the queue void putqueue (int vertex) {st_qelement* QE = (st_qelement*) malloc (sizeof (st_qelement));
        Qe->value = vertex;
        Qe->next = NULL;
        Qe->pre = NULL;
        if (front = = NULL | | rear = = NULL) {front = rear = QE;
                }else{rear->next = QE;
                Qe->pre = rear;
        Rear = QE; }}//Gets an element from the queue int* Getqueue () {if (front= = NULL | |
        Rear = = null) {return null;
                }else{int* res = (int*) malloc (sizeof (int));

                *res = front->value;
                st_qelement* p = Front;
                Front = front->next;
                if (front = = NULL) {rear = front;
                }else{front->pre = NULL;
                } free (p);
                p = NULL;
        return res;
        }}//Place the element associated with the vertex in the queue void Putrelatedinqueue (int (*edge) [vertexnum], int vertex) {int i;
                for (i=0;i<vertexnum;i++) {if (edge[vertex][i] = = 1) {putqueue (i); }
        }
}


The C language is implemented as follows: (using adjacency table storage)
#include <stdio.h> #include <malloc.h> #define VERTEXNUM 5//adjacency table element for storing vertices typedef struct edge{int vert
        Ex
struct edge* next;

}st_edge;
        The elements of the queue typedef struct qelement{int value;
        struct qelement* pre;
struct qelement* next;

}st_qelement;
The front and back end of the queue, the last element into the queue is rear, the first out-of-queue element is front st_qelement* front = NULL;

st_qelement* rear = NULL;
void Creategraph (st_edge** edge, int start, int end);
void Displaygraph (st_edge** edge);
void Delgraph (st_edge** edge);
void BFT (st_edge** edge,int* Vertexstatusarr);
void Bftcore (st_edge** edge,int i,int* Vertexstatusarr);
void putqueue (int vertex);
int* Getqueue ();

void Putrelatedinqueue (st_edge** edge, int vertex);
        int main (void) {//dynamically creates an array of pointers that hold edges st_edge** edge = (st_edge**) malloc (sizeof (st_edge*) *vertexnum);
        int i;
        for (i=0;i<vertexnum;i++) {edge[i] = NULL; }//Storage vertex traversal state, 0: Not traversed, 1: traversed int* Vertexstatusarr = (int*) malloc (sizeof (int) *verteXnum);
        for (i=0;i<vertexnum;i++) {vertexstatusarr[i] = 0;
        } printf ("After init:\n");
		Displaygraph (Edge);
        Create Figure creategraph (edge,0,3);
        Creategraph (edge,0,4);
        Creategraph (edge,3,1);
        Creategraph (edge,3,2);

        Creategraph (edge,4,1);
        printf ("After create:\n");
		
		Displaygraph (Edge);
		
		Breadth-first traversal of BFT (Edge,vertexstatusarr);
        Frees the Memory Delgraph (EDGE) occupied by the adjacency table;
        Edge = NULL;
        Free (Vertexstatusarr);
        Vertexstatusarr = NULL;
return 0; }//create diagram void Creategraph (st_edge** edge, int start, int end) {st_edge* Newedge = (st_edge*) malloc (sizeof (St_edge)
        );
        Newedge->vertex = end;
        Newedge->next = NULL;
        Edge = Edge + start;
        while (*edge! = NULL) {edge = & ((*edge)->next);
} *edge = Newedge;
        }//print stored figure void Displaygraph (st_edge** edge) {int i;
        St_edge* p;for (i=0;i<vertexnum;i++) {printf ("%d:", i);
                p = * (edge+i);
                        while (P! = NULL) {printf ("%d", P->vertex);
                p = p->next;
        } printf ("\ n");
        }}//releasing contiguous table occupied memory void Delgraph (st_edge** edge) {int i;
        St_edge* p;
        St_edge* del;
                for (i=0;i<vertexnum;i++) {p = * (edge+i);
                        while (P! = NULL) {del = p;
                        p = p->next;
                Free (DEL);
        } Edge[i] = NULL;
} free (edge);
        }//breadth-first traversal of void BFT (st_edge** edge,int* vertexstatusarr) {printf ("Start BFT graph:\n");
        int i;
        for (i=0;i<vertexnum;i++) {bftcore (Edge,i,vertexstatusarr);
} printf ("\ n");
        } void Bftcore (st_edge** edge,int i,int* vertexstatusarr) {putqueue (i); int* QevalUE = NULL; while ((Qevalue = Getqueue ()) = NULL) {if (vertexstatusarr[*qevalue] = = 0) {printf (
                        "%d", *qevalue);
                        Vertexstatusarr[*qevalue] = 1;
                Putrelatedinqueue (Edge, *qevalue);
                } free (Qevalue);
        Qevalue = NULL;
        }}//Put the element in the queue void putqueue (int vertex) {st_qelement* QE = (st_qelement*) malloc (sizeof (st_qelement));
        Qe->value = vertex;
        Qe->next = NULL;
        Qe->pre = NULL;
        if (front = = NULL | | rear = = NULL) {front = rear = QE;
                }else{rear->next = QE;
                Qe->pre = rear;
        Rear = QE;
        }}//Gets an element from the queue int* Getqueue () {if (front = = NULL | | rear = = NULL) {return null;
                }else{int* res = (int*) malloc (sizeof (int));

                *res = front->value; St_qelement* p = Front;
                Front = front->next;
                if (front = = NULL) {rear = front;
                }else{front->pre = NULL;
                } free (p);
                p = NULL;
        return res;
        }}//Place the element associated with the vertex in the queue void Putrelatedinqueue (st_edge** edge, int vertex) {st_edge* p = * (Edge+vertex);
                while (P! = NULL) {putqueue (P->vertex);
        p = p->next; }
}

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.