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 <iostream> # include <iomanip> using namespace STD; // maximum weight # define maxweight 100 // graph class graph {private using an adjacent matrix: // whether bool isweighted is authorized; // whether bool isdirected is available; // Number of vertices int numv; // Number of edges int nume; // The 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 !, Enter "; 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 !, Enter "; int I, j, W; If (! Isweighted) // No permission graph {If (! Isdirected) // undirected graph {cout <"Enter the start and end points of each edge: \ n"; for (int K = 0; k <nume; k ++) {CIN> I> J; while (! Check (I, j) {cout <"the input edge is incorrect! Re-input \ n "; CIN> I> J;} matrix [I] [J] = matrix [J] [I] = 1 ;}} else // directed graph {cout <"input start and end of each edge: \ n"; for (int K = 0; k <nume; k ++) {CIN> I> J; while (! Check (I, j) {cout <"the input edge is incorrect! Re-enter \ n "; CIN> I >> J;} matrix [I] [J] = 1 ;}} else // you can view the {If (! Isdirected) // undirected graph {cout <"Enter the start, end, and weight of each edge: \ n"; for (int K = 0; k <nume; k ++) {CIN> I> j> W; while (! Check (I, j, W) {cout <"the input edge is incorrect! Re-input \ n "; CIN> I> j> W;} matrix [I] [J] = matrix [J] [I] = W ;}} else // directed graph {cout <"input start, end, and weight of each edge: \ n"; for (int K = 0; k <nume; k ++) {CIN> I> j> W; while (! Check (I, j, W) {cout <"the input edge is incorrect! Re-input \ n "; 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 <"incorrect input. re-enter the start, end, and weight of the edge "; cin> tail> head> weight;} If (isdirected) matrix [tail] [head] = weight; elsematrix [tail] [head] = matrix [head] [tail] = weight;} else {While (! Check (tail, Head, 1) {cout <"incorrect input, re-enter the start and end points of the edge"; 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" <Endl; cout <"Number of input vertices"; CIN> numv; cout <"indicates whether an edge has a weight, 0 (without) or 1 (with) "; CIN> isweighted; cout <" whether it is a directed graph, 0 (undirected) or 1 (directed )"; cin> isdirected; graph (numv, isweighted, isdirected); cout <"this is a"; isdirected? Cout <",": cout <","; isweighted? Cout <" 图" <Endl: cout <" 图" <Endl; graph. creategraph (); cout <"Print 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 Point, end point, and weight of the input edge"; CIN> tail> head> weight; graph. setedgeweight (tail, Head, weight);} else // For the Untitled 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

Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/38321327

If this is helpful, try again!

Column Directory: Data Structure and algorithm directory