数据结构7-图

1.图的基本概念 

数据结构--图 - 赤兔胭脂小吕布 - 博客园图的简介 图(Graph)结构是一种非线性的数据结构,图在实际生活中有很多例子,比如交通运输网,地铁网络,社交网络,计算机中的状态执行(自动机)等等都可以抽象成图结构。图结构比树结构复杂的非线性结构。https://www.cnblogs.com/ctyzxlb/articles/15713183.html

子图:

生成子图:

连通图:

连通子图:

连通分量:

有向图:强连通图,强连通分量,非强连通图

网:

2.邻接矩阵表示方法

图的存储结构
(1)顺序存储:邻接矩阵
(2)链式存储:邻接表,十字链表,邻接多重表

无向图的邻接矩阵:
(1)对陈矩阵
(2)第i行或第i列的非0元素个数 == 第i个顶点的度
有向图邻接矩阵:
   (1)不一定对称
   (2)第i行非0元素个数,等于第i个顶点的出度
   (3) 第i列非0元素个数,等于第i个顶点的入度

3.邻接矩阵实现

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

// 邻接矩阵的图 
struct AMG_Graph
{
     int vex_num, edge_num;    // 顶点个数,边个数
     char Vex[MAX];            // 顶点信息(一维数组)
     int Edge[MAX][MAX];       // 顶点关系信息 0/1(二维数组)
};

struct AMG_Graph *Creat_AMG_Graph(void);
void Show_AMG_Graph(struct AMG_Graph *graph);
int search_vex(struct AMG_Graph *graph, char c);

// 主函数
int main(void)
{
    struct AMG_Graph *ud_graph;     // 无向图指针
    ud_graph = Creat_AMG_Graph();   // 创建邻接矩阵    
    Show_AMG_Graph(ud_graph);
    return 0;
}


// 创建邻接
struct AMG_Graph *Creat_AMG_Graph(void)
{
     int i,j;
     char u,v;
     struct AMG_Graph *graph;
     graph = (struct AMG_Graph *)malloc(sizeof(struct AMG_Graph));
     printf("please enter the number of vex: ");
     scanf("%d", &graph->vex_num);
     printf("please enter the number of edge: ");
     scanf("%d", &graph->edge_num);
      
     while(getchar() != '\n');  // 消灭回车

     printf("please enter the vertex:\n");
     for(i = 0; i < graph->vex_num; i++)
     {
           graph->Vex[i] = getchar();
           while(getchar() != '\n');
     }
     // 初始化二维数组
     for(i = 0; i < graph->vex_num; i++)
     {
          for(j = 0; j < graph->vex_num; j++)
               graph->Edge[i][j] = 0;
     }
    
     // 输入顶点
     while(graph->edge_num--)
     {
          printf("please enter the vex that connect each other by edge:\n");
          u = getchar();
          while(getchar() != '\n');
          v = getchar();
          while(getchar() != '\n'); 
          
          i = search_vex(graph, u);
          j = search_vex(graph, v);
          
          if(i != -1 && j != -1)
                  graph->Edge[i][j] = graph->Edge[j][i] = 1;  // 无向图
                  //graph->Edge[i][j] = 1;  有向图只需改成这个
          else
          {
                  printf("wrong vex, enter again.\n");
                  graph->edge_num++;
          }
     }
     return graph;
}

// 显示
void Show_AMG_Graph(struct AMG_Graph *graph)
{
     int i,j;
     printf("show the vertex:\n");
     for(i = 0; i < graph->vex_num; i++)
          printf("%c ", graph->Vex[i]);
     printf("\n");
 
     printf("show the adjacency matrices:\n");
     for(i = 0; i < graph->vex_num; i++)
     {
          for(j = 0; j < graph->vex_num; j++)
               printf("%d\t", graph->Edge[i][j]);
          printf("\n");
     }
      
}

// 查找位置
int search_vex(struct AMG_Graph *graph, char c)
{
      int i; 
      for(i = 0; i < graph->vex_num; i++)
      {
          if(c == graph->Vex[i])
             return i;  
      } 
      return -1;
}

4.邻接表的表示方法

5.邻接表的实现

有向图

 

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

//邻接点表
struct AdjNode
{
     int index;
     struct AdjNode *next;
};

//节点表
struct VexNode
{
     char node;
     struct AdjNode *first;
};

//邻接表
struct ALG_Graph
{
     int vex_num, edge_num;    // 顶点个数,边个数
     struct VexNode Vex[MAX];  // 节点信息
};

struct ALG_Graph *Create_ALG_Graph(void);
int search_vex(struct ALG_Graph *graph, char c);
void  create_adj_node_list(struct ALG_Graph *graph, int i, int j);
void Show_ALG_Graph(struct ALG_Graph *graph);


// 主函数
int main(void)
{
        struct ALG_Graph *d_graph;
        d_graph = Create_ALG_Graph();
        Show_ALG_Graph(d_graph);
        return 0;
}

// 节点表
struct ALG_Graph *Create_ALG_Graph(void)
{
        int i, j;
        char u, v;
        struct ALG_Graph *graph;       
        graph = (struct ALG_Graph *)malloc(sizeof(struct ALG_Graph));

        printf("please enter the number of vex: ");
        scanf("%d", &graph->vex_num);
        printf("please enter the number of edge: ");
        scanf("%d", &graph->edge_num);
        while(getchar() != '\n');  // 消灭回车

