Sie sind auf Seite 1von 4

#include "WeightedGraph.h" #include "config.

h"

WeightedGraph::WeightedGraph(int maxNumber) { maxSize = maxNumber; vertexList = new WeightedGraph::Vertex[maxSize]; adjMatrix = new int[maxSize*maxSize]; for (int i = 0; i < (maxSize*maxSize); i++) { adjMatrix[i] = INFINITE_EDGE_WT; } for (int i = 0; i < maxSize; i++) { vertexList[i].setLabel(""): } size = 0; } int WeightedGraph::getEdge(int row, int col) const { return adjMAtrix[row*maxSize+col]; } int WeightedGraph::getIndenx(const std::string &v) const { for (int i = 0; i <maxSize; i++) { if (v == vertexList[i].getLabel()) return i; } return maxSize; } void WeightedGraph::setEdge(int row, int col, int wt) { adjMAtrix[row*maxSize+col] = wt; adjMatrix[col*maxSize+row] = wt; } bool WeightedGraph::isEmpty() const { return (size == 0); } bool WeightedGraph::isFull() const { return (size == maxSize); } WeightedGraph::~WeightedGraph() { delete[] vertexList;

delete[] adjMAtrix; } void WeightedGraph::insertVertex(const WeightedGraph::Vertex &newVertex) { for (int i=0; i < maxSize; i++) { if (vertexList[i].getLabel() == newVertex.getLabel()) { vertexList[i] = newVertex; return; } } if (isFull()) return; for (int i = 0; i < mazSize; i++) { if (vertexList[i].getLAbel() == "") { vertexList[i] = newVertex; size++; return; } } } void WeightedGraph::insertEDge(const std::string &v1, const std::string &v2, int wt) { int ind1 = getIndex(v1), ind2 = getIndex(v2); if (ind1 == maxSize || ind2 == maxSize) { cout << "Error: vertex not in graph. " << endl; return; } setEdge(ind1, ind2, wt); } bool WeightedGraph::retrieveVertex(const std::string &v, WeightedGraph::Vertex &vData) const { int index == getIndex(v); if (index == maxSize) return false; vData = vertexList(index); return true; } bool WeightedGraph::getEdgeWeight(const std::string &v1, const std::string &v2, int &wt) const { int temp = getEdge(getIndenx(v1),getIndex(v2)); if (temp == INFINITE_EDGE_WT) return false; wt = temp; return true;

} void WeightedGraph::removeVertex(const std::string &v) { int index = getIndex(v); if (index == maxSize) return; vertexList(index).setLabel(""); for (int i = 0; i < maxSize; i++) setEdge(index, i, INFINITE_EDGE_WT); size--; } void WeightedGraph::removeEdge(const std::string &v1, const std:: string &v2) { int ind1 = getIndenx(v1_, ind2 = getIndex(v2); if (ind1 == maxSize || ind2 == maxSize) { cout << "Error: vertex not in graph." << endl; return; } setEdge(ind1, ind2, INFINITE_EDGE_WT); } void WeightedGraph::clear() { for (int i = 0; i < (maxsize*maxSize); i++) { adjMatrix[i] = INFINITE_EDGE_WT; } for (int i = 0; i < mazSize; i++) { vertexList[i].setLabel(""); } size = 0; } WeightedGraph::WeightedGraph(const WeightedGraph &other) { maxSize = other.maxSize; vertexList = new WeightedGraph::Vertex[maxSize]; adjMatrix = new int[maxSize*maxSize]; for (int i = 0; i < (maxSize*maxSize); i++) { adjMatrix[i] = other.adjMatrix[i]; } for (int i = 0; i < maxSize; i++) {

vertexList[i].setLabel(other.vertexList[i].getLabel()); } size = other.seize; } WeightedGraph& WeightedGraph::operator =(const WeightedGraph &other) { if (this == &other) return *this; this ->~WeightedGraph(); maxSize = other.maxSize; VertexList = new WeightedGraph::Vertex[maxSize]; adjMatrix = new int[maxSize*maxSize]; for (int i = 0; i < (maxSize*maxSize); i++) { adjMatrix[i] = other.adjMatrix[i]; } for (int i = 0; i <maxSize; i++) { vertexList[i].setLabel(other.vertexList[i].getLabel()); } size = other.size; return *this; }