Grafy (ukazky)
Grafy
Neorientovany graf
class myGraph { private: bool** adjacencyMatrix; int vertexCount; public: myGraph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new bool*[vertexCount]; for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new bool[vertexCount]; for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = false; } } void addEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) { adjacencyMatrix[i][j] = true; adjacencyMatrix[j][i] = true; } } void removeEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) { adjacencyMatrix[i][j] = false; adjacencyMatrix[j][i] = false; } } bool isEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) return adjacencyMatrix[i][j]; else return false; } ~myGraph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } }; int main(){ myGraph *g = new myGraph(5); //mame graf s uzlamy 0,1,2,3 a 4. g->addEdge(0, 1); g->addEdge(0, 2); g->addEdge(0, 4); g->addEdge(1, 3); g->addEdge(1, 2); g->addEdge(3, 2); g->addEdge(3, 4); g->addEdge(1, 5); if (g->isEdge(1, 3)) cout << "Hrana existuje." << endl; else cout << "Hrana neexistuje" << endl; g->removeEdge(1, 3); if (g->isEdge(1, 3)) cout << "Hrana existuje." << endl; else cout << "Hrana neexistuje" << endl; //ulohy na zamyslenie: vypisanie stupna uzla pre kazdy uzol, vypisat uzol s najvacsim stupnom, zistit dosiahnutelnost medzi dvoma uzlami. return 0; }
Orientovany graf
class myGraph { private: bool** adjacencyMatrix; int vertexCount; public: myGraph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new bool*[vertexCount]; for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new bool[vertexCount]; for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = false; } } void addEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { adjacencyMatrix[start_node][end_node] = true; } } void removeEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { adjacencyMatrix[start_node][end_node] = false; } } bool isEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { return adjacencyMatrix[start_node][end_node]; } return false; } ~myGraph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } }; int main(){ myGraph *g = new myGraph(5); //mame graf s uzlamy 0,1,2,3 a 4. g->addEdge(0, 1); g->addEdge(0, 2); g->addEdge(0, 4); g->addEdge(1, 3); g->addEdge(1, 2); g->addEdge(3, 2); g->addEdge(3, 4); g->addEdge(1, 5); if (g->isEdge(1, 3)) cout << "Hrana (1,3) existuje." << endl; else cout << "Hrana (1,3) neexistuje" << endl; if (g->isEdge(3, 1)) cout << "Hrana (3,1) existuje." << endl; else cout << "Hrana (3,1) neexistuje" << endl; g->removeEdge(1, 3); if (g->isEdge(1, 3)) cout << "Hrana (1,3) existuje." << endl; else cout << "Hrana (1,3) neexistuje" << endl; //ulohy na zamyslenie: vypisanie stupna uzla pre kazdy uzol, //vypisat uzol s najvacsim stupnom, zistit dosiahnutelnost medzi dvoma uzlami. return 0; }
Ohodnoteny neorientovany graf
class myGraph { private: int** adjacencyMatrix; int vertexCount; public: myGraph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new int*[vertexCount]; for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new int[vertexCount]; for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = 0; } } void addEdge(int i, int j, int weight) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) { adjacencyMatrix[i][j] = weight; adjacencyMatrix[j][i] = weight; } } void removeEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) { adjacencyMatrix[i][j] = 0; adjacencyMatrix[j][i] = 0; } } bool isEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) return adjacencyMatrix[i][j] > 0; else return false; } int getWeightOfEdge(int i, int j) { if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) return adjacencyMatrix[i][j]; else return 0; } ~myGraph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } }; int main(){ myGraph *g = new myGraph(5); //mame graf s uzlamy 0,1,2,3 a 4. g->addEdge(0, 1,4); g->addEdge(0, 2,1); g->addEdge(0, 4,10); g->addEdge(1, 3,8); g->addEdge(1, 2,10); g->addEdge(3, 2,6); g->addEdge(3, 4,11); g->addEdge(1, 5,20); if (g->isEdge(1, 3)) cout << "Hrana existuje s ohodnotenin " << g->getWeightOfEdge(1,3) << endl; else cout << "Hrana neexistuje" << endl; g->removeEdge(1, 3); if (g->isEdge(1, 3)) cout << "Hrana existuje." << endl; else cout << "Hrana neexistuje" << endl; //ulohy na zamyslenie: vypisanie stupna uzla pre kazdy uzol, vypisat uzol s najvacsim stupnom, zistit dosiahnutelnost medzi dvoma uzlami. return 0; }
Ohodnoteny orientovany graf
class myGraph { private: int** adjacencyMatrix; int vertexCount; public: myGraph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new int*[vertexCount]; for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new int[vertexCount]; for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = 0; } } void addEdge(int start_node, int end_node, int weight) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { adjacencyMatrix[start_node][end_node] = weight; } } void removeEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { adjacencyMatrix[start_node][end_node] = 0; } } bool isEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { return adjacencyMatrix[start_node][end_node] > 0; } return false; } int getWeightOfEdge(int start_node, int end_node) { if (start_node >= 0 && start_node < vertexCount && end_node > 0 && end_node < vertexCount) { return adjacencyMatrix[start_node][end_node]; } return 0; } ~myGraph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } }; int main(){ myGraph *g = new myGraph(5); //mame graf s uzlamy 0,1,2,3 a 4. g->addEdge(0, 1,3); g->addEdge(0, 2,7); g->addEdge(0, 4,1); g->addEdge(1, 3,10); g->addEdge(1, 2,12); g->addEdge(3, 2,4); g->addEdge(3, 4,1); g->addEdge(1, 5,9); if (g->isEdge(1, 3)) cout << "Hrana (1,3) existuje s vahou " << g->getWeightOfEdge(1,3) << endl; else cout << "Hrana (1,3) neexistuje" << endl; if (g->isEdge(3, 1)) cout << "Hrana (3,1) existuje." << endl; else cout << "Hrana (3,1) neexistuje" << endl; g->removeEdge(1, 3); if (g->isEdge(1, 3)) cout << "Hrana (1,3) existuje." << endl; else cout << "Hrana (1,3) neexistuje" << endl; //ulohy na zamyslenie: vypisanie stupna uzla pre kazdy uzol, //vypisat uzol s najvacsim stupnom, zistit dosiahnutelnost medzi dvoma uzlami. return 0; }