用鄰接矩陣和鄰接表創建圖


  1 #include <iostream>
  2 using namespace std;
  3 
  4 #define INFINITY 65536//無窮大
  5 #define    MAX_VERTEX_NUM 10//最大頂點個數
  6 typedef enum{DG,DN,UDG,UDN}GraphKind;//有向圖,有向網,無向圖,無向網
  7 struct Graph
  8 {
  9     char vexs[MAX_VERTEX_NUM];//儲存頂點
 10     int arc[MAX_VERTEX_NUM][MAX_VERTEX_NUM];//鄰接矩陣
 11     int vexnum, arcnum;//頂點數和邊(弧)數
 12     GraphKind kind;//圖的種類
 13 };
 14 //確定頂點序號
 15 int LocateVex(Graph *G, char ch)
 16 {
 17     for (int i = 0; i < G->vexnum; i++)
 18         if (G->vexs[i] == ch)
 19         {
 20             return i;//返回頂點序號
 21             break;
 22         }
 23     return -1;
 24 }
 25 void CreateUDN(Graph *G)//創建無向網
 26 {
 27     int i, j;
 28     char v1, v2;//記錄頂點名稱
 29     int w;//記錄權值
 30     cout << "請輸入頂點個數:" << endl;cin >> G->vexnum;
 31     cout << "請輸入邊的個數:" << endl;cin >> G->arcnum;
 32     cout << "構造頂點:" << endl;
 33     for (int i = 0; i < G->vexnum; i++)//構造頂點
 34         cin >> G->vexs[i];
 35     for (int i = 0; i < G->vexnum; i++)//初始化鄰接矩陣
 36         for (int j = 0; j < G->vexnum; j++)
 37             G->arc[i][j] = 0;
 38     cout << "構造鄰接矩陣:" << endl;
 39     for (int k = 0; k < G->arcnum; k++)
 40     {
 41         cin >> v1 >> v2 >> w;
 42         i = LocateVex(G, v1);
 43         j = LocateVex(G, v2);
 44         G->arc[j][i] = G->arc[i][j] = w;
 45     }
 46 }
 47 void CreateGraph(Graph *G)
 48 {
 49     switch (G->kind)
 50     {
 51     //case DG:return CreateDG(G);
 52     //case DN:return CreateDN(G);
 53     //case UDG:return CreateUDG(G);
 54     case UDN:return CreateUDN(G);
 55     }
 56 }
 57 //遍歷鄰接矩陣
 58 void PrintArc(Graph *G)
 59 {
 60     int i, j;
 61     for (i = 0; i < G->vexnum; i++)
 62     {
 63         for (j = 0; j < G->vexnum; j++)
 64             cout << G->arc[i][j] << " ";
 65         cout << endl;
 66     }
 67 }
 68 //深度優先遍歷(鄰接矩陣儲存)
 69 int visited[MAX_VERTEX_NUM] = { 0 };//初始化元素為0
 70 void DFS_Arc(Graph *G,int v)//遞歸實現,v為某個頂點
 71 {
 72     if(visited[v]==0)
 73         cout << G->vexs[v] << " ";
 74     visited[v] = 1;
 75     for (int j = 0; j < G->vexnum; j++)
 76     {
 77         if (G->arc[v][j] != 0 && visited[j] == 0)
 78             DFS_Arc(G,j);
 79     }
 80 }
 81 
 82 
 83 //鄰接表
 84 struct ENode//邊結點
 85 {
 86     int index;//邊所指向的點的序號
 87     int info;//記錄權值
 88     struct ENode* next;//指向下一條邊
 89 };
 90 struct VNode//頂點結點
 91 {
 92     char data;//頂點名稱
 93     ENode* first;//指向頂點的第一個鄰邊
 94 };
 95 struct AGraph//記錄圖的信息
 96 {
 97     int vexnum, arcnum;//頂點數和邊數
 98     VNode p[MAX_VERTEX_NUM];//頂點數組
 99     GraphKind kind;//圖的種類
100 };
101 
102 int LocateVex(AGraph *G,char ch)
103 {
104     for (int i = 0; i < G->vexnum; i++)
105         if (ch == G->p[i].data)
106         {
107             return i;
108             break;
109         }
110     return -1;
111 }
112 //創建鄰接表
113 void CreateAGraph(AGraph *G)
114 {
115     int i, j, info;
116     char v1, v2;
117     cout << "請輸入頂點個數:" << endl; cin >> G->vexnum;
118     cout << "請輸入邊的個數:" << endl; cin >> G->arcnum;
119     cout << "構造頂點:" << endl;
120     for (int i = 0; i < G->vexnum; i++)//構造頂點數組
121     {
122         cin >> G->p[i].data;
123         G->p[i].first = NULL;
124     }
125     cout << "構造鄰接表:" << endl;
126     for (int k = 0; k < G->arcnum; k++)
127     {
128         cin >> v1 >> v2 >> info;
129         i = LocateVex(G, v1);
130         j = LocateVex(G, v2);
131         ENode *E = (ENode*)malloc(sizeof(ENode));
132         E->index = j;
133         E->info = info;
134         E->next = G->p[i].first;//頭插法
135         G->p[i].first = E;
136     }
137 }
138 //打印鄰接表
139 void PrintAGraph(AGraph *G)
140 {
141     for (int i = 0; i < G->vexnum; i++)
142     {
143         cout << G->p[i].data;
144         if (G->p[i].first != NULL)
145         {
146             ENode *p = G->p[i].first;
147             while (p->next != NULL)
148             {
149                 cout << "->" << p->index;
150                 p = p->next;
151             }
152             cout << "->" << p->index;
153         }
154         cout << endl;
155     }
156 }
157 //深度優先遍歷(鄰接表儲存)
158 int visited1[MAX_VERTEX_NUM] = { 0 };
159 void DFS_List(AGraph *G, int v)
160 {
161     if (visited1[v] == 0)
162         cout << G->p[v].data << " ";
163     visited1[v] = 1;
164     if (G->p[v].first != NULL)
165     {
166         ENode *p = G->p[v].first;
167         DFS_List(G, p->index);
168         while (p->next != NULL)
169         {
170             p = p->next;
171             DFS_List(G, p->index);
172         }
173     }
174 }
175 int main()
176 {
177     Graph *G=(Graph*)malloc(sizeof(Graph));
178     G->kind = UDN;
179     CreateGraph(G);
180     PrintArc(G);
181     cout << endl << "深度優先遍歷:" << endl;
182     DFS_Arc(G, 0);
183     cout << endl;
184     cout << "-------------------------------------" << endl;
185     cout << "鄰接表:" << endl;
186     AGraph *AG=(AGraph*)malloc(sizeof(AGraph));
187     CreateAGraph(AG);
188     PrintAGraph(AG);
189     cout << "深度優先遍歷:" << endl;
190     DFS_List(AG, 0);
191     system("pause");
192     return 0;
193 }

運行結果:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM