Загрузка данных


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAX = 20;

int A[MAX][MAX];
int n = 0;


// Ввод матрицы смежности с клавиатуры
void inputKeyboard()
{
    cout << "Enter number of vertices: ";
    cin >> n;

    cout << "Enter adjacency matrix:" << endl;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> A[i][j];
        }
    }
}


// Ввод матрицы смежности из файла
void inputFile()
{
    string filename;

    cout << "Enter file name: ";
    cin >> filename;

    ifstream file(filename);

    if (!file)
    {
        cout << "File not found!" << endl;
        return;
    }

    file >> n;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            file >> A[i][j];
        }
    }

    file.close();

    cout << "Graph loaded." << endl;
}


// Вывод матрицы смежности
void printAdjacencyMatrix()
{
    cout << endl;
    cout << "Adjacency matrix:" << endl;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << A[i][j] << " ";
        }

        cout << endl;
    }
}


// Вывод списка смежности
void printAdjacencyList()
{
    cout << endl;
    cout << "Adjacency list:" << endl;

    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << ": ";

        for (int j = 0; j < n; j++)
        {
            if (A[i][j] == 1)
            {
                cout << j + 1 << " ";
            }
        }

        cout << endl;
    }
}


// Вывод матрицы инцидентности
void printIncidenceMatrix()
{
    int edgeA[MAX * MAX];
    int edgeB[MAX * MAX];

    int edges = 0;

    // Находим все ребра
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (A[i][j] == 1)
            {
                edgeA[edges] = i;
                edgeB[edges] = j;

                edges++;
            }
        }
    }

    cout << endl;
    cout << "Edges:" << endl;

    for (int i = 0; i < edges; i++)
    {
        cout << "e" << i + 1
             << " = ("
             << edgeA[i] + 1
             << ", "
             << edgeB[i] + 1
             << ")" << endl;
    }


    cout << endl;
    cout << "Incidence matrix:" << endl;

    cout << "   ";

    for (int j = 0; j < edges; j++)
    {
        cout << "e" << j + 1 << " ";
    }

    cout << endl;


    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << ": ";

        for (int j = 0; j < edges; j++)
        {
            if (i == edgeA[j] || i == edgeB[j])
            {
                cout << "1  ";
            }
            else
            {
                cout << "0  ";
            }
        }

        cout << endl;
    }
}


// Вывод списков инцидентности
void printIncidenceList()
{
    int edgeA[MAX * MAX];
    int edgeB[MAX * MAX];

    int edges = 0;

    // Находим ребра
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (A[i][j] == 1)
            {
                edgeA[edges] = i;
                edgeB[edges] = j;

                edges++;
            }
        }
    }

    cout << endl;
    cout << "Incidence lists:" << endl;

    for (int i = 0; i < n; i++)
    {
        cout << i + 1 << ": ";

        for (int j = 0; j < edges; j++)
        {
            if (i == edgeA[j] || i == edgeB[j])
            {
                cout << "e" << j + 1 << " ";
            }
        }

        cout << endl;
    }
}


int main()
{
    int menu;

    do
    {
        cout << endl;
        cout << "MENU" << endl;
        cout << "1 - Input graph from keyboard" << endl;
        cout << "2 - Input graph from file" << endl;
        cout << "3 - Adjacency matrix" << endl;
        cout << "4 - Adjacency list" << endl;
        cout << "5 - Incidence matrix" << endl;
        cout << "6 - Incidence list" << endl;
        cout << "0 - Exit" << endl;

        cout << "Choose: ";
        cin >> menu;


        if (menu == 1)
        {
            inputKeyboard();
        }


        if (menu == 2)
        {
            inputFile();
        }


        if (menu == 3)
        {
            if (n == 0)
                cout << "First enter the graph!" << endl;
            else
                printAdjacencyMatrix();
        }


        if (menu == 4)
        {
            if (n == 0)
                cout << "First enter the graph!" << endl;
            else
                printAdjacencyList();
        }


        if (menu == 5)
        {
            if (n == 0)
                cout << "First enter the graph!" << endl;
            else
                printIncidenceMatrix();
        }


        if (menu == 6)
        {
            if (n == 0)
                cout << "First enter the graph!" << endl;
            else
                printIncidenceList();
        }


    } while (menu != 0);


    return 0;
}