For credit, your program must at least compile and run properly. It should also contain adequate comments such as:
Each student must write and submit his or her own code for the assignment. Discussion of the assignment is allowed. You may share ideas but not code files.
Sample Output (user input in bold and underlined):
Enter filname: a.dat
Graph loaded from a.dat
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 a.dat as above, make sure your .cpp file and a.dat files are in the same folder where your current .cpp file exists.)
L A B S T E P S
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 iLab5.zip (or in Doc Sharing). This contains the shell C++ weighted graph data structure class and test program (TestWtgraph.cpp) and sample input file(a.dat). 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 int size;
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.
Here is an example with a 3X3 matrix
My tutorial contain Visual C++ project.
Please using Visual Studio to open this project.
Thank you for purchase my tutorial.
If you have more question or need help, please contact me via email email@example.com. I will help you any time.