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

边栏推荐
- How does the video platform import the old database into the new database?
- Five advantages and disadvantages of Bi
- variable
- Industry Cloud video editing software
- How to decompile APK files
- Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
- What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
- Ten software development indicators for project managers
- SAP license:sap s/4hana is the answer
- 13 ways to reduce the cost of cloud computing
猜你喜欢

Overall planning and construction method of digital transformation

C language - structure II

Nacos cluster starts throwing set of SQL_ SELECT_ LIMIT is not support
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
![[untitled]](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[untitled]

ASP. Net hosting uploading file message 500 error in IIS
[North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption

Wechat applet development - Implementation of rotation chart

Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province

How MySQL works - Chapter 14
随机推荐
Is it safe to open an account online? What should I do?
Selection (033) - what is the output of the following code?
Three layer switching experiment
SAP license: ERP for supply chain management and Implementation
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
High quality defect analysis: let yourself write fewer bugs
Easygbs video platform TCP active mode streaming exception repair
About whether arm's large and small end mode is related to CPU or compiler
The country has made a move! Launch network security review on HowNet
Get max value of a bit column - get max value of a bit column
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
【你真的会用ES吗】ES基础介绍(一)
[NLP] 3 papers on how Stanford team builds a better chat AI
R中的指数回归
中电投先融期货这家公司怎么样?期货开户办理安全吗?
如何在 R 中创建线性模型预测区间 并可视化
Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province
How does the video platform import the old database into the new database?
On the principle of cloud streaming multi person interaction technology
Number of occurrences of numbers in the array (medium difficulty)