本博客的代码的思想和图片参考:好大学慕课浙江大学陈越老师、何钦铭老师的《数据结构》
算法思想:按照递增(非递减)的顺序找出到各个顶点的最短路径。程序的框架和BFS有点类似
下面是代码演示:
1 /* 2 * singleUnweight.c 3 * 4 * Created on: 2017年5月13日 5 * Author: ygh 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 /* 12 * Algorithm thought: 13 * We easily know this question is BFS. 14 * we access the first level nodes, then access second level nodes 15 * until we reach the sixth level. 16 * We let every node to BFS until it reach the sixth level,then we record the total nodes M 17 * it can reach, calculate the M/N(The total point the test data gives) . 18 */ 19 20 #define MAX_VERTEX_MUM 10001 21 typedef int vertex; /*vertex is the index of point in the graph*/ 22 typedef int dataType; /*dataType is the data type of vertex */ 23 typedef int weightType; /*The data type of weight */ 24 25 /* 26 * Define a data structure to edge 27 */ 28 typedef struct eNode *ptrToENode; 29 typedef struct eNode { 30 vertex v1, v2; 31 weightType wight; 32 }; 33 typedef ptrToENode edge; 34 35 /* 36 * Define a data structure for adjacent table node 37 */ 38 typedef struct adjNode *ptrToAdjNode; 39 typedef struct adjNode { 40 vertex adjVertex; /*The index of vertex in the graph*/ 41 weightType weight; /*the value of the weight*/ 42 ptrToAdjNode next; /*A point to point next node*/ 43 }; 44 45 /* 46 * Define a data structure for adjacent table head point 47 */ 48 typedef struct vNode *ptrToVNode; 49 typedef struct vNode { 50 dataType data; /*The value of every vertex,some times it will be ignore*/ 51 ptrToAdjNode head;/*The point to point the adjacent table first element*/ 52 } adjList[MAX_VERTEX_MUM]; 53 54 /*Define a data structure for graph*/ 55 typedef struct gNode *ptrToGNode; 56 typedef struct gNode { 57 int vertex_num; 58 int edge_num; 59 adjList g; 60 }; 61 typedef ptrToGNode adjacentTableGraph; /*a graph show by adjacent table*/ 62 63 /* 64 create a graph given the vertex number. 65 @param vertexNum The verter number of the graph 66 @return a graph with vertex but no any egdgs 67 */ 68 ptrToGNode createGraph(int vertexNum) { 69 vertex v; 70 adjacentTableGraph graph = (adjacentTableGraph) malloc( 71 sizeof(struct gNode)); 72 graph->vertex_num = vertexNum; 73 graph->edge_num = 0; 74 for (v = 1; v <= graph->vertex_num; v++) { 75 graph->g[v].head = NULL; 76 } 77 return graph; 78 } 79 80 /* 81 insert a edge to graph.We will distinct oriented graph and undirected graph 82 The e->v1 and e->v2 are the vertexs' indexs in the adjacent table 83 @param graph The graph you want to insert edge 84 @param e The edge you want to insert the graph 85 @param isOriented Whether the graph is oriented graph.If the graph is oriented 86 we will set adjacent table graph[v1]->head=v2 and set graph[v1].head=v2 87 otherwise we only set graph[v1].head=v2 88 */ 89 void insertEdge(adjacentTableGraph graph, edge e, int isOriented) { 90 ptrToAdjNode newNode; 91 newNode = (ptrToAdjNode) malloc(sizeof(struct adjNode)); 92 newNode->adjVertex = e->v2; 93 newNode->weight = e->wight; 94 newNode->next = graph->g[e->v1].head; 95 graph->g[e->v1].head = newNode; 96 if (!isOriented) { 97 newNode = (ptrToAdjNode) malloc(sizeof(struct adjNode)); 98 newNode->adjVertex = e->v1; 99 newNode->weight = e->wight; 100 newNode->next = graph->g[e->v2].head; 101 graph->g[e->v2].head = newNode; 102 } 103 } 104 105 adjacentTableGraph buildGraph(int isordered) { 106 adjacentTableGraph graph; 107 edge e; 108 vertex v; 109 int vertex_num; 110 scanf("%d", &vertex_num); 111 graph = createGraph(vertex_num); 112 scanf("%d", &(graph->edge_num)); 113 if (graph->edge_num) { 114 e = (edge) malloc(sizeof(struct eNode)); 115 for (v = 0; v < graph->edge_num; v++) { 116 scanf("%d %d", &e->v1, &e->v2); 117 e->v1--; 118 e->v2--; 119 e->wight = 1; 120 insertEdge(graph, e, isordered); 121 } 122 } 123 return graph; 124 } 125 126 /*==============================define a queue=====================================================*/ 127 /*define a list to store the element in the queue*/ 128 typedef vertex elementType; 129 typedef struct node *pList; 130 typedef struct node { 131 elementType element; 132 struct node *next; 133 }; 134 135 /*define a queue to point the list*/ 136 typedef struct node2 *pQueue; 137 typedef struct node2 { 138 pList front; /*the front point to point the head of the list*/ 139 pList rear; /*the rear point to point the rear of of the list*/ 140 }; 141 142 /*create a empty list to store the queue element*/ 143 pList createEmptyList() { 144 pList list; 145 list = (pList) malloc(sizeof(struct node)); 146 list->next = NULL; 147 return list; 148 } 149 /*create a empty queye*/ 150 pQueue createEmptyQueue() { 151 pQueue queue = (pQueue) malloc(sizeof(struct node2)); 152 queue->front = NULL; 153 queue->rear = NULL; 154 return queue; 155 } 156 157 /* 158 Wether the queue is empty 159 @param queue The queue need to adjust 160 @return If the queue is null,return 1 otherwise return 0 161 */ 162 int isQueueEmpty(pQueue queue) { 163 return (queue->front == NULL); 164 } 165 166 /* 167 Add a element to a queue,If the queue is null,we will create a new queue 168 @parama queue The queue we will add elememt to 169 @prama element The element we will add to queue 170 */ 171 void addQueue(pQueue queue, elementType element) { 172 if (isQueueEmpty(queue)) { 173 pList list = createEmptyList(); 174 list->element = element; 175 queue->front = queue->rear = list; 176 } else { 177 pList newNode = (pList) malloc(sizeof(struct node)); 178 newNode->element = element; 179 newNode->next = queue->rear->next; 180 queue->rear->next = newNode; 181 queue->rear = newNode; 182 } 183 } 184 185 /* 186 delete a element from a queue 187 @param queue The queue will be deleted a element 188 @return The element has been deleted 189 */ 190 elementType deleteEleFromQueue(pQueue queue) { 191 if (isQueueEmpty(queue)) { 192 printf("the queue is empty,don't allow to delete elemet from it!"); 193 return -1; 194 } else { 195 pList oldNode = queue->front; 196 elementType element = oldNode->element; 197 if (queue->front == queue->rear) { 198 queue->rear = queue->front = NULL; 199 } else { 200 queue->front = queue->front->next; 201 } 202 free(oldNode); 203 return element; 204 } 205 } 206 207 208 void unWeight(adjacentTableGraph graph, int *dist, int *path, vertex startPoint) { 209 dist[startPoint] = 0; 210 vertex v; 211 ptrToAdjNode w; 212 pQueue queue = createEmptyQueue(); 213 addQueue(queue, startPoint); 214 while (!isQueueEmpty(queue)) { 215 v = deleteEleFromQueue(queue); 216 for (w = graph->g[v].head; w; w = w->next) { 217 if (dist[w->adjVertex] == -1) { 218 addQueue(queue, w->adjVertex); 219 dist[w->adjVertex] = dist[v] + 1; 220 path[w->adjVertex] = v; 221 } 222 } 223 } 224 } 225 226 /* 227 * Fill a array with value 228 * @param arr The array need to be filled 229 * @param length The length of the array 230 * @param filledValue The value the array will be filled 231 */ 232 void fullArray(int *arr, int length, int filledValue) { 233 int i; 234 for (i = 0; i < length; i++) { 235 arr[i] = filledValue; 236 } 237 } 238 239 /*============================define a stack to print result=============*/ 240 typedef int stackElement; 241 typedef struct node3 { 242 stackElement element; 243 struct node3 *next; 244 } sta, *pStack; 245 246 pStack createEmptyStack() { 247 pStack stack; 248 stack = (pStack) malloc(sizeof(sta)); 249 if (stack) { 250 stack->next = NULL; 251 } 252 return stack; 253 } 254 255 int isEmpty(pStack stack) { 256 if (stack->next == NULL) { 257 return 1; 258 } else { 259 return 0; 260 } 261 } 262 263 void push(pStack stack, stackElement element) { 264 pStack node = (pStack) malloc(sizeof(sta)); 265 node->element = element; 266 node->next = stack->next; 267 stack->next = node; 268 } 269 270 stackElement pop(pStack stack) { 271 stackElement element; 272 pStack topHead; 273 if (isEmpty(stack)) { 274 printf("the stack is empty,can not pop"); 275 return -65536; 276 } else { 277 topHead = stack->next; 278 stack->next = topHead->next; 279 element = topHead->element; 280 free(topHead); 281 return element; 282 } 283 } 284 285 void findPath(int *path, int length, int destination) { 286 pStack stack = createEmptyStack(); 287 vertex v; 288 int index = destination; 289 push(stack, index); 290 while (v != -1) { 291 v = path[index]; 292 push(stack, v); 293 index = v; 294 } 295 296 pop(stack); 297 while (!isEmpty(stack)) { 298 stackElement element = pop(stack); 299 printf("%d ", element+1); 300 } 301 } 302 303 int main() { 304 adjacentTableGraph graph = buildGraph(1); 305 int dist[graph->vertex_num]; 306 int path[graph->vertex_num]; 307 fullArray(dist, graph->vertex_num, -1); 308 fullArray(path, graph->vertex_num, -1); 309 unWeight(graph, dist, path, 2); 310 findPath(path, graph->vertex_num, 6); 311 printf("just test"); 312 return 0; 313 }