Graph C++ vectors

I got the program to run right but for some reason when i select option D which is to show the total amount of even and odd vectors it just shows me the matrix again. please any help would be awesome.

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482


#include <string.h>

const int defMaxGraphSize = 10, // Default number of vertices
vertexLabelLength = 11, // Length of a vertex label
filenameLength = 11, // Length of a filename
infiniteEdgeWt = 9999;



class Vertex
{
public:

char label [vertexLabelLength]; 
};


class WtGraph
{
public:


WtGraph ( int maxNumber = defMaxGraphSize );


~WtGraph ();


int load(char *filename);
void insertVertex ( Vertex newVertex ); // Insert vertex
void insertEdge ( char *v1, char *v2, int wt ); // Insert edge
int retrieveVertex ( char *v, Vertex &vData ) const;
// Get vertex
int edgeWeight ( char *v1, char *v2, int &wt );
// Get edge wt.
void removeVertex ( char *v ); // Remove vertex
void removeEdge ( char *v1, char *v2 ); // Remove edge
void clear (); // Clear graph


int empty () const; 
int full () const;
void degree();


void showStructure ();

private:


int index ( char *v ) const; // Converts vertex label to an

int& edge ( int row, int col ); // Set/get edge weight using



int maxSize, // Maximum number of vertices in the graph
size; // Actual number of vertices in the graph
Vertex *vertexList; // Vertex list
int *adjMatrix; // Adjacency matrix
};



#include <iostream>
#include <fstream>
#include <string>
#include <assert.h>

using namespace std;


WtGraph:: WtGraph ( int maxNumber )


: maxSize(maxNumber),
size(0)
{
vertexList = new Vertex [ maxSize ];
assert ( vertexList != 0 );

adjMatrix = new int [ maxSize*maxSize ];
assert ( adjMatrix != 0 );
}


WtGraph:: ~WtGraph ()



{
delete [] vertexList;
delete [] adjMatrix;
}


int WtGraph:: load ( char *filename )
{
Vertex testVertex; // Vertex
char cmd, v1[vertexLabelLength], v2[vertexLabelLength];
int wt;
ifstream infile;
infile.open(filename);
infile >> cmd;
while (cmd != '#')
{
if (cmd == 'v')
{
infile >> v1;
strcpy(testVertex.label,v1);
insertVertex(testVertex);
}
else
{
infile >> v1 >> v2 >> wt;
insertEdge(v1,v2,wt);
}
infile >> cmd;
}
infile.close();
return 1;
}



void WtGraph:: degree()
{
// This is where it is supposed to add up odds and evens.
int even = 0;
int odd = 0;
int sum = 0;
int i = 0, j = 0;
while(edge(i,j) != infiniteEdgeWt)
{
for(i=0; i < size; i++)
{
for(j=0; i < size; j++)
{
if(edge(i,j) < infiniteEdgeWt)
{
sum = sum + edge(i,j);
}
if(sum == 0)
{
even=even + 1;
}
else
{
odd = odd +1;
}
cout << "The sum of the row is: " << sum << endl;
cout << "Even Rows: " << even << endl;
cout << "Odd Rows: " << odd << endl;
}
sum = 0;
}
}
return;
}


void WtGraph:: insertVertex ( Vertex newVertex )



{
int pos, 
j; 

assert ( size != maxSize ); 

pos = index(newVertex.label);
vertexList[pos] = newVertex;

if ( pos == size )
{
size++;
for ( j = 0 ; j < size ; j++ )
{
edge(size-1,j) = infiniteEdgeWt;
edge(j,size-1) = infiniteEdgeWt;
}
}
}


void WtGraph:: insertEdge ( char *v1, char *v2, int wt )


{
int idx_v1 = index(v1), 
idx_v2 = index(v2); 

assert ( idx_v1 != size && idx_v2 != size );

edge(idx_v1,idx_v2) = wt;
edge(idx_v2,idx_v1) = wt;
}



int WtGraph:: retrieveVertex ( char *v, Vertex &vData ) const


{
int pos,
result; 

pos = index(v);
if ( pos != size )
{
vData = vertexList[pos];
result = 1;
}
else
result = 0;

return result;
}


int WtGraph:: edgeWeight ( char *v1, char *v2, int &wt )


{
int idx_v1 = index(v1), 
idx_v2 = index(v2);

assert ( idx_v1 != size && idx_v2 != size );

wt = edge(idx_v1,idx_v2);
return ( wt != infiniteEdgeWt );
}


void WtGraph:: removeVertex ( char *v )

{
int idx_v = index(v), 
j, k; /
assert ( idx_v != size );

for ( j = idx_v ; j < size-1 ; j++ ) 
vertexList[j] = vertexList[j+1];

for ( j = idx_v ; j < size-1 ; j++ ) 
for ( k = 0 ; k < size ; k++ )
edge(j,k) = edge(j+1,k);

for ( j = idx_v ; j < size-1 ; j++ ) 
for ( k = 0 ; k < size ; k++ )
edge(k,j) = edge(k,j+1);

size--;
}


void WtGraph:: removeEdge ( char *v1, char *v2 )

