算法导论 第22章 图的基本算法 22.1 图的表示

目录(?)[+]


一、综述

图的表示方法通常有两种,即邻接表表示法和邻接矩阵表示法。这两种方法都可以表示有向图和无向图

1.邻接表表示法

(1)用邻接表表示无向图

(2)用邻接表表示有向图

(3)邻接表的优点及适用场合

使用邻接表表示稀疏图比较紧凑

2.邻接矩阵表示法

(1)用邻接矩阵表示无向图

(2)用邻接矩阵表示有向图

(3)邻接矩阵的优点与适用场合

用邻接矩阵表示稠密图可以很快地判别两个给定顶点是否存在邻接边

 

二、代码

1.邻接表表示法 "Link_Graph.h"

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #define N 100  
  5.   
  6. struct Vertex;  
  7. struct Edge  
  8. {  
  9.     int start;  
  10.     int end;  
  11.     int value;  
  12.     Edge *next;  
  13.     Edge(int s, int e, int v):start(s),end(e),value(v),next(NULL){}  
  14. };  
  15. struct Vertex  
  16. {  
  17.     Edge *head;  
  18.     Vertex():head(NULL){};  
  19. };  
  20. class Link_Graph  
  21. {  
  22. public:  
  23.     int n;  
  24.     Vertex *V;  
  25.     Link_Graph(int num):n(num)  
  26.     {  
  27.         V = new Vertex[n+1];  
  28.     }  
  29.     ~Link_Graph(){delete []V;}  
  30.     void AddSingleEdge(int start, int end, int value = 1)  
  31.     {  
  32.         Edge *NewEdge = new Edge(start, end, value);  
  33.         if(V[start].head == NULL || V[start].head->end > end)  
  34.         {  
  35.             NewEdge->next = V[start].head;  
  36.             V[start].head = NewEdge;  
  37.         }  
  38.         else  
  39.         {  
  40.             Edge *e = V[start].head, *pre = e;  
  41.             while(e != NULL && e->end < end)  
  42.             {  
  43.                 pre = e;  
  44.                 e = e->next;  
  45.             }  
  46.             if(e && e->end == end)  
  47.             {  
  48.                 delete NewEdge;  
  49.                 return;  
  50.             }  
  51.             NewEdge->next = e;  
  52.             pre->next = NewEdge;  
  53.         }  
  54.     }  
  55.     void AddDoubleEdge(int a, int b, int value = 1)  
  56.     {  
  57.         AddSingleEdge(a, b, value);  
  58.         AddSingleEdge(b, a, value);  
  59.     }  
  60.     void DeleteSingleEdge(int start, int end)  
  61.     {  
  62.         Edge *e = V[start].head, *pre = e;  
  63.         while(e && e->end < end)  
  64.         {  
  65.             pre = e;  
  66.             e = e->next;  
  67.         }  
  68.         if(e == NULL || e->end > end) return;  
  69.         if(e == V[start].head)  
  70.             V[start].head = e->next;  
  71.         else  
  72.             pre->next = e->next;  
  73.         delete e;  
  74.     }  
  75.     void DeleteDoubleEdge(int a, int b)  
  76.     {  
  77.         DeleteSingleEdge(a, b);  
  78.         DeleteSingleEdge(b, a);  
  79.     }  
  80.     //22.1-1  
  81.     void OutDegree();  
  82.     void InDegree();  
  83.     //22.1-2  
  84.     void Print();  
  85.     //22.1-3  
  86.     Link_Graph* Reverse();  
  87.     //22.1-5  
  88.     Link_Graph* Square();  
  89. };  
  90.   
  91. void Link_Graph::OutDegree()  
  92. {  
  93.     int i, cnt = 0;  
  94.     Edge *e;  
  95.     for(i = 1; i <= n; i++)  
  96.     {  
  97.         cnt = 0;  
  98.         e = V[i].head;  
  99.         while(e)  
  100.         {  
  101.             cnt++;  
  102.             e = e->next;  
  103.         }  
  104.         cout<<"顶点"<<i<<"的出度为"<<cnt<<endl;  
  105.     }  
  106. }  
  107.   
  108. void Link_Graph::InDegree()  
  109. {  
  110.     int ret[N+1] = {0}, i;  
  111.     Edge *e;  
  112.     for(i = 1; i <= n; i++)  
  113.     {  
  114.         e = V[i].head;  
  115.         while(e)  
  116.         {  
  117.             ret[e->end]++;  
  118.             e = e->next;  
  119.         }  
  120.     }  
  121.     for(i = 1; i <= n; i++)  
  122.         cout<<"顶点"<<i<<"的入度为"<<ret[i]<<endl;  
  123. }  
  124.   
  125. void Link_Graph::Print()  
  126. {  
  127.     int i;  
  128.     Edge *e;  
  129.     for(i = 1; i <= n; i++)  
  130.     {  
  131.         cout<<i;  
  132.         e = V[i].head;  
  133.         while(e)  
  134.         {  
  135.             cout<<"->"<<e->end;  
  136.             e = e->next;  
  137.         }  
  138.         cout<<endl;  
  139.     }  
  140. }  
  141.   
  142. Link_Graph* Link_Graph::Reverse()  
  143. {  
  144.     Link_Graph *ret = new Link_Graph(n);    
  145.     int i;    
  146.     //遍历图G中的每一条边,以终点为起点,以起点为终点,加入到新图RET中    
  147.     for(i = 1; i <= n; i++)    
  148.     {    
  149.         Edge *e = V[i].head;    
  150.         while(e)    
  151.         {    
  152.             ret->AddSingleEdge(e->end, e->start);    
  153.             e = e->next;    
  154.         }    
  155.     }    
  156.     return ret;   
  157. }  
  158.   
  159. Link_Graph* Link_Graph::Square()  
  160. {  
  161.     int i;  
  162.     Link_Graph *ret = new Link_Graph(n);    
  163.     //遍历图中每个顶点    
  164.     for(i = 1; i <= n; i++)    
  165.     {    
  166.         //遍历以该顶点为起点的边    
  167.         Edge *e = V[i].head, *e2;    
  168.         while(e)    
  169.         {    
  170.             //把原来有的边加入到新图中    
  171.             ret->AddSingleEdge(e->start, e->end);    
  172.             //把以E的终点为起点的边加入新图中    
  173.             e2 = V[e->end].head;    
  174.             while(e2)    
  175.             {    
  176.                 ret->AddSingleEdge(e->start, e2->end);    
  177.                 e2 = e2->next;    
  178.             }    
  179.             e = e->next;    
  180.         }       
  181.     }    
  182.     return ret;  
  183. }  


 

2.邻接矩阵表示法

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #define N 100  
  5.   
  6. class Mat_Graph  
  7. {  
  8. public:  
  9.     int n;  
  10.     int Map[N+1][N+1];  
  11.   
  12.     Mat_Graph(int num):n(num)  
  13.     {  
  14.         memset(Map, 0, sizeof(Map));  
  15.     }  
  16.     void AddDoubleEdge(int a, int b, int value = 1)  
  17.     {  
  18.         AddSingleEdge(a, b, value);  
  19.         AddSingleEdge(b, a, value);  
  20.     }  
  21.     void AddSingleEdge(int start, int end, int value = 1)  
  22.     {  
  23.         Map[start][end] = value;  
  24.     }  
  25.     void DeleteDoubleEdge(int a, int b)  
  26.     {  
  27.         DeleteSingleEdge(a, b);  
  28.         DeleteSingleEdge(b, a);  
  29.     }  
  30.     void DeleteSingleEdge(int start, int end)  
  31.     {  
  32.         Map[start][end] = 0;  
  33.     }  
  34.     //22.1-2  
  35.     void Print();  
  36.     //22.1-3  
  37.     Mat_Graph* Reverse();  
  38.     //22.1-5  
  39.     Mat_Graph* Square();  
  40.     //22.1-6  
  41.     bool Universal_Sink();  
  42. };  
  43.   
  44. void Mat_Graph::Print()  
  45. {  
  46.     int i, j;  
  47.     for(i = 1; i <= n; i++)  
  48.     {  
  49.         for(j = 1; j <= n; j++)  
  50.             cout<<Map[i][j]<<' ';  
  51.         cout<<endl;  
  52.     }  
  53. }  
  54.   
  55. Mat_Graph* Mat_Graph::Reverse()  
  56. {  
  57.     int i, j;  
  58.     Mat_Graph *ret = new Mat_Graph(n);  
  59.     for(i = 1; i <= n; i++)  
  60.     {  
  61.         for(j = 1; j <= n; j++)  
  62.             if(Map[i][j])  
  63.                 ret->AddSingleEdge(j, i);  
  64.     }  
  65.     return ret;  
  66. }  
  67.   
  68. Mat_Graph* Mat_Graph::Square()  
  69. {  
  70.     int i, j, k;  
  71.     Mat_Graph *ret = new Mat_Graph(n);    
  72.     //遍历图中每个顶点    
  73.     for(i = 1; i <= n; i++)    
  74.     {    
  75.         for(j = 1; j <= n; j++)    
  76.         {    
  77.             if(Map[i][j])    
  78.             {    
  79.                 //把原来有的边加入到新图中    
  80.                 ret->AddSingleEdge(i, j);  
  81.                 //把以E的终点为起点的边加入新图中    
  82.                 for(k = 1; k <= n; k++)    
  83.                     if(Map[j][k])    
  84.                         ret->AddSingleEdge(i, k);    
  85.             }    
  86.         }    
  87.     }    
  88.     return ret;  
  89. }  
  90.   
  91. bool Mat_Graph::Universal_Sink()  
  92. {  
  93.     //i指向有可能是汇的编号最小的点    
  94.     int i = 1, j = 1;    
  95.     while(j <= n)    
  96.     {    
  97.         //map[i][j] = 0,那么j没有i的入,一定不是汇,i可能是汇    
  98.         if(Map[i][j] == 0)    
  99.             j++;    
  100.         //若map[i][j] = 1,则(1)i有出,i不是汇(2)map[i][i+1..j-1]=0,i+1..j-1都不是汇(3)j及j以后的点可能是汇    
  101.         //若i=j,j也不是汇,j+1可能是汇        
  102.         else if(i == j)    
  103.         {    
  104.             i++;    
  105.             j++;    
  106.         }    
  107.         //若i!=j,j以前的点都不是汇,j可能是汇    
  108.         else i = j;    
  109.     }    
  110.     //没有汇    
  111.     if(i > n)    
  112.         return false;    
  113.     //找到有可能是汇的点,但是还是需要验证一下    
  114.     else    
  115.     {    
  116.         //汇的纵向都是1,除了map[i][i]    
  117.         for(j = 1; j <= i-1; j++)    
  118.         {    
  119.             if(Map[i][j] == 1)    
  120.                 return false;    
  121.         }    
  122.         //汇的横向都是0    
  123.         for(j = 1; j <= n; j++)    
  124.             if( i != j && Map[j][i] == 0)    
  125.                 return false;    
  126.         return true;    
  127.     }    
  128. }  


3.main.cpp 

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #include "Link_Graph.h"  
  5. #include "Mat_Graph.h"  
  6.   
  7. void Test1()  
  8. {  
  9.     cout<<"Test1"<<endl;  
  10.     int n, a, b;  
  11.     cin>>n;  
  12.     Link_Graph *LG = new Link_Graph(n);  
  13.     while(cin>>a>>b && a && b)//输入00时结束  
  14.         LG->AddSingleEdge(a, b);  
  15.     LG->OutDegree();  
  16.     LG->InDegree();  
  17.     delete LG;  
  18. }  
  19. void Test2()  
  20. {  
  21.     cout<<"Test2"<<endl;  
  22.     Link_Graph *LG = new Link_Graph(6);  
  23.     Mat_Graph *MG = new Mat_Graph(6);  
  24.     int edge[6][2] = {{1,2},{1,3},{2,4},{2,5},{3,6},{3,6}};  
  25.     int i = 0;  
  26.     for(i = 0; i < 6; i++)  
  27.     {  
  28.         LG->AddSingleEdge(edge[i][0], edge[i][1]);  
  29.         MG->AddSingleEdge(edge[i][0], edge[i][1]);  
  30.     }  
  31.     LG->Print();  
  32.     MG->Print();  
  33.     delete LG;  
  34.     delete MG;  
  35. }  
  36. void Test3()  
  37. {  
  38.     cout<<"Test3"<<endl;  
  39.     int n, a, b;  
  40.     cin>>n;  
  41.     Link_Graph *LG = new Link_Graph(n);  
  42.     Mat_Graph *MG = new Mat_Graph(n);  
  43.     while(cin>>a>>b && a && b)//输入00时结束  
  44.     {  
  45.         LG->AddSingleEdge(a, b);  
  46.         MG->AddSingleEdge(a, b);  
  47.     }  
  48.     Link_Graph *NewLG = LG->Reverse();  
  49.     NewLG->Print();  
  50.     Mat_Graph *NewMG = MG->Reverse();  
  51.     NewMG->Print();  
  52.     delete LG;  
  53.     delete MG;  
  54.     delete NewLG;  
  55.     delete NewMG;  
  56. }  
  57. void Test5()  
  58. {  
  59.     cout<<"Test5"<<endl;  
  60.     int n, a, b;  
  61.     cin>>n;  
  62.     Link_Graph *LG = new Link_Graph(n);  
  63.     Mat_Graph *MG = new Mat_Graph(n);  
  64.     while(cin>>a>>b && a && b)//输入00时结束  
  65.     {  
  66.         LG->AddSingleEdge(a, b);  
  67.         MG->AddSingleEdge(a, b);  
  68.     }  
  69.     Link_Graph *NewLG = LG->Square();  
  70.     NewLG->Print();  
  71.     Mat_Graph *NewMG = MG->Square();  
  72.     NewMG->Print();  
  73.     delete LG;  
  74.     delete MG;  
  75.     delete NewLG;  
  76.     delete NewMG;  
  77. }  
  78.   
  79. void Test6()  
  80. {  
  81.     cout<<"Test6"<<endl;  
  82.     int n, a, b;  
  83.     cin>>n;  
  84.     Mat_Graph *MG = new Mat_Graph(n);  
  85.     while(cin>>a>>b && a && b)//输入00时结束  
  86.         MG->AddSingleEdge(a, b);  
  87.     if(MG->Universal_Sink())  
  88.         cout<<"包含通用的汇"<<endl;  
  89.     else  
  90.         cout<<"不包含通用的汇"<<endl;  
  91.     delete MG;  
  92. }  
  93. /* 
  94. 6 
  95. 1 2 
  96. 1 4 
  97. 4 2 
  98. 2 5 
  99. 5 4 
  100. 3 5 
  101. 3 6 
  102. 6 6 
  103. 0 0 
  104. */  
  105. int main()  
  106. {  
  107.     Test1();  
  108.     Test2();  
  109.     Test3();  
  110.     Test5();  
  111.     Test6();  
  112.     return 0;  
  113. }  


 

三、练习

22.1-1

计算出度的时间:E

计算入度的时间:E

  1. void Test1()  
  2. {  
  3.     cout<<"Test1"<<endl;  
  4.     int n, a, b;  
  5.     cin>>n;  
  6.     Link_Graph *LG = new Link_Graph(n);  
  7.     while(cin>>a>>b && a && b)//输入00时结束  
  8.         LG->AddSingleEdge(a, b);  
  9.     LG->OutDegree();  
  10.     LG->InDegree();  
  11.     delete LG;  
  12. }  


22.1-2

  1. void Test2()  
  2. {  
  3.     cout<<"Test2"<<endl;  
  4.     Link_Graph *LG = new Link_Graph(6);  
  5.     Mat_Graph *MG = new Mat_Graph(6);  
  6.     int edge[6][2] = {{1,2},{1,3},{2,4},{2,5},{3,6},{3,6}};  
  7.     int i = 0;  
  8.     for(i = 0; i < 6; i++)  
  9.     {  
  10.         LG->AddSingleEdge(edge[i][0], edge[i][1]);  
  11.         MG->AddSingleEdge(edge[i][0], edge[i][1]);  
  12.     }  
  13.     LG->Print();  
  14.     MG->Print();  
  15.     delete LG;  
  16.     delete MG;  
  17. }  


 

 邻接表表示:

1->2->3

2->4->5

3->6->7

4

5

6

7

矩阵表示:

0110000

0001100

0000011

0000000

0000000

0000000

0000000

 

22.1-3

邻接表表示的有向图的转置

  1. Link_Graph* Link_Graph::Reverse()  
  2. {  
  3.     Link_Graph *ret = new Link_Graph(n);    
  4.     int i;    
  5.     //遍历图G中的每一条边,以终点为起点,以起点为终点,加入到新图RET中    
  6.     for(i = 1; i <= n; i++)    
  7.     {    
  8.         Edge *e = V[i].head;    
  9.         while(e)    
  10.         {    
  11.             ret->AddSingleEdge(e->end, e->start);    
  12.             e = e->next;    
  13.         }    
  14.     }    
  15.     return ret;   
  16. }  


邻接矩阵表示的有向图的转置 

  1. Mat_Graph* Mat_Graph::Reverse()  
  2. {  
  3.     int i, j;  
  4.     Mat_Graph *ret = new Mat_Graph(n);  
  5.     for(i = 1; i <= n; i++)  
  6.     {  
  7.         for(j = 1; j <= n; j++)  
  8.             if(Map[i][j])  
  9.                 ret->AddSingleEdge(j, i);  
  10.     }  
  11.     return ret;  
  12. }  


 

22.1-4

遍历多重图中的每一条边,通过AddDoubleAdge()函数把它加入到另一个图中

 

22.1-5

邻接表表示的有向图的平方图

  1. Link_Graph* Link_Graph::Square()  
  2. {  
  3.     int i;  
  4.     Link_Graph *ret = new Link_Graph(n);    
  5.     //遍历图中每个顶点    
  6.     for(i = 1; i <= n; i++)    
  7.     {    
  8.         //遍历以该顶点为起点的边    
  9.         Edge *e = V[i].head, *e2;    
  10.         while(e)    
  11.         {    
  12.             //把原来有的边加入到新图中    
  13.             ret->AddSingleEdge(e->start, e->end);    
  14.             //把以E的终点为起点的边加入新图中    
  15.             e2 = V[e->end].head;    
  16.             while(e2)    
  17.             {    
  18.                 ret->AddSingleEdge(e->start, e2->end);    
  19.                 e2 = e2->next;    
  20.             }    
  21.             e = e->next;    
  22.         }       
  23.     }    
  24.     return ret;  
  25. }  


邻接矩阵表示的有向图的平方图

  1. Mat_Graph* Mat_Graph::Square()  
  2. {  
  3.     int i, j, k;  
  4.     Mat_Graph *ret = new Mat_Graph(n);    
  5.     //遍历图中每个顶点    
  6.     for(i = 1; i <= n; i++)    
  7.     {    
  8.         for(j = 1; j <= n; j++)    
  9.         {    
  10.             if(Map[i][j])    
  11.             {    
  12.                 //把原来有的边加入到新图中    
  13.                 ret->AddSingleEdge(i, j);  
  14.                 //把以E的终点为起点的边加入新图中    
  15.                 for(k = 1; k <= n; k++)    
  16.                     if(Map[j][k])    
  17.                         ret->AddSingleEdge(i, k);    
  18.             }    
  19.         }    
  20.     }    
  21.     return ret;  
  22. }  


 

22.1-6

如果A[i, j] = 1,即(i, j)∈E(1≤i≤|V|,1≤j≤|V|,E是G的边集),那么顶点i就不可能是通用汇点,因为它有一条出边。

现在假设A[i, j] = 0,即(i, j)∉E,且i≠j。在这种情况下,顶点j就不可能是通用汇点,因为它的入度小于|V|-1

因此,这个问题等价于:给定一个有向图G的|V|×|V|邻接矩阵A,在O(|V|)时间内判断是否存在一个整数j(1≤j≤|V|),使得对于所有的i(1≤i≤|V|且i≠j)都有A[i, j] = 1,对于所有的k(1≤k≤|V|)都有A[j, k] = 0。更形象地说,就是判断A里面是否有这样一个“十”字:这“十”字的横全是0,竖全是1(除了“十”字的中心)。

  1. bool Mat_Graph::Universal_Sink()  
  2. {  
  3.     //i指向有可能是汇的编号最小的点    
  4.     int i = 1, j = 1;    
  5.     while(j <= n)    
  6.     {    
  7.         //map[i][j] = 0,那么j没有i的入,一定不是汇,i可能是汇    
  8.         if(Map[i][j] == 0)    
  9.             j++;    
  10.         //若map[i][j] = 1,则(1)i有出,i不是汇(2)map[i][i+1..j-1]=0,i+1..j-1都不是汇(3)j及j以后的点可能是汇    
  11.         //若i=j,j也不是汇,j+1可能是汇        
  12.         else if(i == j)    
  13.         {    
  14.             i++;    
  15.             j++;    
  16.         }    
  17.         //若i!=j,j以前的点都不是汇,j可能是汇    
  18.         else i = j;    
  19.     }    
  20.     //没有汇    
  21.     if(i > n)    
  22.         return false;    
  23.     //找到有可能是汇的点,但是还是需要验证一下    
  24.     else    
  25.     {    
  26.         //汇的纵向都是1,除了map[i][i]    
  27.         for(j = 1; j <= i-1; j++)    
  28.         {    
  29.             if(Map[i][j] == 1)    
  30.                 return false;    
  31.         }    
  32.         //汇的横向都是0    
  33.         for(j = 1; j <= n; j++)    
  34.             if( i != j && Map[j][i] == 0)    
  35.                 return false;    
  36.         return true;    
  37.     }    
  38. }  

 

22.1-7

B = -BT

BBT = -B2

 

22.1-8

类似于邻接表与邻接矩阵的折中策略

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值