Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Diskret / Graph_paint / main

.cpp
Скачиваний:
5
Добавлен:
18.08.2019
Размер:
9.67 Кб
Скачать
#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
#define MAXVERTEXES 30    //maximal amount of vertexes
int a[MAXVERTEXES][MAXVERTEXES]; //adjacency matrix
int colour[MAXVERTEXES];    //colour of the vertex
int maxColour=0;                    //amount of colours
int amount;                                //amount of vertexes

struct vertex {    //structure for sorting vertexes
    int number;        //number of vertex
    int inc;            //amount of incedent edges for this vertex
};

vertex vert[MAXVERTEXES]; //array of vertexes with its power

int amOper=0;    //amount of operations
int amEdges=0;    //amount of edges
char steps;    //indicator that steps are shown

void input();
void init();
int colourize();
void sortVertex();


int main(){
    char answer;
    do {
        amOper=0;
        amEdges=0;
        input();                        //create adjacency matrix
        init();                            //initialization
        sortVertex();                //sort vertexes by power

        cout<<"Do you want to see steps of algorithm's work? (y/n) ";
        steps=getch();                //steps == 1 if need show steps
        if (steps=='y' || steps=='Y') steps=1; else steps=0;
        cout<<endl;


        unsigned long startTime=GetTickCount();    //remember time before executing algorithm
        maxColour=colourize();                //give colours for every vertex
        unsigned long finishTime=GetTickCount();//remember time after executing algorithm


        //print amount of colours and number od colour for every vertex
        cout<<"\n\nYou need "<<maxColour<<" colours to paint your graph. Vertexes will have next colors:\n";
        for (int i=0; i<amount; i++) printf("Vertex %d will have colour #%d.\n",i,colour[i]);

        cout<<"\nDo you want to repeat with another graph? (y/n)";
        answer=getch();
    } while (answer=='y' || answer=='Y');
}

// ======================== init ==========================================
void init() {
    for (int u=0; u<amount; u++) {    //initialization d/span>
        colour[u]=0;                //colour is undefined
        vert[u].number=u;    //number of vertex
        vert[u].inc=0;        //power of vertex
        for (int v=0; v<amount; v++) if (a[u][v]!=0) vert[u].inc++;    //find power of vertex
    }
}

//========================= Colourize ======================================
int colourize() {
    int amWhite=amount;    //amount of non painted vertexes
    int colourIsUsed=0;
    while (amWhite!=0) {//repeat while white vertexes are present
        colourIsUsed++;    //use next colour
        if (steps) {
            printf("\nCurrent colour is  colour #%d.\n",colourIsUsed);
            //getch();
        }
        for (int i=0; i<amount; i++)  //look every vertex by decreasing power
            if (colour[vert[i].number]==0) {//if vertex is white
                if (steps)
                    printf("Vertex #%d has the biggest power between white vertexes.\n",vert[i].number);
                int u=vert[i].number;
                bool canDraw=true;
                for (int v=0; v<amount; v++)//check the neighours
                    if (a[u][v]!=0 && colour[v]==colourIsUsed) canDraw=false;
                if (canDraw) {    //if neighbours have another colours
                    colour[u]=colourIsUsed;    //paint current vertex in chosen colour
                    amWhite--;    //decrease amount of white vertexes
                    if (steps)
                        printf("Neighbours of #%d aren't painted in colour #%d, so paint vertex #%d in colour #%d.\n",u, colourIsUsed,u, colourIsUsed);
                }//end if can draw
                else if (steps)
                    printf("Vertex #%d has incedent vertexes with colour #%d. We can't use this colour.\n",u,colourIsUsed);
                if (steps) getch();
        } //end if colour white
    } //end while
    return colourIsUsed;
}

// ==================== Sort vertexes by power =======================
void sortVertex() {    //sort by method of choseness
    for (int i=0; i<amount-1; i++) {    //make n-1 iteration
        int max=i;    //find max element of current iteration
        for (int j=i+1; j<amount; j++) if (vert[j].inc>vert[max].inc) max=j;
        vertex tmp=vert[i];    //change places between current element and maximal element
        vert[i]=vert[max];
        vert[max]=tmp;
    }
}

