c++, graphs, from adjacency matrix to incidence matrix, undirected graph
Please help me how to go from the adjacency matrix to the incidence matrix, I have already gone through everything and have not come to anything. I entered the adjacency matrix, maybe there are some ideas? And what is the general pattern between these two matrices?
#include "iostream"
#include "conio.h"
#include <stdlib.h>
using namespace std;
int main()
{
int G[100][100], Vertex, Edge, x, y;
cout << "Enter number of vertexes:";
cin >> Vertex;
cout << "\nEnter number of edges:";
cin>> Edge;
for(int i=1;i<=Vertex;i++) // t.k matrica smejnosti eto -
for(int j=1;j<=Vertex;j++) // bibarnaia n x n matrica
{
G[i][j]=0; // prisvaivaem 0 elemantam matrici
}
cout << "\nEnter edges:\n " << endl;
for(int i=1;i<=Edge;i++)
{
cout << "e1=" ;
cin >> x;
cout << "e2=";
cin >> y;
cout << endl;
G[x][y]=1;
G[y][x]=1;
}
cout << "\nAdjacency matrix:\n" << endl;
for(int i=1;i<=Vertex;i++)
{
for(int j=1;j<=Vertex;j++)
{
cout << G[i][j] ;
}
cout << endl;
}
cout << endl;
return 0;
system("pause");
}
0
1 answers
Decision:
#include <iostream>
using namespace std;
int main()
{
int vertexCount, edgeCount;
cout << "Enter number of vertexes:";
cin >> vertexCount;
cout << "\nEnter number of edges:";
cin>> edgeCount;
auto matrix = new int*[edgeCount];
for(auto i = 0; i < edgeCount; ++i){
matrix[i] = new int[vertexCount];
}
cout << "\nEnter edges:\n " << endl;
for(auto i = 0; i < edgeCount ; ++i)
{
int x,y = 0;
cout << "e1=" ;
cin >> x;
cout << "e2=";
cin >> y;
cout << endl;
matrix[x][y]=1;
matrix[y][x]=1;
}
cout << "\nAdjacency matrix:\n" << endl;
auto resultMatrix = new int*[edgeCount];
for(auto i = 0; i < edgeCount; ++i){
resultMatrix[i] = new int[vertexCount];
}
auto pointer = 0;
for(auto i = 0; i < edgeCount; ++i) {
for (auto j = i + 1; j < vertexCount; ++j) {
if(matrix[i][j] == 1){
resultMatrix[i][pointer]=1;
resultMatrix[j][pointer]=1;
++pointer;
}
}
}
for(auto i = 0; i < edgeCount; ++i) {
for (auto j = 0; j < vertexCount; ++j) {
cout << resultMatrix[i][j] << " ";
}
cout << endl;
}
for(auto i = 0; i < vertexCount; ++i){
delete[] matrix[i];
}
delete[] matrix;
for(auto i = 0; i < vertexCount; ++i){
delete[] resultMatrix[i];
}
delete[] resultMatrix;
return 0;
}
1
Author: Peter Lavreniuk, 2020-09-26 13:46:10