[Data Structure] FIG traversal (BFS and DFS)

Traversing Graph

Traversing the graph refers to a departure from the vertices in the graph, according to some search methods once and access time along side the way to visit all the vertices figures only. There are two main graph traversal algorithms: breadth-first search and depth-first search.

BFS breadth-first traversal

Breadth-first traversal (BFS also called breadth-first search) similar to the binary tree sequence traversal algorithm
Here Insert Picture Description

#define MaxSize 100;
bool visited[MaxSize];		//访问数组,记录顶点是否被访问过,初始都赋值为false
void BFS(Graph G,int v){	//图用邻接表存储,从下标为v的位置开始遍历
	ArcNode *p;             //工作指针p
    InitQueue(Q);           //初始化一个队列
    visit(v);		        //访问第一个顶点v 具体可以是Print	
    visited[v]=TRUE;	    //对v做已访问标记
    Enqueue(Q,v);	        //顶点v入队列
    while(!isEmpty(Q)){     //只要队列不空
		DeQueue(Q,v);  	    //顶点v出队列
		p=G->adjList[v].firstedge; 			//指针p指向当前顶点的边表链表头指针
        while(p){											                    
        	if(!visited[p->adjvex]){	    //p所指向顶点如果未被访问	
            	visit(p->adjvex);	        //访问p所指向的顶点
				visited[p->adjvex]=TRUE;    //对这个顶点做已访问标记
				EnQueue(Q,p->adjvex);	    //这个顶点入队列
			  } 
              p=p->next;					//p指向该顶点的下一条边
	     }
      }
}
void BFSTraverse(Graph  G){
	int i;  //单独定义是为了方便多个循环中使用
    for(i=0; i<G->vexnum; i++)visited[i]=false; //将标志数组初始化 (全局数组)
    for(i=0; i<G->vexnum; i++){                                      
    	if(!visited[i])BFS(G,i);}  //为了避免非连通图一些顶点访问不到 若是连通图只会执行一次
    }
}
BFS complexity analysis
  1. Whether adjacency table or storage adjacency matrix, the BFS algorithm requires a means of auxiliary queue Q, n vertices enqueue time required, in the worst case space complexity O ( V ) O (| V |) .
  2. When using the adjacent table when storage, each vertex require a search (or enqueue time) regardless of the time complexity is O ( V ) O (| V |) , when searching for any temporary contacts a vertex, each edge need access time, so the time complexity is O ( E ) O (| E |) . The total time complexity of the algorithm is O ( V + E ) O (| V | + | E |)
  3. When using the adjacency matrix during storage for the required clinical point of time of each vertex O ( V ) O (| V |) , so that the time complexity of the algorithm is O ( V 2 ) O (| V | ^ 2)
BFS application

BFS single-source shortest path problem solving non-weighted graph: according to the distance from near to far to traverse each vertex FIG.

void BFS_MIN_Distance(Graph G,int u){      
    //d[i]表示从u到i结点的最短路径
	for(i=0;i<G.vexnum;++i) d[i]=∞; //初始化路径长度
	visited[u]=TRUE; d[u]=0;
	EnQueue(Q,u);
	while(!isEmpty(Q)){				
		DeQueue(Q,u);  				
		ArcNode *p=G->adjList[u].firstedge; 
        while(p){	
        	If(!visited[p->adjvex]){    
            	visited[p->adjvex]=TRUE;  
                //路径长度加1   
                d[p->adjvex]=d[u]+1; 
                EnQueue(Q, p->adjvex);	
            } 
            p=p->next;
         }
   }				
}
Breadth-first spanning tree

Here Insert Picture Description

Depth-first traversal DFS

Depth-first traversal (the DFS: Depth-First-Search) : depth-first traversal of the tree-like preorder traversal algorithm
first accesses a drawing start vertex v, then starting from v, v abutment with access to any and has not been accessed for a vertex w1, w1 and then access and not be accessed by any adjacent vertex w2, ...... repeat the process. When access can not continue down, turn back to the vertex most recently accessed , if it has not been visited adjacent vertices, from that point on the above search process continues until all the vertices of the figure are visited so far
Here Insert Picture Description

#define MaxSize 100;
bool visited[MaxSize];
void DFS(Graph G,int v){
    ArcNode *p;     	 //工作指针p
	visit(v);	         //访问顶点v(一般是打印,即printf)
	visited[v]=TRUE;	 //修改访问标记
	p=G->adjlist[v].firstarc;   	//指针p开始指向该顶点的第一条边
    while(p!=NULL){         		//没遍历完顶点的所有邻接顶点
		if(!visited[p->adjvex]){    //如果该顶点没被访问
		      DFS(G,p->adjvex); 	//递归访问该顶点
        } 
        p=p->nextarc;   			//看还有没有其他未访问的顶点
}
void DFSTraverse(Graph  G){
	int i;  									 //单独定义是为了方便多个循环中使用
    for(i=0; i<G->vexnum; i++)visited[i]=false; //将标志数组初始化 (全局数组)
    for(i=0; i<G->vexnum; i++){                                      
    	if(!visited[i]) DFS(G,i);  				 //对所有
}

DFS algorithm is a recursive algorithm, it needs a recursive stack of work, so her space complexity O ( V ) O (| V |) .
FIG traversal process is essentially to find its temporary contacts for each vertex process, which depends on the storage time-consuming configuration employed.

  1. When at the adjacent table when it said time is required to find all the vertices of the contact Pro O ( E ) O (| E |) , the time required to access the vertex O ( V ) O (V) , at this time, the total time complexity is O ( V + E ) O (| V | + | E |)
  2. When in adjacency matrix when said time required for each vertex find neighbors O ( V ) O (| V |) , so the total time complexity is O ( V 2 ) O (| V | ^ 2)
Depth-first Spanning Tree

Here Insert Picture Description

Reference material

Wang data structure

Published 48 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_37551036/article/details/100061579