/*
On-machine Test 5-building and traversal of graphs
1) Establish the Adjacency table storage structure of "undirected" and "non-connected" graphs, requiring no fewer than 15 vertices.
2) using DFS and BFS to traverse this adjacency table, print out the order of two kinds of traversed vertex accesses.
3) Given any two vertices v1 and v2 and integer k in the graph, determine if there is a simple path with a path length of k from V1 to v2, if a sequence of vertices on the path is printed (requires no loops on the path).
Further: Find the "simple" path of all paths from V1 to v2 with a length of K. (A simple path is a path to a vertex in a vertex sequence that does not contain a "repro".) )
*/
#include <memory>
#include <cstdio>
#include <malloc.h>
#define POINTNUM 15
using namespace Std;
struct v{
int vertex;
int distance;
int sign;
struct v* next;
}VERTEX[POINTNUM+1];
int P[pointnum];
int union_find (int x)//and check Set
{
while (p[x]!=x) x=p[x];
return x;
}
void DFS (int vertex_);
void BFS (int vertex_);
int DFS (int V1, int V2, int V3, int kn);
int lujing[pointnum+1],lujing_i;
int main ()
{
int times,vertexnumbers,edgenumbers,i,j,v1,v2,distance,n=1,v1,v2,kn;
struct V *p,*q,*temp;
printf ("Please enter the number of times you want to test:");
scanf ("%d", ×);
while (times--)
{
printf ("\ n%d data entry. \ n", n++);
printf ("Please enter the number of vertices:");
scanf ("%d", &vertexnumbers); To ensure Vertexnumbers <= Pointnum
for (i = 0; i < vertexnumbers; i + +)
{
Vertex[i].vertex = i;//////////////////////////
Vertex[i].distance = 0;////////////////////////
vertex[i].sign = 0;
Vertex[i].next = NULL;
}
printf ("Please enter the number of sides:");
scanf ("%d", &edgenumbers); To ensure Vertexnumbers <= Pointnum
for (i = 0; i < edgenumbers; i + +)
{
printf ("Please enter data:");
scanf ("%d%d%d", &v1,&v2,&distance);
p = &vertex[v1];q = Vertex[v1].next;
while (q! = NULL) {
p = q;
Q = q->next;
When}//comes out, Q points to vertex[v1] at the end of the adjacency table-null,
temp = (struct V *) malloc (sizeof (vertex[0]));
P->next = temp; Temp->next = null;//connected to Temp
Temp->vertex = V2; temp->distance = distance;temp->sign = 0; Assigning a value to Temp
p = &vertex[v2];q = Vertex[v2].next;
while (q! = NULL) {
p = q;
Q = q->next;
}
temp = (struct V *) malloc (sizeof (vertex[0));///////////temp problem???????
P->next = temp; Temp->next = null;//connected to Temp
Temp->vertex = v1;temp->distance = distance;temp->sign = 0; Assigning a value to Temp
}
for (i = 0; i < vertexnumbers; i + +)
{
printf ("\ndfs Traversal of vertex Access order:");
DFS (i);
for (int j = 0; J < Vertexnumbers; J + +)
{
vertex[j].sign = 0;
}
}
printf ("\ n");
for (i = 0; i < vertexnumbers; i + +)
{
for (int j = 0; J < Vertexnumbers; J + +)
{
vertex[j].sign = 0;
}
printf ("\NBFS Traversal of vertex Access order:");
BFS (i);
}
for (j = 0; J < Vertexnumbers; J + +)
{
vertex[j].sign = 0;
}
printf ("Input v1, v2, and integer kn: \ n");
scanf ("%d%d%d", &v1,&v2,&kn);
for (j = 0; J < Vertexnumbers; J + +)
{
vertex[j].sign = 0;
}
lujing_i = 0;
DFS (V1,V2,V1,KN);
}
return 0;
}
void BFS (int vertex_)
{
struct V *p = &Vertex[vertex_];
int Others[pointnum];
bo=
if (vertex[p->vertex].sign = = 0) {
printf ("%d->", P->vertex);//Output Access node
Vertex[p->vertex].sign = 1;//Representative has visited//
Others[p->vertex] = -1;//represents this one can proceed with BFS
}
else Others[p->vertex] = p->vertex;//Record existing points
p = p->next;//Main
}while (P! = NULL);
p = &Vertex[vertex_];
while (p->next! = NULL)//Some have no connection to consider ~ ~!!!!!!!
{
p = p->next;
if (Others[p->vertex] = =-1) BFS (P->vertex);
}
}
void DFS (int vertex_)
{
struct V *q = vertex[vertex_].next;//starts with: p = &vertex[i];q = Vertex[i].next;
Vertex[vertex_].sign = 1;//The Representative has already visited
printf ("%d->", vertex_);//
do{
for (; Q! = NULL && Vertex[q->vertex].sign = = 1;q = Q->next);//Determine if the node has been visited//the original is Q->next! = null//A large error::: ::::::::::: Vertex[q->vertex].sign = = 1 cannot be used::: Q.sign = = 1 (********************
if (q = = null) return;//(Q->next = = NULL && vertex_sum = = vertexnumbers)
else DFS (Q->vertex);//continued deep search from Q->vertex *********************q->sign actually did not assign the initial value!!!! Large error:::::::::::::: Vertex[q->vertex].sign = = 1 cannot be used::: Q->sign = = 1 (**************
}while (q! = NULL);//Some have no connection to consider ~ ~!!!!!!!!
}
int DFS (int V1, int V2, int V3, int kn)
{
struct V *q = vertex[v1].next;//start
int sum,i;
Vertex[v1].sign = 1;//The Representative has already visited
Lujing[lujing_i + +] = v1;//printf ("%d->", V1);//
if (V1 = = V2)
{
for (sum=0,i=0; i < lujing_i; i++)
{
if (I >= 1)//start at the second point
{
for (q = &Vertex[lujing[i-1]]; q = NULL; q = q->next)
{
if (Q->vertex = = Lujing[i]) sum + = q->distance;
}
}
}
if (kn==sum)
{
printf ("%d to%d path with%d distance is:", v3,kn,sum);
for (i=0; i < lujing_i; i++)
{
printf ("%d->", Lujing[i]);
}
printf ("%d\n", sum);
}
return sum;
}
do{
for (; Q! = NULL && Vertex[q->vertex].sign = = 1;q = Q->next);//Determine if the node has been visited//the original is Q->next! = null//A large error::: ::::::::::: Vertex[q->vertex].sign = = 1 cannot be used::: Q.sign = = 1 (********************
if (q = = null) return-1;//(Q->next = = NULL && vertex_sum = = vertexnumbers)
else {
DFS (Q->VERTEX,V2,V3,KN);//continued deep search from Q->vertex *********************q->sign actually did not assign the initial value!!!! Large error:::::::::::::: Vertex[q->vertex].sign = = 1 cannot be used::: Q->sign = = 1 (**************
Vertex[q->vertex].sign = 0;lujing_i--; q = q->next;
}
}while (q! = NULL);//Some have no connection to consider ~ ~!!!!!!!!
return 0;
}
/*
Input:
10
4
4
0 1 34
0 2 55
1 3 55
2 3 34
0 3 89
5
5
0 1 1
0 3 1
0 4 1
1 2 1
1 3 1
7
10
0 1 12
0 4 34
1 2 43
1 5 543
2 3 54
2 4 54
2 5 54
3 6 64
4 5 64
5 6 654
15
16
0 1 1
1 2 1
1 10 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
6 14 1
7 8 1
8 9 1
9 10 1
10 11 1
11 12 1
12 13 1
13 14 1
5
5
5 4 12
0 1 12
0 2 32
3 0 43
4 0 43
Output:
Vertex access order for DFS traversal: 0, 1, 2, 3, 4
Vertex access order for DFS traversal: 1, 0, 3, 4, 2
Vertex access order for DFS traversal: 2, 1, 0, 3, 4
Vertex access order for DFS traversal: 3, 0, 1, 2, 4
Vertex access order for DFS traversal: 4, 0, 1, 2, 3
The vertex access order of BFS traversal: 0, 1, 3, 4, 2
The vertex access order of BFS traversal: 1, 0, 2, 3, 4
The vertex access order of BFS traversal: 2, 1, 0, 3, 4
The vertex access order of BFS traversal: 3, 0, 1, 4, 2
The vertex access order of BFS traversal: 4, 0, 1, 3, 2
Vertex access Order of DFS traversal: 0, 1, 2, 3, 6, 5
Vertex access Order of DFS traversal: 1, 0, 4, 2, 3, 6
Vertex access Order of DFS traversal: 2, 1, 0, 4, 5, 6
Vertex access Order of DFS traversal: 3, 2, 1, 0, 4, 5
Vertex access Order of DFS traversal: 4, 0, 1, 2, 3, 6
Vertex access Order of DFS traversal: 5, 1, 0, 4, 2, 3
Vertex access Order of DFS traversal: 6, 3, 2, 1, 0, 4
The vertex access order of BFS traversal: 0, 1, 4, 2, 5, 3
The vertex access order of BFS traversal: 1, 0, 2, 5, 4, 3
The vertex access order of BFS traversal: 2, 1, 3, 4, 5, 0
The vertex access order of BFS traversal: 3, 2, 6, 1, 4, 5
The vertex access order of BFS traversal: 4, 0, 2, 5, 1, 3
The vertex access order of BFS traversal: 5, 1, 2, 4, 6, 0
The vertex access order of BFS traversal: 6, 3, 5, 2, 1, 4
The 3rd set of data input.
Please enter the number of vertices:
*/
The deep search and breadth Search program of the graph implemented by C language