Experiment five
First, the experimental purpose
1. Structure of unparalleled abutment matrix and adjacent table
2. Establishment algorithm for unparalleled matrices and adjacent layers
3. Master the traversal algorithm
Second, the experiment content
1. Write an algorithm to establish a neighboring matrix of a figures, and start from different vertices, with depth priority search traversal. (Folder: no map adjacency matrix)
// The adjacency matrix type of the figure is defined. H
Const int N = 8;
Const Int E = 10;
Typedef char vendype;
Typedef int adjtype;
Typedef struct
{
VEXTYPE VEXS [N];
Adjtype Arcs [N] [N];
} graph;
// Establish a directionless image adjacency matrix .h
Using namespace std;
EXTERN graph * g;
Void Creatgraph ()
{
INT I, J, K;
COUT << "Please enter the value of" << n << "node (do not use space intervals, for example: abcdefgh):" << endl;
For (i = 0; i g-> vexs [i] = getchar (); For (i = 0; i For (j = 0; j g-> ARCS [I] [J] = 0; Cout << "Please enter" << E << "strip (for example: 0 1, with blank character interval between numbers):" << Endl; For (k = 0; k { CIN >> I >> J; G-> ARCS [I] [J] = g-> ARCS [J] = 1; } } // Depth priority search traversal .h Using namespace std; EXTERN graph * g; Extern int visited [n]; Void DFSA (INT V) { Visited [V] = 1; Cout << "Node:" << g-> vexs [v] << endl; For (int J = 0; j { IF (g-> arcs [v] [j] == 1 && visited [j]! = 1) DFSA (J); } } // Non-map neighbor matrix main program file .CPP #include #include #include #include "diagram of the adjacency matrix type definition .h" #include "establish a directionless map neighbor matrix .h" #include "Depth Priority Search Traverse. H" Using namespace std; GRAPH * G = New graph; Int visited [n]; int main () { CREATGRAPH (); INT I; While (1) { For (i = 0; i Visited [i] = 0; Cout << "Enter the starting point number (0-7), the input -1 end:"; CIN >> I; IF (i == - 1) Break; DFSA (i); } System ("pause"); Return 0; } // / * Input format: Abcdefgh 0 1 0 2 1 3 1 4 2 5 2 6 3 7 4 7 5 7 6 7 * /