[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.