Добавил:
ivanov666
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Основы объектно-ориентированного программирования задач на графах. Учебное пособие
.pdf
Приложение 6
121
//--------к о н с т р у к т о р класса Matr----------
Matr::Matr(int n1):Graph(n1)
{
matr=new bool *[N];
for(int i=0; i<N;i++)
matr[i]= new bool [N];
}
//------- д е с т р у к т о р класса Matr-------------
Matr::~Matr()
{
for(int i=0; i<N;i++)
delete[] matr[i];
delete[] matr;
}
//-- ф у н к ц и я проверки смежности двух вершин ---
int Matr::adjacent(int x1,int x2)
{ if(matr[x1-1][x2-1]) return 1;
else return 0;
}
//----- ф у н к ц и я ввода матрицы смежности -------
void Matr::input_graph()
{
for(int i=0;i<N;i++)
for(int j=0;j< N;j++)
{ cout << " matr["<<i<<"]["<<j<<"]=";
cin >> matr[i][j];
}
cout <<endl;
}

Приложения
122
//----- ф у н к ц и я вывода матрицы смежности ------
void Matr::output_graph()
{
for( int i=0;i< N;i++)
for( int j=0;j< N;j++)
{ cout << " matr["<<i<<"]["<<j<<"]=";
cout << matr[i][j]<<endl;
}
}
//------ о п и с а н и е класса List ---------------
class List : public Graph
{
protected: int **list;
public:
void input_graph();
void output_graph();
int adjacent(int,int);
List (int n1):Graph(n1){}
~List ();
};
//------ д е с т р у к т о р класса List ------------
List::~List()
{ if(list!=NULL)
{
for(int i=0; i<N; i++)
delete[] list[i];
delete[] list;
}
}

Приложение 6
123
//------ ф у н к ц и я ввода списка -----------------
void List :: input_graph()
{
int * mas = new int [N];
list = new int * [N];
int k,i,j;
for( i=0; i<N; i++)
{ k=0;
cout<<"Tops, adjacent of top
"<<i+1<<endl;
while(getch()!=27)
cin >> mas[k++];
list[i]=new int [k+1];
*list[i]=k;
for( j=0;j<*list[i];j++)
*(list[i]+j+1)=mas[j];
}
delete[] mas;
}
//------ ф у н к ц и я вывода списка-----------------
void List :: output_graph()
{
int i,j;
cout<<" Out List";
for( i=0; i<N; i++)
{
cout<< endl<< i+1<< ": ";
for( j=1; j<=*list[i]; j++)
cout<< *(list[i]+j)<< ",";
}
}

Приложения
124
//--- ф у н к ц и я проверки смежности двух вершин---
int List :: adjacent(int x1,int x2)
{
for(int j=1; j<=*list[x1-1]; j++)
if(*(list[x1-1]+j)==x2) return 1;
return 0;
}
//-------- Г л а в н а я функция --------------------
#pragma argsused
int main(int argc, char* argv[])
{
textbackground(WHITE);
textcolor(BLACK);
clrscr();
char key; cout<< "key="; cin>>key;
int num;
cout<< "Number of Graph Tops:";
cin>> num;
if (key=='L')
{
List g1 (num);
g1.input_graph();
g1.output_graph();
if(g1.adjacent(1,2))
cout<<endl<<"Tops Adjacent" ;
else cout<<endl<<"Tops NOT Adjacent";
}
else if(key=='M')
{
Matr g2 (num);
g2.input_graph();
cout<<endl<<" Out Matrix"<<endl;
g2.output_graph();

Приложение 6
125
if(g2.adjacent(1,2))
cout<<endl<<"Tops Adjacent" ;
else cout<<endl<<"Tops NOT Adjacent";
}
else cout<<"error";
getch();
return 0;
}
//----------------------------------------------------

