与图相关的算法

Graphtest.h

ExpandedBlockStart.gif 代码
/*  c1.h (程序名)  */
 #include
< string .h >
 #include
< ctype.h >
 #include
< malloc.h >   /*  malloc()等  */
 #include
< limits.h >   /*  INT_MAX等  */
 #include
< stdio.h >   /*  EOF(=^Z或F6),NULL  */
 #include
< stdlib.h >   /*  atoi()  */
 #include
< io.h >   /*  eof()  */
 #include
< math.h >   /*  floor(),ceil(),abs()  */
 #include
< process.h >   /*  exit()  */
 
/*  函数结果状态代码  */
 
#define  TRUE 1
 
#define  FALSE 0
 
#define  OK 1
 
#define  ERROR 0
 
#define  INFEASIBLE -1
 
/*  #define OVERFLOW -2 因为在math.h中已定义OVERFLOW的值为3,故去掉此行  */
 typedef 
int  Status;  /*  Status是函数的类型,其值是函数结果状态代码,如OK等  */
 typedef 
int  Boolean;  /*  Boolean是布尔类型,其值是TRUE或FALSE  */


#define  MAX_NAME 5  /*顶点字符串的最大长度+1 */
typedef 
int  VRType;
typedef 
char  InfoType;
typedef 
char  VertexType[MAX_NAME];

#define  INFINITY INT_MAX
#define  MAX_VERTEX_NUM 20
typedef 
enum {DG,DN,AG,AN}GraphKind;

