YHS_CODE的专栏

一步一个脚印

邻接多重表(Adjacency Multilist)

#include<stdio.h>
#include<malloc.h>
#include<string.h>
/*----------------------------邻接多重表的存储结构---------------------------*/
#define  MAX_VERTEX_NUM     20

typedef int  VRType;
typedef int  VertexType;

int visited[MAX_VERTEX_NUM];
int low[MAX_VERTEX_NUM];
int count;
struct EBox {
   int   mark;
   int   ivex,jvex;
   struct EBox *ilink,*jlink;
   /* VRType   weight; */
};

struct VexBox {
  VertexType    data;
  struct EBox  *firstedge;
};

typedef struct {
  struct VexBox  adjmulist[MAX_VERTEX_NUM];
  int vexnum,arcnum;
}AMLGraph;

/*==========森林========*/
struct CSNode {
  int data;
  struct CSNode *firstchild,*nextsibling;
};

/*============================================================================*/
/*                                                                            */
/*                         顺   序   队  列                                   */
/*                                                                            */
/*============================================================================*/
typedef struct{
    int data;
}elemtype;

#define QUEUE_MAXSIZE 100
typedef struct{
    elemtype elem[QUEUE_MAXSIZE];
    int front ,rear;
    int length;
}QUEUE;

void queue_init(QUEUE *q)
{
   (*q).front=(*q).rear=-1; (*q).length=0;
}

int queue_empty(QUEUE *q)
{
   if((*q).length==0) return 1;
   else               return 0;
}

void queue_push(QUEUE *q,elemtype e)
{
   if((*q).length==QUEUE_MAXSIZE) { printf("QUEUE IS FULL"); return ;}
   (*q).rear=++(*q).rear%QUEUE_MAXSIZE;
   (*q).elem[(*q).rear]=e;
   (*q).length++;
   if((*q).length==1&&(*q).front==-1) (*q).front=0;
}

void queue_pop(QUEUE *q,elemtype *e)
{
   if((*q).length==0) { printf("QUEUE IS EMPTY"); return ;}
   *e=(*q).elem[(*q).front++];
   (*q).front=(*q).front%QUEUE_MAXSIZE;
   (*q).length--;
   if((*q).length==0) (*q).front=(*q).rear=-1;
}

/*============================邻接多重表的基本操作============================*/
int GetVex(AMLGraph G,int v)
{
  int vex_data;
  vex_data =G.adjmulist[v].data;
  return vex_data;
}

int LocateVex(AMLGraph *G,VertexType v)
{
  int i;
  for(i = 0;i < G->vexnum;i ++)
    if(v == (G->adjmulist[i]).data )
       return i;
  return -1;
}

/*返回ivex第一个邻接顶点的位置*/
int FirstAdjVex(AMLGraph *G,int ivex)
{
   int f_adjvex;
   struct EBox *firstarc;

   firstarc = G->adjmulist[ivex].firstedge;
   if(firstarc != NULL) f_adjvex = ((ivex == firstarc->ivex)?(firstarc->jvex):(firstarc->ivex));
   else            f_adjvex = -1;

   return f_adjvex;
}

/*返回ivex相对于wvex的下一个邻接顶点的位置*/
int NextAdjVex(AMLGraph *G,int ivex ,int wvex)
{
   int f_next,temp;
   struct EBox *firstarc;

   /*返回顶点ivex的出度相对于wvex的下一个邻接点*/
   firstarc = G->adjmulist[ivex].firstedge;
   if(firstarc != NULL)
   {
      while(firstarc != NULL )
      {
        temp=((ivex == firstarc->ivex)?(firstarc->jvex):(firstarc->ivex));
        if(wvex == temp)
        {
           firstarc = ((ivex == firstarc->ivex)?(firstarc->ilink):(firstarc->jlink));
           if(firstarc != NULL) f_next = ((ivex == firstarc->ivex)?(firstarc->jvex):(firstarc->ivex));
           else            f_next = -1;
           break;
        }
           firstarc =((ivex == firstarc->ivex)?(firstarc->ilink):(firstarc->jlink));
      }/*while*/
   }
   else  f_next= -1;
   return f_next;
}

