Storage form of graphs: adjacency matrix (Array)

Source: Internet
Author: User

adjacency Matrix : Information that stores data elements (vertices) and the relationships between data elements (edges or arcs) with two arrays.

Consider, for example, the following map:

If the adjacency matrix is stored, it can be expressed as:

1. Vertex array:

2. Adjacency Matrix:

Traversal of graphs:

Depth First (DFS):

The depth-first search traversal is similar to the first root traversal of a tree, which is the generalization of the tree's first root traversal. Assuming that the initial state is not accessed by all vertices in the graph, the depth-first search can be set off from a vertex v in the graph, access this vertex, and then proceed from the inaccessible adjacency point of V to the depth first to traverse the graph, until all the vertices in the diagram and V have paths are accessed; The previous procedure is repeated until all vertices are accessed, as the starting point of the unreachable vertex in the alternate diagram.

Upper image Depth First traversal result: Abcdfeghi

Breadth First (BFS):

Breadth-First search is similar to a hierarchical traversal of trees. Assuming that from a vertex v in the diagram, after the V is accessed, each of the inaccessible adjacent points of V is accessed sequentially, and then the neighboring points are accessed from the neighboring points, and then the adjacency points of the first accessed vertices are accessed before the adjacent points of the vertex being accessed. The adjacent points of all the vertices that have been accessed in the diagram are accessed. If a vertex is still not reachable at this time, the remaining vertex in the alternate diagram as the starting point repeats the process until all vertices are accessed.

Breadth-First traversal results above: ABGCEHIDF

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.