datastruct_7

datastruct_7

Charles Lv7

图的相关笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max 100
typedef struct {
char Vex[Max];
bool Edge[Max][Max];
int vex_num, arc_num;
} Graph;
typedef struct ArcNode {
int adjvex;
struct ArcNode* next;
} ArcNode;
typedef struct VNode {
int data;
ArcNode* first;
} VNode, AdjList[Max];
typedef struct {
AdjList vertices;
int vex_num, arc_num;
} ALGraph;

bool visited[Max];
void BFS(Graph G, int v) {
visit(v);
visited[v] = true;
Enqueue(Q, v);
while (!isEmpty(Q)) {
DeQueue(Q, v);
int w;
for (w = FirstNeighbor(G, v); w >= 0; w = NextNeighbor(G, v, w)) {
if (!visited[w]) {
visit(w);
visited[w] = true;
EnQueue(Q, w);
}
}
}
}
void BFSTraverse(Graph G) {
for (int i = 0; i < G.vex_num; ++i) {
visited[i] = false;
}
InitQueue(Q);
for (int i = 0; i < G.vex_num; ++i) {
if (!visited[i]) {
BFS(G, i);
}
}
}

bool visited[Max];
void DFSTraverse(Graph G) {
for (int i = 0; i < G.vex_num; ++i) {
visited[i] = false;
}
for (int i = 0; i < G.vex_num; ++i) {
if (!visited[i]) {
DFS(G, i);
}
}
}
void DFS(Graph G, int v) {
visit(v);
visited[v] = true;
for (w = FirstNeighbor(G, v); w >= 0; w = NextNeighbor(G, v, w)) {
if (!visited[w]) {
DFS(G, w);
}
}
}

void BFS_MIN_Distance(Graph G, int u) {
for (i = 0; i < G.vex_num; ++i) {
d[i] = INT_MAX;
path[i] = -1;
}
d[u] = 0;
visited[u] = true;
Enqueue(Q, u);
while (!isEmpty(Q)) {
DeQueue(Q, u);
int w;
for (w = FirstNeighbor(G, u); w >= 0; w = NextNeighbor(G, u, w)) {
if (!visited[w]) {
d[w] = d[u] + 1;
path[w] = u;
visited[w] = true;
EnQueue(Q, w);
}
}
}
}

void dp() {
int i, j, k;
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a[i][j] > a[i][k] + a[k][j]) {
a[i][j] = a[i][k] + a[k][j];
path[i][j] = k;
}
}
}
}
}

#define Max 100
int in_degree[Max];
int print[Max];
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;

} ArcNode;
typedef struct VNode {
int data;
ArcNode* firstarc;
} VNode, AdjList[Max];
typedef struct {
AdjList vertices;
int vex_num, arc_num;
} Graph;
bool TopologicalSort(Graph G) {
InitStack(S);
int i;
for (i = 0; i < G.vex_num; i++) {
if (in_degree[i] == 0) {
Push(S, i);
}
}
int count = 0;
while (!IsEmpty(S)) {
Pop(S, i);
print[count++] = i;
for (p = G.vertices[i].firstarc; p; p = p->nextarc) {
v = p->adjvex;
if (!(--in_degree[v])) {
Push(S, v);
}
}
}
if (count < G.vex_num) {
return false;
} else {
return true;
}
}

void DFSTraverse(Graph G) {
int v;
for (v = 0; v < G.vex_num; ++v) {
visited[v] = false;
}
for (v = 0; v < G.vex_num; ++v) {
if (!visited[v]) {
DFS(G, v);
}
}
}
void DFS(Graph G, int v) {
visited[v] = true;
int w;
for (w = FirstNeighbor(G, v); w >= 0; w = NextNeighbor(G, v, w)) {
if (!visited[w]) {
DFS(G, w);
}
}
printf("%d", v);
}
  • Title: datastruct_7
  • Author: Charles
  • Created at : 2022-12-29 11:02:31
  • Updated at : 2023-07-18 21:15:01
  • Link: https://charles2530.github.io/2022/12/29/datastruct-7/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
datastruct_7