Chinaunix首页 | 论坛 | 博客
  • 博客访问: 621170
  • 博文数量: 172
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 1252
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-29 22:26
文章分类

全部博文(172)

文章存档

2011年(6)

2010年(7)

2009年(159)

我的朋友

分类: C/C++

2011-01-11 00:43:03


#include
#include
#include
 
typedef struct tagGraph
{
    unsigned int ulVecNum;
    unsigned int ulEdgeNum;
    int **pplAdj;
}GraphStru;
 
typedef struct tagEdge
{
    unsigned int ulVec1;
    unsigned int ulVec2;
    int lDirect;
}EdgeStru;
 
EdgeStru RandEdge(int ulVecMaxNum)
{
    EdgeStru stEdge;
    stEdge.ulVec1 = rand() % ulVecMaxNum; 
    stEdge.ulVec2 = rand() % ulVecMaxNum;
    stEdge.lDirect = 0;
    return stEdge;
}
 
void GraphInserEdge(GraphStru *pstGraph, EdgeStru *pstEdge)
{
    pstGraph->pplAdj[pstEdge->ulVec1][pstEdge->ulVec2] = 1;
    pstGraph->pplAdj[pstEdge->ulVec2][pstEdge->ulVec1] = 1;
}
 
void GraphShow(GraphStru *pstGraph)
{
    int i = 0;
    int j = 0;
    for(i = 0; i < pstGraph->ulVecNum; i++)
    {
        for(j = 0; j < pstGraph->ulVecNum; j++)
        {
            printf("%d ", pstGraph->pplAdj[i][j]);
        }
        printf("\n");
    }
}
 
void GraphRandEdge(GraphStru *pstGraph, unsigned int ulEdgeNum)
{
    int i = 0;
    EdgeStru stEdge = {0};
    srand(time(NULL));
    for(i = 0; i < ulEdgeNum; i++)
    {
        do
        {
            stEdge = RandEdge(pstGraph->ulVecNum);
        }while(stEdge.ulVec1 == stEdge.ulVec2);     
        GraphInserEdge(pstGraph, &stEdge);
    }
}
 
GraphStru *GraphInit(unsigned int ulVecNum, unsigned int ulEdgeNum)
{
    int i = 0;
 
    GraphStru *pstGraph = malloc(sizeof(*pstGraph));
 
    pstGraph->ulVecNum = ulVecNum;
    pstGraph->ulEdgeNum = ulEdgeNum;
     
    pstGraph->pplAdj = (int **)calloc(ulVecNum * sizeof(int*), 1);
    pstGraph->pplAdj[0] = (int *)calloc(ulVecNum * ulVecNum * sizeof(int), 1);
    for(i = 1; i < ulVecNum; i++)
    {
        pstGraph->pplAdj[i] = pstGraph->pplAdj[0] + (ulVecNum * i);
    }
     
    GraphRandEdge(pstGraph, ulEdgeNum);
    return pstGraph;
}
 
int main()
{
    GraphStru *pstGraph = GraphInit(20,80);
    GraphShow(pstGraph);
    return 0;
}

输出结果:
0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 1 0 1 1 1
1 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0
1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 1
0 0 1 0 1 1 1 0 1 0 0 0 0 1 0 0 0 0 0 1
0 1 1 1 0 1 0 1 0 1 1 0 0 0 1 0 0 0 0 1
0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0
1 0 0 1 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1
1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0
0 0 0 0 1 0 1 0 1 0 1 0 0 1 0 0 0 1 0 0
1 0 1 0 1 0 1 0 0 1 0 1 1 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0
0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 0
0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 1 0
1 1 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1
1 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 0
0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0
1 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0
1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0
阅读(1959) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-08 13:51:51

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com