Intsident matritsasidan qo'shni matritsani hosil qiladigan funktsiyani yarating


Download 313.93 Kb.
Sana21.11.2023
Hajmi313.93 Kb.
#1790405

Asl graf berilgan. Individual topshiriqga mos taqdimini yarating va grafni ko'rsating. Graf ma'lumotlar tuzilmasi bilan ishlash standart operatsiyalarni tavsiflang (yangi tugun qo'shish, yangi qirralarni qo'shish, tugun yoki qirralarni olib tashlash, tuzilmani yaratish va chiqarish, shuningdek individual topshiriq).
Intsident matritsasidan qo'shni matritsani hosil qiladigan funktsiyani yarating.

Dastur kodi:


#include
#include

class Graph {


public:
Graph(int vertices) : numVertices(vertices) {
adjacencyMatrix.resize(numVertices, std::vector(numVertices, 0));
}

void addVertex() {


numVertices++;
for (int i = 0; i < numVertices - 1; i++) {
adjacencyMatrix[i].push_back(0);
}
adjacencyMatrix.push_back(std::vector(numVertices, 0));
}

void addEdge(int source, int destination) {


if (source >= 0 && source < numVertices && destination >= 0 && destination < numVertices) {
adjacencyMatrix[source][destination] = 1;
adjacencyMatrix[destination][source] = 1;
}
}

void removeVertex(int vertex) {


if (vertex >= 0 && vertex < numVertices) {
adjacencyMatrix.erase(adjacencyMatrix.begin() + vertex);
for (int i = 0; i < numVertices; i++) {
adjacencyMatrix[i].erase(adjacencyMatrix[i].begin() + vertex);
}
numVertices--;
}
}

void removeEdge(int source, int destination) {


if (source >= 0 && source < numVertices && destination >= 0 && destination < numVertices) {
adjacencyMatrix[source][destination] = 0;
adjacencyMatrix[destination][source] = 0;
}
}

void printGraph() {


for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
std::cout << adjacencyMatrix[i][j] << " ";
}
std::cout << std::endl;
}
}

private:
int numVertices;


std::vector> adjacencyMatrix;
};

int main() {


Graph myGraph(3);

myGraph.addEdge(0, 1);


myGraph.addEdge(1, 2);
myGraph.addVertex();
myGraph.addEdge(2, 3);
myGraph.printGraph();

myGraph.removeVertex(2);


myGraph.removeEdge(0, 1);
myGraph.printGraph();

return 0;


}

Download 313.93 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling