Introduction to algorithms-Traversal of the graph (Bfs+dfs,matlab)

Source: Internet
Author: User

Huadian North Wind Blows
Key laboratory of cognitive computing and application, Tianjin University
Last Modification Date: 2015/8/22

There are adjacency matrices, adjacency linked lists, sparse matrices and so on for the storage of the graph.

The non-direction graph mainly includes the two sides content, the graph traverse and seeks the unicom component.

One, the traversal of the graph without direction
There are two ways to traverse a non-graph-breadth-first search (BFS) and depth-first search (DFS). Breadth-First search when traversing all nodes of a vertex, the nodes of the current node are traversed first. It then iterates through all the neighboring nodes of the first adjacent node of the current node, and the breadth-first search is implemented using a queue. Depth-First search visits the first adjacent node of the current node when traversing all adjacent nodes of the current node. It then iterates through the neighboring nodes of the first neighboring node. Recursively, so the depth-first search uses the stack implementation.

1, BFS diagram Traversal code:

 function result=bfstraversal(startnode,graph) % Breadth First search% Graph Graph Connectivity Matrix[M N]=size(Graph); nodelist=Zeros(M,1); Queue=startnode;nodelist (startnode) =1; result=startnode; while IsEmpty(queue) ==falseI=queue (1); Queue1)=[]; for J=1: Nif(Graph (I,J) >0&&nodelist (J)==0&&I~=J) queue=[Queue;j]; NodeListJ)=1; result=[Result;j];End    EndEnd

2. DFS Graph Traversal code

function result=DFSTraversal(startNode,Graph)global nodelistm=size(Graph,1);nodelist=zeros(m,1);result=DFSRecursion(startNode,Graph);
 function result=dfsrecursion(startnode,graph) GlobalNodelistnodelist (Startnode) =1; result=[Startnode]; n=size(Graph,2); for J=1: Nif(Graph (Startnode,J) >0&&nodelist (J)==0&&startnode~=J) result=[Result Dfsrecursion (j,graph)];EndEnd

Ii. searching for the components of Unicom
The way to find the Unicom component is to find out all the neighboring nodes of a node, and then select a node in the non-visited node to search for adjacent nodes by traversal method.

1. Search for the Unicom component code based on BFS:

 function resultSet=bfsdividegraph(Graph) % connected components (breadth-first search)% graph graph connected matrix, undirected graph. symmetric matricesresultset=[];[M N]=size(Graph); nodelist=Zeros(M,1);p =1; fork=1: Mif(NodeList (k) = =0) Startnode=k;        Queue=startnode; NodeList (Startnode) =1; Result=startnode; while IsEmpty(queue) ==falseI=queue (1); Queue1)=[]; for J=1: Nif(Graph (I,J) >0&&nodelist (J)==0&&I~=J) queue=[Queue;j]; NodeListJ)=1; result=[Result;j];End            End        EndResultSet (P). Graph=result; p=p+1;EndEnd

2. Search for the Unicom component code based on BFS:
Select the non-visited node in the Access record list as the starting point for the depth-first search until all the nodes have been interviewed.

Third, the high-level application of graphs
1. BFS
Shortest path (Dijkstra). The minimum spanning tree (Prim). Topological sorting

2. DFS
Topological ordering, strongly connected components

The code in this article is based on adjacency matrices.

Introduction to algorithms-Traversal of the graph (Bfs+dfs,matlab)

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.