{
int idx_v1 = index(v1),
idx_v2 = index(v2); 

assert ( idx_v1 != size && idx_v2 != size );

edge(idx_v1,idx_v2) = infiniteEdgeWt;
edge(idx_v2,idx_v1) = infiniteEdgeWt;
}


void WtGraph:: clear ()



{
size = 0;
}



int WtGraph:: empty () const

{
return ( size == 0 );
}


int WtGraph:: full () const



{
return ( size == maxSize );
}


void WtGraph:: showStructure ()

{
int wt, 
row, col; 

if ( size == 0 )
cout << "Empty graph" << endl;
else
{
cout << endl << "Vertex list : " << endl;
for ( row = 0 ; row < size ; row++ )
cout << row << '\t' << vertexList[row].label << endl;

cout << endl << "Edge matrix : " << endl << '\t';
for ( col = 0 ; col < size ; col++ )
cout << col << '\t';
cout << endl;
for ( row = 0 ; row < size ; row++ )
{
cout << row << '\t';
for ( col = 0 ; col < size ; col++ )
{
wt = edge(row,col);
if ( wt == infiniteEdgeWt )
cout << "- \t";
else
cout << wt << '\t';
}
cout << endl;
}
}
}


int WtGraph:: index ( char *v ) const



{
int j; 

for ( j = 0 ;
j < size && strcmp(vertexList[j].label,v) != 0 ;
j++ );
return j;
}


int& WtGraph:: edge ( int row, int col )


{
return adjMatrix[maxSize*row+col];
}




void main()
{
WtGraph testGraph(10); // Test graph
Vertex testVertex; // Vertex
char v1[vertexLabelLength],
v2[vertexLabelLength], // Vertex labels
filename[filenameLength], // Filename
cmd; // Input command
int wt; // Edge weight

cout << endl << "Commands:" << endl;
cout << " L : Load a weighted graph" << endl;\
cout << " +v : Insert (or update) vertex v" << endl;
cout << " =v w wt : Insert an edge with weight wt between "
<< "vertices v and w" << endl;
cout << " ?v : Retrieve vertex" << endl;
cout << " #v w : Display the weight of the edge between "
<< "vertices v and w" << endl;
cout << " -v : Remove vertex v" << endl;
cout << " !v w : Remove the edge between vertices v and w"
<< endl;
cout << " D : Output the number of even and odd degree vertices" << endl;
cout << " C : Clear the graph" << endl;
cout << " E : Empty graph?" << endl;
cout << " F : Full graph?" << endl;
cout << " Q : Quit the test program" << endl;
cout << endl;

do
{
testGraph.showStructure(); // Output graph

cout << endl << "Command: "; // Read command
cin >> cmd;
if ( cmd == '+' || cmd == '?' || cmd == '-' )
cin >> v1;
else if ( cmd == '#' || cmd == '!' )
cin >> v1 >> v2;
else if ( cmd == '=' )
cin >> v1 >> v2 >> wt;
else if (cmd == 'L' || cmd == 'l')
{
cout << "Enter filname: ";
cin >> filename;
}

switch ( cmd )
{
case '+' : // insertVertex
cout << "Insert vertex : " << v1 << endl;
strcpy(testVertex.label,v1);
testGraph.insertVertex(testVertex);
break;

case '=' : // insertEdge
cout << "Insert edge : " << v1 << " " << v2 << " "
<< wt << endl;
testGraph.insertEdge(v1,v2,wt);
break;

case '?' : // retrieveVertex
if ( testGraph.retrieveVertex(v1,testVertex) )
cout << "Vertex " << v1 << " exists" << endl;
else
cout << "Vertex NOT found" << endl;
break;

case '#' : // edgeWeight
if ( testGraph.edgeWeight(v1,v2,wt) )
cout << "Weight = " << wt << endl;
else
cout << "No edge between these vertices" << endl;
break;

case '-' : // removeVertex
cout << "Remove vertex " << v1 << endl;
testGraph.removeVertex(v1);
break;

case '!' : // removeEdge
cout << "Remove the edge between vertices "
<< v1 << " and " << v2 << endl;
testGraph.removeEdge(v1,v2);
break;

case 'C' : case 'c' : // clear
cout << "Clear the graph" << endl;
testGraph.clear();
break;

case 'E' : case 'e' : // empty
if ( testGraph.empty() )
cout << "Graph is empty" << endl;
else
cout << "Graph is NOT empty" << endl;
break;

case 'F' : case 'f' : // full
if ( testGraph.full() )
cout << "Graph is full" << endl;
else
cout << "Graph is NOT full" << endl;
break;

case 'L' : case 'l' : // load
if ( testGraph.load(filename) )
cout << "Graph loaded from " << filename << endl;
else
cout << "Invalid input file" << endl;
break;

case 'D' : case 'd' : // output even/odd degree count
testGraph.degree();
break;

case 'Q' : case 'q' : // Quit test program
break;

default : // Invalid command
cout << "Invalid command" << endl;
}
}
while ( cmd != 'Q' && cmd != 'q' );
} 
Topic archived. No new replies allowed.