Graph operations and L-Adjacent matrix Storage

Source: Internet
Author: User
/*


1. A graph is a data structure composed of the vertex set and the relationship set between the vertex. Graph definition: g = (V, E)
2. vertices and edges: nodes in a graph are generally called vertices, and the links between vertices are called
3. Full graph: In an undirected graph of N vertices, if n (n-1)/2 edges exist, that is, two vertices have only one edge.
A completely undirected graph. If n (n-1) edges exist in the Right Direction graph of N vertices, that is, two edges exist between two vertices and have only two edges in the opposite direction,
This is called a directed full graph.
4. Degree of vertex: Degree of vertex = degree of exit + degree of entry
5. Connected Graph and strongly connected graph:
In an undirected graph, if a path exists between the vertex VI and the vertex VJ, the vertex VI is connected to the vertex VJ. If any pair of vertices in the graph are connected
This graph is a connected graph.
In a directed graph, if any pair of vertex VI and vertex VJ (Vi! = VJ) If all paths exist, graph G is a strongly connected graph.
6. Spanning Tree: The smallest connected subgraph of a connected graph is called the Spanning Tree of the graph. The spanning tree of a connected graph with n vertices has n vertices and n-1-1 edges.
7. simple path and loop: If the vertices V1, V2, and V3 .... VM. This path is called a simple path. If the first vertex of the path is V1 and the last vertex
VM coincidence, such a path is a loop or loop
8. The graph storage structure mainly refers to the graph edge storage structure. The graph storage structure mainly includes two types: the adjacent matrix and the adjacent table.
When the number of vertices in the graph is small and the number of edges is large, the efficiency of using the graph's Adjacent matrix storage structure is high, that is, it is best to use two-dimensional array storage for the dense matrix; when the number of vertices in the graph is large and the number of edges is far smaller
When the number of edges of a complete graph with the same vertex is used, the storage structure of the adjacent street table of the graph is highly efficient, that is, the sparse matrix is used. It is best to use a triple linked list with a row pointer array structure.


*/


