Data Structure: Graph implementation-adjacent table, data structure-adjacent table
Graph implementation: 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 <iostream> # include <iomanip> 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 is the number of vertices, isWeighted Whether the request has a weight value and whether isDirected has a direction */Graph (int numV, bool isWeighted = false, bool isDirected = false); // create a 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 !, Enter "; cin> numV;} this-> numV = numV; this-> isWeighted = isWeighted; this-> isDirected = isDirected; // The number of edges is initialized to 0 numE = 0; 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 () {// use a new variable to represent the number of edges. the numE modification is left to insertedge () int numEdge = 0; cout <"Number of input edges"; while (cin> numEdge & numEdge <0) cout <"incorrect input !, Enter "; int I, j, w; if (! IsWeighted) // No permission graph {cout <"Enter the start and end points of each edge: \ n"; for (int k = 0; k <numEdge; k ++) {cin> I> j; while (! Check (I, j) {cout <"the input edge is incorrect! Re-input \ n "; cin> I> j;} insertEdge (I, j );}} else // right chart {cout <"Enter the start point, end point, and weight of each edge: \ n"; for (int k = 0; k <numEdge; k ++) {cin> I> j> w; while (! Check (I, j, w) {cout <"the input edge is incorrect! Re-input \ n "; 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-enter \ n "; cin> tail> head;} insertEdge (tail, head);} else // The right graph {while (! Check (tail, head, weight) {cout <"the input edge is incorrect! Re-enter \ n "; 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 proper position 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; // when the new node is in the first position of the table, if (adjList [vertex]. next = p) adjList [vertex]. next = r; elseq-> next = r; numE ++ ;}} else {p = new EdgeNode; p-> adjvex = adjvex; p-> weight = weight; p-> next = NULL; adjList [vertex]. next = p; numE ++; }}// input the 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 voi D Graph: printAdjacentList () {int I; EdgeNode * edge = NULL; for (I = 0; I <numV; I ++) {edge = adjList [I]. next; if (edge) // Why do I add an if statement? It depends on the line break. If a vertex does not have any adjacent contact (boundless), a row of {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" <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 graph (numV, isWeighted, isDirected); cout <"this is a"; isDirected? Cout <",": cout <","; isWeighted? Cout <" 图" <endl: cout <" 图" <endl; graph. createGraph (); cout <"Print 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 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 Success! "<Endl; cout <" Print adjacent matrix "<endl; graph. printAdjacentList (); system (" pause "); return 0 ;}
Run
Adjacent table
Download the complete code: Graph implementation: adjacent table
Reprint please indicate the source, this article address: http://blog.csdn.net/zhangxiangdavaid/article/details/38323593
If this is helpful, try again!
Column Directory: Data Structure and algorithm directory
Data Structure: Implementation of the graph's adjacent table
I sent it to your mailbox. Give it a score !!!!!
Data Structure: Implementation of the graph's adjacent table
Sent to your mailbox