[Library] C implementation diagram (array representation)

Source: Internet
Author: User

--------------------------- Graph. h --------------------

/* The simplest graph implementation provides only the most basic interfaces
* An undirected graph stored in an array. This Code uses the bool type added to c99.
* We recommend that you use Dev C ++ 4.9.9.0 to compile the code */
# Ifndef graph_h
# Define graph_h
/* Function return status */
# Ifndef OK
# Define OK 1/* succeeded */
# Define err-1/* failed */
# Endif
/* Define the maximum and minimum values that may be used in a function */
# Ifndef Max
# Deprecision max 100
# Define min 0
# Endif
/**/
Typedef int status;/* function return status */
Typedef int elemtype;/* the user changes the data type as needed */
Typedef struct {
Elemtype date;/* graph vertex Storage Data */
Int arcn;/* vertex degree */
} Date;/* vertex of the graph */
Typedef bool adjmatrix [Max] [Max];/* graph relational matrix type */
Typedef struct {
Date VEC [Max];/* store the vertex vector */
Adjmatrix arcs;/* graph relationship matrix */
Int node_number, arc_number;/* Number of vertices and edges */
} Graph;/* define a graph type */
Status creategraph (graph * gptr);/* Create a graph */
Int find_gnode (const graph *, const elemtype);/* Find a vertex */
Status print_graph (const graph * gptr);/* print */
# Endif

--------------------- Graph. c -------------------------------------------

# Include <stdio. h>
# Include "graph. H"
Status creategraph (graph * gptr)/* Create a graph */
{
Int I, J, K;
If (gptr = NULL)/* if the pointer is null */
Return err;
Printf ("Enter the number of vertices :");
Scanf ("% d", & gptr-> node_number );
Printf ("Enter the number of edges :");
Scanf ("% d", & gptr-> arc_number );
Printf ("Enter % d Data:", gptr-> node_number );
For (I = 0; I <gptr-> node_number; ++ I)
{/* Initialize and set the data vector */
Scanf ("% d", & gptr-> VEC [I]. date );
Gptr-> VEC [I]. arcn = 0;
}
For (I = 0; I <gptr-> node_number; ++ I)
For (j = 0; j <gptr-> node_number; ++ J)
Gptr-> arcs [I] [J] = false;/* initialize the relational matrix */
For (k = 0; k <gptr-> arc_number; ++ K)
{/* Design relationship matrix */
Elemtype A, B;
Printf ("Enter data of adjacent nodes :");
Scanf ("% d", & A, & B );
I = find_gnode (gptr, );
J = find_gnode (gptr, B );
If (I <0) | (j <0 ))
Return err;
Gptr-> arcs [I] [J] = gptr-> arcs [J] [I] = true;
}
For (I = 0; I <gptr-> node_number; ++ I)
For (j = 0; j <gptr-> node_number; ++ J)
Gptr-> VEC [I]. arcn + = gptr-> arcs [I] [J];
/* Calculate the degree of each vertex */
Return OK;
}
Int find_gnode (const graph * gptr, const elemtype date)
{/* Find vertex */
Int I;
If (gptr = NULL)/* if the pointer is null, a negative value is returned */
Return-1;
For (I = 0; I <gptr-> node_number; ++ I)
If (date = gptr-> VEC [I]. Date)
Return I;
Return-1;/* if no value is found, a negative value is returned */
}
Status print_graph (const graph * gptr)/* print */
{
Int I, J;
If (gptr = NULL)
Return err;
Printf ("data stored in the figure (node level) is as follows:/N ");
For (I = 0; I <gptr-> node_number; ++ I)
Printf ("% d (% d)/T", gptr-> VEC [I]. Date, gptr-> VEC [I]. arcn );
Printf ("/N ");
Printf ("Graph Relationship Matrix:/N ");
For (I = 0; I <gptr-> node_number; ++ I)
{For (j = 0; j <gptr-> node_number; ++ J)
Printf ("% d/T", gptr-> arcs [I] [J]);
Printf ("/N ");
}
Return OK;
}

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.