• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            eryar

            PipeCAD - Plant Piping Design Software.
            RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
            posts - 603, comments - 590, trackbacks - 0, articles - 0

            圖的鄰接矩陣表示

            Posted on 2012-04-30 12:52 eryar 閱讀(3288) 評論(2)  編輯 收藏 引用

            圖的鄰接矩陣表示

            Adjacency Matrix of the Graph

            一、鄰接矩陣定義

            圖的鄰接矩陣定義:設圖G=(V, E)的頂點集為V(G)={v1, v2,v3,…,vp},用aij表示G中頂點vivj之間的邊數,則n階方陣M(G)=(aij)pxp稱為G的鄰接矩陣(Adjacency Matrix)。

            Graph

            上圖所示的圖的鄰接矩陣如下:

            Adjacency Matrix

            圖的鄰接矩陣有以下明顯的性質:

            l 鄰接矩陣是一個對稱矩陣;

            l 若圖G為無環圖,則M(G)中第i行(列)的元素之和等于頂點的度數;

            一般說來,圖的鄰接矩陣比它的關聯矩陣小得多,通常圖就以其鄰接矩陣的形式存貯在計算機中。

            二、圖的鄰接矩陣表示法的程序實現

            圖的鄰接矩陣表示法的數據結構為:

               1:  typedef struct SArc
               2:  {
               3:      double  dWeight;
               4:  }AdjMatrix[VERTEX_NUM][VERTEX_NUM];
               5:   
               6:  typedef struct SGraph
               7:  {
               8:      int         iVertexNum;
               9:      int         iArcNum;
              10:      int         aVertex[VERTEX_NUM];
              11:      AdjMatrix   mArcs;
              12:  }Graph;
             
            .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

            在此基礎上實現圖的創建:

            程序代碼如下:

               1:  //------------------------------------------------------------------------------
               2:  //    Copyright (c) 2012 eryar All Rights Reserved.
               3:  //
               4:  //        File    : Main.cpp
               5:  //        Author  : eryar@163.com
               6:  //        Date    : 2012-4-11 20:33
               7:  //        Version : 1.0v
               8:  //
               9:  //    Description : Use adjacency matrix of a graph.
              10:  //
              11:  //==============================================================================
              12:   
              13:  #include <IOSTREAM>
              14:  using namespace std;
              15:   
              16:  const int INFINIY       = INT_MAX;
              17:  const int VERTEX_NUM    = 20;
              18:   
              19:  typedef struct SArc
              20:  {
              21:      double  dWeight;
              22:  }AdjMatrix[VERTEX_NUM][VERTEX_NUM];
              23:   
              24:  typedef struct SGraph
              25:  {
              26:      int         iVertexNum;
              27:      int         iArcNum;
              28:      int         aVertex[VERTEX_NUM];
              29:      AdjMatrix   mArcs;
              30:  }Graph;
              31:   
              32:  void    CreateGraph(Graph& graph);
              33:  int     LocateVertex(const Graph& graph, int vertex);
              34:  void    ShowGraph(const Graph& graph);
              35:   
              36:  ///////////////////////////////////////////////////////////////////////////////
              37:  // Main function.
              38:   
              39:  int main(int argc, char* argv[])
              40:  {
              41:      Graph   graph;
              42:   
              43:      CreateGraph(graph);
              44:   
              45:      ShowGraph(graph);
              46:   
              47:      return 0;
              48:  }
              49:   
              50:  ///////////////////////////////////////////////////////////////////////////////
              51:  /*
              52:  * Create a graph.
              53:  */
              54:  void    CreateGraph(Graph& graph)
              55:  {
              56:      cout<<"Create the graph"<<endl;
              57:      cout<<"Input vertex number:"; 
              58:      cin>>graph.iVertexNum;
              59:   
              60:      cout<<"Input arc number:";
              61:      cin>>graph.iArcNum;
              62:   
              63:      // Input vertex
              64:      for (int iVertex = 0; iVertex < graph.iVertexNum; iVertex++)
              65:      {
              66:          cout<<"Input "<<iVertex+1<<" vertex value:";
              67:          cin>>graph.aVertex[iVertex];
              68:      }
              69:   
              70:      // Initialize adjacency matrix.
              71:      for (int i = 0; i < graph.iVertexNum; i++)
              72:      {
              73:          for (int j = 0; j < graph.iVertexNum; j++)
              74:          {
              75:              graph.mArcs[i][j].dWeight   = 0;
              76:          }
              77:      }
              78:   
              79:      // Build adjacency matrix.
              80:      int     iFirst  = 0;
              81:      int     iSecond = 0;
              82:      int     xPos    = 0;
              83:      int     yPos    = 0;
              84:      double  dWeight = 0;
              85:   
              86:      for (int k = 0; k < graph.iArcNum; k++)
              87:      {
              88:          cout<<"Input edge first vertex:";
              89:          cin>>iFirst;
              90:   
              91:          cout<<"Input edge second vertex:";
              92:          cin>>iSecond;
              93:   
              94:          cout<<"Input the weight:";
              95:          cin>>dWeight;
              96:   
              97:          xPos  = LocateVertex(graph, iFirst);
              98:          yPos  = LocateVertex(graph, iSecond);
              99:   
             100:          // 
             101:          graph.mArcs[xPos][yPos].dWeight = dWeight;
             102:          graph.mArcs[yPos][xPos].dWeight = dWeight;
             103:      }
             104:  }
             105:   
             106:  /**
             107:  * Show a graph.
             108:  */
             109:  void    ShowGraph(const Graph& graph)
             110:  {
             111:      cout<<"Show the graph represented by adjacency matrix:"<<endl;
             112:   
             113:      // Output adjacency matrix.
             114:      for (int m = 0; m < graph.iVertexNum; m++)
             115:      {
             116:          for (int n = 0; n < graph.iVertexNum; n++)
             117:          {
             118:              cout<<graph.mArcs[m][n].dWeight<<"\t";
             119:          }
             120:   
             121:          cout<<endl;
             122:      }
             123:  }
             124:   
             125:  /**
             126:  * Locate vertex position in the adjacency matrix.
             127:  */
             128:  int LocateVertex( const Graph& graph, int vertex )
             129:  {
             130:      for (int i = 0; i < graph.iVertexNum; i++)
             131:      {
             132:          if (graph.aVertex[i] == vertex)
             133:          {
             134:              return i;
             135:          }
             136:      }
             137:   
             138:      return -1;
             139:  }
            .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

            程序運行效果如下圖所示:

            Run the program

            Feedback

            # re: 圖的鄰接矩陣表示[未登錄]  回復  更多評論   

            2012-05-21 08:45 by jack
            大哥,你這是用mfc編的?

            # re: 圖的鄰接矩陣表示  回復  更多評論   

            2012-05-22 14:04 by eryar
            不是, 控制臺的小程序@jack
            久久一区二区三区99| 狠狠久久综合| 久久人人爽人人爽人人av东京热| 久久综合中文字幕| 久久精品午夜一区二区福利| 国产亚洲美女精品久久久2020| 久久精品国产一区二区三区| 国产精品毛片久久久久久久| 国产精品禁18久久久夂久| 国产A三级久久精品| 精品久久久无码21p发布| 一本久道久久综合狠狠躁AV| 亚洲va久久久久| 久久久久高潮综合影院| 国内精品综合久久久40p| 亚洲精品美女久久777777| 久久精品久久久久观看99水蜜桃| 狠狠色狠狠色综合久久 | 日韩人妻无码精品久久久不卡| 大香伊人久久精品一区二区| 狠狠色婷婷久久综合频道日韩| 国内精品久久久久久99| 日本久久久久久中文字幕| 久久久久国产精品麻豆AR影院 | 精品久久久无码中文字幕| 久久人人爽人人爽AV片| 久久人人爽人人人人片av| 久久99国产精品99久久| 伊人热热久久原色播放www| 99久久国产精品免费一区二区| 丰满少妇人妻久久久久久| 91精品无码久久久久久五月天| 婷婷久久综合九色综合绿巨人| 久久婷婷激情综合色综合俺也去| 久久久中文字幕| yy6080久久| 国产精品午夜久久| 无码AV波多野结衣久久| 久久精品?ⅴ无码中文字幕| 一本久久a久久精品亚洲| 久久精品国产亚洲Aⅴ蜜臀色欲|