| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2002 人关注过本帖
标题:这代码哪错了 采用邻接矩阵法构造有向图,深度优先搜索输出
只看楼主 加入收藏
不同认为
Rank: 1
等 级:新手上路
帖 子:93
专家分:3
注 册:2015-11-25
结帖率:57.14%
收藏
已结贴  问题点数:20 回复次数:3 
这代码哪错了 采用邻接矩阵法构造有向图,深度优先搜索输出
#include <stdio.h>
#define MAX_VERTEX_NUM 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int VRType;   
typedef char VertexType;
typedef char InfoType;
typedef enum {DG,DN,UDG,UDN}GraphKind;
int visited[MAX_VERTEX_NUM];
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;
int LocateVex(MGraph G,VertexType v)
{
     int j=-1,k;
     for(k=0;k<G.vexnum;k++)
         if(G.vexs[k]==v)
        {
            j=k;
            break;
        }
     return j;
}
Status CreateUDN(MGraph &G)
{
    int i,j,k;
    VertexType v1,v2;
    scanf("%d%d",&G.vexnum,&G.arcnum);
    getchar();
    for(i=0;i<G.vexnum;i++)
        scanf("%c",&G.vexs[i]);
    for(i=0;i<G.vexnum;i++)
    for(j=0;j<G.vexnum;j++)
        G.arcs[i][j].adj=0;
      for (k=0;k<G.arcnum;k++)
      {   
          getchar();
        scanf("%c%c",&v1,&v2);  
        i=LocateVex(G,v1);j=LocateVex(G,v2);  
        G.arcs[i][j].adj=1;
      }
      return OK;
  
}
Status FirstAdjVex(MGraph G,VertexType v)
{
    int j,i;
    i=LocateVex(G,v);
    for(j=0;j<G.vexnum;j++)
        if(G.arcs[i][j].adj==1)
            return j;
        return ERROR;
}
Status NextAdjVex(MGraph G,VertexType v,VRType w)
{
    int j,i;
    i=LocateVex(G,v);
    for(j=w+1;j<G.vexnum;j++)
        if(G.arcs[i][j].adj==1)
       return j;
        return ERROR;
}
void DFS(MGraph G,int v)
{
  int w;
  visited[v]=true;
  printf("%c",G.vexs[v]);
  for(w=FirstAdjVex(G,v);w;w=NextAdjVex(G,v,w))
    if(!visited[w])
        DFS(G,w);
}
void DFSTraverse(MGraph G)
{   
    int v;
    for(v=0;v<G.vexnum;v++)
        if(!visited[v])
            DFS(G,v);
}
int main()
{
    MGraph G;
    int i;
    CreateUDN(G);
    for(i=0;i<MAX_VERTEX_NUM;i++)
    visited[i]=0;
    DFSTraverse(G);
    printf("\n");
    return 0;
}
搜索更多相关主题的帖子: include 
2015-11-25 18:19
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:20 
你的代码太乱了,还有c++的引用

这是我整理后的,把参数改为指针可以节省很多内存
程序代码:
#include <stdio.h>

#define MAX_VERTEX_NUM 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef int Status;
typedef int VRType;
typedef char VertexType;
typedef char InfoType;

typedef enum
{
    DG, DN, UDG, UDN
} GraphKind;

int visited[MAX_VERTEX_NUM] = { FALSE };

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, *pMGraph;

int LocateVex(pMGraph pG, VertexType v)
{
    for (int i = 0;i < pG->vexnum;i++)
        if (pG->vexs[i] == v)
        {
            return i;
        }
    printf("ERROR on find %c\n", v);
    return -1;
}

Status CreateUDN(pMGraph pG)
{
    VertexType v1, v2;

    scanf("%d%d", &pG->vexnum, &pG->arcnum);
    getchar();
    for (int i = 0;i < pG->vexnum;i++)
    {
        scanf("%c", &pG->vexs[i]);
    }
    getchar();
    for (int i = 0;i < pG->arcnum;i++)
    {
        scanf("%c%c", &v1, &v2);
        getchar();
        pG->arcs[LocateVex(pG, v1)][LocateVex(pG, v2)].adj = 1;
    }
    return OK;
}

int FirstAdjVex(pMGraph pG, VertexType v)
{
    int i = LocateVex(pG, v);
    for (int j = 0;j < pG->vexnum;j++)
    {
        if (pG->arcs[i][j].adj == 1) return j;
    }
    return ERROR;
}

