/*
* ( )
*/
#include<stdio.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef struct
{
int vertexNum;//
VertexType vertex[MAX_VERTEX_NUM];//
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//
}GraphMatrix,*PGraphMatrix;
// vertex , ,
VertexType vertex[MAX_VERTEX_NUM];
//
void createGraph(PGraphMatrix pGraph)
{
int i,j,arcNum,from,to,weight;
printf("please enter the numbers of vertex( ):
");
scanf("%d",&pGraph->vertexNum);
for(i=0;i<pGraph->vertexNum;i++)
pGraph->vertex[i]='a'+i;
for(i=0;i<pGraph->vertexNum;i++)
for(j=0;j<pGraph->vertexNum;j++)
pGraph->arcs[i][j]=0;
printf("please enter the numbers of arcs( ):
");
scanf("%d",&arcNum);
for(i=0;i<arcNum;i++){
printf("please enter the information of arcs( ),( ):
");
scanf("%d%d%d",&from,&to,&weight);
pGraph->arcs[from][to]=weight;
pGraph->arcs[to][from]=weight;
}
}
//
VertexType getVertexByIndex(PGraphMatrix pGraph,int index)
{
return pGraph->vertexNum==0?'0':pGraph->vertex[index];
}
//
VertexType getFirstVertex(PGraphMatrix pGraph)
{
return pGraph->vertexNum==0?'0':pGraph->vertex[0];
}
// index
VertexType getVertexAfterIndex(PGraphMatrix pGraph,int index)
{
return (index>=0 && index+1 < pGraph->vertexNum) ? pGraph->vertex[index+1] : '0';
}
// index
VertexType getVertexNextIndex(PGraphMatrix pGraph,int index)
{
int j;
if(index>=0&&index<pGraph->vertexNum){
for(j=0;j<pGraph->vertexNum;j++)
if(j!=index&&pGraph->arcs[index][j]!=0)
return pGraph->vertex[j];
}
return '0';
}
//
int findVertex(PGraphMatrix pGraph,VertexType vertex)
{
int i;
for(i=0;i<pGraph->vertexNum;i++)
if(pGraph->vertex[i]==vertex)
return i;
return -1;
}
//
int getArcsNum(PGraphMatrix pGraph,int index)
{
int j,k=0;
if(index>=0&&index<pGraph->vertexNum){
for(j=0;j<pGraph->vertexNum;j++)
if(pGraph->arcs[index][j]!=0)
k++;
return k;
}
return -1;
}
//
void breadthTraverse(PGraphMatrix pGraph)
{
int i,j,k;
int flag[MAX_VERTEX_NUM];//
int front,rear;
front=rear=0;
for(i=0;i<MAX_VERTEX_NUM;i++)
flag[i]=-1;
//
front=rear=0;
vertex[rear++]=pGraph->vertex[0];
flag[0]=0;
while(front<rear){
i=flag[front];
for(j=0;j<pGraph->vertexNum;j++){// i
for(k=0;k<rear;k++)//
if(flag[k]==j)
break;
if(k==rear && i!=j && pGraph->arcs[i][j]!=0){
vertex[rear]=pGraph->vertex[j];
flag[rear++]=j;
}
}
front++;
}
}
//
void depthTraverse(PGraphMatrix pGraph)
{
int i,j,top=0,k,m;
int flag[MAX_VERTEX_NUM];//
for(i=0;i<MAX_VERTEX_NUM;i++)
flag[i]=-1;
//
vertex[top++]=pGraph->vertex[0];
flag[0]=0;
while(top>0 && top<pGraph->vertexNum){
if(j==pGraph->vertexNum)// ,
i=flag[--m];
else
m=i=flag[top-1];
for(j=0;j<pGraph->vertexNum;j++){
for(k=0;k<top;k++)
if(flag[k]==j)
break;
if(k==top && i!=j && pGraph->arcs[i][j]!=0){
vertex[top]=pGraph->vertex[j];
flag[top++]=j;
break;
}
}
}
}
//
void main()
{
int i;
GraphMatrix graph;
createGraph(&graph);
printf(" :%c
",getVertexByIndex(&graph,1));
printf(" :%c
",getFirstVertex(&graph));
printf("index :%c
",getVertexAfterIndex(&graph,0));
printf(" index :%c
",getVertexNextIndex(&graph,1));
printf(" :%d
",findVertex(&graph,'b'));
printf(" :%d
",getArcsNum(&graph,0));
printf("----------- -----------------
");
breadthTraverse(&graph);
for(i=0;i<graph.vertexNum;i++)
printf("%-3c",vertex[i]);
printf("
------- ------------------
");
depthTraverse(&graph);
for(i=0;i<graph.vertexNum;i++)
printf("%-3c",vertex[i]);
printf("
");
}