Depth-first traversal of graphs: adjacency table implementation

Source: Internet
Author: User
Tags printf

This uses adjacency table to realize the depth first traversal of graphs, which is realized by recursion.

#include <iostream> using namespace std;    
    #define Vertexnum 5//knot points struct Edgenode {int to; int weight;    
The weight value of the edge is Edgenode *next;    
};    
    struct Vnode {int from;    
Edgenode *first;  
};  
void Creategraph (Vnode *adjilist, int start, int end,int weight);  
void Displaygraph (Vnode *adjilist,int nodenum);  
void DFT (Vnode *adjilist,int* vertexstatusarr,int nodenum);  
void Dftcore (Vnode *adjilist,int i,int* Vertexstatusarr);  
        /* More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/programming/sjjg/*/int main (void) {//create diagram  
        Vnode Adjilist[vertexnum];  
        int nodenum=vertexnum;  
        for (int i=0;i<nodenum;i++) adjilist[i].first=null;  
        int vertexstatusarr[vertexnum]={0};  
        cout<<vertexstatusarr[4]<<endl;  
        Creategraph (adjilist,0,3,0);  
        Creategraph (adjilist,0,4,0);  
        Creategraph (adjilist,3,1,0); Creategraph (adjilist,3,2,0);  
      
        Creategraph (adjilist,4,1,0);  
        printf ("After create:\n");  
        Displaygraph (Adjilist,nodenum);  
        Depth first traversal DFT (adjilist,vertexstatusarr,nodenum);  
        System ("pause");  
return 0;  
    //create diagram, take the front interpolation void Creategraph (Vnode *adjilist, int start, int end,int weight) {adjilist[start].from=start;  
    Edgenode *p=new Edgenode;  
    p->to=end;  
    p->weight=weight;  
    p->next=adjilist[start].first;  
adjilist[start].first=p;  
    }//print stored figure void Displaygraph (Vnode *adjilist,int nodenum) {int i,j;  
    Edgenode *p;  
        for (i=0;i<nodenum;i++) {p=adjilist[i].first; while (p) {cout<< ' (' <<adjilist[i].from<< ', ' <<p->to<< ') ' <<en  
            dl  
        p=p->next; }}//Depth First traversal void DFT (Vnode *adjilist, int* vertexstatusarr,int nodenum) {printf ("Start BFT gr APH:\ n ");  
        int i;  
        for (i=0;i<nodenum;i++) {dftcore (Adjilist,i,vertexstatusarr);  
printf ("\ n");  
    } void Dftcore (Vnode *adjilist,int i,int* Vertexstatusarr) {if (vertexstatusarr[i) = = 1) return;  
    printf ("%d", I);  
    Vertexstatusarr[i] = 1;  
    Edgenode *p;      
    for (P=adjilist[i].first;p;p=p->next) {Dftcore (adjilist, P->to, Vertexstatusarr); }  
}

Time Complexity of O (v+e).

Author: csdn Blog Duplan

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.