How can I combine these two functions to create a program that will allow a user to enter the count of vertices and then enter the graph edges?
#include <iostream>
using namespace std;
int **malloc2d(int r, int c)
{
int **t = new int*[r];
for(int i = 0; i < r; i++)
t[i] = new int[c];
return t;
}
int main()
{
int i;
int j;
int adj[V][V];
for(i = 0; i < V; i++)
for(j = 0 ; j < V; j++)
adj[i][j] = 0;
for(i = 0; i < V; i++)
adj[i][i]= 1;
while (cin>> i >> j)
{
adj[i][j] = 1;
adj[j][i] = 1;
}
system("pause");
return 0;
}
You could use std::vector<std::vector<bool>>
or boost::adjacency_matrix
, it's simpler.
std::size_t v;
std::cin >> v;
std::vector<std::vector<bool>> g(v);
for(auto& elem: g)
elem.resize(v, false);
boost::adjacency_matrix<boost::undirectedS> ug(v);
std::size_t e;
std::cin >> e;
for(std::size_t i = 0; i < e; ++i)
{
std::size_t v1, v2;
std::cin >> v1 >> v2;
g.at(v1).at(v2) = true;
g.at(v2).at(v1) = true;
boost::add_edge(v1, v2, ug);
}