当前位置:网站首页>Graph traversal (BFS and DFS) C language pure handwriting

Graph traversal (BFS and DFS) C language pure handwriting

2022-06-24 18:34:00 One star accompanies the moon

One's deceased father grind ing, In case the test restricts the use STL Tools such as ,c Language pure handwritten graph and queue ( Adjacency list )

/*  Be careful : 1、 Vertex numbers and adjacency subscripts are from 0 Start  2、 The code does not set fault-tolerant processing , Please make sure that the input data is correct  */
#include <stdio.h>
#include<stdlib.h>

typedef enum {
    false,true} bool;
#define ERROR -1

/*  Maximum number of vertices  */
#define MaxVertexNum 10

/*  Definition of adjacency point  */
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
    
	/*  Adjacency subscript  */
	int AdjV;
	/*  Pointer to the next adjacent point  */
	PtrToAdjVNode Next;
};

/*  Definition of vertex header node  */
typedef struct Vnode {
    
	/*  Side header pointer  */
	PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

/*  Definition of node  */
typedef struct GNode {
    
	/*  Number of vertices  */
	int Nv;
	/*  Number of edges  */
	int Ne;
	/*  Adjacency list  */
	AdjList G;
}*LGraph;

/*  Define the queue  */
typedef struct QNode {
    
	int* Data;
	int Front, Rear;
	int MaxSize;
}*Queue;


/*  Access token for vertex  */
bool Visited[MaxVertexNum];

/*  Create a diagram and add Visited Initialize to false */
LGraph CreateGraph() {
    

	LGraph Graph;
	int VertexNum, i, E1, E2;

	printf(" Please enter the number of vertices :");
	scanf("%d",&VertexNum);
	Graph = (LGraph)malloc(sizeof(struct GNode));
	Graph->Nv = VertexNum;
	Graph->Ne = 0;
	for(i = 0; i < Graph->Nv; i++) {
    
		Graph->G[i].FirstEdge = NULL;
		Visited[i] = false;
	}

	printf(" Please enter the number of edges :");
	scanf("%d",&Graph->Ne);
	if(Graph->Ne) {
    
		for(i = 0; i < Graph->Ne; i++) {
    
			/*  Take an undirected graph as an example  */
			scanf("%d%d",&E1,&E2);
			PtrToAdjVNode NewNode;
			NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
			NewNode->AdjV = E2;
			NewNode->Next = Graph->G[E1].FirstEdge;
			Graph->G[E1].FirstEdge = NewNode;

			NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
			NewNode->AdjV = E1;
			NewNode->Next = Graph->G[E2].FirstEdge;
			Graph->G[E2].FirstEdge = NewNode;
		}
	}
	
	return Graph;
}


/*  initialization Visited */
void InitVisited(LGraph Graph){
    
	int i;
	for(i = 0; i < Graph->Nv; i++){
    
		Visited[i] = false;
	}
}

/*  Print the results  */
void Print(int  V) {
    
	printf(" %d", V);
}


/*  Create a queue  */
Queue CreatQueue(int MaxSize) {
    
	Queue Q = (Queue)malloc(sizeof(struct QNode));
	Q->Data = (int*)malloc(MaxSize*sizeof(int));
	Q->Front = Q->Rear = 0;
	Q->MaxSize = MaxSize;
	return Q;
}

/*  Full sentence  */
bool IsFull(Queue Q) {
    
	return ((Q->Rear + 1)%Q->MaxSize == Q->Front);
}

/*  Sentenced to empty  */
bool IsEmpty(Queue Q) {
    
	return (Q->Front == Q->Rear);
}

/*  The team  */
bool InQueue(Queue Q,int X) {
    
	if(IsFull(Q)) {
    
		return false;
	} else {
    
		Q->Rear = (Q->Rear + 1) % Q->MaxSize;
		Q->Data[Q->Rear] = X;
		return true;
	}
}

/*  Out of the team  */
int OutQueue(Queue Q) {
    
	if (IsEmpty(Q))
		return ERROR;
	else {
    
		Q->Front = (Q->Front + 1) % Q->MaxSize;
		return Q->Data[Q->Front];
	}
}


/* BFS */
void BFS (LGraph Graph, int S, void (*Print)(int)) {
    
	Queue Q;
	PtrToAdjVNode W;
	int V;
	Q = CreatQueue(MaxVertexNum);
	Print(S);
	Visited[S] = true;
	InQueue(Q, S);
	while (!IsEmpty(Q)) {
    
		V = OutQueue(Q);
		for (W = Graph->G[V].FirstEdge; W; W = W->Next){
    
			if (!Visited[W->AdjV]) {
    
				Print(W->AdjV);
				Visited[W->AdjV] = true;
				InQueue(Q, W->AdjV);
			}	
		}
	}
}

/* DFS */
void DFS(LGraph Graph,int S,void (*Print)(int)){
    
	Visited[S] = true;
	Print(S);
	PtrToAdjVNode W;
	W = Graph->G[S].FirstEdge;
	while(W){
    
		if(!Visited[W->AdjV]){
    
			DFS(Graph,W->AdjV,Print);
		}
		W = W->Next;
	}
} 

int main() {
    
	LGraph G;
	int S;
	G = CreateGraph();
	scanf("%d", &S);
	printf("BFS from %d:", S);
	BFS(G, S, Print);
	InitVisited(G);
	printf("\nDFS from %d:", S);
	DFS(G, S, Print);
	return 0;
}
/*  Test data  7 9 6 5 5 4 5 2 5 1 4 0 3 2 3 1 3 0 2 0 2 */

 Insert picture description here

原网站

版权声明
本文为[One star accompanies the moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333580456.html