Приложение 3
126
Приложение 7
#pragma hdrstop
//----------------------------------------------------
#pragma argsused
#include <iostream.h>
#include <conio.h>
//----------------------------------------------------
class Graph
{
protected: int N;
public: void output_N(){cout<<N;}
int return_N(){return N;}
virtual void input_graph() = 0;
virtual void output_graph() = 0;
virtual void rand_graph() = 0;
Graph(int n1){N=n1;}
};
//----------------------------------------------------
class Matr : public Graph
{
protected: bool **matr;
public: void input_graph();
void output_graph();
void rand_graph();
Matr(int n1);
~Matr();
};

Приложение 7
127
//----------------------------------------------------
class Task : public Matr
{
public: Task(int n1) : Matr(n1){};
void task1();
void task2();
};
//----------------------------------------------------
Matr::Matr(int n1):Graph(n1)
{
matr=new bool *[N];
for(int i=0; i<N;i++)
matr[i]= new bool [N];
}
//----------------------------------------------------
Matr::~Matr()
{
for(int i=0; i<N;i++)
delete[] matr[i];
delete[] matr;
}
//----------------------------------------------------
void Matr::input_graph()
{
for(int i=0;i<N;i++)
for(int j=0;j< N;j++)
{ cout << " matr["<<i<<"]["<<j<<"]=";
cin >> matr[i][j];
}
cout <<endl;
}

Приложения
128
//----------------------------------------------------
void Matr::output_graph()
{
for( int i=0;i< N;i++)
{
for( int j=0;j< N;j++)
cout << matr[i][j]<< ' ';
cout << endl;
}
}
//----------------------------------------------------
void Matr::rand_graph()
{
for( int i=0;i< N-1;i++)
for( int j=i;j< N;j++)
matr[i][j]= matr[j][i]=rand()%2;
for( int i=0;i< N;i++)
matr[i][i]=0;
}
//--- ф у н к ц и я определения паросочетания графа---
void Task :: task1()
{
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if (matr[i][j]!=0)
{
//------обнуление строк и столбцов вершин ребра
//-------паросочетания-----
for(int k=0;k<N;k++)
{ matr[i][k]=0;

Приложение 7
129
matr[k][i]=0;
matr[j][k]=0;
matr[k][j]=0;
}
cout<< "eidge= "<<i+1<<','<<j+1<<endl;
}
}
//---- ф у н к ц и я выделения семейства клик,--------
//---- покрывающих все вершины графа -----------------
void Task :: task2()
{
int *P=0, *Cl=0;
bool *M=0;
int z=0;
// z – количество вершин в выделямой клике
// P – указатель на массив флажков покрытия вершин
// графа кликами
// Cl – указатель на массив номеров вершин выделяемой
// клики
//--- выделение динамической памяти
//---- для вспомогательных массивов
M = new bool[N];
P = new int [N];
Cl = new int [N];
//---- обнуление массива флажков
for (int i=0; i<N; i++)
P[i]=0;
//--- выделение клики графа для непокрытой вершины
for (int i=0; i<N; i++)

Приложения
130
{
z=0;
if(P[i]==0)
{
Cl[z]=i+1; // запись номера вершины клики
z=z+1;
//--копирование строки матрицы смежности с номером i+1
//-----------в массив M[]-------------
for(int j=0; j<N; j++)
M[j]= matr[i][j];
//------промежуточный вывод -------
cout<<endl<<"M=";
for(int i1=0; i1<N; i1++)
cout<<M[i1]<<' ';
//-------------------------------------
//---логическое произведение элементов массива М и
//---и строки j матрицы, для которой M[j]==1
for (int j=0; j<N; j++)
if(M[j]==1)
{
Cl[z++]=j+1;
cout<<" nomer="<<z<<' '<<Cl[z-1];
for(int l=j+1; l<N; l++)
M[l]= M[l]*matr[j][l];
}
//---помечаем вершины, покрытые кликой------------
for(int i1=0; i1<z; i1++)
P[Cl[i1]-1]=1;
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]
