Breadth first traversal algorithm of graph based on Java implementation

This paper describes the implementation method of breadth first traversal algorithm of graph based on Java in the form of examples. The specific methods are as follows:

Method of storing graph with adjacency matrix:

1. Determine the number of vertices and edges of the graph 2 The input vertex information is stored in the one-dimensional array vertex 3 Initializing adjacency matrix; 4. Input each edge in turn and store it in the adjacency matrix arc

Enter the sequence numbers I, J of the two vertices to which the edge is attached; Set the element value of row I and column J of the adjacency matrix to 1; Set the element value of row J and column I of the adjacency matrix to 1;

Breadth first traversal implementation:

1. Initialize queue Q 2 Access vertex v; visited[v]=1; Vertex v join Q; 3. While (queue q is not empty)

V = queue head element of queue Q out of the queue; W = the first adjacent contact of vertex v while (W exists)

If W is not accessed, access vertex W; visited[w]=1; Vertex W into queue Q

W = next adjacent node of vertex v

The implementation code is as follows:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>