0

помогите пожалуйста, как из матрицы смежности перейти к матрицы инцидентности, я уже все перебрал и ни к чему так и не пришел. Ввел матрицу смежности, может есть какие-то идеи? И какая вообще закономерность между двумя этими матрицами? введите сюда описание изображениявведите сюда описание изображения

#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");
} 
2
  • В интернете есть полная информация 26 сен 2020 в 13:18
  • добро пожаловать на Stack Overflow на русском! текстовую информацию лучше прилагать в виде текста: а) читать легче; б) можно копировать; в) поиск работает. исправить текст вопроса можно, нажав править ниже текста вопроса 27 сен 2020 в 7:00

1 ответ 1

1

решение:

#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;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.