//================================= Creating adjacency matrix  =========================================
void input() {
    char repeat;
    do {
        repeat=0;
        system("cls");
        cout<<"   -----------------------------------------------------------------------\n";
        cout<<"   |                       Painting of the graph                         |\n";
        cout<<"   -----------------------------------------------------------------------\n\n";
        cout<<"\n1 - Enter weights of edges by keyboard";            //building menu
        cout<<"\n2 - Read weights of edges from text file";
        cout<<"\n3 - Random weights of edges";
        char answer=0;                    //chosen punct of menu
        FILE *file=0;                        //pointer on input file
        while (answer<49 || answer>51) answer=getch();
        system("cls");
        if (answer=='2') {            //----------------------- if read from file ---------------------------------
            char retry='y';
            while (file==0 && retry=='y') {
                if (retry=='y') {
                    system("cls");
                    cout<<"Enter name of the file: ";
                    char fileName[50];                    //name of the file
                    fflush(stdin);
                    gets(fileName);                            //reading name
                    file=fopen(fileName,"rt");    //open file for reading
                }
                if (file==0) {                                //if file doesn't exist offer repeaet entering name
                    cout<<"File not found. Do you want to retry? (y/n) ";
                    retry=getch();
                }
            }
            if (file!=0) fscanf(file,"%d",&amount);            //read amount of vertaxes
        }                                                //---------------------------------------------------------------------------
        else {                                                        //if random or enter matrix
            srand((unsigned) time(NULL));        //initialization random generator
            cout<<"Enter amount of vertexes (0<amount<"<<MAXVERTEXES<<"): ";
            cin>>amount;                                        //reading amount of vertaxes
            while (amount<0 || amount>=MAXVERTEXES) {                    //if entered uncorrect data
                cout<<"Error. You've entered uncorrect value.\n";
                cout<<"Enter amount of vertexes (0<amount<"<<MAXVERTEXES<<"): ";
                cin>>amount;                                                                        //repeat reading
            }
        }
        if (!(answer=='2' && file==0) ) {
        for (int i=0; i<amount; i++)        //for every element of matrix
            for (int j=0; j<amount; j++)
                switch (answer) {
                    case '1':                                    //if enter elements
                        {
                            if (i==j) a[i][j]=0;    //distance between vertex and the same vertex is 0
                            else {
                                cout<<"Enter weight of edge between "<<i<<" and "<<j<<" vertaxes (weight>=0): ";
                                cin>>a[i][j];                //reading value of element
                                while (a[i][j]<0) {
                                    cout<<"Error. Enter weight of edge between "<<i<<" and "<<j<<" vertaxes (weight>=0): ";
                                    cin>>a[i][j];            //repeat if uncorrect data was entered
                                }
                            }
                            break;
                        }
                    case '2': {fscanf(file,"%d",&a[i][j]); break;} //read element from file
                    case '3':
                        {
                            if (i==j) a[i][j]=0;                        //set the diagonal element as 0
                            else if (j>i) a[i][j]=rand()%10;    //random another element
                            else a[i][j]=a[j][i];
                            break;
                        }
                }
        system("cls");
        cout<<"   -----------------------------------------------------------------------\n";
        cout<<"   |                       Painting of the graph                         |\n";
        cout<<"   -----------------------------------------------------------------------\n\n";
        cout<<"Adjacency matrix\n";
        cout<<"    |"; for (int i=0; i<amount; i++) printf(" %3d",i);
        cout<<"\n----+";
        for (int i=0; i<amount; i++) cout<<"----";
        cout<<'\n';
        for (int i=0; i<amount; i++) {        //printing matrix
            printf("%3d |",i);
            for (int j=0; j<amount; j++) {
                printf(" %3d", a[i][j]);
            }
            cout<<endl;
        }
    } else repeat=1;
    } while (repeat);
}
Соседние файлы в папке Graph_paint
  • #
    18.08.20191.12 Кб5Graph_paint.cbp
  • #
    18.08.2019322 б5Graph_paint.layout
  • #
    18.08.20199.67 Кб5main.cpp