Use three ways to realize the storage of graphs to adapt to different situations.
Reference: ACM-ICPC Programming Series--Graph theory and application
Mode 1: adjacency matrix
Adjacency matrix is the simplest and most common one of the data structure of the graph.
Implementation: The two-dimensional array map[maxn][maxn],map[i][j] represents the distance from point I to to J.
Initialize: map[i][i] = 0,map[i][j] = INF (i!=j), read in data map[i][j] = W.
Time complexity: Initialize O (n^2), map requires O (m), Total time complexity O (n^2).
Advantages and disadvantages: simple and intuitive, can be directly queried between the point I and Point J whether there is an edge, the traversal efficiency is low, not
Can store the renumbering, low initialization efficiency, large graph overhead, suitable for dense graphs with less storage points.
Way 2: The Forward star
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 100;const int MAXM = 100;int head[maxn];//The position of the first edge of the storage starting point of i struct node{int from;//start int to;//end point int w;//weight Value};node edge[maxm];bool cmp (node A,node b)//edge Sort {if (A.from = b.from && a.to = = b.to) return A.W < B.W; if (A.from = = B.from) return a.to < b.to; return A.from < B.from;} int main () {int n,m; CIN >> n >> m; for (int i = 0; i < m; i++) {cin >> edge[i].from >> edge[i].to >> edge[i].w; } sort (edge,edge+m,cmp); memset (head,-1,sizeof (head)); Head[edge[0].from] = 0; for (int i = 1; i < m; i++)//Determine starting point for I first edge position {if (edge[i].from! = edge[i-1].from) Head[edge[i].from] = i; } for (int i = 1; I <= n; i++)//traversal diagram {//k is the starting position for the I-node edge for (int k = Head[i]; edge[k].from = i &&A mp K < M; k++) { cout << edge[k].from << ' << edge[k].to << ' << edge[k].w << Endl; }} return 0;} /*7 81 1 11 3 11 3 23 5 13 6 14 6 12 4 11 2 1*/
Mode 3: Adjacency table-Dynamic Build table
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn = 100;struct edgenode<span style= "white-space:pre" ></span>//adjacency table node {int To;<span style = "White-space:pre" ></span>//end int W;<span style= "white-space:pre" ></span>//weight EdgeNode *next; <span style= "White-space:pre" ></span>//pointer to next edge};struct vnode<span style= "White-space:pre" > </span>//Start table Node {int From;<span style= "White-space:pre" ></span>//start Edgenode *first;<span style= "White-space:pre" ></span>//adjacency table head pointer}; Vnode adjlist[maxn];<span style= "White-space:pre" ></span>//the adjacency table of the entire graph int main () {int n,m,x,y,w; CIN >> n >> m; for (int i = 0; i < m; i++)//read in graph {cin >> x >> y >> w; Edgenode *p = new Edgenode (); P->to = y; P->w = W; P->next = Adjlist[x].first; Adjlist[x].first = p; } cout << Endl; for (int i = 1; I <= n; i++)//traverse diagram {for (Edgenode *k = Adjlist[i].first; K! = NULL; k = k->next) cout << i << ' << k->to << ' << k->w << Endl; } return 0;}
Method 4: Adjacency table--vector simulation linked list implementation
#include <iostream> #include <algorithm> #include <vector> #include <cstdio> #include < cstring>using namespace Std;const int maxn = 100;const int maxm = 100;struct edgenode<span style= "White-space:pre" &G t;</span>//Edge table node type {int To;<span style= "white-space:pre" ></span>//endpoint int w;<span style= "white-sp Ace:pre "></span>//weight value};vector<edgenode> map[maxn];int main () {Edgenode E; int n,m,x,y,w; CIN >> n >> m; for (int i = 0; i < m; i++) <span style= "White-space:pre" ></span>//read in diagram {cin >> x >> y >> W; e.to = y; E.W = W; Map[x].push_back (e); } cout << Endl; for (int i = 1; I <= n; i++) <span style= "White-space:pre" ></span>//traverse diagram {for (vector <EDGENODE&G T;::iterator k = Map[i].begin (); K!=map[i].end (); k++) {Edgenode t = *k; cout << i << ' << t.to << ' ≪< T.W << Endl; }} return 0;}
Mode 5: Adjacency table--chain forward star ★
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 100;const int maxm = 100;int head[maxn];struct edgenode{ int to; int W; int next;}; Edgenode Edges[maxm];int Main () { int n,m,x,y,w; CIN >> n >> m; memset (head,-1,sizeof (head)); for (int i = 0; i < m; i++) { cin >> x >> y >> w; edges[i].to = y; EDGES[I].W = W; Edges[i].next = head[x]; HEAD[X] = i; } cout << Endl; for (int i = 1, i <= N; i++) {for (int k = head[i]; k!=-1; k = edges[k].next) cout << i << ' ' << edges[k].to << ' << edges[k].w << Endl; } return 0;}
Five kinds of storage methods of graphs "graph theory"