2

Yes, this is a homework assignment (before you ask), but I'm having troubles and I feel like people on here are much less pretentious than my fellow CS students.

My question is, what would I need to do to create a matrix in a template graph class in c++ using a two dimensional vector. I always can visualize what I need to do, but run into troubles when it comes to typing it out.

Here is what I have so far:

using namespace std;

namespace GraphNameSpace
{

   enum isDirected {DIRECTED, UNDIRECTED};
   enum isWeighted {WEIGHTED, UNWEIGHTED};

   template <class T>
   class Graph
   {
      public:
         Graph<T>();
         Graph<T>(isDirected);
         Graph<T>(isWeighted);
         Graph<T>(isDirected, isWeighted);
         Graph<T>(isWeighted, isDirected);

         void createMatix();
         void destroy();
         bool isEmpty() const;
         bool isFull() const;
         bool isAdjacentTo(T fromVertex, T toVertex) const;
         int edgeWeight(T fromVertex, T toVertex) const;
         int edgeCount() const;
         int vertexCount() const;
         void insertVertex(T Vertex);
         void insertEdge(T fromVertex, T toVertex, int weight);
         void deleteVertex(T Vertex);
         void deleteEdge(T fromVertex, T toVertex);
         int findVertex(T Vertex) const;
         void dump() const;

      private:
         vector<int> row(100);
         int numVertices;
         int numEdges;
         static isDirected dir;
         static isWeighted wght;
   };
}

I guess the problem is with the vector row(100), but may also be with the void createMatrix(). I'm just desperately trying to understand how to do this, so an explanation with example code would be appreciated. (The rest of the code is exactly as he wants it). Thanks in advance for trying to help me, it's deeply appreciated.

  • [What have you tried?](http://mattgemmell.com/what-have-you-tried/) – Ivan Aksamentov - Drop Apr 02 '16 at 15:03
  • "static isDirected dir;" Trouble ahead! Using static means that every instance will have the same value - almost certainly not what you want. – ravenspoint Apr 02 '16 at 16:50
  • "but I'm having troubles and I feel like people on here are much less pretentious than my fellow CS students." -- One advice: No matter if in study, research or work, it is really important to talk with each other and work out things together. You will most likely learn from other opinions and have more fun. – Maikel Apr 02 '16 at 19:20
  • I have friends in my field of study that I do ask for advice, and I usually do in other fields, but I just get this feeling (I'm a pretty social person to be honest) that most people in this department feel like they are better than you just because they know more on the subject. They're willing to help, but feel the need to shame you for not knowing what they do. – David Corneliusen Apr 02 '16 at 19:38

1 Answers1

3

It seems like you need a two dimensional vector of T to store your vertices

Something like this:

vector < vector < T > > Vertices;

However, this is terribly inefficient!

Perhaps the thing to do is to have a one dimensional vector of vertices and then define an edge and store all the edges in another one dimensional vector. You can generate the adjacency matrix from this on the fly if needed.

vector < T > vertices;
typedef pair <int,int> edge_t;    // Indices into vertices vector
vector < edge_t > edges;

This wastes no memory.

The problem I see is that there is no way in this scheme to store properties for the edges ( e.g. weight or length or cost ). To do that you need to template for both the edges and the vertices

 template <class V, class E>
   class Graph

So now you need a vector for the user edges, a vector for the vertices, and a vector for the three indices: source vetrex, destination vertex, and edge.

Maybe something like this:

vector < V > vertices;
vector < E > edges;
struct adjacency_s {
   int src;
   int dst;
   int edge;
};
vector < adjacency_s > adjacencies;

In reality it is a bad idea to roll your own graph code. Better to learn how to use an established library as discussed here

Community
  • 1
  • 1
ravenspoint
  • 15,243
  • 4
  • 52
  • 87
  • I would use the established library, but for assignment purposes we are supposed to learn how to create the templated graph class on our own using (from what I understand) an adjacency matrix or an adjacency list. This can be done with arrays, vectors, or mapping. I don't know what would be the best approach, and yes, we must be able to have weight involved, whether it is actually a weighted graph, or just by having a filler of int 1. – David Corneliusen Apr 02 '16 at 16:31
  • I have described what I consider to be the best approach. – ravenspoint Apr 02 '16 at 16:47
  • Having a list (or a vector) of adjacencies is just one way of storing the graph. Having a matrix for enumerated vertices is just the other major way to do it. And for dense graphs it can be good (memory vs. access times). OP was asked to do a adjacency matrix (or not) and you simply propose not to do it without going into pro and contra. I like that you show alternatives but it bugs me to say matrices are "terrible" (it depends). – Maikel Apr 02 '16 at 19:10
  • @Maikel In the overwhelming majority of real world problems the adjacency matrix is extremely sparse. As a general purpose graph data structure a matrix is terrible. – ravenspoint Apr 03 '16 at 15:51
  • @ravenspoint Yes and I'll totally give you that! I also believe that you know a lot more about graphs than I do ;-). I just wanted to point to the studying aspect of this question. – Maikel Apr 03 '16 at 16:28