-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphClient.c
103 lines (94 loc) · 2.72 KB
/
GraphClient.c
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
//-----------------------------------------------------------------------------
//
// GraphClient.c
//
// This is a fancy test client that calculates the Radius and Diameter of
// the graph defined on lines 41-49, along with its Central and Peripheral
// vertices. The definitions of these terms at:
//
// http://en.wikipedia.org/wiki/Distance_(graph_theory)
//
// Place this file in a directory with copies of your List.c, List.h, Graph.c,
// Graph.h and an appropriate Makefile, then compile and run. The output
// is contained in the file GraphClientOut.
//
// This program does not exercise all functions in your Graph ADT, but it
// does a pretty good job of testing BFS(). If your output differs from
// the above, you have a logical problem in either your Graph or List ADT.
//
// Remember you are required to submit a file named GraphTest.c with pa4 that
// exercises your Graph functions. Do not submit this file.
//
//-----------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include"Graph.h"
int main(int argc, char* argv[]){
int i, s, max, min, d, n=35;
List C = newList(); // central vertices
List P = newList(); // peripheral vertices
List E = newList(); // eccentricities
Graph G = NULL;
// Build graph G
G = newGraph(n);
for(i=1; i<n; i++){
if( i%7!=0 ) addEdge(G, i, i+1);
if( i<=28 ) addEdge(G, i, i+7);
}
addEdge(G, 9, 31);
addEdge(G, 17, 13);
addEdge(G, 14, 33);
// Print adjacency list representation of G
printGraph(stdout, G);
// Calculate the eccentricity of each vertex
for(s=1; s<=n; s++){
BFS(G, s);
max = getDist(G, 1);
for(i=2; i<=n; i++){
d = getDist(G, i);
max = ( max<d ? d : max );
}
append(E, max);
}
// Determine the Radius and Diameter of G, as well as the Central and
// Peripheral vertices.
append(C, 1);
append(P, 1);
min = max = front(E);
moveFront(E);
moveNext(E);
for(i=2; i<=n; i++){
d = get(E);
if( d==min ){
append(C, i);
}else if( d<min ){
min = d;
clear(C);
append(C, i);
}
if( d==max ){
append(P, i);
}else if( d>max ){
max = d;
clear(P);
append(P, i);
}
moveNext(E);
}
// Print results
printf("\n");
printf("Radius = %d\n", min);
printf("Central vert%s: ", length(C)==1?"ex":"ices");
printList(stdout, C);
printf("\n");
printf("Diameter = %d\n", max);
printf("Peripheral vert%s: ", length(P)==1?"ex":"ices");
printList(stdout, P);
printf("\n");
// Free objects
freeList(&C);
freeList(&P);
freeList(&E);
freeGraph(&G);
return(0);
}