The difference between a directed graph and an undirected graph is a code difference. You can just comment out the code in the undirected graph.
The difference between a directed graph and a directed network, that is, the difference in the weight value. A directed graph must be assigned a value of 1 for a directed graph with a link.
The depth-first uses recursive methods (refer to the previous articles, which contain specific depth-first and breadth-first access and basic queue operations)
Queue is preferred for breadth
Code on
[Cpp] # include <stdio. h>
# Include <stdlib. h>
# Define OK 1
# Define ERROR-1
# Define OVERFLOW 0
# Define MAXVER 20 // defines the maximum number of vertices
Typedef enum {UDG, UDN, DG, DN} GraphKind; // defines an undirected network directed graph with a Directed Network
Typedef char verType; // defines the vertex type.
Typedef int status;
Typedef struct
{
VerType verx [MAXVER]; // defines vertices
Int arcs [MAXVER] [MAXVER]; // defines the Arc
Int vernum, arcsnum; // defines the maximum number of vertices and arcs.
GraphKind kind; // category
} MGraph;
Int visited [MAXVER] = {0}; // vertex has not been accessed at the beginning
// Find the subscript of the vertex in the array
Int locate (MGraph G, verType ch ){
Int I;
For (I = 0; I <G. vernum & ch! = G. verx [I]; I ++ );
Return I;
}
// Create an undirected graph
Status createUDN (MGraph & G, int & v)
{
Int I, j, k;
VerType success, ch2;
Printf ("Enter the number of vertices and the number of arcs in an undirected graph. Format: 2 3 \ n ");
Scanf ("% d", & G. vernum, & G. arcsnum );
Fflush (stdin );
Printf ("Enter the vertex symbol \ n ");
For (I = 0; I <G. vernum; I ++ ){
Scanf ("% c", & G. verx [I]);
Fflush (stdin );
}
For (I = 0; I <G. vernum; I ++ ){
For (j = 0; j <G. vernum; j ++)
G. arcs [I] [j] = 0; // The initial value is 0.
}
Printf ("enter A connected vertex in the format of a B \ n ");
For (I = 0; I <G. arcsnum; I ++ ){
Printf ("Enter the % d pair value \ n", I + 1 );
Scanf ("% c", & margin, & ch2); // enter the vertex symbol and weight
Fflush (stdin );
K = locate (G, substring); // obtain the vertex subscript.
J = locate (G, ch2 );
G. arcs [k] [j] = 1; // assign a value to the critical matrix
G. arcs [j] [k] = G. arcs [k] [j]; // The undirected graph is a symmetric matrix.
}
Return OK;
}
// Create a Directed Graph
Status createDN (MGraph & G, int & v)
{
Int I, j, k;
VerType success, ch2;
Printf ("Enter the number of vertices and the number of arcs in the directed graph, for example, 2 3 \ n ");
Scanf ("% d", & G. vernum, & G. arcsnum); // vertex count and arc count Input
Fflush (stdin); // clear the carriage return
Printf ("Enter the vertex symbol \ n ");
For (I = 0; I <G. vernum; I ++ ){
Scanf ("% c", & G. verx [I]); // vertex symbol Input
Fflush (stdin );
}
For (I = 0; I <G. vernum; I ++ ){
For (j = 0; j <G. vernum; j ++)
G. arcs [I] [j] = 0; // The initial value is 0.
}
Printf ("Enter the vertex symbol in the format of a B 3 \ n ");
For (I = 0; I <G. arcsnum; I ++ ){
Printf ("Enter the % d pair value \ n", I + 1 );
Scanf ("% c", & margin, & ch2); // enter the vertex symbol and weight
Fflush (stdin );
K = locate (G, substring); // obtain the vertex subscript.
J = locate (G, ch2 );
G. arcs [k] [j] = 1; // assign a value to the critical matrix
// G. arcs [j] [k] = G. arcs [k] [j]; // The undirected graph is a symmetric matrix.
}
Return OK;
}
// Perform depth-first Traversal
Void DFS (MGraph G, int v) {// parameter 1 Fig 2 passed in subscript
Int I;
Printf ("% c", G. verx [v]);
Visited [v] = 1;
For (I = 0; I <G. vernum; I ++)
If (visited [I] = 0 & G. arcs [v] [I]! = 0) {// determines whether the access is a weight or edge.
DFS (G, I );
}
}
// Graph Traversal
Void DFSTravers (MGraph G ){
Int I;
For (I = 0; I <G. vernum; I ++ ){
Visited [I] = 0;
}
For (I = 0; I <G. vernum; I ++ ){
If (visited [I] = 0)
DFS (G, I );
}
}
Int main ()
{
MGraph G;
Int v = 0;
If (createUDN (G, v) {// create an undirected graph
Printf ("the traversal result is \ n ");
DFSTravers (G); // traverses an undirected graph.
}
If (createDN (G, v) {// create a Directed Graph
Printf ("the traversal result is \ n ");
DFSTravers (G); // traverses a directed graph.
}
Return 0;
}
# Include <stdio. h>
# Include <stdlib. h>
# Define OK 1
# Define ERROR-1
# Define OVERFLOW 0
# Define MAXVER 20 // defines the maximum number of vertices
Typedef enum {UDG, UDN, DG, DN} GraphKind; // defines an undirected network directed graph with a Directed Network
Typedef char verType; // defines the vertex type.
Typedef int status;
Typedef struct
{
VerType verx [MAXVER]; // defines vertices
Int arcs [MAXVER] [MAXVER]; // defines the Arc
Int vernum, arcsnum; // defines the maximum number of vertices and arcs.
GraphKind kind; // category
} MGraph;
Int visited [MAXVER] = {0}; // vertex has not been accessed at the beginning
// Find the subscript of the vertex in the array
Int locate (MGraph G, verType ch ){
Int I;
For (I = 0; I <G. vernum & ch! = G. verx [I]; I ++ );
Return I;
}
// Create an undirected graph
Status createUDN (MGraph & G, int & v)
{
Int I, j, k;
VerType success, ch2;
Printf ("Enter the number of vertices and the number of arcs in an undirected graph. Format: 2 3 \ n ");
Scanf ("% d", & G. vernum, & G. arcsnum );
Fflush (stdin );
Printf ("Enter the vertex symbol \ n ");
For (I = 0; I <G. vernum; I ++ ){
Scanf ("% c", & G. verx [I]);
Fflush (stdin );
}
For (I = 0; I <G. vernum; I ++ ){
For (j = 0; j <G. vernum; j ++)
G. arcs [I] [j] = 0; // The initial value is 0.
}
Printf ("enter A connected vertex in the format of a B \ n ");
For (I = 0; I <G. arcsnum; I ++ ){
Printf ("Enter the % d pair value \ n", I + 1 );
Scanf ("% c", & margin, & ch2); // enter the vertex symbol and weight
Fflush (stdin );
K = locate (G, substring); // obtain the vertex subscript.
J = locate (G, ch2 );
G. arcs [k] [j] = 1; // assign a value to the critical matrix
G. arcs [j] [k] = G. arcs [k] [j]; // The undirected graph is a symmetric matrix.
}
Return OK;
}
// Create a Directed Graph
Status createDN (MGraph & G, int & v)
{
Int I, j, k;
VerType success, ch2;
Printf ("Enter the number of vertices and the number of arcs in the directed graph, for example, 2 3 \ n ");
Scanf ("% d", & G. vernum, & G. arcsnum); // vertex count and arc count Input
Fflush (stdin); // clear the carriage return
Printf ("Enter the vertex symbol \ n ");
For (I = 0; I <G. vernum; I ++ ){
Scanf ("% c", & G. verx [I]); // vertex symbol Input
Fflush (stdin );
}
For (I = 0; I <G. vernum; I ++ ){
For (j = 0; j <G. vernum; j ++)
G. arcs [I] [j] = 0; // The initial value is 0.
}
Printf ("Enter the vertex symbol in the format of a B 3 \ n ");
For (I = 0; I <G. arcsnum; I ++ ){
Printf ("Enter the % d pair value \ n", I + 1 );
Scanf ("% c", & margin, & ch2); // enter the vertex symbol and weight
Fflush (stdin );
K = locate (G, substring); // obtain the vertex subscript.
J = locate (G, ch2 );
G. arcs [k] [j] = 1; // assign a value to the critical matrix
// G. arcs [j] [k] = G. arcs [k] [j]; // The undirected graph is a symmetric matrix.
}
Return OK;
}
// Perform depth-first Traversal
Void DFS (MGraph G, int v) {// parameter 1 Fig 2 passed in subscript
Int I;
Printf ("% c", G. verx [v]);
Visited [v] = 1;
For (I = 0; I <G. vernum; I ++)
If (visited [I] = 0 & G. arcs [v] [I]! = 0) {// determines whether the access is a weight or edge.
DFS (G, I );
}
}
// Graph Traversal
Void DFSTravers (MGraph G ){
Int I;
For (I = 0; I <G. vernum; I ++ ){
Visited [I] = 0;
}
For (I = 0; I <G. vernum; I ++ ){
If (visited [I] = 0)
DFS (G, I );
}
}
Int main ()
{
MGraph G;
Int v = 0;
If (createUDN (G, v) {// create an undirected graph
Printf ("the traversal result is \ n ");
DFSTravers (G); // traverses an undirected graph.
}
If (createDN (G, v) {// create a Directed Graph
Printf ("the traversal result is \ n ");
DFSTravers (G); // traverses a directed graph.
}
Return 0;
}