#include <iostream>
#include "grafo.h"
using namespace std;

Grafo::Grafo(int k){
	(k<=0)? N=5 : N=k;
	mat = new bool[N*N];
	for (int n=0; n<N*N; n++)
		mat[n]=false;
}


bool Grafo::aggiungiArco(int u, int v){
	if (u<=0 || u>N)
		return false;
	u--;
	if (v<=0 || v>N)
		return false;
	v--;
	mat[u*N+v] = true;
	return true;
}

bool Grafo::eliminaArco(int u, int v){
	if (u<=0 || u>N)
		return false;
	u--;
	if (v<=0 || v>N)
		return false;
	v--;
	if (mat[u*N+v]){
		mat[u*N+v] = false;
		return true;
	}else
		return false;
}

void Grafo::stampa(){
        cout<<' ';
		for (int c=0; c<N; c++)
                cout<<c+1;
        cout<<endl;

        for (int r=0; r<N; r++){
			cout<<r+1;
            for (int c=0; c<N; c++)
				if (mat[r*N+c])
					cout<<'x';
				else
					cout<<' ';
            cout<<endl;
        }
}


bool Grafo::operator %(int u)const{
	if (u<=0 || u>N)
		return false;
	u--;
	for (int n=0;n<N; n++)
		if ( mat[n*N+u] || mat[u*N+n] )
			return false;
	return true;
}

void Grafo::invertiArchi(){ // funzione che inverte il verso di ciascun arco. La matrice di incidenza diventa quella opposta.
	for (int r=0; r<N; r++){
		for (int c=r+1; c<N; c++){
				bool tmp = mat[r*N+c];
				mat[r*N+c]=mat[c*N+r];
				mat[c*N+r] = tmp;
		}
	}
}

bool Grafo::operator!()const{ // test di non orientamento degli archi, ossia di simmetria della matrice
	for (int r=0; r<N; r++){
        for (int c=0; c<N; c++){
			if (mat[r*N+c]!=mat[c*N+r])
				return false;
		}
	}
	return true;
}
