Home > Back-end >  Seek advice
Seek advice

Time:11-22

For bosses to solve, this is can output a connected graph in two loops of the code, but I want to put the two ring nodes are stored in two arrays, every time I think of is depth-first traversal after will get a ring then endures the elements of an array, how should do? Because I want to figure out whether a point on a ring, so how to get to the store this two rings, next to my judgment?
 
#include
#include
using namespace std;
Const int MAX_Vertex_Num=12;


Template
The class MGraph
{
Public:
Void CreateGraph ();//create the figure
Void CheckCircle ();

Private:
VexType vexs [MAX_Vertex_Num]=,1,2,3,4,5,6,7,8,9,10,11 {0};//vertex vector
ArcType arcs [MAX_Vertex_Num] [MAX_Vertex_Num];//here said the adjacency matrix type with template, mainly for processing shall have the right to value, such as: weight for decimal (price), can also as an integer
Int vexnum=12;//vertices
Int arcnum=12;//number of edges

Private:
Void DFS (int x, bool visited [MAX_Vertex_Num], ArcType arcs [MAX_Vertex_Num] [MAX_Vertex_Num]);
};

Template

Void MGraph : : CreateGraph ()
{
Int I, j, k;

Cout & lt; <"Vertex number is:" & lt; Cout & lt; <"Number of edges is:" & lt; //initialize the adjacency matrix
for (int i=0; I & lt; Vexnum; I++)
{
Vexs [I]=I;
}
Ifstream infile.
Infile. Open (" inputdata. TXT ");
For (I=0; I & lt; Vexnum; I++)
{
For (j=0; J & lt; Vexnum; J++)
{
Arcs [I] [j]=0;
}
}
For (k=0; k {
Infile & gt;> I & gt;> j;
Arcs [I] [j]=1;
Arcs [j] [I]=1;
}
Infile. Close ();
}

/*
Check whether have ring in the figure
Thought:
If there is a loop, it will exist a child figure, is a loop, the loop for all the vertices in the c & gt;=2,
Specific algorithm:
1, judge undirected graph have ring
Step 1: statistics of each vertex degree
Step 2: remove all degrees & lt;=1 vertices and edges, and the other associated with these and other vertex degree of minus one, until the diagram node degrees are & gt;=2
If finally, there is not deleted vertices, there are ring, otherwise no ring,
2, the number of output link and node in each ring
Step 1: get degrees are after 2 nodes, using deep traversal, get the number of the number of connected components for the ring in the
The second step: each connected components is a ring, connected components element is
element in the ring*/

Template

Void MGraph : : CheckCircle ()
{
//into a stack, specialized storage node degree of 1
Int top=1;//initialize the stack space, which indicates that the stack is empty
Int stack [MAX_Vertex_Num];
//the introduction of an array, deposit every vertex degree
Int degree [MAX_Vertex_Num]={0};
////the introduction of a temporary adjacency matrix, the processing of completed
//ArcType arcsTemp [MAX_Vertex_Num] [MAX_Vertex_Num];
//for (int I=0; I & lt; Vexnum; I++)
//{
//for (int j=0; J & lt; Vexnum; J++)
//{
//arcsTemp [I] [j]=arcs [I] [j];//temporary the adjacency matrix of the same as the original matrix
//}
//}
//statistical degrees of each node, and the degree of array assignment

for (int i=0; I & lt; Vexnum; I++)
{
Int count=0;
for (int j=0; J & lt; Vexnum; J++)
{
If (arcs [I] [j]==1)
{
count++;
}
}
Degree [I]=count;
}
//degrees of 1 nodes in the stack
for (int i=0; I & lt; Vexnum; I++)
{
If (degree [I]==1)
{
Stack [+ + top]=I;
}
}
//array processing degree node of moderate to 1
While (top & gt; 1)
{
Int x=stack [top --];
//processing degree of 1 nodes, delete all with the edge of the node
for (int j=0; J & lt; Vexnum; J++)
{
If (arcs [x] [j]==1)//processing lines
{
Arcs [x] [j]=0;
Degree [x] -;//delete an edge, the initial vertex and termination is minus one, since x is just out of the stack, after - degree 0
}
If (arcs [j] [x]==1)//processing column, undirected graph is symmetrical, processing line, column also have to deal with the
{
Arcs [j] [x]=0;
[j] - degree;
If (degree [j]==1)
{
Stack [+ + top]=j;
}
}
}
}
//the adjacency matrix arcsTemp are degree of 2 nodes - this can be achieved by depth traversal of each ring node

Bool visited [MAX_Vertex_Num]={false};
Int num=0;
for (int i=0; I & lt; Vexnum; I++)
{
If (visited [I]==false & amp; & Degree [I]!=0)
{
num++;
Cout & lt; Visited [I]=true;
DFS (I visited, arcs);
}
}
If (num==0)
{
Cout & lt; <"The figure does not exist in the ring!" }
The else
{
Cout & lt; }
}
Template

Void MGraph : : DFS (int x, bool visited [MAX_Vertex_Num], ArcType arcs [MAX_Vertex_Num] [MAX_Vertex_Num])
{

Cout & lt;
for (int j=0; J & lt; Vexnum; J++)
{
If (arcs [x] [j]==1 & amp; & Visited [j]==false)//this point if there are neighbors haven't access to access it, recursive calls, depth-first traversal
{
Visited [j]=true;
DFS (j, visited, arcs);

}
}
}

Int main ()
{
MGraph G;
Right reateGraph ();
//coutRight heckCircle ();

system("pause");
return 1;
}

  • Related