Write algorithms step by step (graph structure)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

Graphs are an important chapter in the data structure. Through the graph, we can determine whether two points are connected. Through the graph, we can also calculate the minimum distance between two points. Through the graph, we can also according to different requirements, find different appropriate paths. Of course, sometimes we need to abstract the minimum spanning tree for the sake of computing needs, so that we do not need to constantly judge whether a circular node is encountered during traversal computing. Of course, all of this starts with the representation of the graph.

 

1) Matrix Representation

 

Matrix Representation is the simplest representation. If a graph has five vertices, we can build a 5*5 matrix. If there is a connection between the vertex and the vertex, fill in 1; otherwise, if there is no connection, it can be expressed with 0. Of course, the point above the diagonal line is meaningless. As shown in:

 

 

Static int graph [5] [5] =

{

{0, 1, 0, 1, 1 },

{1, 0, 1, 0, 1 },

{0, 1, 0, 1, 0 },

{1, 0, 1, 0, 1 },

{1, 1, 0, 1, 0}

};

Static int graph [5] [5] =

{

{0, 1, 0, 1, 1 },

{1, 0, 1, 0, 1 },

{0, 1, 0, 1, 0 },

{1, 0, 1, 0, 1 },

{1, 1, 0, 1, 0}

}; If there is still a direction between the vertex and the vertex, they are asymmetrical about the (x, x) symmetry axis, so the result may also be like this:

 

 

Static int graph [5] [5] =

{

{0, 0, 0, 0, 0 },

{1, 0, 0, 0, 0 },

{0, 1, 0, 0, 0 },

{1, 0, 1, 0, 0 },

{1, 1, 0, 1, 0}

};

Static int graph [5] [5] =

{

{0, 0, 0, 0, 0 },

{1, 0, 0, 0, 0 },

{0, 1, 0, 0, 0 },

{1, 0, 1, 0, 0 },

{1, 1, 0, 1, 0}

}; Of course, if the relationship between the vertex and the vertex has a certain weight, for example, distance, we can use it to replace the original data 1:

 

 

Static int graph [5] [5] =

{

{0, 0, 0, 0, 0 },

{3, 0, 0, 0, 0 },

{0, 6, 0, 0, 0 },

{8, 0, 4, 0, 0 },

{9, 2, 0, 7, 0}

};

Static int graph [5] [5] =

{

{0, 0, 0, 0, 0 },

{3, 0, 0, 0, 0 },

{0, 6, 0, 0, 0 },

{8, 0, 4, 0, 0 },

{9, 2, 0, 7, 0}

};

A matrix indicates that the graph structure is very intuitive. However, a matrix is a waste of space. This is because there are only five vertices in the example here. But imagine how much space 10000*10000 would be if there are 10000 nodes on a graph. The important thing is that most of the above 10000x10000 points are 0, so the waste of space is considerable.

 

 

 

 

2) array structure

 

To change the space waste of matrices, we can create a data space consisting of vertices and edges. For example, we define a structure like this:

 

 

Typedef struct _ LINE

{

Int start;

Int end;

Int weight;

Int isDirection;

} LINE;

Typedef struct _ LINE

{

Int start;

Int end;

Int weight;

Int isDirection;

} LINE; the data structure defined above is very concise. 1st are start vertices, 2nd are endpoints, 3rd are weights, and 4th are used to determine whether the current edge is directed. In the figure, we need to define the number of such data sides. If we put the data of these edges together to form an array, we can use this array to represent all the information of the graph.

 

However, we still feel sorry. This data structure overemphasizes the meaning and importance of edges and ignores the vertices themselves. This is because when we emphasize edges, we should add relevant features to the vertex. Without the support of vertices, simple edge information has no meaning.

 

 

 

 

3) Graph Representation Based on vertex linked list

 

First, we define the basic structure of the vertex:

 

 

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

} VECTEX;

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

} VECTEX; we use VECTEX to record vertex information. LINE indicates node information. If LINE is a variable in VECTEX, neighbor indicates that the starting point of all nodes is the start point. If it is a variable in the PATH, the starting point of next is the previous vertex of the LINE link. Do you know if I have made it clear? The following is the definition of PATH between a vertex and a vertex.

 

 

Typedef struct _ PATH

{

Int start;

Int end;

Int lenth;

LINE * next;

} PATH;

Typedef struct _ PATH

{

Int start;

Int end;

Int lenth;

LINE * next;

} PATH; where start is the starting point, end is the ending point, next is the next point of the start link, and lenth is the total length of the PATH. Of course, you can change it to another weight form.

 

Note:

 

1) arrays and linked lists are the basis of the graph structure.

 

2) each data structure has its own application scenarios. The key is to understand the ideas and methods.

 

3) graph representation is the basis of graph operations. Understanding them is the basic condition for further learning.

Related Article

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.