Data Structure: Graph implementation-Adjacent matrix
Implementation of the adjacent matrix of the graph Structure
To represent the Association between vertices in a graph, we can use the adjacent matrix to implement the graph structure. The so-called adjacent matrix is a two-dimensional array that reflects the link between edges. The two-dimensional array is represented by matrix [numV] [numV], where numV is the number of vertices.
For a no-Permission Graph
If an edge exists between vertex Vi and Vj, matrix [Vi] [Vj] = 1; otherwise, matrix [Vi] [Vj] = 0.
Right chart
If the vertex Vi and Vj have edges and the weight is weight, matrix [Vi] [Vj] = weight; otherwise, matrix [Vi] [Vj] = 0 or MAXWEIGHT (obtain the minimum or maximum weight ).
The following is an example.
Class Definition
# Include
# Include
Using namespace std; // maximum weight # define MAXWEIGHT 100 // use the adjacent matrix to implement the Graph class Graph {private: // whether to enable bool isWeighted; // whether bool isDirected is available; // Number of vertices int numV; // Number of edges int numE; // adjacent matrix int ** matrix; public: /* constructor numV indicates the number of vertices, whether isWeighted has a weight value, and whether isDirected has a direction */Graph (int numV, bool isWeighted = false, bool isDirected = false ); // create a graph void createGraph (); // The Destructor ~ Graph (); // get the number of vertices int getVerNums () {return numV;} // get the number of edges int getEdgeNums () {return numE ;} // set the value of the specified edge void setEdgeWeight (int tail, int head, int weight); // print the adjacent matrix void printAdjacentMatrix (); // check the input bool check (int I, int j, int w = 1 );};
Class implementation
/* Constructor numV indicates the number of vertices, whether isWeighted has a weight value, and whether isDirected has a direction */Graph: Graph (int numV, bool isWeighted, bool isDirected) {while (numV <= 0) {cout <The number of input vertices is incorrect !, Re-input; cin> numV;} this-> numV = numV; this-> isWeighted = isWeighted; this-> isDirected = isDirected; matrix = new int * [numV]; // pointer array int I, j; for (I = 0; I <numV; I ++) matrix [I] = new int [numV]; // initialize the image if (! IsWeighted) // No permission graph {// The ownership value is initialized to 0for (I = 0; I <numV; I ++) for (j = 0; j <numV; j ++) matrix [I] [j] = 0;} else // permission graph {// The maximum value of ownership is initialized to (I = 0; I <numV; I ++) for (j = 0; j <numV; j ++) matrix [I] [j] = MAXWEIGHT ;}// create a diagram void Graph: createGraph () {cout <number of input edges; while (cin> numE & numE <0) cout <incorrect input !, Re-input; int I, j, w; if (! IsWeighted) // No permission graph {if (! IsDirected) // undirected graph {cout <start and end of each input edge:; for (int k = 0; k <numE; k ++) {cin> I> j; while (! Check (I, j) {cout <the input edge is incorrect! Re-input; cin> I> j;} matrix [I] [j] = matrix [j] [I] = 1 ;}} else // directed graph {cout <start and end of each input edge:; for (int k = 0; k <numE; k ++) {cin> I> j; while (! Check (I, j) {cout <the input edge is incorrect! Re-input; cin> I >> j;} matrix [I] [j] = 1 ;}} else // you can view the {if (! IsDirected) // undirected graph {cout <start point, end point, and weight of each input edge:; for (int k = 0; k <numE; k ++) {cin> I> j> w; while (! Check (I, j, w) {cout <the input edge is incorrect! Re-input; cin> I> j> w;} matrix [I] [j] = matrix [j] [I] = w ;}} else // directed graph {cout <start point, end point, and weight of each input edge:; for (int k = 0; k <numE; k ++) {cin> I> j> w; while (! Check (I, j, w) {cout <the input edge is incorrect! Re-input; cin> I> j> w;} matrix [I] [j] = w ;}}// destructor Graph ::~ Graph () {int I = 0; for (I = 0; I <numV; I ++) delete [] matrix [I]; delete [] matrix ;} // set the weight of the specified edge void Graph: setEdgeWeight (int tail, int head, int weight) {if (isWeighted) {while (! Check (tail, head, weight) {cout <the input is incorrect. Enter the start, end, and weight of the edge again. cin> tail> head> weight ;} if (isDirected) matrix [tail] [head] = weight; elsematrix [tail] [head] = matrix [head] [tail] = weight;} else {while (! Check (tail, head, 1) {cout <the input is incorrect. Enter the start and end points of the edge again. cin> tail> head;} if (isDirected) matrix [tail] [head] = 1-matrix [tail] [head]; elsematrix [tail] [head] = matrix [head] [tail] = 1-matrix [tail] [head] ;}// input to check bool Graph :: check (int I, int j, int w) {if (I> = 0 & I <numV & j> = 0 & j <numV & w> 0 & w <= MAXWEIGHT) return true; elsereturn false;} // print the adjacent matrix void Graph: printAdjacentMatrix () {int I, j; cout. setf (ios: left); cout <setw (4) <; for (I = 0; I <numV; I ++) cout <setw (4) <I; cout <endl; for (I = 0; I <numV; I ++) {cout <setw (4) <I; for (j = 0; j <numV; j ++) cout <setw (4) <matrix [I] [j]; cout <endl ;}}
Main Function
Int main () {cout <******* use the adjacent matrix to implement the graph structure ***** by David *** <endl; bool isDirected, isWeighted; int numV; cout <graph creation <endl; cout <number of input vertices; cin> numV; cout <whether the edge has a weight value, 0 (without) or 1 (); cin> isWeighted; cout <whether it is a directed Graph, 0 (undirected) or 1 (directed); cin> isDirected; graph Graph (numV, isWeighted, isDirected ); cout <This is one; isDirected? Cout <,: cout <,; isWeighted? Cout <图 <endl: cout <图 <endl; graph. createGraph (); cout <print the adjacent matrix <endl; graph. printAdjacentMatrix (); cout <endl; int tail, head, weight; cout <modify the weight of a specified edge <endl; if (isWeighted) // For the permission graph {cout <start, end, and weight of the input edge; cin> tail> head> weight; graph. setEdgeWeight (tail, head, weight);} else // For the non-Permission graph {cout <start and end of the input edge; cin> tail> head; graph. setEdgeWeight (tail, head, 1) ;}cout <modified successfully! <Endl; cout <print adjacent matrix <endl; graph. printAdjacentMatrix (); system (pause); return 0 ;}
Run
Instance 1
Instance 2
Download complete code: Graph structure implementation: adjacent matrix