让矩阵具有4个顶点和4个边:(1,2),(1,4),(2,3),(2,4)。我需要填写邻接关系(我已经完成了)并将此邻接矩阵转换为关联矩阵。结果应为矩阵Incidence={(0111),(1010),(1101),(1010)}
。什么是最佳算法。我找不到解决方案,所有想法都带有错误。 :-(谢谢
#include "iostream"
#include "conio.h"
#include <stdlib.h>
using namespace std;
struct Rebro {
int x=0;
int y=0;
};
int main()
{
int Graph[100][100], Vertex, Edge, x, y;
int VG, EG;
int Incidence[100][100];
cout << "Enter number of vertexes:";
cin >> Vertex;
cout << "\nEnter number of edges:";
cin >> Edge;
//_________________________NULL
for (int i = 1; i <= Vertex; i++)
for (int j = 1; j <= Vertex; j++)
{
Graph[i][j] = 0;
}
//_________________________NULL
for (int i = 1; i <= Vertex; i++)
for (int j = 1; j <= Vertex; j++)
{
Incidence[i][j] = 0;
}
//_________________________INPUT
Rebro mass[100];
cout << "\nEnter edges:\n " << endl;
for (int i = 1; i <= Edge; i++)
{
cout << "e1=";
cin >> x;
cout << "e2=";
cin >> y;
cout << endl;
Graph[x][y] = 1;
Graph[y][x] = 1;
mass[i].x =x ;
mass[i].y =y ;
}
//_________________________OUTPUT
cout << "\nAdjacency matrix:\n" << endl;
for (int i = 1; i <= Vertex; i++)
{
for (int j = 1; j <= Vertex; j++)
{
cout <<" "<< Graph[i][j];
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= Edge; i++) {
Incidence[i][mass[i].x] = 1;
Incidence[i][mass[i].y] = 1;
}
//_________________________OUTPUT
cout << "\nIncidence matrix:\n" << endl;
int count = 0;
for (int j = 1; j <= Vertex; j++)
{
for (int i = 1; i <= Vertex; i++)
{
cout << " " << Incidence[i][j];
}
cout << endl;
}
cout << endl;
_getch();
}
或者通常是一个错误的想法:
#include "iostream"
#include "conio.h"
#include <stdlib.h>
using namespace std;
int main()
{
int Graph[100][100], Vertex, Edge, x, y;
int VG, EG;
int Incidence[100][100];
cout << "Enter number of vertexes:";
cin >> Vertex;
cout << "\nEnter number of edges:";
cin >> Edge;
//_________________________NULL
for (int i = 1; i <= Vertex; i++)
for (int j = 1; j <= Vertex; j++)
{
Graph[i][j] = 0;
}
//_________________________NULL
for (int i = 1; i <= Vertex; i++)
for (int j = 1; j <= Vertex; j++)
{
Incidence[i][j] = 0;
}
//_________________________INPUT
cout << "\nEnter edges:\n " << endl;
for (int i = 1; i <= Edge; i++)
{
cout << "e1=";
cin >> x;
cout << "e2=";
cin >> y;
cout << endl;
Graph[x][y] = 1;
Graph[y][x] = 1;
}
//_________________________OUTPUT
cout << "\nAdjacency matrix:\n" << endl;
for (int i = 1; i <= Vertex; i++)
{
for (int j = 1; j <= Vertex; j++)
{
cout <<" "<< Graph[i][j];
}
cout << endl;
}
cout << endl;
for (int i =1; i<=Edge; i++) {
for (int j=1; j<=Edge; j++) {
if (Graph[x][y]==1) {
if ((i==x)||(i==y)||(j==x)||(j==y)) {
Incidence[i][j]==1;
Incidence[j][i]==1;
}
else {
Incidence[i][j]==0;
Incidence[j][i]==0;
}
}
}
}
//_________________________OUTPUT
cout << "\nIncidence matrix:\n" << endl;
int count = 0;
for (int j = 1; j <= Vertex; j++)
{
for (int i = 1; i <= Vertex; i++)
{
cout << " " << Incidence[i][j];
}
cout << endl;
}
cout << endl;
_getch();
}