圖的深度優先搜尋(DFS)——圖用鄰接矩陣表示

來源:互聯網
上載者:User

該程式是用圖的鄰接舉著實現的。如果想用圖的鄰接鏈表實現DFS演算法,只要把該程式中的firstAdj()和nextAdj()以及圖的建立等操作改用鄰接鏈表實現即可,其他的如DFS主程式架構可以不用修改。

temp.cpp

//圖的深度優先搜尋(DFS)//圖用鄰接矩陣表示#include<iostream>#include<fstream>#include "temp.h"using namespace std;int LocateVex(MGraph G, VertexType v){      //在圖G的vexs(頂點)中找值為v的元素的下標,如果沒有則返回-1;       for(int i = 0; i < G.vexnum; i++){          if(G.vexs[i] == v){              return i;          }      }      return -1;  }  Status CreateMGraph(MGraph &G){      //建立圖G,輸入(讀檔案)的值有       //1.圖的頂點數,邊/弧的數目,圖的類型       //2.依次輸入頂點的值       //3.依次輸入一條邊的起點和終點;       ifstream in;      in.open("data3.txt", ios::in);      cout<<"下列資料從檔案data3.txt讀入"<<endl<<endl;      cout<<"請輸入圖的頂點數,邊/弧的數目,圖的類型"<<endl;      int kind;      in>>G.vexnum>>G.arcnum>>kind;//從檔案讀入資料,遇到任何結束標記(如空格和換行)則停止。       switch(kind){      case 0 :          G.kind = NG;          break;  case 1:  G.kind = DG;  break;  case 2:  G.kind = NW;  break;      case 3:          G.kind = DW;          break;      }      cout<<"請依次輸入頂點的值"<<endl;      for(int i = 0; i < G.vexnum; i++){          in>>G.vexs[i];      }  for(int i = 0; i < G.vexnum; i++){  for(int j = 0; j < G.vexnum; j++){  G.arcs[i][j] = 0;  }  }      cout<<"請依次輸入一條邊的起點和終點:"<<endl;      for(int k = 0; k < G.arcnum; k++){          int v1, v2, i, j;          in>>v1>>v2;          i = LocateVex(G, v1);          j = LocateVex(G, v2);          G.arcs[i][j] = 1;          if(G.kind == NG)//無向圖               G.arcs[j][i] = 1;      }      cout<<endl;      return OK;  }  int FirstAdj(MGraph G, int v){for(int i = 0; i < G.vexnum; i++){if(G.arcs[v][i]){return i;}}return -1;}int NextAdj(MGraph G, int v, int w){for(int i = w + 1; i < G.vexnum; i++){if(G.arcs[v][i]){return i;}}return -1;}void visitFuc(MGraph G, int v){cout<<G.vexs[v]<<endl;}void DFS(MGraph G,  int v, int visited[])  {  //從序號為v的頂點出發,對圖G做一次深度優先搜尋遍曆     visitFuc(G, v);                   //訪問v     visited[v]=TRUE;     // 打上訪問標記     for(int w = FirstAdj(G, v); w != -1; w = NextAdj(G, v, w) ){         if( !visited[w] )   DFS(G, w, visited); }}void DFSTraverse(MGraph G){  //深度優先搜尋遍曆圖Gint visited[VISITED_NUMBER];for(int v=0; v < G.vexnum;v++){       visited[v]=FALSE;    //初始化}    for(int v=0; v<G.vexnum;v++){  if(!visited[v]){DFS(G,v, visited); //若頂點v未被訪問,從v開始遍曆cout<<"這是一次DFS"<<endl;  }}}int main(){MGraph G;      CreateMGraph(G);DFSTraverse(G);}

temp.h

#define VertexType int   #define AdjMatrix int       typedef enum{NG, DG, NW, DW}GraphKind;  #define Status int   #define MAX_N 10   #define OK 1   #define ERROR 0  #define FALSE 0#define TRUE 1#define VISITED_NUMBER 10  typedef struct{      VertexType vexs[MAX_N];//表示頂點的數組;       AdjMatrix arcs[MAX_N][MAX_N];//表示邊的二維數組;       int vexnum,arcnum;//頂點和邊的個數;       GraphKind kind;  }MGraph;//定義圖MGraph;用鄰接矩陣表示。   

data3.txt

5 4 01 2 3 4 51 2 1 32 4 2 3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.