Hi everyone,
I think I nearly have this program running right except for printing the adjacency matrix. Here's the code I have:
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
class Graph
{
public:
static const int MAX_ROWS = 6;
static const int MAX_COLS = 6;
Graph();
~Graph() { }; // Destructor does nothing
bool adjacent(int i, int j);
void updateNode(int position, string data);
void insertEdge(int i, int j);
string retrieve(int position);
// Other primative methods usually used in graphs. Implement the
// delete methods for your lab exercise ...
void deleteNode(int position);
void deleteEdge(int i, int j);
void printGraph();
void printData();
private:
// This matrix defines each node's neighbors, where 0 means
// "is not a neighbor of" and 1 means "is a neighbor of".
int adjacencyMatrix[MAX_ROWS][MAX_COLS];
// This array will hold the data. Notice we only need a
// one-dimensional array since each node is represented
// in both the rows and columns of the adjacency matrix.
// So we'll use rows, but columns will also work.
string nodes[MAX_ROWS];
};
Graph::Graph()
{
// Initialize every row and column in the matrix to 0.
memset(adjacencyMatrix, 0, sizeof(adjacencyMatrix));
}
// Predicate method reports true if two nodes are adjacent, false
// otherwise.
bool Graph::adjacent(int i, int j)
{
return (adjacencyMatrix[i][j] = 1);
}
// Inserts an edge between the two given nodes
void Graph::insertEdge(int i, int j)
{
// TODO: Set the position i, j in the matrix equal to 1
(adjacencyMatrix[i][j] = 1);
return;
}
// Sets data the next node in the list.
void Graph::updateNode(int position, string data)
{
nodes[position] = data;
return;
}
void Graph::deleteNode(int position)
{
// TODO: Set the position i, j in the matrix equal to 0
int i = 0;
int j = 0;
(adjacencyMatrix[i][j] = 0);
}
void Graph::deleteEdge(int i, int j)
{
// TODO: Set the node at given position equal to ""
string nodes = "";
}
// Retrieve a node at the given position
string Graph::retrieve(int position)
{
string data;
// TODO: Set data equal to the value of the nodes array at
// the provided position.
data = nodes[position];
return data;
}
// Display the adjacency matrix.
void Graph::printGraph()
{
// TODO: Add the necessary code to print the adjacency matrix
// Use two for-loops within each other. One with index i and the other with
// index j. Print nodes[i][j].
cout << "Adjacency Matrix:" << endl;
for (int i = 0; i < 6; i++)
{
cout << "" << i << endl;
for (int j = 0; j < 6; j++)
cout << "" << j << endl;
}
}
// Display all of the nodes in the list.
void Graph::printData()
{
cout << "The data are: " << endl << endl;
for (int i=0; i < MAX_ROWS; i++) {
cout << i << ": " << nodes[i] << endl;
}
cout << endl;
return;
}
The output I'm supposed to get is this:
Adjacency Matrix:
0 1 2 3 4 5
0 0 1 1 1 0 0
1 0 0 0 0 1 0
2 0 1 0 0 1 0
3 0 0 0 0 0 0
4 0 0 0 0 0 0
5 0 1 0 1 0 0
The data are:
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
0 is connected to 1
3 is not connected to 2
** Press any key to continue **
I just don't get the matrix as shown. I also just realized that MY output says that 3 IS connected to 2 instead of IS NOT. Can anyone help me sort this out? Thanks.