Write an algorithm step by step (Save the graph)

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]

 

 

 

 

In the previous blogs, we have made basic definitions of graphs and introduced how to create, add, and delete graphs. Today, let's talk about how a graph is stored in peripherals. These external devices can be of various types, such as hard disks, SD cards, and network hard disks. Essentially, the topic we are discussing today is how to keep the graph data locally permanently. In addition, if you need to load the data, you can quickly restore the original image. If you have forgotten the graph data structure, you can review the code and review our previous definition method.

 

 

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

 

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH;

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

Www.2cto.com

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH; there are many pointers in the data structure. So how should I save it in external storage? For external storage, pointers have no significance. You will understand it when you think about it. In fact, we can regard all these pointers as offset values. GRAPH is composed of many vertices. The vertex structure has many edge connections, so we can save the data in the following order.

 

 

/*

*---------------------------------------------------------------------------

* | GRAPH | vectex1 | vectex2 | ...... | vectex n | LINE1 | ...... | LINE n |

*---------------------------------------------------------------------------

*/

/*

*---------------------------------------------------------------------------

* | GRAPH | vectex1 | vectex2 | ...... | vectex n | LINE1 | ...... | LINE n |

*---------------------------------------------------------------------------

*/How to arrange the offset value?

 

A) GRAPH Structure

 

 

Head is the offset address of the first vectex.

 

 

 

 

B) VECTEX Structure

 

Neighbor records the offset position of the first edge, and next records the Offset Value of the next node.

 

 

 

 

C) LINE Structure

 

Next is the Offset Value of the next edge.

 

 

 

 

 

However, if we make some optimizations, we need to save less data. For example, if the first vectex node is behind the GRAPH node, the head does not need to be saved. In the vectex structure, because we need to know the LINE offset value, therefore, the offset of neighbor needs to be known, but next is no longer needed, because the vectex data itself is tied together; finally, because we need to put all the edges belonging to the same vectex together, the next data in the LINE structure can be omitted. Therefore, the modified data structure should be like this:

 

 

Typedef struct _ LINE

{

Int end;

Int weight;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

} VECTEX;

 

Typedef struct _ GRAPH

{

Int count;

} GRAPH;

Typedef struct _ LINE

{

Int end;

Int weight;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

} VECTEX;

 

Typedef struct _ GRAPH

{

Int count;

} GRAPH;

With the above data structure, loading data from the outer layer is a reverse operation, and it is no longer complicated.

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.