int NextAdjVex(pMGraph pG, VertexType v, VRType w)
{
    int i = LocateVex(pG, v);

    for (int j = w + 1;j < pG->vexnum;j++)
    {
        if (pG->arcs[i][j].adj == 1) return j;
    }
    return ERROR;
}

void DFS(pMGraph pG, int v)
{
    visited[v] = TRUE;
    printf("%c", pG->vexs[v]);
//  for (int w = FirstAdjVex(pG, v);w;w = NextAdjVex(pG, v, w))  // 调用这两个函数时应该传节点具体值而不是下标
    for (int w = FirstAdjVex(pG, pG->vexs[v]);w;w = NextAdjVex(pG, pG->vexs[v], w))
    {
        if (!visited[w]) DFS(pG, w);
    }
}

void DFSTraverse(pMGraph pG)
{
    for (int v = 0;v < pG->vexnum;v++)
    {
        if (!visited[v]) DFS(pG, v);
    }
    printf("\n");
}

int main()
{
    MGraph G = { { 0 } };
    CreateUDN(&G);
    DFSTraverse(&G);
    return 0;
}


问题出现在DFS函数


[fly]存在即是合理[/fly]
2015-11-26 10:20
不同认为
Rank: 1
等 级:新手上路
帖 子:93
专家分:3
注 册:2015-11-25
收藏
得分:0 
明白了
2015-11-26 12:46
不同认为
Rank: 1
等 级:新手上路
帖 子:93
专家分:3
注 册:2015-11-25
收藏
得分:0 
回复 2楼 azzbcc
这个有向图广度优先遍历哪里错了  帮忙看看
#include <stdio.h>
#include <malloc.h>
#define MAX_VERTEX_NUM 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int VRType;   
typedef char VertexType;
typedef char InfoType;
typedef enum {DG,DN,UDG,UDN}GraphKind;
typedef char QElemType;
int visited[MAX_VERTEX_NUM];
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;
typedef struct QNode
{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;  
Status InitQueue(LinkQueue &Q)  
{
   Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
   if(!Q.front) return ERROR;
   Q.front->next=NULL;   
   return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{
    QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    if(!p) return ERROR;        
    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;
  Q.front->next=p->next;
  if(Q.rear==p)
      Q.rear=Q.front;
  free(p);
  return OK;
}
int LocateVex(MGraph G,VertexType v)
{
     int j=-1,k;
     for(k=0;k<G.vexnum;k++)
         if(G.vexs[k]==v)
        {
            j=k;
            break;
        }
     return j;
}
Status CreateUDN(MGraph &G)
{
    int i,j,k;
    VertexType v1,v2;
    scanf("%d%d",&G.vexnum,&G.arcnum);
    getchar();
    for(i=0;i<G.vexnum;i++)
        scanf("%c",&G.vexs[i]);
    for(i=0;i<G.vexnum;i++)
    for(j=0;j<G.vexnum;j++)
        G.arcs[i][j].adj=0;
      for (k=0;k<G.arcnum;k++)
      {   
          getchar();
        scanf("%c%c",&v1,&v2);  
        i=LocateVex(G,v1);j=LocateVex(G,v2);  
        G.arcs[i][j].adj=1;
      }
      return OK;
 }
Status first(MGraph G,int v)
{
    int j;
    for(j=0;j<G.vexnum;j++)
        if(G.arcs[v][j].adj==1)
            return j;
        return -1;
}
Status next(MGraph G,int v,int w)
{
    int j;
    for(j=w+1;j<G.vexnum;j++)
        if(G.arcs[v][j].adj==1)
            return j;
        return -1;
}
void guangdu(MGraph G)
{
    int j,w,v;
    LinkQueue Q;
    for(v=0;v<G.vexnum;v++)
        visited[v]=0;
    InitQueue(Q);
    for(v=0;v<G.vexnum;v++)
        if(!visited[v])
        {
          visited[v]=TRUE;
          printf("%c",G.vexs[v]);
          EnQueue(Q,G.vexs[v]);
        while(Q.front!=Q.rear)
        {
           DeQueue(Q,G.vexs[v]);
           for(w=first(G,v);w!=-1;w=next(G,v,w))
               if(!visited[w])
              { visited[w]=true;
                printf("%c",G.vexs[w]);
                 EnQueue(Q,G.vexs[w]);
               }
        }
        }
}
int main()
{
  MGraph G;
  CreateUDN(G);
  guangdu(G);
  printf("n");
  return 0;
}




        

           




2015-11-26 16:01
快速回复:这代码哪错了 采用邻接矩阵法构造有向图,深度优先搜索输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018476 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved