You are on page 1of 14

// Computing_Vertex_Connectivity.cpp : main project file.

/******************************************************************************
Author:TuanAnh-PC**************************************************************
******************************************************************************/

#include <stdafx.h>
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>

typedef struct tagVertex


{
int preVertex; //the previous Vertex
int delta; //the value that the flow of the arc can add on the
increment-flow path
}Vertex,*pVertex;
typedef struct tagEdge
{
int cap; //capacity
int flow; //the flow on the arc
}Edge,*pEdge; // canh va con tro canh
#define SizeOfVertex sizeof(Vertex)
#define SizeOfEdge sizeof(Edge)
#define SizeOfpEdge sizeof(pEdge)
#define maxCapacity 32767
//**************Khai bao cac bien toan cuc *******************//
pVertex arrVertices = NULL;
pEdge *arrGraph = NULL;
pVertex arrVerticesH = NULL;
pEdge *arrGraphH = NULL;
int **arrResult = NULL;
int srcVertex; //Dinh nguon
int trgVertex; //Dinh Dich
int srcVertexH; //Dinh nguon
int trgVertexH; //Dinh Dich
int totalVertices; //Tong so dinh
int totalVerticesH; //Tong so dinh
int totalEdges; //Tong so canh
int increaseValue; //Gia tri tang luong
int maxflow; //luong cuc dai
bool pathFound; //Co tim thay duong tang luong ?
int vertexConnectivity;//Do lien ket dinh
//***************Khai bao Function*****************************//
void introductionProgram(); //Gioi thieu chuong trinh
void menu(); //Sau khi da nhap xong do thi
void initializeGraph(); //Khoi tao do thi
void initializeMemory(); //Khoi tao bo nho cung cap cho mang chua
dinh va ma tran ke
void inputGraphFromKeyBoard(); //Khoi tao do thi tu ban phim
void inputRandomGraph(); //Khoi tao do thi Random
void inputGraphFromFile(); //Khoi tao tu File van ban
void enterTotalVertices(); //Nhap vao tong so dinh
bool findPath(int s, int t); //TIm luong cuc dai
void increaseFlow(int s, int t); //Tang luong
void maxFlow(int s, int t); //Tinh luong cuc dai
void saveGraphToFile(); //Luu do thi ra file
void computingVertexConnectivity(); //Tinh do lien ket dinh
void getarrResult(int mf,int s, int t); //Nap cac duong tang luong vao mang ket qua
void Algorithm9(int src,int trg); //Thuat toan 9 dung
de tinh k(u,v)
void Algorithm10(); //Thuat toan 10dung de tinh k(u,v)
void Algorithm11(); //Thuat toan 11dung de tinh k(u,v)

void displayResult(); //Hien thi ket qua rut gon


