ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 图的生成,遍历

图的生成,遍历

原创 Linux操作系统 作者:dongyu2013 时间:2014-04-16 10:45:13 0 删除 编辑

点击(此处)折叠或打开

  1. typedef char VertexType;
  2. typedef int EdgeType;
  3. #define MAXVEX 100
  4. #define INFINITY 65535

  5. typedef struct
  6. {
  7.     VertexType vexs[MAXVEX];
  8.     EdgeType arc[MAXVEX][MAXVEX];
  9.     int numVertexes, numEdges;
  10. }MGraph;

  11. void CreateMGraph( MGraph *G)
  12. {
  13.    int i,j,k,w;
  14.    printf(\"输入顶点数和变数:\\n\");
  15.    scanf(\"%d,%d\", &G->numVertexes, &G->numEdges);
  16.    for(i=0; i<G->numVertexes; i++)
  17.    {
  18.      scanf(\"%d\",&G->vexs[i]);
  19.    }
  20.    for(i=0; i<G->numVertexes; i++)
  21.    {
  22.      for(j=0; j<G->numVertexes; j++)
  23.      {
  24.          G->arc[i][j]=INFINITY;
  25.      }
  26.    }
  27.    for(k=0; k<G->numEdges; k++)
  28.    {
  29.      printf(\"输入边(vi,vj)上的下标i,下标j和权w:\\n\");
  30.      scanf(\"%d,%d,%d\",&i,&j,&w);
  31.      G->arc[i][j]=w;
  32.      G->arc[j][i]=G->arc[i][j];
  33.    }
  34. }

  35. typedef struct EdgeNode
  36. {
  37.     int adjvex;
  38.     EdgeType weight;
  39.     struct EdgeNode *next;
  40. }EdgeNode;

  41. typedef struct VertexNode
  42. {
  43.     VertexType data;
  44.     EdgeNode *firstedge;
  45. }VertexNode, AdjList[MAXVEX];

  46. typedef struct {
  47.     
  48.     AdjList adjList;
  49.     int numVertexes, numEdges;

  50. }GraphAdjList;

  51. void CreateALGraph(GraphAdjList *G)
  52. {
  53.     int i,j,k;
  54.     EdgeNode *e;
  55.     printf(\"输入顶点数和边数:\\n\");
  56.     scanf(\"%d,%d\",&G->numVertexes,&G->numEdges);
  57.     for(i=0; i<G->numVertexes; i++)
  58.     {
  59.         scanf(\"%d\",&G->adjList[i].data);
  60.         G->adjList[i].firstedge=NULL;
  61.     }
  62.     for(k=0; k<G->numEdges; k++)
  63.     {
  64.         printf(\"输入边(vi,vj)上的顶点序号:\\n\");
  65.         scanf(\"%d,%d\",&i,&j);
  66.         e=(EdgeNode *)malloc(sizeof(EdgeNode));
  67.         e->adjvex=j;
  68.         e->next=G->adjList[i].firstedge;
  69.         G->adjList[i].firstedge=e;
  70.         e=(EdgeNode *)malloc(sizeof(EdgeNode));
  71.         e->adjvex=i;
  72.         e->next=G->adjList[j].firstedge;
  73.         G->adjList[j].firstedge=e;
  74.     }
  75. }

  76. typedef int Boolean;
  77. Boolean visited[MAXVEX];

  78. void DFS(MGraph G, int i)
  79. {
  80.      int j;
  81.      visited[i]=true;
  82.      printf(\"%c\",G.vexs[i]);
  83.      for(j=0; j<G.numVertexes; j++)
  84.      {
  85.          if(G.arc[i][j]==1&&!visited[j])
  86.          {
  87.              DFS(G, j);
  88.          }
  89.      }

  90. }

  91. void DFSTraverse(MGraph G)
  92. {
  93.    int i;
  94.    for(i=0; i<G.numVertexes; i++)
  95.      visited[i]=false;
  96.    for(i=0; i<G.numVertexes; i++)
  97.    {
  98.      if(!visited[i])
  99.      {
  100.          DFS(G,i);
  101.      }
  102.    }
  103. }

  104. void DFS(GraphAdjList *GL, int i)
  105. {
  106.    EdgeNode *p;
  107.    visited[i]=true;
  108.    printf(\"%c\", GL->adjList[i].data);
  109.    p=GL->adjList[i].firstedge;
  110.    while(p)
  111.    {
  112.        if(!visited[p->adjvex])
  113.        {
  114.          DFS(GL, p->adjvex);
  115.        }
  116.      p=p->next;
  117.    }
  118. }

  119. void DFSTraverse(GraphAdjList *GL)
  120. {
  121.     int i;
  122.     for(i=0; i<GL->numVertexes; i++)
  123.     {
  124.         visited[i]=false;
  125.     }
  126.     for(i=0; i<GL->numVertexes; i++)
  127.     {
  128.         if(!visited[i])
  129.         {
  130.             DFS(GL, i);
  131.         }
  132.     }
  133. }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1143032/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-12-25

  • 博文量
    263
  • 访问量
    238073