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 176 177 178 179 180 181 182 183
| #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MaxVertexNum 10
typedef int Vertex; typedef int WeightType; typedef char DataType; bool Visited[MaxVertexNum];
typedef struct GNode *PtrToGNode; typedef PtrToGNode MGraph; struct GNode { int Nv; int Ne; WeightType G[MaxVertexNum][MaxVertexNum]; DataType Data[MaxVertexNum]; };
typedef struct ENode *PtrToENode; typedef PtrToENode Edge; struct ENode { Vertex V1, V2; WeightType Weight; };
typedef struct QNode *PtrToQNode;
struct QNode { Vertex *Data; int front, rear; int MaxSize; }; typedef PtrToQNode Queue;
Queue CreateQueue(int MaxSize) { Queue queue; queue = (Queue) malloc(sizeof(struct QNode)); queue->Data = (Vertex *) malloc(sizeof(Vertex) * MaxSize); queue->MaxSize = MaxSize; queue->front = 0; queue->rear = 0; return queue; }
bool IsEmpty(Queue queue) { return queue->rear == queue->front; }
bool IsFull(Queue queue) { return (queue->rear + 1) % queue->MaxSize == queue->front; }
bool AddQ(Queue queue, Vertex item) { bool IsFull(Queue queue); if (IsFull(queue)) { printf("queue is full!"); return false; } queue->rear = (queue->rear + 1) % queue->MaxSize; queue->Data[queue->rear] = item; return true; }
Vertex DeleteQ(Queue queue) { bool IsEmpty(Queue queue); if (IsEmpty(queue)) { printf("queue is empty!"); return -1; } queue->front = (queue->front + 1) % queue->MaxSize; return queue->Data[queue->front]; }
MGraph CreateGraph(int VertexNum, int EdgeNum) { Vertex V, W; MGraph Graph; Graph = (MGraph) malloc(sizeof(struct GNode)); Graph->Nv = VertexNum; Graph->Ne = EdgeNum; for (V = 0; V < Graph->Nv; V++) { Visited[V] = false; for (W = 0; W < Graph->Nv; W++) Graph->G[V][W] = -1; } return Graph; }
void InsertEdge(MGraph Graph, Edge E) { Graph->G[E->V1][E->V2] = E->Weight; Graph->G[E->V2][E->V1] = E->Weight; }
MGraph BuildGraph() { MGraph Graph; Edge E; int Nv, Ne, i; scanf("%d %d", &Nv, &Ne); Graph = CreateGraph(Nv, Ne); if (Graph->Ne != 0) { E = (Edge) malloc(sizeof(struct ENode)); for (i = 0; i < Graph->Ne; i++) { scanf("%d %d", &E->V1, &E->V2); E->Weight = 1; InsertEdge(Graph, E); } } return Graph; }
bool isEdge(MGraph Graph, Vertex V, Vertex W) { return Graph->G[V][W] <= -1 ? true : false; }
void Visit(Vertex V) { printf("%d ", V); }
void BFS(MGraph Graph, Vertex S, void(*Visit)(Vertex)) { Queue Q; Vertex V, W; Q = CreateQueue(MaxVertexNum); Visit(S); Visited[S] = true; AddQ(Q, S); while (!IsEmpty(Q)) { V = DeleteQ(Q); for (W = 0; W < Graph->Nv; W++) { if (!Visited[W] && Graph->G[V][W] > 0) { Visit(W); Visited[W] = true; AddQ(Q, W); } } } }
void DFS(MGraph Graph, Vertex V, void(*Visit)(Vertex)) { Visit(V); Visited[V] = true; Vertex W; for (W = 0; W < Graph->Nv; W++) { if (Graph->G[V][W] > 0 && !Visited[W]) { DFS(Graph, W, Visit); } } }
int main() { MGraph Graph = BuildGraph(); Vertex S; for (S = 0; S < Graph->Nv; S++) { if (!Visited[S]) { printf("{ "); DFS(Graph, S, &Visit); printf("}\n"); } } for (S = 0; S < MaxVertexNum; S++) { Visited[S] = false; } for (S = 0; S < Graph->Nv; S++) { if (!Visited[S]) { printf("{ "); BFS(Graph, S, &Visit); printf("}\n"); } } return 0; }
|