当前位置:网站首页>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 */

边栏推荐
- Optimizing bloom filter: challenges, solutions, and comparisons
- Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
- Do you know CMDB?
- What is business intelligence (BI)?
- Three years of bug free, tips for improving code quality
- Project Management Guide: tips, strategies and specific practices
- Tencent cloud TCS: an application-oriented one-stop PAAS platform
- The country has made a move! Launch network security review on HowNet
- Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
- Skills of writing test cases efficiently
猜你喜欢
![717.1-bit and 2-bit characters [sliding window]](/img/61/449566d2a8efbd403ae0361f839683.jpg)
717.1-bit and 2-bit characters [sliding window]

Four security issues of low code and no code development

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck

13 ways to reduce the cost of cloud computing

What is decision intelligence?

How does the chief information security officer discuss network security with the enterprise board of directors

How to decompile APK files

About pyqt5 to realize paging function (one window implements different interfaces)

He "painted" what a smart city should look like with his oars

Get the actual name of the method parameter through the parameter
随机推荐
How can an enterprise successfully complete cloud migration?
Number of occurrences of numbers in the array (medium difficulty)
Skills of writing test cases efficiently
Complete Guide to web application penetration testing
Application service access configuration parameters
TCE入围2020年工信部信创典型解决方案
Why should state-owned enterprises accelerate the digital transformation
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
The mixed calculation of rpx and PX in JS by the uniapp applet
Get the actual name of the method parameter through the parameter
这个巡检平台你还不知道,真是亏大了!
About pyqt5 to realize paging function (one window implements different interfaces)
Specification for self test requirements of program developers
Get max value of a bit column - get max value of a bit column
Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
SDL: cannot play audio after upgrading openaudio to openaudiodevice
SAP license: what is ERP supply chain
2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
TCE was shortlisted as a typical solution for ICT innovation of the Ministry of industry and information technology in 2020
Five skills of selecting embedded programming language