/* int visited[MAX_VERTEX_NUM] */
void VisitFunc(AMLGraph *G,int ivex)
{
  printf("%6d",G->adjmulist[ivex].data);

}

void DFS( AMLGraph *G,int i_vex )
{
   int w_vex;
   visited[i_vex] = 1; VisitFunc(G,i_vex);
   for(w_vex = FirstAdjVex(G,i_vex); w_vex>=0; w_vex=NextAdjVex(G,i_vex,w_vex) )
     if(!visited[w_vex])  DFS(G,w_vex);
}

void DFSTraverse( AMLGraph *G )
{
   int i_vex;
   for(i_vex = 0; i_vex < G->vexnum; i_vex ++) visited[i_vex] = 0;
   for(i_vex = 0; i_vex < G->vexnum; i_vex ++)
     if(!visited[i_vex]) DFS(G,i_vex);
}

void BFSTraverse(AMLGraph *G)
{
  int i_vex,u_vex,w_vex;
  elemtype e1,e2,e3;
  QUEUE Q;

  for(i_vex = 0; i_vex < G->vexnum;i_vex ++)  visited[i_vex] = 0;
  queue_init(&Q);

  for(i_vex = 0;i_vex < G->vexnum;i_vex ++)
    if(!visited[i_vex])
    {
      visited[i_vex]=1; VisitFunc(G,i_vex);
      e1.data = i_vex;
      queue_push(&Q,e1);

      while(!queue_empty(&Q))
      {
        queue_pop(&Q,&e2); u_vex = e2.data;
        for(w_vex = FirstAdjVex(G,u_vex);w_vex >= 0;w_vex = NextAdjVex(G,u_vex,w_vex))
         if(!visited[w_vex])
         {
            visited[w_vex]=1;VisitFunc(G,w_vex);
            e3.data =w_vex;queue_push(&Q,e3);
          }/* if */
      }/* while */
  }/* if */
}
/* =============================生成树和生成森林============================= */
void DFSTree(AMLGraph G, int v, struct CSNode **T)
{
  int w,first;
  struct CSNode *p,*q;

  first =1; visited[v] = 1;
  for(w=FirstAdjVex(&G,v);  w>=0;  w=NextAdjVex(&G,v,w))
    if (!visited[w]) {
      p = (struct CSNode *) malloc (sizeof (struct CSNode));
      p->data = GetVex(G,w);
      p->firstchild=NULL;
      p->nextsibling=NULL;
      if(first) { (*T)->firstchild = p;  first = 0; }
      else      { q->nextsibling = p;}
      q = p;
      DFSTree(G,w,&q) ;
    }
}

void DFSForest(AMLGraph G,struct CSNode **T)
{
  int v;
  struct CSNode *p,*q;
  *T = NULL;
  for (v=0; v<G.vexnum; ++v)  visited[v] = 0;
  for (v=0; v<G.vexnum; ++v)
    if (!visited[v]) {
      p= (struct CSNode *)malloc(sizeof(struct CSNode));
      p->data=GetVex(G,v);
      p->firstchild=NULL;
      p->nextsibling=NULL;
      if (!*T) *T = p;
      else q->nextsibling = p;
      q = p;
      DFSTree(G,v,&p);
    }
}

void Forest_Traverse(struct CSNode *p)
{
    if(p){
      printf("%6d",p->data);
      Forest_Traverse( p->firstchild );
      Forest_Traverse( p->nextsibling );
    }
}

/* ==========================图的关节点和重连通分量========================= */
void  DFSArticul(AMLGraph G, int v0 )
{
  int min,w;
  visited[v0] = min = ++count; 
  for (w=FirstAdjVex(&G,v0);  w>=0;  w=NextAdjVex(&G,v0,w))
  {             
    if (visited[w] == 0) {     
      DFSArticul(G, w);        
      if (low[w] < min)  min = low[w];
      if (low[w] >= visited[v0])  printf("%6d",G.adjmulist[v0].data); 
    }
    else if (visited[w] < min)  min = visited[w];  
  }
  low[v0] = min;
} /*  DFSArticul */

void  FindArticul(AMLGraph G)
{
  /*  全局量count对访问计数。 */
  int i,v=0,w; struct EBox  *p;
  count=1; visited[0] = count ;        /*  设定邻接表上0号顶点为生成树的根 */

  for(i=1; i<G.vexnum; ++i) visited[i] = 0;
  p = G.adjmulist[v].firstedge; 
  w = FirstAdjVex(&G,v);
  DFSArticul(G,w);
           
  if (count < G.vexnum)
  { 
      printf ("%6d",G.adjmulist[v].data); 
      while (w>=0)
      { 
        w = NextAdjVex(&G,v,w);
        if (visited[w]==0)  DFSArticul(G, w);
      }
  }
 
} /*  FindArticul */

/*=========================创建邻接多重表存储的有向图=========================*/
int CreateUDG(AMLGraph *G)
{
   int i,j,k;
   /* VRType weight=0;*/
   VertexType v1,v2;
   struct EBox *p;

   printf("/n Adjacencry Multilist Create UDG/n");


   printf("/n/n The Graph's number of Vertex :"); scanf("%d",&(G->vexnum));
   printf(" The Graph's number of Arcnum :"); scanf("%d",&(G->arcnum));
   printf("/n");
   for(i=0;i<G->vexnum;i++) {
      printf(" The Graph's %d Vertex's NAME:",i+1); scanf("%d",&(G->adjmulist[i].data));
      G->adjmulist[i].firstedge = NULL;
   }

   for(k=0;k<G->arcnum;k++)       /*create adjacency multilist */
   {
     printf("/n The %d Arc ./n",k+1);
     printf(" The tail vertex:");do{ scanf("%d",&v1);if((v1<=0)||(v1>G->vexnum))printf(" ERROR/n The tail vertex:"); }while((v1<=0)||(v1>G->vexnum));
     printf(" The head vertex:");do{ scanf("%d",&v2);if((v2<=0)||(v2>G->vexnum))printf(" ERROR/n The head vertex:"); }while((v2<=0)||(v2>G->vexnum));
     /*printf(" The arc  weight:");do{ scanf("%d",&weight); if(weight < 0) printf(" ERROR/n The arc  weight:"); }while(weight < 0);*/

     i = LocateVex(G,v1); j = LocateVex(G,v2);

     p = (struct EBox *)malloc(sizeof(struct EBox));
     p->ivex = i;
     p->jvex = j;
     p->ilink = G->adjmulist[i].firstedge;
     p->jlink = G->adjmulist[j].firstedge;
     /* p->weight= 0; */
     G->adjmulist[i].firstedge = G->adjmulist[j].firstedge = p;

   }
   return 0;
}

void Prin_AMLGraph(AMLGraph *G)
{
   int k;
   struct EBox *p;

   printf("/n/n The Adjacenacy Arc  =>/n/n");
   for(k = 0;k <G->vexnum;k ++)
   {
      p = G->adjmulist[k].firstedge;
      printf("  /tVertex %d :",k+1);
      while( p != NULL ){
        printf(" (%-2d,%-2d) ",(k == p->ivex)?(1+p->ivex):(1+p->jvex),(k == p->jvex)?(1+p->ivex):(1+p->jvex));
        if(k == p->ivex)      { p = p->ilink; }
        else if(k == p->jvex) { p = p->jlink; }
      }
      printf("/n");
   }

}
/*------------------------------------------------------------------------------*/
int main()
{
   struct CSNode *T=NULL;
   AMLGraph G;
   CreateUDG(&G);
   Prin_AMLGraph(&G);
   printf("/n DFSTraverse AMLGraph:/n");
   DFSTraverse(&G);
   printf("/n BFSTraverse AMLGraph:/n");
   BFSTraverse(&G);  
   printf("/n AMLGraph DFSForest:/n");
   DFSForest(G,&T);
   Forest_Traverse(T);
   printf("/n AMLGraph Arcticular Point:/n");
   FindArticul(G);
   printf("/n/n");
   getch();


}

阅读更多
文章标签: struct null graph 存储
个人分类: 数据结构
想对作者说点什么? 我来说一句

防止表单重复提交-php开发网站

-

学院

1970年01月01日 08:00

没有更多推荐了,返回首页

不良信息举报

邻接多重表(Adjacency Multilist)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