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
|
#include "GraphInterface.h"
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#ifndef _GRAPH
#define _GRAPH
template<class LabelType>
class Graph : public GraphInterface<LabelType>
{
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<class LabelType>
Graph<LabelType>::Graph()
{}
// -------------------------------------------
// getNumVertices: get the number of vertices
// -------------------------------------------
template<class LabelType>
int Graph<LabelType>::getNumVertices() const
{
return size;
}
// -----------------------------------------
// getNumEdges: get the number of edges
// -----------------------------------------
template<class LabelType>
int Graph<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;
}
// ------------------------------------------------------------------------
// 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<class LabelType>
bool Graph<LabelType>::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<class LabelType>
bool Graph<LabelType>::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<class LabelType>
int Graph<LabelType>::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<class LabelType>
void Graph<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);
}
}
// ------------------------------------------------------------------------------------------
// 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<class LabelType>
void Graph<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;
}
#endif
|