typedef 
struct  ArcCell{
    VRType adj;
// 顶点关系类型
    InfoType  * info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef 
struct  
{
    VertexType vexs[MAX_VERTEX_NUM];
    AdjMatrix arcs;
    
int  vexnum,arcnum;
    GraphKind kind;
}MGraph;

 

 

fun.h

 

ExpandedBlockStart.gif 代码
Status LocateVex(MGraph G,VertexType u);
Status CreateFAG(MGraph 
* G);
void  Display(MGraph G);
Status Visit(VertexType v);
int  FirstAdjVex(MGraph G, int  v);
int  NextAdjVex(MGraph G, int  v, int  w);
Status Visit(VertexType v);
void  DFSTraverse(MGraph G,Status ( *  Visit)(VertexType v));
void  DFS(MGraph G, int  v);
void  BFSTraverse(MGraph G, Status( *  Visit)(VertexType v));

 

 

Queue.h

 

ExpandedBlockStart.gif 代码
typedef  int  QElemType;
typedef 
struct  QNode
{
    QElemType data;
    
struct  QNode  * next;
}QNode,
* QueuePtr;

typedef 
struct
{
    QueuePtr front,rear;
}LinkQueue;


Status InitQueue(LinkQueue 
* Q)
{
    (
* Q).front = ( * Q).rear = (QueuePtr)malloc( sizeof (QNode));
    
if ( ! ( * Q).front)
        exit(OVERFLOW);
    (
* Q).front -> next = NULL;
    
return  OK;
}

Status EnQueue(LinkQueue 
* Q,QElemType e)
{
    QueuePtr p
= (QueuePtr)malloc( sizeof (QNode));
    
if ( ! p)
        exit(OVERFLOW);
    p
-> data = e;
    p
-> next = NULL;
    (
* Q).rear -> next = p;
    (
* Q).rear = p;
    
return  OK;
}

Status DeQueue(LinkQueue 
* Q,QElemType  * e)
{
    QueuePtr p;
    
if (( * Q).front == ( * Q).rear)
        
return  ERROR;
    p
= ( * Q).front -> next;
    
* e = p -> data;
    (
* Q).front -> next = p -> next;
    
if (( * Q).rear == p)
        (
* Q).rear = ( * Q).front;
    free(p);
    
return  OK;
}

Status QueueEmpty(LinkQueue Q)
{
    
if (Q.front == Q.rear)
        
return  TRUE;
    
else
        
return  FALSE;
}

 

 

Traverse.cpp

 

 

ExpandedBlockStart.gif 代码
// #include "Graphtest.h"
#include  " stdafx.h "
#include 
" Graphtest.h "
#include 
" fun.h "

Boolean visited[MAX_VERTEX_NUM];
Status (
* VisitFunc)(VertexType v);


int  LocateVex(MGraph G,VertexType u)
{
    
int  i;
    
for (i = 0 ;i < G.vexnum;i ++ )
        
if (strcmp(u,G.vexs[i]) == 0 )
            
return  i;
    
return   - 1 ;
}

Status CreateFAG(MGraph 
* G)
{
    
int  i,j,k;
    
char  filename[ 13 ];
    VertexType va,vb;
    FILE 
* graphlist;
    printf(
" please input data filename(f7-1.dat):\n " );
    scanf(
" %s " ,filename);
    graphlist
= fopen(filename, " r " );
    
    fscanf(graphlist,
" %d " , & ( * G).vexnum);
    fscanf(graphlist,
" %d " , & ( * G).arcnum);

    
for (i = 0 ;i < ( * G).vexnum;i ++ )
    {
        fscanf(graphlist,
" %s " ,( * G).vexs[i]);
        printf(
" %s\t " ,( * G).vexs[i]);
    }

    
for (i = 0 ;i < ( * G).vexnum;i ++ )
        
for (j = 0 ;j < ( * G).vexnum;j ++ )
        {
            (
* G).arcs[i][j].adj = 0 ;
            (
* G).arcs[i][j].info = NULL;
        }

    
for (k = 0 ;k < ( * G).arcnum;k ++ )
    {
        fscanf(graphlist,
" %s%s " ,va,vb);
        i
= LocateVex( * G,va);
        j
= LocateVex( * G,vb);
        (
* G).arcs[i][j].adj = ( * G).arcs[j][i].adj = 1 ;
    }

    fclose(graphlist);
    (
* G).kind = AG;
    
    
return  OK;
}


void  Display(MGraph G)
{
    
int  i,j;
    
char  s[ 7 ],s1[ 3 ];
    
switch (G.kind)
    {
        
case  DG:strcpy(s, " 有向图\0 " );
            strcpy(s1,
" 弧\0 " );
            
break ;
             
case  DN: strcpy(s, " 有向网\0 " );
              strcpy(s1,
" 弧\0 " );
              
break ;
     
case  AG: strcpy(s, " 无向图\0 " );
              strcpy(s1,
" 边\0 " );
              
break ;
     
case  AN: strcpy(s, " 无向网\0 " );
              strcpy(s1,
" 边\0 " );
   }

    printf(
" %d个顶点%d条%s的%s\n " ,G.vexnum,G.arcnum,s1,s);

    
for (i = 0 ;i < G.vexnum; ++ i)
        printf(
" G.vexs[%d]=%s\n " ,i,G.vexs[i]);
    printf(
" G.arcs.adj:\n " );
    
for (i = 0 ;i < G.vexnum;i ++ )
    {
        
for (j = 0 ;j < G.vexnum;j ++ )
            printf(
" %6d " ,G.arcs[i][j].adj);
        printf(
" \n " );
    }
    printf(
" G.arcs.info:\n " );
    printf(
" 顶点1(弧尾) 顶点2(弧头) 该%s信息:\n " ,s1);
    
if (G.kind < 2 )
        
for (i = 0 ;i < G.vexnum;i ++ )
            
for (j = 0 ;j < G.vexnum;j ++ )
            {
                
if (G.arcs[i][j].info)
                    printf(
" %5s %11s   %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
            }
    
else
    {
        
for (i = 0 ;i < G.vexnum;i ++ )
            
for (j = i + 1 ;j < G.vexnum;j ++ )
                
if (G.arcs[i][j].info)
                    printf(
" %5s %11s     %s\n " ,G.vexs[i],G.vexs[j],G.arcs[i][j].info);
   }
 }


int  FirstAdjVex(MGraph G, int  v)
{
    
int  i;
    
for (i = 0 ;i < G.vexnum;i ++ )
        
if (G.arcs[v][i].adj != 0 )
            
return  i;
    
return   - 1 ;
}

int  NextAdjVex(MGraph G, int  v, int  w)
{
    
int  i;
    
for (i = w + 1 ;i < G.vexnum;i ++ )
        
if (G.arcs[v][i].adj != 0 )
            
return  i;
    
return   - 1 ;
}


Status Visit(VertexType v)
{
    printf(
"  visit %s\n " ,v);
    
return  OK;
}

void  DFSTraverse(MGraph G,Status ( *  Visit)(VertexType v))
{
    
int  v;
    VisitFunc
= Visit;
    
for (v = 0 ;v < G.vexnum;v ++ )
        visited[v]
= FALSE;
    
for (v = 0 ;v < G.vexnum;v ++ )
        
if ( ! visited[v])
            DFS(G,v);
}

void  DFS(MGraph G, int  v)
{
    
int  w;
    visited[v]
= true ;
    VisitFunc(G.vexs[v]);
    
// Visit(G.vexs[v]);
     for (w = FirstAdjVex(G,v);w >= 0 ;w = NextAdjVex(G,v,w))
        
if ( ! visited[w])
            DFS(G,w);
}

#include 
" Queue.h "
void  BFSTraverse(MGraph G, Status( *  Visit)(VertexType v))
{
    
int  v,u,w;
    LinkQueue Q;
    
for (v = 0 ;v < G.vexnum;v ++ )
        visited[v]
= FALSE;
    InitQueue(
& Q);
    
for (v = 0 ;v < G.vexnum;v ++ )
    {
        
if ( ! visited[v])
        {
            visited[v]
= TRUE;
            Visit(G.vexs[v]);
            EnQueue(
& Q,v);
            
while ( ! QueueEmpty(Q))
            {
                DeQueue(
& Q, & u);
                
for (w = FirstAdjVex(G,u);w >= 0 ;w = NextAdjVex(G,u,w))
                {
                    
if ( ! visited[w])
                    {
                        visited[w]
= true ;
                        Visit(G.vexs[w]);
                        EnQueue(
& Q,w);
                    }
                }
            }
        }
    }
}


 

 

Graphtest.cpp

 

 

ExpandedBlockStart.gif 代码
#include  " stdafx.h "
#include 
" Graphtest.h "
#include 
" fun.h "



int  _tmain( int  argc, _TCHAR *  argv[])
{
    MGraph g;
    CreateFAG(
& g);
    Display(g);

    printf(
" now DFS the Graph:\n " );
    DFSTraverse(g,Visit);

    printf(
" now BFS the Graph:\n " );
    BFSTraverse(g,Visit);

    
while ( 1 );
    
return   0 ;
}

 

 

 

转载于:https://www.cnblogs.com/newgreen/archive/2010/10/17/1853675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值