Data Structure: Graph implementation-adjacent table
Graph structure using an adjacent table
When the number of edges in the graph is small, using the adjacent table to implement the graph structure will waste a lot of memory space. Therefore, consider another method to implement the graph structure: the adjacent table. There are two types of node structures in the adjacent table:
Vertex Node
Edge node
View Code directly
Class Definition
# Include
# Include
Using namespace std; // maximum weight # define MAXWEIGHT 100 // edge node typedef struct edgenode_tag {int adjvex; // adjacent vertex int weight; // edgenode_tag * next;} EdgeNode; // vertex node typedef struct vertex_tag {int Vertex; // vertex EdgeNode * next;} Vertex; class Graph {private: // whether bool isWeighted is authorized; // whether bool isDirected is available; // Number of vertices int numV; // Number of edges int numE; // The adjacent table Vertex * adjList; public: /* constructor numV indicates the number of vertices, whether isWeighted has a weight value, and whether isDirected has a direction */Graph (int nu MV, bool isWeighted = false, bool isDirected = false); // create a diagram void createGraph (); // destructor ~ Graph (); // get the number of vertices int getVerNums () {return numV;} // get the number of edges int getEdgeNums () {return numE ;} // insert edge void insertEdge (int tail, int head, int weight = 1); void insertedge (int tail, int head, int weight ); // set the value of the specified edge void setEdgeWeight (int tail, int head, int weight); // print the adjacent table void printAdjacentList (); // check the input bool check (int tail, int head, int weight = 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; adjList = new Vertex [numV]; // pointer array for (int I = 0; I <numV; I ++) {adjList [I]. vertex = I; adjList [I]. next = NULL ;}// create a Graph 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 {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;} insertEdge (I, j );}} else // right chart {cout <enter the start point, end point, and weight of each 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;} insertEdge (I, j, w) ;}}// destructor Graph ::~ Graph () {int I; EdgeNode * p, * q; for (I = 0; I <numV; I ++) {if (adjList [I]. next) {p = adjList [I]. next; while (p) {q = p-> next; delete p; p = q ;}} delete [] adjList ;} // set the weight of the specified edge void Graph: setEdgeWeight (int tail, int head, int weight) {if (! IsWeighted) // No permission graph {while (! Check (tail, head) {cout <the input edge is incorrect! Re-input; cin> tail> head;} insertEdge (tail, head);} else // You Have The Right To figure {while (! Check (tail, head, weight) {cout <the input edge is incorrect! Re-input; cin> tail> head> weight;} insertEdge (tail, head, weight) ;}// insert edge void Graph: insertEdge (int vertex, int adjvex, int weight) {insertedge (vertex, adjvex, weight); if (! IsDirected) // undirected Graph insertedge (adjvex, vertex, weight);} void Graph: insertedge (int vertex, int adjvex, int weight) {EdgeNode * p, * q, * r; p = q = r = NULL; if (adjList [vertex]. next) // not the first node {p = adjList [vertex]. next; // move p to the End Node while (p & (p-> adjvex <adjvex) {q = p; p = p-> next ;} if (p & (p-> adjvex = adjvex) // modify the existing Edge weight p-> weight = weight; else {r = new EdgeNode; r-> adjvex = adjvex; r-> weight = weight; r-> next = p; q-> next = r ;}} else {p = new EdgeNode; p-> adjvex = adjvex; p-> weight = weight; p-> next = NULL; adjList [vertex]. next = p ;}} // input check bool Graph: check (int tail, int head, int weight) {if (tail> = 0 & tail <numV & head> = 0 & head <numV & weight> 0 & weight <= MAXWEIGHT) return true; elsereturn false;} // print the adjacent table void Graph: printAdjacentList () {int I; EdgeNode * edge = NULL; for (I = 0; I <numV; I ++) {edge = adjList [I]. next; while (edge) {cout <w (<I <, <edge-> adjvex <) = <edge-> weight <; edge = edge-> next;} cout <endl ;}}
Main Function
Int main (void) {cout <****** use an adjacent table 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 <indicates whether the edge has a weight value, 0 (not included) or 1 (band); cin> isWeighted; cout <whether it is a directed Graph, 0 (undirected) or 1 (directed); cin> isDirected; graph Graph (numV, isWeighted, isDirected); cout <This Is A; isDirected? Cout <,: cout <,; isWeighted? Cout <图 <endl: cout <图 <endl; graph. createGraph (); cout <print the adjacent table <endl; graph. printAdjacentList (); 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. printAdjacentList (); system (pause); return 0 ;}
Run