Breadth Traversal (BFS) and depth traversal (DFS) of graphs

Source: Internet
Author: User
Tags stdin

Reprint indicated source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457


breadth Traversal (BFS) and depth traversal (DFS) of graphs


Train of thought: Read the user input node number, edge of the vertices, using an adjacency matrix to represent the connectivity in the diagram. Then take the first node and put it into a two-terminal queue for BFS.


Reprinted annotated Source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457/* BFS * * * #include <stdio.h> #include <

Deque> using namespace std;
    typedef struct NODE {int flag, d, pre;
Node (): Flag (0), D (10000), pre (-1) {};

}vertex;
const int MAXV = 10000;

int MATRIX[MAXV][MAXV];
    void Readedge (int num) {int STARTV, ENDV;
    printf ("Enter your edges:\n"); while (~SCANF ("%d,%d", &startv, &AMP;ENDV) && (StarTV < num) && (STARTV >= 0) && (endv &L T
    num) && (endv >= 0)) {MATRIX[STARTV][ENDV] = Matrix[endv][startv] = 1;
    } void BFS (deque<int> Q, vertex *a, int num) {int i;
    A[0].flag = 1;

    A[0].D = 0;

    Q.push_back (0); while (!
        Q.empty ()) {int u = q.front ();
        Q.pop_front ();
                for (i = 0; i < num i++) {if (Matrix[u][i]!= 0 && a[i].flag = 0) {
                A[i].flag = 1;
      A[I].D = a[u].d + 1;          A[i].pre = u;
            Q.push_back (i);
    } A[u].flag = 2;

    } void Printvertex (vertex* A, int num) {int I, j, K;
        for (j = 0; J < Num. ++j) {for (k = 0; k < num; ++k) {printf ("%d", matrix[j][k]);
    printf ("\ n"); for (i = 0; i < num ++i) {printf ("Vertex:%d, flag:%d, pre:%d, D:%d\n", I, A[i].flag, A[i].pre, A
    [I].D);
    
    int main () {freopen ("J://test.txt", "R", stdin);
    int vertexn;
    printf ("Please enter the quantity of your vertex\n");
    scanf ("%d", &vertexn);
        if (Vertexn < 1 | | vertexn > MAXV) {perror ("Invalid parameter:vertexn\n");
    return-1;

    } vertex VERTEXS[VERTEXN];

    Readedge (VERTEXN);
    Deque<int> Vertexq;

    Vertexq.clear ();

    BFS (VERTEXQ, Vertexs, VERTEXN);

    Printvertex (Vertexs, VERTEXN);
return 0; }

Idea: Dfs mainly uses recursive return for depth-first traversal


Reprinted annotated Source, original address: http://blog.csdn.net/powerwoo25/article/details/47869457/* DFS/* #include <stdio.h> using

 namespace Std;
    typedef struct Node {int flag;
    int pre;
    int d, F;
Node (): Flag (0), Pre ( -1), D (0), F (0) {};

}vertex;
const int MAXV = 10000;
int time = 0;

int MATRIX[MAXV][MAXV];
    void Readedge (int num) {int STARTV, ENDV;
    printf ("Enter your edges:\n"); while (~SCANF ("%d,%d", &startv, &AMP;ENDV) && (StarTV < num) && (STARTV >= 0) && (endv &L T
    num) && (endv >= 0)) {MATRIX[STARTV][ENDV] = Matrix[endv][startv] = 1;

    } void Printvertex (vertex* A, int num) {int I, j, K;
        for (j = 0; J < Num. ++j) {for (k = 0; k < num; ++k) {printf ("%d", matrix[j][k]);
    printf ("\ n"); for (i = 0; i < num ++i) {printf ("Vertex:%d, flag:%d, pre:%d, D:%d, F:%d\n", I, A[i].flag, A[i]
    . Pre, A[I].D, A[I].F); } void DFs_visit (vertex *a, int index, int num) {int i;
    A[INDEX].D = ++time;
    A[index].flag = 1; 
                for (i = 0; i < num ++i) {if (Matrix[index][i]!= 0) {if (A[i].flag = = 0) {
                A[i].pre = index;
            Dfs_visit (A, I, num);
    }} A[index].flag = 2;
A[INDEX].F = ++time;
    } void DFS (vertex *a, int num) {int i;
        for (i = 0; i < num; ++i) {if (A[i].flag = = 0) {dfs_visit (A, I, num);
    ()} int main () {freopen ("J://test.txt", "R", stdin);
    int vertexn;
    printf ("Please enter the quantity of your vertex\n");
    scanf ("%d", &vertexn);
        if (Vertexn < 1 | | vertexn > MAXV) {perror ("Invalid parameter:vertexn\n");
    return-1;

    } vertex VERTEXS[VERTEXN];

    Readedge (VERTEXN);

    DFS (Vertexs, VERTEXN);

    Printvertex (Vertexs, VERTEXN);
return 0; }


# test.txt
0,2
3,4
2,4
4,1
3,1 5,6 2,6 7,9 3,8 8,9


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.