My path to the soft test (v)--data structure and algorithm (3) diagram

Source: Internet
Author: User

the graph is a nonlinear structure as well as a tree. I look a little complicated. In fact, it's very easy. The tree has a hierarchical relationship, and the upper element is able to connect to the next number of elements. But only one element of the upper layer can be connected. In the graph structure, the connection between nodes is arbitrary, regardless of what element can be connected with other elements.

The diagram is relatively easy, we only introduce the graph of the traversal and minimal spanning tree. Now we start.


Traverse


1. Concept

Starting at one vertex, visit each node in the diagram and ask for access only once and not repeatedly.

2. Methods

(1) Breadth-first traversal

Basic idea : first access to the vertex. Again, visit all the unnamed neighbors of the vertex, and then visit all the nodes of the neighboring node (similar to the tree's hierarchy traversal ).

Breadth-First traversal: V1,V2,V3,V4. V5,v6 or V1. V4. V3,v2,v6,v5

(2) Depth-first traversal

Basic idea : first visit the Vertex, then visit each node of the vertex, continue the depth-first traversal from that point ( similar to the tree's pre-sequence traversal )

Depth-First traversal:v1,v2,v5,v3,v6,v4 or v1,v4. V6. V3,v5,v2


Summarize. The results of the breadth-first traversal and depth-first traversal of graphs are not unique.


Minimum spanning tree

(1) Primm (Prim) algorithm
Basic idea : Choose a vertex to start with. Find a vertex adjacent to the vertex with the lowest cost (edge value), until the end.


For example: V1 as the vertex, v1->v3->v6->v4,v3->v2->v5, the connection diagram of all the nodes can be.
(2) Kruskal (Kruskal) algorithm
Basic idea : Select the smallest edge in the graph until all nodes are connected.
For example: The first small side: V1->v3, second small side: V4->v6. Third small side: V2-v5. The small side: v3->v6, five small side: V3->v2, at this time all the nodes are connected together.
(3) algorithm comparison
The Primm algorithm pays more attention to the node, the shortest distance between points and the priority; the Kruskal algorithm is more focused on edges, sorting edges, and minimizing edges in front. The largest side is at the back.


Summarize

because the content of the graph is relatively simple, the content of our narration is relatively small. Just be a better one. The following sorting and algorithms are our focus. The post is immediately killed.



Maybe the blog update list, please look forward to.

My path to the soft test (i)--Opening ( updated )

My road of Soft Examination (ii)--J2SE macro Summary ( updated )

My path to the soft test (iii)--Data structure and algorithm (1) Linear table ( updated )

My path to the soft test (IV.)--Data structure and algorithm (2) tree with two fork tree ( updated )

My path to the soft test (v)--data structure and algorithm (3) diagram ( updated )

My path to the soft test (vi)--Data structure and algorithm (4) eight sorts ( updated )

My path to the soft test (vii)--Data structure and algorithm (5) Find ( updated )


My path to the soft test (v)--data structure and algorithm (3) diagram

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.