1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#ifndef _GRAPH_INTERFACE
#define _GRAPH_INTERFACE
template < class LabelType>
class GraphInterface
{
public:
/** Gets the number of vertices in this graph.
@pre None.
@return The number of vertices in the graph. */
virtual int getNumVertices() const = 0;
/** Gets the number of edges in this graph.
@pre None.
@return The number of edges in the graph. */
virtual int getNumEdges() const = 0;
/** Creates an undirected edge in this graph between two vertices
that have the given labels. If such vertices do not exist, creates
them and adds them to the graph before creating the edge.
@param start A label for the first vertex.
@param end A label for the second vertex.
@param edgeWeight The integer weight of the edge.
@return True if the edge is created, or false otherwise. */
virtual bool add(LabelType start, LabelType end, int edgeWeight) = 0;
/** 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.
@pre None.
@param start A label for the first vertex.
@param end A label for the second vertex.
@return True if the edge is removed, or false otherwise. */
virtual bool remove(LabelType start, LabelType end) = 0;
/** Gets the weight of an edge in this graph.
@return The weight of the specified edge.
If no such edge exists, returns a negative integer. */
virtual int getEdgeWeight(LabelType start, LabelType end) const = 0;
/** Performs a depth-first search of this graph beginning at the given
vertex and calls a given function once for each vertex visited.
@param start A label for the first vertex.
@param visit A client-defined function that performs an operation on
or with each visited vertex. */
virtual void depthFirstTraversal(LabelType start, void visit(LabelType&)) = 0;
/** Performs a breadth-first search of this graph beginning at the given
vertex and calls a given function once for each vertex visited.
@param start A label for the first vertex.
@param visit A client-defined function that performs an operation on
or with each visited vertex. */
virtual void breadthFirstTraversal(LabelType start, void visit(LabelType&)) = 0;
/** Destroy this graph and frees assigned memory*/
virtual ~GraphInterface(){ }
}; // end GraphInterface
#endif
#ifndef _GRAPH
#define _GRAPH
#include "GraphInterface.h"
#include <string>
#include <fstream>
#include <iostream>
template<class LabelType>
class Graph : public GraphInterface<LabelType>
{
private:
LabelType nVal;
LabelType** adj;
bool* visitedEdge;
int edgeWeight;
int numOfEdges;
int numOfVertices;
public:
Graph(LabelType n);
int getNumVertices() const;
int getNumEdges() const;
void add(LabelType start, LabelType end);
bool remove(LabelType start, LabelType end);
int getEdgeWeight(LabelType start, LabelType end) const;
void breadthFirstTraversal(LabelType start, void visit(LabelType&));
void depthFirstTraversal(LabelType start, void visit(LabelType&));
void display();
};
template<class LabelType>
Graph<LabelType>::Graph(LabelType nVal)
{
this->nVal = nVal;
visitedEdge = new bool[nVal];
adj = new int* [nVal];
for (int it = 0; it < nVal; it++)
{
adj[it] = new int[nVal];
for (int jth = 0; jth < nVal; jth++)
{
adj[it][jth] = 0;
}
}
}
template<class LabelType>
void Graph<LabelType>::add(LabelType start, LabelType end)
{
if (start > nVal || end > nVal || start < 0 || end < 0)
{
std::cout << "Invalid edge!\nVal";
}
else
{
adj[start - 1][end - 1] = 1;
}
}
template<class LabelType>
int Graph<LabelType>::getNumVertices() const
{
return numOfVertices;
}
template<class LabelType>
int Graph<LabelType>::getNumEdges() const
{
return numOfEdges;
}
template<class LabelType>
int Graph<LabelType>::getEdgeWeight(LabelType start, LabelType end) const
{
return numOfVertices;
}
template<class LabelType>
bool Graph<LabelType>::remove(LabelType start, LabelType end)
{
bool result = false;
if (start > nVal || end > nVal || start < 0 || end < 0)
{
std::cout << "Invalid edge!\nVal";
}
else
{
adj[start - 1][end - 1] = -1;
}
return result;
}
template<class LabelType>
void depthFirstTraversal(LabelType start, void visit(LabelType&)) {
}
template<class LabelType>
void breadthFirstTraversal(LabelType start, void visit(LabelType&)) {
}
#endif
|