graph using djacency matrix c++
C++
// Adjacency Matrix representation in C++
#include <iostream>
using namespace std;
class Graph {
private:
bool** adjMatrix;
int numVertices;
public:
// Initialize the matrix to zero
Graph(int numVertices) {
this->numVertices = numVertices;
adjMatrix = new bool*[numVertices];
for (int i = 0; i < numVertices; i++) {
adjMatrix[i] = new bool[numVertices];
for (int j = 0; j < numVertices; j++)
adjMatrix[i][j] = false;
}
}
// Add edges
void addEdge(int i, int j) {
adjMatrix[i][j] = true;
adjMatrix[j][i] = true;
}
// Remove edges
void removeEdge(int i, int j) {
adjMatrix[i][j] = false;
adjMatrix[j][i] = false;
}
// Print the martix
void toString() {
for (int i = 0; i < numVertices; i++) {
cout << i << " : ";
for (int j = 0; j < numVertices; j++)
cout << adjMatrix[i][j] << " ";
cout << "\n";
}
}
~Graph() {
for (int i = 0; i < numVertices; i++)
delete[] adjMatrix[i];
delete[] adjMatrix;
}
};
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.toString();
}
Also in C++:
- Title
- new c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- strchr function in c++
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- Given an undirected graph, count the number of connected components.
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- traverse a map
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- c++ transform
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- minimum swaps to sort an array
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- c++ find object in vector by attribute
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- inheritance protected in c++
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- c++ char if
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- how to use assrt c++
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- count a character in a string c++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- how to delete something in an array c++
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- unordered_set c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++