void displayDetailResult(); //Hien thi ket qua chi tiet
void saveResultToFile(); //Luu ket qua ra File text
void saveResultToFileAlgorithm10();
//*****************Chuong trinh Tinh Do Lien Ket Dinh*********************//
int main(array<System::String ^> ^args)
{
system("cls");
introductionProgram();
return 0;
}
void introductionProgram()
{
printf("|---------------------GRAPH VERTEX CONNECTIVITY------------------------|\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| TINH DO LIEN KET DINH |\n");
printf("| |\n");
printf("| Sinh Vien thuc hien : Nguyen Tuan Anh Tin4-K50 |\n");
printf("| Giang Vien huong dan : GS.Nguyen Duc Nghia |\n");
printf("| Khoa Cong Nghe Thong Tin |\n");
printf("| Truong Dai Hoc Bach Khoa Ha Noi |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|------------------Bam phim bat ki de chay chuong
trinh!---------------|\n\n");
getch();
system("cls");
initializeGraph();
}
void initializeGraph()
{
InitGraph:
printf("|---------------------GRAPH VERTEX CONNECTIVITY------------------------|\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| KHOI TAO DO THI |\n");
printf("| |\n");
printf("| 1.Khoi tao tu ban phim may tinh. |\n");
printf("| 2.Khoi tao Random. |\n");
printf("| 3.Khoi tao tu File van ban. |\n");
printf("| 4.Thoat. |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|--------------------Quay lai Gioi thieu chuong
trinh(b)---------------|\n\n");
char flag;
fflush(stdin);
scanf("%c",&flag);
switch(flag)
{
case 'b':
system("cls");
introductionProgram();
return;
break;
case '1':
system("cls");
inputGraphFromKeyBoard();
break;
case '2':
system("cls");
inputRandomGraph();
break;
case '3':
system("cls");
inputGraphFromFile();
break;
case '4':
exit(1);
break;
default :
system("cls");
goto InitGraph;
break;
}
}

void initializeMemory()
{
//Khoi Tao cac dinh
arrVertices=(pVertex)malloc((totalVertices+1)*SizeOfVertex);
for (int i=0;i<=totalVertices;i++)
{
arrVertices[i].preVertex=0;
arrVertices[i].delta=maxCapacity;
}
//Mang H
totalVerticesH = 2*totalVertices;
arrVerticesH=(pVertex)malloc((totalVerticesH+1)*SizeOfVertex);
for (int i=0;i<=totalVerticesH;i++)
{
arrVerticesH[i].preVertex=0;
arrVerticesH[i].delta=maxCapacity;
}
//Khoi Tao ma tran ke bieu dien do thi
arrGraph=(pEdge*)malloc((totalVertices+1)*SizeOfpEdge);
for (int i=0;i<=totalVertices;i++)
{
arrGraph[i]=NULL;
arrGraph[i]=(pEdge)malloc((totalVertices+1)*SizeOfEdge);
}
for (int i=0;i<=totalVertices;i++)
{
for (int j=0;j<=totalVertices;j++)
{
arrGraph[i][j].cap=0;
arrGraph[i][j].flow=0;
}
}
//Mang H
arrGraphH=(pEdge*)malloc((totalVerticesH+1)*SizeOfpEdge);
for (int i=0;i<=totalVerticesH;i++)
{
arrGraphH[i]=NULL;
arrGraphH[i]=(pEdge)malloc((totalVerticesH+1)*SizeOfEdge);
}
for (int i=0;i<=totalVerticesH;i++)
{
for (int j=0;j<=totalVerticesH;j++)
{
arrGraphH[i][j].cap=0;
arrGraphH[i][j].flow=0;
}
}
//Khoi tao cho ket qua

if(arrResult==NULL)
{
arrResult=(int**)malloc((totalVertices+1)*sizeof(int*));
if(arrResult==NULL)
{
printf("\nError memory!");
getch();
exit(1);
}
for (int i=0;i<=totalVertices;i++)
{
arrResult[i]=NULL;
}
}
}
void enterTotalVertices()
{
totalVertices =0 ;
printf("\n\t\tSo dinh: ");
scanf("%d",&totalVertices);
if(totalVertices >= 1)
{
printf("\n");
}
else
{
printf("\n\t\t\tBan da nhap sai so dinh!\n\t\t\tXin vui long nhap lai so
dinh\n");
enterTotalVertices();
}
if(totalVertices == 1)
{
printf("\n\t\t\tDo thi duoc nhap la Do thi tam thuong(Chi co 1 dinh)");
getch();
system("cls");
initializeGraph();
Menu :
printf("\n\n\n\t\t\tMENU");
printf("\n\t\t1.Quay lai Khoi tao do thi");
printf("\n\t\t2.Thoat\n\t\t\t");
fflush(stdin);
char flag1;
scanf("%c",&flag1);
switch(flag1)
{
case '1' :
system("cls");
initializeGraph();
break;
case '2':
exit(1);
break;
default :
goto Menu;
break;
};
}
}
void menu()
{
system("cls");
printf("|---------------------GRAPH VERTEX CONNECTIVITY------------------------|\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| MENU |\n");
printf("| |\n");
printf("| 1.Tinh do lien ket dinh. |\n");
printf("| 2.Luu do thi ra file. |\n");
printf("| 3.Thoat. |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|--------------------Quay lai Khoi tao do
thi(b)-----------------------|\n\n");
fflush(stdin);
char flag1;
scanf("%c",&flag1);
switch(flag1)
{
case 'b' :
system("cls");
initializeGraph();
break;
case '1':
computingVertexConnectivity();
break;
case '2':
saveGraphToFile();
break;
case '3':
exit(1);
break;
default :
menu();
break;
};
}
void inputGraphFromKeyBoard()
{
printf("|---------------------GRAPH VERTEX
CONNECTIVITY------------------------|\n\n\n\n");
printf("\t\t\tKHOI TAO DO THI TU BAN PHIM\n");

enterTotalVertices();
totalEdges =0;
int u = -1;
int v = -1;
enter_srcV_trgV:
printf("\t\tDinh nguon va dinh dich :");
printf("\n\t\tForm: s t\n\t\t ");
scanf("%d%d",&srcVertex,&trgVertex);
if(srcVertex < 1 || srcVertex > totalVertices || trgVertex< 1|| trgVertex >
totalVertices || srcVertex==trgVertex)
{
printf("\n\t\t\tBan da nhap sai dinh nguon ,dinh dich \n\t\t\tXin vui long nhap
lai !\n");
goto enter_srcV_trgV;
}
initializeMemory();
// Thiet lap tung canh duoc nhap tu ban phim
printf("\n\t\tNhap cac canh cua Do thi G :");
printf("\n\t\tCanh: u v ( u,v = 0 de Thoat)\n");
//fflush(stdin);

while( (u != 0) && (v != 0) )
{
printf("\t\tCanh thu %d: ",(totalEdges+1));
scanf("%d%d",&u,&v);
if( u<0 || u>totalVertices || v<0 || v>totalVertices || (u==v && u!=0 ) )
{
printf("\t\t\tDa nhap sai canh %d\n\t\t\tXin vui long nhap lai !\n",
(totalEdges+1));
}
else
{
if( arrGraph[u][v].cap == 0 && arrGraph[v][u].cap == 0 && u!=0) // do
thi vo huong nen a[i][j] = a[j][i]
{
arrGraph[v][u].cap=1;
arrGraph[u][v].cap=1;
totalEdges++;
}
else
if(u!=0) printf("\t\t\tCanh %d da duoc nhap !\n\t\t\tXin vui
long nhap lai !\n",(totalEdges+1));
}
};
printf("\n\n\n\n");
printf("|---------------------HOAN TAT KHOI TAO DO THI-------------------------|\n");
printf("\n\n\t\t\tDO THI G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\tTong so canh: %d ",totalVertices,srcVertex,trgVertex,totalEdges);
printf("\n| |");
printf("\n|----------------------Bam phim bat ki de tiep
tuc!--------------------|\n");
getch();
menu();

}
void inputRandomGraph()
{
printf("|---------------------GRAPH VERTEX
CONNECTIVITY------------------------|\n\n\n\n");
printf("\t\t\tKHOI TAO DO THI RANDOM\n");
enterTotalVertices();
totalEdges=0;
entermaxtotalEdges :
int maxtotalEdges =0 ;
printf("\t\tGioi han so canh toi da(enter -1 to Random) : ");
scanf("%d",&maxtotalEdges);
if(maxtotalEdges >= -1)
{
printf("");
}
else
{
printf("\n\t\t\tBan da nhap sai so canh toi da!\n\t\t\tXin vui long nhap lai so
canh toi da\n");
goto entermaxtotalEdges;
}

srand((unsigned)time(NULL));
do
{
srcVertex=rand()%(totalVertices) +1; //Thuat toan rand()%(n) --> a thuoc [0,n-
1]
trgVertex=rand()%(totalVertices) +1;
}
while(srcVertex==trgVertex);
initializeMemory();
char flagPrt='n';
if(maxtotalEdges!=0)
{
prtEdges :
printf("\n\t\tBan co muon hien thi cac canh khong ?(y/n)\t");
fflush(stdin);
scanf("%c",&flagPrt);
switch(flagPrt)
{
case 'y' :
break;
case 'n':
break;
default :
goto prtEdges;
break;
};
}

//Random cac canh dua vao random bac cua moi dinh
totalEdges = 0 ;
for (int i=1;i<=totalVertices;i++)
{
int degree=rand()%totalVertices; //bac cua dinh i
if ((flagPrt=='y') && (degree > 0))
printf("\n\t\t");
for (int j=1;j<=degree;j++)
{
int adj=0;
do
{
adj=rand()%(totalVertices); //dinh ke voi dinh i
}
while(adj == i);
if( (adj != 0 )&& ( totalEdges<maxtotalEdges || maxtotalEdges==-1 ) )
{
if (arrGraph[i][adj].cap==0)
{
arrGraph[i][adj].cap=1;
arrGraph[adj][i].cap=1;
totalEdges++;
if (flagPrt=='y' ) printf("(%d,%d) ",i,adj);
}
}
}
}
printf("\n\n\n\n");
printf("|---------------------HOAN TAT KHOI TAO DO THI RANDOM------------------|\n");
printf("\n\n\t\t\tDO THI G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\tTong so canh: %d ",totalVertices,srcVertex,trgVertex,totalEdges);
printf("\n| |");
printf("\n|----------------------Bam phim bat ki de tiep
tuc!--------------------|\n");
getch();
menu();
}
void inputGraphFromFile()
{
printf("|---------------------GRAPH VERTEX
CONNECTIVITY------------------------|\n\n\n\n");
printf("\t\t\tKHOI TAO DO THI TU FILE VAN BAN\n");
char directory[50];
int u,v;
FILE *f = NULL;
enterDirectory:
printf("\n\t\tDuong dan tuyet doi toi File chua do thi G : ");
fflush(stdin);
scanf("%s",directory);
f=fopen(directory,"rt");
if (f == NULL)
{
printf("\n\t\t\tDuong dan khong chinh xac !\n\t\tXin vui long nhap lai!");
goto enterDirectory;
}
fscanf(f,"%d%d%d%d",&totalVertices,&totalEdges,&srcVertex,&trgVertex);
initializeMemory();
printf("\n\t\t");
for(int i=1;i<= totalEdges;i++)
{
fscanf(f,"%d%d",&u,&v);
arrGraph[u][v].cap=1;
arrGraph[v][u].cap=1;
printf("(%d,%d) ",u,v);
}
fclose(f);
printf("\n\n\n\n");
printf("|---------------------HOAN TAT KHOI TAO TU FILE------------------------|\n");
printf("\n\n\t\t\tDO THI G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\tTong so canh: %d ",totalVertices,srcVertex,trgVertex,totalEdges);
printf("\n| |");
printf("\n|----------------------Bam phim bat ki de tiep
tuc!--------------------|\n");
getch();
menu();
}

void saveGraphToFile()
{
printf("\n\t\t\tLUU DO THI RA FILE VAN BAN\n");
enterDirectoryWrite:
printf("\n\t\tDuong dan tuyet doi toi File chua do thi G : ");
char directory[50];
FILE *f=NULL;
fflush(stdin);
scanf("%s",directory);
f=fopen(directory,"wt");
if (f==NULL)
{
printf("\n\t\t\tDuong dan khong chinh xac !\n\t\tXin vui long nhap lai!");
goto enterDirectoryWrite;
}
fprintf(f,"\n\t\t\tDO THI G\n\n\t\t");
fprintf(f,"%d %d %d %d",totalVertices,totalEdges,srcVertex,trgVertex);

for (int i=1;i<=totalVertices;i++)


{
fprintf(f,"\n\t\t");
for(int j=i;j<=totalVertices;j++)
{
if(arrGraph[i][j].cap==1 && i<j)
fprintf(f,"%d %d\t",i,j);
}
}
fclose(f);
printf("\n\t\tDa ghi Do thi G vao file %s",directory);
printf("\n\t\tbam phim bat ki de tro lai menu tinh !");
getch();
menu();
}
void computingVertexConnectivity()
{
system("cls");
printf("|---------------------GRAPH VERTEX CONNECTIVITY------------------------|\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| TINH DO LIEN KET DINH k(u,v) |\n");
printf("| |\n");
printf("| 1.Su dung Thuat Toan 9 |\n");
printf("| 2.Su dung Thuat Toan 10 |\n");
printf("| 3.Su dung Thuat Toan 11 |\n");
printf("| 4.Thoat. |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|--------------------Quay lai Menu lua chon
(b)------------------------|\n\n");
fflush(stdin);
char flag1;
scanf("%c",&flag1);
switch(flag1)
{
case 'b' :
system("cls");
menu();
break;
case '1':
system("cls");
Algorithm9(srcVertex,trgVertex);
displayResult();
break;
case '2':
system("cls");
Algorithm10();
//displayResult();
break;
case '3':
system("cls");
Algorithm11();
displayResult();
break;
case '4':
exit(1);
break;
default :
menu();
break;
};

}
void getarrResult(int mf,int s, int t)
{
int tmpSrcVertex,tmpTrgVertex,count;
tmpSrcVertex=s;
tmpTrgVertex=t;
count=1;
while(tmpTrgVertex != s)
{
tmpTrgVertex=arrVerticesH[tmpTrgVertex].preVertex;
count++;
}
arrResult[mf]=(int*)malloc((count+1)*sizeof(int));
for(int i=count-1;i>=0;i--)
{
arrResult[mf][i]=tmpTrgVertex;
tmpTrgVertex=arrVerticesH[tmpTrgVertex].preVertex;
}
arrResult[mf][count]=-1;

}
bool findPath(int s, int t)
{
int* Queue=(int*)malloc((totalVerticesH+1)*sizeof(int));
if (Queue==NULL)
{
exit(1);
}
int First=1;
int Last=1;
Queue[1]=s;
int u,v,c,f;
for (int i=1;i<=totalVerticesH;i++)
{
arrVerticesH[i].preVertex=0;
}
arrVerticesH[s].preVertex=s;
arrVerticesH[s].delta=maxCapacity;
while (First<=Last)
{
u=Queue[First++];
for(v=1;v<=totalVerticesH;v++)
{
if (!arrVerticesH[v].preVertex)
{
c=arrGraphH[u][v].cap;
f=arrGraphH[u][v].flow;
if (c>0&&f<c)
{
arrVerticesH[v].preVertex=u;
arrVerticesH[v].delta=((c-f) < arrVerticesH[u].delta) ?
(c-f) : arrVerticesH[u].delta;
Queue[++Last]=v;
if (v==t)
{
return true;
}

}
else
{
c=arrGraphH[v][u].cap;
f=arrGraphH[v][u].flow;
if (c>0&&f>0)
{
arrVerticesH[v].preVertex=-u;
arrVerticesH[v].delta=f<arrVerticesH[u].delta?
f:arrVerticesH[u].delta;
Queue[++Last]=v;
if (v==t)
{
return true;
}
}
}
}
}
}
free(Queue);
return false;
}
void increaseFlow(int s, int t)
{

int v=t;
int u=arrVerticesH[v].preVertex;
increaseValue = arrVerticesH[t].delta;
do
{
if (u>0)
{
arrGraphH[u][v].flow+=increaseValue;
}
else
{
u=-u;
arrGraphH[v][u].flow-=increaseValue;
}
v=u;
u=arrVerticesH[v].preVertex;
} while(v!=s);
}
void maxFlow(int s, int t)
{
bool Stop=true;
maxflow=0;
while (Stop)
{
if (findPath(s,t))
{
increaseFlow(s,t);
maxflow += increaseValue;
getarrResult(maxflow,s,t);
}
else
{
Stop=false;
}
}
}

void Algorithm9(int src ,int trg )


{
int n=totalVertices;
vertexConnectivity=0;
for(int m=1;m<=totalVertices;m++)
{
if(arrResult[m]!=NULL)
{
free(arrResult[m]);
arrResult[m]=NULL;
}
}
for(int i=1;i <= totalVertices ; i++)
{
if (i!=src && i!=trg) arrGraphH[i][i+n].cap=1;
for(int j=i;j<=totalVertices;j++)
{
if (j!=src && j!=trg) arrGraphH[j][j+n].cap=1;
if(arrGraph[i][j].cap==1)
{
if((i==src || i==trg) && (j==src || j==trg ))
{
vertexConnectivity = n-1;
}
else
{
if(i!=src && i!=trg && j!=src && j!=trg )
{
arrGraphH[i+n][j].cap=1;
arrGraphH[j+n][i].cap=1;
}
if( (i==src || i==trg) && (j!=src && j!=trg))
{
arrGraphH[i][j].cap=1;
arrGraphH[j+n][i].cap=1;
}
if((i!=src && i!=trg) &&(j==src || j==trg))
{
arrGraphH[j][i].cap=1;
arrGraphH[i+n][j].cap=1;
}
}

}
}
}
maxFlow(src,trg);
if (vertexConnectivity==0) vertexConnectivity=maxflow;

}
void Algorithm10()
{
printf("|---------------------GRAPH VERTEX CONNECTIVITY-
ALGORITHM10-------------|\n\n\n\n");
printf("\n\n\t\t\tDO THI G\n\t\tTong so dinh : %d\n\t\tTong so canh:
%d",totalVertices,totalEdges);
printf("\n\t\tCac canh cua do thi G :");
bool flag;
for(int i=1;i<= totalVertices ; i++)
{
flag=false;
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) flag= true;
}
if(flag) printf("\n\t\t");
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) printf("(%d,%d) ",i,j);
}
}
int n=totalVertices;
int i=1;
int N=n-1;
for(i=1;i<= n ; i++)
{
if(i<=N)
{
printf("\n\t\t");
for(int j=i+1;j<=n;j++)
{
if(i!=j && arrGraph[i][j].cap==0)
{
Algorithm9(i,j );
for(int m=1;m<=totalVertices;m++)
{
if(arrResult[m]!=NULL)
{
free(arrResult[m]);
arrResult[m]=NULL;
}
}
printf("k(%d,%d)=%d ",i,j,vertexConnectivity);
if(N>vertexConnectivity) N =vertexConnectivity;

}
}
vertexConnectivity = N;

}
else vertexConnectivity = N;
}
printf("\n\t\tVertexConnectivity = %d",vertexConnectivity);
saveToFile :
printf("\n\n\t\tBan co muon Luu chi tiet ket qua khong ?(y/n)\t");
char flagPrt;
fflush(stdin);
scanf("%c",&flagPrt);
switch(flagPrt)
{
case 'y' :
saveResultToFileAlgorithm10();
break;
case 'n':
computingVertexConnectivity();
break;
default :
goto saveToFile;
break;
};
}
void Algorithm11()
{

}
void displayResult()
{
printf("|---------------------GRAPH VERTEX CONNECTIVITY-
ALGORITHM10------------|\n\n\n\n");
printf("\n\n\t\t\tDO THI G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\tTong so canh: %d",totalVertices,srcVertex,trgVertex,totalEdges);
printf("\n\t\tVertexConnectivity k(%d,%d) =
%d",srcVertex,trgVertex,vertexConnectivity);
prtDetail :
printf("\n\n\t\tBan co muon hien thi chi tiet ket qua khong ?(y/n)\t");
char flagPrt;
fflush(stdin);
scanf("%c",&flagPrt);
switch(flagPrt)
{
case 'y' :
displayDetailResult();
break;
case 'n':
computingVertexConnectivity();
break;
default :
goto prtDetail;
break;
};
}
void displayDetailResult()
{
system("cls");
printf("|---------------------GRAPH VERTEX CONNECTIVITY-
ALGORITHM10------------|\n\n\n\n");
printf("\n\n\t\t\tDO THI VO HUONG G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich
: %d %d\n\t\tTong so canh: %d",totalVertices,srcVertex,trgVertex,totalEdges);
printf("\n\t\tCac canh cua do thi G :");
bool flag;
for(int i=1;i<= totalVertices ; i++)
{
flag=false;
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) flag= true;
}
if(flag) printf("\n\t\t");
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) printf("(%d,%d) ",i,j);
}
}
if(totalVertices>=100)
{
//system("cls");
getch();
}
printf("\n\n\t\t\tMANG \n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\t",totalVerticesH,srcVertex,trgVertex);
printf("\n\t\tCac cung cua mang H:");
for(int i=1;i<= totalVerticesH ; i++)
{
flag=false;
for(int j=1;j<= totalVerticesH ; j++)
{
if(arrGraphH[i][j].cap==1) flag= true;
}
if(flag) printf("\n\t\t");
for(int j=1;j<= totalVerticesH ; j++)
{
if(arrGraphH[i][j].cap==1) printf("(%d,%d) ",i,j);
}
}
if(totalVertices>=100)
{
//system("cls");
getch();
}
if(vertexConnectivity!= totalVertices-1)
{
printf("\n\t\tDuong tang luong mang H:");
for(int i=1;i<=maxflow;i++)
{
int j=0;
printf("\n\t\t%d. ",i);
while(arrResult[i][j+1]!=-1)
{
printf("%d -> ",arrResult[i][j]);
j++;
}
printf("%d",arrResult[i][j]);
}
}

printf("\n\t\tVertexConnectivity k(%d,%d) =
%d",srcVertex,trgVertex,vertexConnectivity);
saveToFile :
printf("\n\n\t\tBan co muon Luu chi tiet ket qua khong ?(y/n)\t");
char flagPrt;
fflush(stdin);
scanf("%c",&flagPrt);
switch(flagPrt)
{
case 'y' :
saveResultToFile();
break;
case 'n':
computingVertexConnectivity();
break;
default :
goto saveToFile;
break;
};
}
void saveResultToFile()
{
printf("\n\t\t\tLUU KET QUA RA FILE VAN BAN\n");
enterDirectoryWrite:
printf("\n\t\tDuong dan tuyet doi toi File chua do thi G : ");
char directory[50];
FILE *f=NULL;
fflush(stdin);
scanf("%s",directory);
f=fopen(directory,"wt");
if (f==NULL)
{
printf("\n\t\t\tDuong dan khong chinh xac !\n\t\tXin vui long nhap lai!");
goto enterDirectoryWrite;
}
fprintf(f,"|---------------------GRAPH VERTEX
CONNECTIVITY------------------------|\n\n\n\n");
fprintf(f,"\n\n\t\t\tDO THI VO HUONG G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh
dich : %d %d\n\t\tTong so canh: %d",totalVertices,srcVertex,trgVertex,totalEdges);
fprintf(f,"\n\t\tCac canh cua do thi G :");
bool flag;
for(int i=1;i<= totalVertices ; i++)
{
flag=false;
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) flag= true;
}
if(flag) fprintf(f,"\n\t\t");
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) fprintf(f,"(%d,%d) ",i,j);
}
}
fprintf(f,"\n\n\t\t\tMANG \n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh dich : %d
%d\n\t\t",totalVerticesH,srcVertex,trgVertex);
fprintf(f,"\n\t\tCac cung cua mang H:");
for(int i=1;i<= totalVerticesH ; i++)
{
flag=false;
for(int j=1;j<= totalVerticesH ; j++)
{
if(arrGraphH[i][j].cap==1) flag= true;
}
if(flag) fprintf(f,"\n\t\t");
for(int j=1;j<= totalVerticesH ; j++)
{
if(arrGraphH[i][j].cap==1) fprintf(f,"(%d,%d) ",i,j);
}
}
for(int i=1;i<=maxflow;i++)
{
int j=0;
fprintf(f,"\n\t\t%d. ",i);
while(arrResult[i][j+1]!=-1)
{
fprintf(f,"%d -> ",arrResult[i][j]);
j++;
}
fprintf(f,"%d",arrResult[i][j]);
}
fprintf(f,"\n\t\tVertexConnectivity k(%d,%d) =
%d",srcVertex,trgVertex,vertexConnectivity);;

fclose(f);

printf("\n\t\tDa ghi thanh cong ket qua chi tiet vao file %s!",directory);
printf("\n\t\tBam phim bat ki de quay lai menu tinh!");
getch();
computingVertexConnectivity();
}
void saveResultToFileAlgorithm10()
{
printf("\n\t\t\tLUU KET QUA RA FILE VAN BAN\n");
enterDirectoryWrite:
printf("\n\t\tDuong dan tuyet doi toi File chua do thi G : ");
char directory[50];
FILE *f=NULL;
fflush(stdin);
scanf("%s",directory);
f=fopen(directory,"wt");
if (f==NULL)
{
printf("\n\t\t\tDuong dan khong chinh xac !\n\t\tXin vui long nhap lai!");
goto enterDirectoryWrite;
}
fprintf(f,"|---------------------GRAPH VERTEX
CONNECTIVITY------------------------|\n\n\n\n");
fprintf(f,"\n\n\t\t\tDO THI VO HUONG G\n\t\tTong so dinh : %d\n\t\tDinh nguon , dinh
dich : %d %d\n\t\tTong so canh: %d",totalVertices,srcVertex,trgVertex,totalEdges);
fprintf(f,"\n\t\tCac canh cua do thi G :");
bool flag;
for(int i=1;i<= totalVertices ; i++)
{
flag=false;
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) flag= true;
}
if(flag) fprintf(f,"\n\t\t");
for(int j=i;j<= totalVertices ; j++)
{
if(arrGraph[i][j].cap==1) fprintf(f,"(%d,%d) ",i,j);
}
}
fprintf(f,"\n\t\tVertexConnectivity k(G) = %d",vertexConnectivity);;

fclose(f);

printf("\n\t\tDa ghi thanh cong ket qua chi tiet vao file %s!",directory);
printf("\n\t\tBam phim bat ki de quay lai menu tinh!");
getch();
computingVertexConnectivity();
}