1. The concept of graphs
A graph is a data structure consisting of a set of vertices and the relationship of vertices.
a complete graph: a n-1 graph consisting of n vertices, if n (a)/2 edges, is called a complete graph of non-direction graphs.
In a direction graph composed of n vertices, if there is n (n-1) edges, it is called a forward complete graph.
B Right: in some graphs, the edge has a value associated with it, called the weight. Weights can represent distances from one vertex to another, costs, and so on. This weighted graph is also called a network.
c) Degree: the number of edges associated with vertex v is called degrees. The degree of vertex in a graph is equal to the sum of its degree. An entry is an edge that ends with a node, and the degree is an edge with a node as the starting point.
d) Connected graph:
In graph theory, connected graphs are based on the concept of connectedness. In a undirected graph G, if a path is connected from the Vertex VI to the vertex VJ (of course there must be a path from VJ to VI), it is said that VI and VJ are connected. If G is a direction graph, then all sides of the path connecting VI and VJ must be in the same direction. If any two points in the graph are connected, then the graph is called a connected graph. If the graph is a directional graph, it is called a strong-connected graph (Note: You need to have paths in both directions).
e) Connected components: Connected components: A maximal connected subgraph of undirected graph G is called a connected component (or connected branch) of G. A connected graph has only one connected component, i.e. its own; a disconnected undirected graph has multiple connected components. 2. Adjacency Matrix:
The so-called adjacency matrix (adjacency matrix) storage structure is to use the one-dimensional array to store the vertex information in the graph, using the matrix to represent the adjacency relationship between the vertices. Assuming that the graph g= (v,e) has n definite vertices, that is, the V={v0,v1,..., vn-1}, it means that the adjacent relation of each vertex in G is a nxn matrix, and the elements of the matrix are:
The
Representations of the adjacency matrix are shown in Figure 8.7:
3. Code implementation:
Graphmtx.h
#ifndef graphmtx_h #define GRAPHMTX_H #include <Graph.h> #include <iostream> #include <stdlib.h> using
namespace Std;
Template <class T,class e> class Graphmtx {public:e maxweight;
/** Default Constructor */graphmtx (int sz=defaultvertices) {maxvertices=sz;
numvertices=0;
numedges=0;
Verticeslist =new T[maxvertices];
Edge = (E * * *) new E *[maxvertices];
Initializes the adjacency matrix for (int i=0; i<maxvertices; i++) edge[i]=new e[maxvertices]; for (int i=0; i<maxvertices; i++) for (int j=0; j<maxvertices; j + +) Edge[i][j]=i==j?0:maxw
eight;
}/** Default destructor */~graphmtx () {delete []verticeslist;
Delete Edge;
///current fixed-point number int numberofvertices () {return numvertices;
//Returns the current number of edges int numberofedges () {return numedges; //Gets the value of the vertex T getValue (int i) {return i>=0&&i<numvertices?
Verticeslist[i]:null; //Get the weight on the edge E getweight (int v1,int v2) {return-1!=v1&&-1!=v2?
edge[v1][v2]:0; //Gets the first contiguous vertex int getfirstneighbor (int v) {if ( -1!=v) {for (int) {col=0; col< of Vertex v) Numvertices;
col++) if (edge[v][col]>0&&edge[v][col]<maxweight) return col;
} return-1; }//Get the next contiguous vertex int Getnextneighbor (int v,int w) {if ( -1!=v&&-1!=w) for (int c) of the adjacent vertex W of V ol=w+1; col<maxvertices;
col++) if (edge[v][col]>0&&edge[v][col]<maxweight) return col;
return-1; }//Insert vertex bool Insertvertex (const T &vertex) {if (numvertices==maxvertices) return Fals
E
Verticeslist[numvertices++]=vertex;
return true; }//Insert edge bool Insertedge (int v1,int v2,const E cost) {IF (v1>-1&&v1<numvertices&&v2>-1&&v2<numvertices&&edge[v1][v2]==
Maxweight) {edge[v1][v2]=edge[v2][v1]=cost;
numedges++;
return true;
return false; //delete vertex V and all associated side bool Removevertex (int v) {//v do not delete if (v<0| |
V>numvertices) return false;
Only one vertex is left, and if (1==numvertices) {return false is not deleted;
} Verticeslist[v]=verticeslist[numvertices-1];
Subtract the side for (int i=0; i<numvertices; i++) if (edge[v][i]>0&&edge[v][i]<maxweight) associated with V
numedges--;
Fill in the last column for the first for (int i=0; i<numvertices; i++) edge[i][v]=edge[i][numvertices-1];
numvertices--;
Use the last line to fill the first V line for (int i=0; i<numvertices; i++) edge[v][i]=edge[numvertices-1][i];
return true; }//delete edge (V1,V2) boolRemoveedge (int v1,int v2) {if (v1>-1&&v1<numvertices&&v2>-1&&v2<numvertic Es&&edge[v1][v2]>0&&edge[v1][v2]<maxweight) {Edge[v1][v2]=edge[v2][v1]=maxweigh
T
numedges--;
return true;
return false; Template <class v,class w> friend istream& operator >> (IStream & in,graphmtx<v,w> ;
G);
Template <class v,class w> friend ostream& operator<< (ostream &out,Graphmtx<V,W> &g);
Private://MAX vertex tree int maxvertices;
Current number of edges int numedges;
The current vertex tree int numvertices;
Vertex table T *verticeslist;
Adjacency matrix E **edge; Gives the position of the vertex in the diagram int getvertexpos (T vertex) {for (int i=0; i<numvertices; i++) {if (Ver
Ticeslist[i]==vertex) return i;
} return-1;
}
};
#endif//Graphmtx_h
Main.cpp
#include <iostream> #include <Graphmtx.h> using namespace std;
int main () {graphmtx<int,int> G (5);
cin>>g;
cout<<g;
return 0; Template <class v,class w> istream& operator >> (IStream & in,graphmtx<v,w> &g) {//n is top
Points, M is the number of edges int n,m;
V E1,e2;
W weight;
cout<< "Please enter the top points, number of edges:" <<endl;
in>>n>>m;
cout<< "Please enter a vertex:" <<endl;
for (int i=0; i<n; i++) {in>>e1;
G.insertvertex (E1);
int i=0,j,k;
cout<< "Please enter two vertices and weights for the edge:" <<endl;
while (i<m) {in>>e1>>e2>>weight;
J=g.getvertexpos (E1);
K=g.getvertexpos (E2); If the information on both ends of the IF ( -1==j&&-1==k) {cout<<) is incorrect, re-enter it.
"<<endl;
else {G.insertedge (j,k,weight);
i++;
} return in; } template <class V,class w> ostream&operator<< (ostream &out,Graphmtx<V,W> &g) {//output all vertices and edges of the information int n,m;
V E1,e2;
W W;
N=g.numberofvertices ();
M=g.numberofedges ();
out<< "A total number of vertices:" <<n<< "Edge:" <<m<< "Strip" <<endl;
for (int i=0; i<n; i++) for (int j=i+1; j<n; J + +) {w=g.getweight (i,j);
if (w>0&&w<g.maxweight) {e1=g.getvalue (i);
E2=g.getvalue (j);
out<< "(" <<e1<< "," <<e2<< "," <<w<< ")" <<endl;
} return out;
}
Project Source Please click on my github