/* * So, maybe this is the core of the spanning-tree protocol used between * switches to avoid link redundancy which will definitely cause broadcast * storm. Of course, in the IOS of CISCO catalyst series, the codes must be * optimized. I am just so curious what the original codes in IOS will be. * But it is hard to get such resources, and disassembily is impossible for * me, which is also illegal. * by: double; version 0.1; date: 2009-06-21 */ #include "/usr/c/head.h" #define MAX 20 #define INT_MAX ((int)(~0U >> 1)) /*define INT_MAX, this will be used to initilize the matrix graph*/ typedef char vertex_type; typedef int arc_type; typedef struct matrix_graph { vertex_type vexs[MAX]; arc_type arcs[MAX][MAX]; int vexnum, arcnum; }matrix_graph; typedef struct close { /* define the adjacency array, which contents the vertex and weight of it; /* vertex_type data; arc_type weight; }close; close closedge[MAX]; /* define two overall variables */ matrix_graph G; int locate_vex(matrix_graph G, vertex_type v) { int i; for (i = 0; i < G.vexnum; i++) if (G.vexs[i] == v) return i; return -1; } status init_graph(matrix_graph * G) { /* It is so complex to * use "if (scanf()) while () * {;}" style for just input * an int or char type variable * Maybe, MACRO is a good idea. */ int temp, i, j, p1, p2; vertex_type v1, v2; printf("Input the vexnum:"); if (scanf("%d", & G->vexnum) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} for (i = 0; i < G->vexnum; i++) { printf("No.%d:", i + 1); if (scanf("%c", &G->vexs[i]) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} } for (i = 0; i < G->vexnum; i++) for (j = 0; j < G->vexnum; j++) G->arcs[i][j] = INT_MAX; printf("Input the arcnum:"); if (scanf("%d", &G->arcnum) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} for (i = 0; i < G->arcnum; i++) { printf("No.%d, v1:", i + 1); if (scanf("%c", &v1) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} printf("No.%d, v2:", i + 1); if (scanf("%c", &v2) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} p1 = locate_vex(*G, v1); if (p1 == -1) { printf("v1 = %c out of range!/n", v1); exit(ERROR); } p2 = locate_vex(*G, v2); if (p2 == -1) { printf("v2 = %c out of range!/n", v2); exit(ERROR); } printf("Weight:"); if (scanf("%d", &G->arcs[p1][p2]) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} G->arcs[p2][p1] = G->arcs[p1][p2]; } return OK; } int minimum(close * closedge) { int i, k = -1; arc_type temp; for (i = 0; i < G.vexnum; i++) if (closedge[i].weight != 0 && closedge[i].weight != INT_MAX) { temp = closedge[i].weight; k = i; } for (i = 0; i < G.vexnum; i++) if (closedge[i].weight != 0 && closedge[i].weight < temp) { temp = closedge[i].weight; k = i; } return k; } status mini_spanning_tree (matrix_graph G, vertex_type v) {/* Core function */ int k, i, j; k = locate_vex(G, v); for (i = 0; i < G.vexnum; i++) if (i != k) { closedge[i].data = v; closedge[i].weight = G.arcs[k][i]; } closedge[k].weight = 0; for (i = 1; i < G.vexnum; i++) { k = minimum(closedge); printf("(%c,%c)/t", closedge[k].data, G.vexs[k]); closedge[k].weight = 0; for (j = 0; j < G.vexnum; j++) if (G.arcs[k][j] < closedge[j].weight) { closedge[j].data = G.vexs[k]; closedge[j].weight = G.arcs[k][j]; } } return OK; } status print_graph(matrix_graph G) { /* It is just for debug*/ int i, j; for (i = 0; i < G.vexnum; i++) printf("%c/t", G.vexs[i]); printf("/n"); for (i = 0; i < G.vexnum; i++) { for (j = 0; j < G.vexnum; j++) printf("%d/t", G.arcs[i][j]); printf("/n/n/n/n"); } } int main(void) { vertex_type v; int temp, i, j; init_graph(&G); printf("Matrix graph initilized OK!/n"); printf("The graph is:/n"); print_graph(G); putchar('/n'); printf("Input the first vertex:"); if (scanf("%c", &v) != EOF) while ((temp = getchar()) != '/n' && temp != EOF) {;} mini_spanning_tree(G, v); printf("/n"); return 0; }