// Implementation of the operations on the storage structure of the adjacent matrix # include <stdio. h> # define maxsize 100 // define the size of the element typedef char datatype; // define a type # define maxvertices 10 // define the maximum vertex # define maxweight 10000 // define the specific value of infinity typedef struct {// define a struct datatype list [maxsize]; int size; // struct element size} seqlist; // struct object typedef struct {seqlist vertices; // sequence table for storing vertices int edge [maxvertices] [maxvertices]; // The adjacent matrix int numofedges for storing edges; // Number of edges} adjmgraph; typedef struct {int row; // The row subscript int Co L; // column subscript int weight; // weight} rowcolweight; // edge information structure // initialize void initiate (seqlist * l) {L-> size = 0; // define the number of initialization elements} // calculate the number of current elements int getlength (seqlist L) {return L. size; // return length} // insert data element int insertdata (seqlist * l, int I, datatype X) {// Insert the data element x before the position I (0 <= I <= size) of the sequence table L // If the insertion is successful, 1 is returned. If the output fails, 0 Int J is returned; if (L-> size> = maxsize) {printf ("the sequence table is full and cannot be inserted !! \ N "); Return 0;} else if (I <0 | I> L-> size) {printf (" the inserted position is invalid and is not in the specified range, the parameter I is invalid! \ N "); Return 0;} else {// move data from the back to the forward in a consistent manner to prepare for insertion (j = L-> size; j> I; j --) {L-> list [J] = L-> list [J-1];} l-> list [I] = x; L-> size ++; return 1 ;}} // Delete int deletedata (seqlist * l, int I, datatype * X) {// Delete the data in the sequence table where I is located. I> = 0 & I <= size-1. Save the data to X. // If deletion is successful, 1 is returned, otherwise, the return value is 0int J; If (L-> size <= 0) {printf ("the sequence table is empty and no data elements can be deleted! \ N "); Return 0;} else if (I <0 | I> L-> size-1) {printf (" parameter I is invalid and cannot be deleted! \ N "); Return 0;} else {* x = L-> list [I]; for (j = I + 1; j <= L-> size-1; j ++) {// forward from the previous one L-> list [J-1] = L-> list [J];} l-> size --; // subtract one from the data element return 1 ;}// retrieve the data element int getdata (seqlist L, int I, datatype * X) {if (I <0 | I> L. size-1) {printf ("parameter I is invalid and cannot be deleted! \ N "); Return 0;} else {* x = L. list [I]; return 1 ;}/// initialize the sequence table with n vertices and the adjacent matrix void initiateg (adjmgraph * g, int N) {// initialize int I, j; for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) {if (I = J) {G-> edge [I] [J] = 0;} else {G-> edge [I] [J] = maxweight; // maxweight indicates infinity }}g-> numofedges = 0; // set the number of edges to 0 initiate (& G-> vertices ); // sequence table initialization} // insert vertex void insertvertex (adjmgraph * g, datatype vertex) {// insert vertex vertexinsertdata (& G-> vertices, g-> vertices. size, vertex); // sequence table End insert} // Insert the edge void insertedge (adjmgraph * g, int V1, int V2, int weight) {// Insert the edge <V1, V2>, and edge <V1, v2> permission: weightif (V1 <0 | V1> = G-> vertices. size | V2 <0 | V2> = G-> vertices. size) {printf ("An error occurred while out-of-bounds V1 or V2 parameter !!! \ N "); return;} G-> edge [V1] [V2] = weight; printf (" (% d )", g-> edge [V1] [V2]); G-> numofedges ++;} // delete an edge void deleteedge (adjmgraph * g, int V1, int V2) {// delete an edge <V1, V2> If (V1 <0 | V1> = G-> vertices. size | V2 <0 | V2> = G-> vertices. size) {printf ("An error occurred while out-of-bounds V1 or V2 parameter !!! \ N "); return;} If (G-> edge [V1] [V2] = maxweight | V1 = V2) {printf (" this edge does not exist !!! \ N "); return;} G-> edge [V1] [V2] = maxweight; G-> numofedges --;} // obtain the first adjacent vertex int getfirstvex (adjmgraph g, int V) {// search for the first adjacent vertex of the vertex whose serial number is V in graph G // if such vertex exists, return the serial number of the adjacent vertex; otherwise, return-1int Col; if (v <0 | V> = G. vertices. size) {printf ("An error occurred while out-of-bounds V1 parameter !!! \ N "); Return-1 ;}for (COL = 0; Col <G. vertices. size; Col ++) {If (G. edge [v] [col]> 0 & G. edge [v] [col] <maxweight) {return Col ;}} return-1 ;}// obtain the next adjacent vertex int getnextvex (adjmgraph g, int V1, int V2) {// in the graph, find the next adjacent vertex of the adjacent vertex V2 of the V1 vertex. // if such an adjacent vertex exists, the serial number of the adjacent vertex is returned; otherwise,-1 // The numbers of vertices V1 and V2 are int col. If (V1 <0 | V1> G. vertices. size | V2 <0 | V2> = G. vertices. size) {printf ("An error occurred while out-of-bounds V1 or V2 parameter !!! \ N "); Return-1 ;}for (COL = V2 + 1; Col <G. vertices. size; Col ++) {If (G. edge [V1] [col]> 0 & G. edge [V1] [col] <maxweight) {return Col;} return-1;} void creatgraph (adjmgraph * g, datatype V [], int N, rowcolweight E [], int e) {// insert n vertex information in the graph V and e edge information Eint I, K; initiateg (G, N ); // D vertex sequence table initialization for (I = 0; I <n; I ++) {insertvertex (G, V [I]); // insert vertex} For (k = 0; k <E; k ++) {printf ("% d", E [K]. weight); insertedge (G, E [K]. row, E [K]. col, E [K]. weight); // insert edge} void main () {adjmgraph G1; datatype A [] = {'A', 'B', 'C', 'D ', 'E'}; rowcolweight RCW [] = {, 10}, {, 20}, {, 30}, {, 40, 50 }}; int n = 5, E = 5; int I, j; creatgraph (& G1, A, N, RCW, e ); // create a graph deleteedge (& G1,); // delete an edge <> printf ("vertex set:"); for (I = 0; I <g1.vertices. size; I ++) {printf ("% C", g1.vertices. list [I]);} printf ("\ n"); printf ("weight set: \ n"); for (I = 0; I <g1.vertices. size; I ++) {for (j = 0; j <g1.vertices. size; j ++) {printf ("% 5d", g1.edge [I] [J]);} printf ("\ n ");}}




















































Graph operations and L-Adjacent matrix Storage

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.