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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include <iostream>
#include <string>
#include <vector>
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
template<class LabelType>
class GraphTwo : public GraphInterface<LabelType>
{
private:
// Define maximum number of nodes
static const int size = 10;
/*std::list <int>*/ int adj[size][size] = {0};
/*std::list <bool>*/ int visited[size] = {0};
public:
GraphTwo();
// 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<class LabelType>
GraphTwo<LabelType>::GraphTwo()
{}
template<class LabelType>
int GraphTwo<LabelType>::getNumVertices() const
{
return size;
}
template<class LabelType>
int GraphTwo<LabelType>::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;
}
template<class LabelType>
bool GraphTwo<LabelType>::add(LabelType start, LabelType end, int edgeWeight)
{
adj[start][end] = edgeWeight;
adj[end][start] = edgeWeight;
return true;
}
template<class LabelType>
bool GraphTwo<LabelType>::remove(LabelType start, LabelType end)
{
adj[start][end] = 0;
adj[end][start] = 0;
return true;
}
template<class LabelType>
int GraphTwo<LabelType>::getEdgeWeight(LabelType start, LabelType end) const
{
return adj[start][end];
}
template<class LabelType>
void GraphTwo<LabelType>::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);
}
}
template<class LabelType>
void GraphTwo<LabelType>::breadthFirstTraversal(LabelType start, void visit(LabelType&))
{
// Vector that contains the adjacent nodes
std::vector<LabelType> 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;
}
int main()
{
GraphTwo<int> g2;
}
|