### How allow the graph to be either weighted or unweighted and either directed or undirected?

I have code for an adjacency matrix which is use to represent a graph. How would I get this code to allow the graph to be either weighted or unweighted and either directed or undirected.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199`` `````` #include "GraphInterface.h" #include #include #include #include #ifndef _GRAPH #define _GRAPH template class Graph : public GraphInterface { private: // Define maximum number of nodes static const int size = 10; int adj[size][size] = { 0 }; bool visited[size] = { 0 }; public: Graph(); // Get the number of vertices int getNumVertices() const; // Get the number of the edges int getNumEdges() const; // Creates an undirected edge in this graph between two vertices // that have the given labels.If such vertices do not exist, creates // themand adds them to the graph before creating the edge bool add(LabelType start, LabelType end, int edgeWeight); // Removes an edge from this graph. If a vertex has no other edges, // it is removed from the graph since this is a connected graph. bool remove(LabelType start, LabelType end); // Gets the weight of an edge in this graph. int getEdgeWeight(LabelType start, LabelType end) const; // Performs a depth - first search of this graph beginning at the given // vertex and calls a given function once for each vertex visited. void depthFirstTraversal(LabelType start, void visit(LabelType&)); // Performs a breadth - first search of this graph beginning at the given // vertex and calls a given function once for each vertex visited. void breadthFirstTraversal(LabelType start, void visit(LabelType&)); }; template Graph::Graph() {} // ------------------------------------------- // getNumVertices: get the number of vertices // ------------------------------------------- template int Graph::getNumVertices() const { return size; } // ----------------------------------------- // getNumEdges: get the number of edges // ----------------------------------------- template int Graph::getNumEdges() const { int edgeCount = 0; for (int i = 0; i < size; ++i) for (int j = 0; j < size; ++j) if (adj[i][j] != 0) ++edgeCount; return edgeCount / 2; } // ------------------------------------------------------------------------ // add: Creates an undirected edge in this graph between two vertices // that have the given labels.If such vertices do not exist, creates // themand adds them to the graph before creating the edge // start: 1st vertex // end: last vertex // edgeWeight: integer weight of the edge // returns true if edge was created // ------------------------------------------------------------------------ template bool Graph::add(LabelType start, LabelType end, int edgeWeight) { adj[start][end] = edgeWeight; adj[end][start] = edgeWeight; return true; } // -------------------------------------------------------------- // remove: Removes an edge from this graph. If a vertex has no other edges, // it is removed from the graph since this is a connected graph. // start: 1st vertex // end: last vertex // edgeWeight: integer weight of the edge // returns true if edge was removed // ------------------------------------------------------------------------ template bool Graph::remove(LabelType start, LabelType end) { adj[start][end] = 0; adj[end][start] = 0; return true; } // ---------------------------------------------------------------------------- // getEdgeWeight: gets the weight of the edge in this graph // start: 1st vertex // end: last vertex // return weight of the specified edge // ----------------------------------------------------------------------------- template int Graph::getEdgeWeight(LabelType start, LabelType end) const { return adj[start][end]; } // ------------------------------------------------------------------------------------------- // depthFirstTraversal: Performs a depth-first search of this graph beginning at the given // vertex and calls a given function once for each vertex visited. // start: 1st vertex // visit A client-defined function that performs an operation on // for with each visited vertex // -------------------------------------------------------------------------------------------- template void Graph::depthFirstTraversal(LabelType start, void visit(LabelType&)) { // Visit the current node visit(start); // Mark the current node as visited visited[start] = true; // For all other nodes for (int i = 0; i < size; ++i) { if (adj[start][i] != 0 && (!visited[i])) depthFirstTraversal(i, visit); } } // ------------------------------------------------------------------------------------------ // breadthFirstTraversal: Performs a breadth-first search of this graph beginning at the given // vertex and calls a given function once for each vertex visited. // start: 1st vertex // visit A client-defined function that performs an operation on // for with each visited vertex // -------------------------------------------------------------------------------------------- template void Graph::breadthFirstTraversal(LabelType start, void visit(LabelType&)) { // Vector that contains the adjacent nodes std::vector alist; alist.push_back(start); // Mark current node as visited visited[start] = true; int check; while (!alist.empty()) { check = alist[0]; // Print node visit(check); alist.erase(alist.begin()); // Every vertex adjacent for (int i = 0; i < size; ++i) { if (adj[check][i] != 0 && (!visited[i])) { // Add node to the queue alist.push_back(i); // Mark next node as visited visited[i] = true; } } } // Reset visited as all false for (int i = 0; i < size; ++i) visited[i] = false; } #endif ``````
if you set weight = 0, then the edge would be unweighted
if when you add an edge from start to end you also add an edge from end to start, then the graph would be undirected
Okay that helps. Thank you @ne555.
Topic archived. No new replies allowed.