#include <iostream>
#include <stdlib.h>
#include <string.h>
#define INFINITY 1000//MAX ∞
#define MAX_VERTEX_NUM 20//maximum number of vertices
#define MAXLISTSIZE 100//maximum capacity of the cyclic queue
#define TRUE 1
#defineFALSE 0
using namespace Std;
typedef ENUM{DG, DN, AG, An}graphkind; Type flag {forward graph, have to net, no direction graph, no net}
typedef int VRTYPE;
typedef char Vertextype;
typedef int ELEMTYPE;
BOOL Visited[max_vertex_num]; Global variables, Access flags array
typedef struct ARCCELL
{//ARC definition
Vrtype adj; Vrtype is a vertex relationship type. In the case of an unauthorized graph, a value of 1 or 0 is used to denote adjacent no;
}arccell, Adjmatrix[max_vertex_num][max_vertex_num];
Definition of typedef struct{//diagram
Vertextype Vexs[max_vertex_num]; Vertex information
Adjmatrix arcs; A two-dimensional array that represents the relationship between vertices
int Vexnum, arcnum; Number of current vertices and arcs (edges) of the graph
int kind; The type of the graph symbol
}mgraph;
typedef struct
{
Elemtype *elem; Storage space Base
int rear; Team Tail Hands
int front; Team Head pointer
int queuesize; Maximum allowable storage space, in element units
}queue;
void Initqueue (Queue &q, int maxsize)
{
Constructs an empty queue with a maximum storage space of maxsize Q
if (maxsize = = 0)
MaxSize = maxlistsize;
Q.elem = new Elemtype[maxsize]; Allocating storage space for a circular queue
if (! Q.elem) exit (1); Storage Allocation failure
Q.queuesize = maxsize;
Q.front = q.rear = 0;
}//initqueue
BOOL EnQueue (Queue &q, Elemtype e)
{
If the current queue is dissatisfied, this is after the tail element of the current queue, inserting element e as a new queue tail element, and returning true, otherwise false
if ((q.rear + 1)% Q.queuesize = = Q.front)
return FALSE;
Q.elem[q.rear] = e;
Q.rear = (q.rear+1)% Q.queuesize;
return TRUE;
}
BOOL DeQueue (Queue &q, Elemtype &e)
{
If the queue is not empty, delete the header element in the current queue Q, return its value with E, and return true, otherwise FALSE
if (Q.front = = q.rear)
return FALSE;
e = Q.elem[q.front];
Q.front = (q.front+1)% Q.queuesize;
return TRUE;
}
BOOL Queueempty (Queue Q)
{
if (Q.front = = q.rear)
return TRUE;
Else
return FALSE;
}
void Destroyqueue (Queue &q)
{
Delete[] Q.elem;
q.queuesize = 0;
}
int Locatevex (mgraph G, Vertextype v)
{//Initial conditions: Figure G exists, V and g vertices have the same characteristics
Operation Result: If there is a vertex u in G, the vertex is returned in the figure position; otherwise 1
int i;
for (i = 0; i < g.vexnum; ++i)
if (v = = G.vexs[i])
return i;
return-1;
}
Void Createdg (Mgraph &g)
{//the array (adjacency matrix) notation is used to construct the graph G.
int I, j, K;
Vertextype va, VB;//vertex
cout << "Please enter the number of vertices and sides of the adjacency matrix:";
CIN >> G.vexnum >> G.arcnum;
cout << "Enter the vertices of the adjacency matrix:";
for (i = 0; i < g.vexnum; ++i)//construct vertex vector
Cin >> g.vexs[i];
for (i = 0; i < g.vexnum; ++i)//Initialize adjacency matrix
for (j = 0; j < G.vexnum; ++j)
{
G.arcs[i][j].adj = 0;//NET, initialized to 0}
for (k = 0; k < g.arcnum; ++k)//construct adjacency matrix
{
cout << "Enter the beginning (line) and end point (column) of the" arc "):"; Br>cin >> va >> vb; Enter the vertex attached to an edge
I = Locatevex (G, VA);
j = Locatevex (G, VB); Determine the position of VA and VB in G
G.arcs[i][j].adj = 1;//vertices have an edge assignment of 1
}//for
}//createdg
Void Createdn (Mgraph &g)
{//the array (adjacency matrix) notation is used to construct the net G.
int I, j, K;
Vrtype W;//Weight
Vertextype va, VB;//vertex
cout << Please enter the number of vertices and sides of the adjacency matrix: ";
CIN >> G.vexnum >> G.arcnum;
cout << "Enter the vertices of the adjacency matrix:";
for (i = 0; i < g.vexnum; ++i)//construct vertex vector
Cin >> g.vexs[i];
for (i = 0; i < g.vexnum; ++i)//Initialize adjacency matrix
for (j = 0; j < G.vexnum; ++j)
{
G.arcs[i][j].adj = INFINITY;//NET, Initialize to a maximum of
}
for (k = 0; k < g.arcnum; ++k)//construct adjacency matrix
{
cout << "Enter section" << K + 1 << "beginning (line) and end of Arc" Points (columns) and weights: ";
Cin >> va >> vb >> w;//enter vertices and weights attached to an edge
I = Locatevex (G, VA);
j = Locatevex (G, VB); Determine the position of VA and VB in G
G.arcs[i][j].adj = w;//w as weight
}//for
}//createdn
Void Createag (Mgraph &g)
{//the array (adjacency matrix) notation is used to construct the graph G.
int I, j, K;
Vertextype va, VB;//vertex
cout << "Please enter the number of vertices and sides of the adjacency matrix:";
CIN >> G.vexnum >> G.arcnum;
cout << "Enter the vertices of the adjacency matrix:";
for (i = 0; i < g.vexnum; ++i)//construct vertex vector
Cin >> g.vexs[i];
for (i = 0; i < g.vexnum; ++i)//Initialize adjacency matrix
for (j = 0; j < G.vexnum; ++j)
{
G.arcs[i][j].adj = 0;//no map, initialize to 0}
for (k = 0; k < g.arcnum; ++k)//construct adjacency matrix
{
cout << "Please enter the two vertices of" << K + 1 << "arc:";
Cin >> va >> vb;//Enter the vertex attached to the edge
i = Locatevex (G, VA);
j = Locatevex (G, VB); Determine the position of VA and VB in G
G.arcs[i][j].adj = 1;//vertices have an edge assignment of 1
g.arcs[j][i] = g.arcs[i][j];
} For
}//createag
Void Createan (Mgraph &g)
{//the array (adjacency matrix) notation is used to construct the net-free G.
int I, j, K;
Vrtype W;//Weight
Vertextype va, VB;//vertex
cout << Please enter the number of vertices and sides of the adjacency matrix: ";
CIN >> G.vexnum >> G.arcnum;
cout << "Enter the vertices of the adjacency matrix:";
for (i = 0; i < g.vexnum; ++i)//construct vertex vector
Cin >> g.vexs[i];
for (i = 0; i < g.vexnum; ++i)//Initialize adjacency matrix
for (j = 0; j < G.vexnum; ++j)
{
G.arcs[i][j].adj = INFINITY;//No NET, Initialize to a maximum of
}
for (k = 0; k < g.arcnum; ++k)//construct adjacency matrix
{
cout << "Enter the two vertices and weights of the arc" << K + 1 << ": ";
Cin >> va >> vb >> w;//enter vertices and weights attached to an edge
I = Locatevex (G, VA);
j = Locatevex (G, VB); Determine the position of VA and VB in G
G.arcs[i][j].adj = w;//w as weight
G.arcs[j][i] = g.arcs[i][j];
} For
}//createan
void Creategraph (Mgraph &g)
{//use array (adjacency matrix) notation, construct figure G.
cout << "Please enter the type of the graph (enter the preceding number) {0.), 1. The direction of the net, 2. No direction, 3. No net}:" << Endl;
Cin >> G.kind;
Switch (g.kind)
{
Case DG:
CREATEDG (G); Construct a map with direction
Break
Case DN:
Createdn (G); Construct a network with a direction
Break
Case AG:
Createag (G); Constructing the graph without direction
Break
Case an:
Createan (G); Construct the non-direction net
Break
Default
cout << "Type error of the input graph:" << Endl;
Break
}
Return
}
int Firstadjvex (mgraph G, int v)
{
Int J, k =-1;
for (j = 0; J < G.vexnum; J + +)
if (g.arcs[v][j].adj==1)
{
K = J;
Break
}
return k;
}
int Nextadjvex (mgraph G, int v, int w)
{
Int J, k =-1;
for (j = w + 1; j < G.vexnum; J + +)
if (g.arcs[v][j].adj==1)
{
K = J;
Break
}
return k;
}
void DFS (mgraph G, int v)
{
Int J;
cout << G.vexs[v] << "";
VISITED[V] = TRUE;
for (j = 0; J < G.vexnum; J + +)
{
if (G.kind = = DG | | G.kind = = AG)
if (!visited[j] && G.arcs[v][j].adj = = 1)
DFS (G, J);
if (G.kind = = DN | | G.kind = = an)
if (!visited[j] && G.arcs[v][j].adj! = INFINITY)
DFS (G, J);
}
}
void Dfstraverse (Mgraph G)
{
int V;
for (v = 0; v < g.vexnum; v++)
VISITED[V] = FALSE; Initialize to False, and change to true after traversal
for (v = 0; v < g.vexnum; v++)
if (!visited[v])
DFS (G, v);
}
Void Bfstraverse (Mgraph g)
{//The breadth-first search for the graph G represented in array storage traversal
Queue Q;//queue Q
int u, V, W;
for (v = 0; v < g.vexnum; ++v)
Visited[v] = FALSE;
Initqueue (Q, g.vexnum); Set empty queue Q
for (v = 0; v < g.vexnum; ++v)
if (!visited[v])
{//From each unreachable vertex for breadth-first search
Visited[v] = TRUE;
cout << G.vexs[v] << ""; Accesses the V vertex in the output graph
EnQueue (Q, v);//v into the queue
while (! Queueempty (q))
{
DeQueue (q, u);//Team head element out of line and set to U
for (w = 0; w < g.vexnum; w++)
if (G.arcs[u][w].adj & &! VISITED[W])
{
Visited[w] = TRUE;
cout << g.vexs[w] << ""; Accesses the first W vertex of the output graph
EnQueue (q, W),/////Current access vertex W into queue Q
}//if
}//while
}//if
Destroyqueue (q);
} Bfstraverse
int main (void)
{
Mgraph G;
int I, J;
Creategraph (G);
cout << "adjacency matrix:" << Endl;
for (i = 0; i < g.vexnum; i++)
{
for (j = 0; J < G.vexnum; J + +)
{
cout << G.arcs[i][j].adj << "\ t";
}
cout << Endl;
}
cout << "Depth-first traversal:";
Dfstraverse (G);
cout << Endl;
cout << "breadth-first traversal:";
Bfstraverse (G);
cout << Endl;
return 0;
}
Adjacency matrix of data Structures (C + +)