Data structure routines--traversal of graphs

Source: Internet
Author: User

This article is a routine of the 6th lesson [graph traversal] in [Data Structure basic series (7): Figure].

1, Depth first traversal--dfs (Linklist.h is the graph storage structure of the "algorithm library" in the header file, details, click the link ... )

#include <stdio.h>#include <malloc.h>#include "graph.h"intVisited[maxv];void DFS (algraph*g,intV) {Arcnode*p;intW visited[v]=1;printf("%d ", v); p=g->adjlist[v].firstarc; while(P!=null) {w=p->adjvex;if(visited[w]==0) DFS (G,W);    p=p->nextarc; }}intMain () {intI Algraph*g;inta[5][5]=    {        {0,1,0,1,0},        {1,0,1,0,0},        {0,1,0,1,1},        {1,0,1,0,1},        {0,0,1,1,0}    }; Arraytolist (a[0],5, G); for(i=0; i<maxv; i++) visited[i]=0;printf("Starting at 2 depth traversal:"); DFS (G,2);printf("\ n"); for(i=0; i<maxv; i++) visited[i]=0;printf("Starting at 0 depth traversal:"); DFS (G,0);printf("\ n");return 0;}

The diagram used for testing is that you can use other types of diagrams instead.

2, Breadth first traversal--BFS (Linklist.h is the graph storage structure of the "algorithm library" in the header file, details, click the link ... )

#include <stdio.h>#include <malloc.h>#include "graph.h"voidBFS (Algraph *g,intV) {Arcnode *p;intW,i;int Queue[maxv],front=0, rear=0;//define Loop queue    intVISITED[MAXV];//Define an array of access flags that hold the node     for(i=0; i<g->n; i++) visited[i]=0;//Access flag array initialization    printf("%2d", v);//Output The number of the accessed vertexvisited[v]=1;//Set access tagRear= (rear+1)%MAXV;Queue[Rear]=v;//v into the team .     while(front!=rear)//Loop If queue is not empty{front= (front+1)%MAXV; w=Queue[Front];//out of the team and assigned to Wp=g->adjlist[w].firstarc;//Find the first adjacency point of W         while(P!=null) {if(visited[p->adjvex]==0)            {printf("%2d", P->adjvex);//access to thevisited[p->adjvex]=1; Rear= (rear+1)%MAXV;//The vertex enters the team                Queue[rear]=p->adjvex; } p=p->nextarc;//Find Next adjacency vertex}    }printf("\ n");}intMain () {algraph *g;inta[5][5]=    {        {0,1,0,1,0},        {1,0,1,0,0},        {0,1,0,1,1},        {1,0,1,0,1},        {0,0,1,1,0}    }; Arraytolist (a[0],5, G);printf("by 2, the breadth is traversed:"); BFS (G,2);printf("by 0, the breadth is traversed:"); BFS (G,0);return 0;}

The diagram used for testing is that you can use other types of diagrams instead.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure routines--traversal of graphs

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.