        //节点信息
        printf("please enter the vertex:\n");
        for(i = 0; i < graph->vex_num; i++)
        {
              graph->Vex[i].node = getchar();
              while(getchar() != '\n');
        }
        //指针部分设为空
        for(i = 0; i < graph->vex_num; i++)
        {
              graph->Vex[i].first = NULL;
        }
        
        // 输入顶点信息
        while(graph->edge_num--)
        {
             printf("please enter the vex that connect each other by edge:\n");
             u = getchar();
             while(getchar() != '\n');
             v = getchar();
             while(getchar() != '\n'); 
             
             i = search_vex(graph, u);
             j = search_vex(graph, v);
             
             if(i != -1 && j != -1)
                    create_adj_node_list(graph, i, j);    // 创建邻接点表
             else
             {
                    printf("wrong vex, enter again.\n");
                    graph->edge_num++;
             }        
       }
       return graph;
}

// 创建邻接点表(重点)
void create_adj_node_list(struct ALG_Graph *graph, int i, int j)
{
        struct AdjNode *s = (struct AdjNode *)malloc(sizeof(struct AdjNode));
        s->index = j;    
        s->next = graph->Vex[i].first;
        graph->Vex[i].first = s;
}

// 查找位置
int search_vex(struct ALG_Graph *graph, char c)
{
        int i; 
        for(i = 0; i < graph->vex_num; i++)
        {
             if(c == graph->Vex[i].node)
                    return i;  
        } 
        return -1;
}

// 显示
void Show_ALG_Graph(struct ALG_Graph *graph)
{
        int i;
        struct AdjNode *t;

        printf("show the ALG Graph:\n"); 
        for(i = 0; i < graph->vex_num; i++) 
        {
             printf("%c: ", graph->Vex[i].node);
             t = graph->Vex[i].first;
             while(t != NULL)
             {
                  printf("%d ", t->index);
                  t = t->next;
             }
             printf("\n");
        } 
        
}

无向图 

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

//邻接点表
struct AdjNode
{
     int index;
     struct AdjNode *next;
};

//节点表
struct VexNode
{
     char node;
     struct AdjNode *first;
};

//邻接表
struct ALG_Graph
{
     int vex_num, edge_num;    // 顶点个数,边个数
     struct VexNode Vex[MAX];  // 节点信息
};

struct ALG_Graph *Create_ALG_Graph(void);
int search_vex(struct ALG_Graph *graph, char c);
void  create_adj_node_list(struct ALG_Graph *graph, int i, int j);
void Show_ALG_Graph(struct ALG_Graph *graph);


// 主函数
int main(void)
{
        struct ALG_Graph *ud_graph;
        ud_graph = Create_ALG_Graph();
        Show_ALG_Graph(ud_graph);
        return 0;
}

// 节点表
struct ALG_Graph *Create_ALG_Graph(void)
{
        int i, j;
        char u, v;
        struct ALG_Graph *graph;       
        graph = (struct ALG_Graph *)malloc(sizeof(struct ALG_Graph));

        printf("please enter the number of vex: ");
        scanf("%d", &graph->vex_num);
        printf("please enter the number of edge: ");
        scanf("%d", &graph->edge_num);
        while(getchar() != '\n');  // 消灭回车

        //节点信息
        printf("please enter the vertex:\n");
        for(i = 0; i < graph->vex_num; i++)
        {
              graph->Vex[i].node = getchar();
              while(getchar() != '\n');
        }
        //指针部分设为空
        for(i = 0; i < graph->vex_num; i++)
        {
              graph->Vex[i].first = NULL;
        }
        
        // 输入顶点信息
        while(graph->edge_num--)
        {
             printf("please enter the vex that connect each other by edge:\n");
             u = getchar();
             while(getchar() != '\n');
             v = getchar();
             while(getchar() != '\n'); 
             
             i = search_vex(graph, u);
             j = search_vex(graph, v);
             
             if(i != -1 && j != -1)
             {
                    create_adj_node_list(graph, i, j);    // 创建邻接点表
                    create_adj_node_list(graph, j, i);    // 无向图
             }
             else
             {
                    printf("wrong vex, enter again.\n");
                    graph->edge_num++;
             }        
       }
       return graph;
}

// 创建邻接点表(重点)
void create_adj_node_list(struct ALG_Graph *graph, int i, int j)
{
        struct AdjNode *s = (struct AdjNode *)malloc(sizeof(struct AdjNode));
        s->index = j;    
        s->next = graph->Vex[i].first;
        graph->Vex[i].first = s;
}

// 查找位置
int search_vex(struct ALG_Graph *graph, char c)
{
        int i; 
        for(i = 0; i < graph->vex_num; i++)
        {
             if(c == graph->Vex[i].node)
                    return i;  
        } 
        return -1;
}

// 显示
void Show_ALG_Graph(struct ALG_Graph *graph)
{
        int i;
        struct AdjNode *t;

        printf("show the ALG Graph:\n"); 
        for(i = 0; i < graph->vex_num; i++) 
        {
             printf("%c: ", graph->Vex[i].node);
             t = graph->Vex[i].first;
             while(t != NULL)
             {
                  printf("%d ", t->index);
                  t = t->next;
             }
             printf("\n");
        } 
        
}

6.十字链表

7.邻接多重表

猜你喜欢

转载自blog.csdn.net/qq_44177768/article/details/127663398
今日推荐