STEP 1: Adjacency Matrix in an Array
Write pseudocode (comments in your program) for how you can determine the degree of a vertex given the adjacency matrix.
Download Math233 [url removed, login to view] (or in Doc Sharing). This contains the shell C++ weighted graph data structure class and test program ([url removed, login to view]) and sample input file([url removed, login to view]). Make sure to notice in the WtGraph constructor it allocates the adjacency matrix as a one-dimensional array large enough to store an NxN two-dimensional matrix.
adjMatrix = new int [ maxSize*maxSize ];
Then the WtGraph "edge" function is used to access or update a row,column position using "offset" in the one-dimensional array.
edge ( int row, int col ) //Note that, for example edge(0,3), represents the row 0 column 3 entry of the edge matrix
// Gets/sets adjMatrix[row][col].
Note that, when running the program, instead of loading the above file, you may also insert vertices one at a time with the commands:
The weights can also be inserted one at a time:
=0 1 10
inserts a weight of 10 to to the edge joining v(0)=a and v(1)=b.
STEP 2: Count Odd and Even Degree Vertices
Write a new member function "degree" for the Wtgraph C++ class given. The "degree" function should determine and output how many even degree vertices and how many odd degree vertices a weighted, undirected graph has. The .cpp source code has a marker where you are to insert your code.
An idea is if you think of the edge matrix above as a weighted adjacency matrix where
M(i,j) = the number of edges joining v(i) & v(j), with the exception that for M(i,i) count each loop twice.
this will guarantee that the sum of row i is the degree of v(i).
Some general notes:
You will only add code to the
void WtGraph:: degree()
portion of the .cpp file. That will be the only portion of the program where you will do modifications.
For each row you want to:
(i) add up all its entries.
e.g., row 0 in up above is
0 10 0 5 0 0 1 0 0
Add them: 0 +10+ 0+ 5 + 0 + 0 + 1 + 0 + 0 = 16
(Thus, vertex 0 has degree 16)
(ii) determine if that sum is odd or even
e.g., for row 0 it is even, since 16 is even <----row 0 has even degree
16%2 =0 <-----------------------------------16/2 has remainder 0
Then determine how many rows have an even sum and how many have an odd sum. This will involve keeping track and counting the number of even sums and the number of odd sums.
The variable declared as
in the .cpp program is the number of vertices and thus it is the number of rows (and columns, too).
The variable declared as
int& edge ( int row, int col );
in the .cpp program is the actual matrix that you see in the iLab itself.
Thus, edge(0,6) would represent the 0,6 element of the matrix.
Note that up above edge(0,6)=1
Note this command in the .cpp file
infiniteEdgeWt = 9999;
A use for infiniteEdgeWT is that blank, in this case 0, entries are preset to infiniteEdgeWT. Hence, in the matrix up above we have:
Thus, you will need a condition so that blank entries are not added to the sum. Here is one idea for what can be inserted inside the innermost nested for loop:
if (edge(i,j) < infiniteEdgeWt) // this is needed because blank entries are preset to 9999 and not 0 in this program, do not want to add 9999 to the sum
then add edge(i,j) to the sum.
Command: L (user input)
Enter filname: [url removed, login to view] (user input)
Graph loaded from [url removed, login to view]
Vertex list :
Edge matrix :
0 1 2 3 4 5 6 7 8
0 - 10 - 5 - - 1 - -
1 10 - 4 - - - - - -
2 - 4 - - - 2 - - 8
3 5 - - - 5 - 4 - -
4 - - - 5 - 3 - - -
5 - - 2 - 3 - - - 2
6 1 - - 4 - - - 6 -
7 - - - - - - 6 - 6
8 - - 8 - - 2 - 6 -
Number of Even Degree Vertices = 7
Number of Odd Degree Vertices = 2
(Note: If you choose to load [url removed, login to view] as above, make sure your .cpp file and [url removed, login to view] files are in the same folder where your current .cpp file exists.)