About storage methods for graphs (static adjacency table, forward star, Edge set array)

Source: Internet
Author: User
Tags printf

I. adjacency MATRIX (not much said) G[u][v]

Ii. adjacency Table

1, dynamic linked list (pointer) an array header (U) + struct node (v), the chain, if the right value and other information in the node to add the corresponding domain.

2. static list (array) first array (analog header, u) + Edge set Array (numbered E,u, V) + next Array (analog pointer). This with 1 seems a bit different (the former chain is the point, the latter chain is the edge), in fact, is no difference, because to use the array to implement the linked list, so 1 of all the nodes of the E number, meaning is "edge."

Common Implementation method: Open five array first[maxn]; U[MAXM], V[MAXM], W[MAXM], NEXT[MAXM].

Third, Edge set array

is to put all the edges in an array so that you can walk through all the edges (very earthy = =). It is usually necessary to do some auxiliary storage according to actual needs.

1, the above array implementation adjacency table is the edge set array plus the first array and the next array.

2, forward to the star . Similar to 1, the difference is that he sorts the edge set array by the U point (the previous endpoint) in ascending order, so that the edges starting at the same point are grouped together. Coupled with the auxiliary array F[MAXN] (similar to the previous first array), the node I starts at the beginning of the position in the edge set array.

So notice that the forward star is actually doing a condensed storage processing, and by a single order , the next array (static adjacency table) is omitted. Of course, you can also do not sort, more maintenance of a next array.

Common Implementation method: Open four array f[maxn]; U[MAXM], V[MAXM], W[MAXM].


Report:

① Static adjacency table +dijkstra+heap

dijkstra+ static adjacency table #include <cstdio> #include <cstring> #include <iostream> #include <queue>

using namespace Std;		#define MAXN #define MAXM #define INF 1<<30 typedef pair<int, int> PII;
(Dist[v], v) priority_queue<pii, vector<pii>, greater<pii> > Q;
int FIRST[MAXN];
int U[MAXM], V[MAXM], W[MAXM], NEXT[MAXM];	int DIST[MAXN], INS[MAXN];

Ins[] is an int n, m in the S collection;
	void Dijkstra (int st) {for (int i=0; i<n; i++) dist[i] = i==st? 0:inf;
	memset (ins, 0, sizeof (INS));
Q.push (Make_pair (dist[st], ST));						INS[ST] = 1;
		Do not mix with SPFA inq, take out in the priority queue is the INS the while (!q.empty ()) {PII p = q.top (); Q.pop ();
		int x = P.second;
			if (!ins[x]) {ins[x] = 1;  for (int i=first[x]; i!=-1; I=next[i]) {if (Dist[v[i] > Dist[x]+w[i])//relax {dist[v[i]] = dist[x] +
					W[i];
				Q.push (Make_pair (Dist[v[i]), v[i]);
	}}}}//end of While} void Read_graph () {scanf ("%d%d", &n, &m); memset (First, -1, sizeof (first));
		for (int i=0; i<m; i++) {scanf ("%d%d%d", &u[i], &v[i], &w[i]);
		Next[i] = first[u[i]];
	First[u[i]] = i;
	}} int main () {read_graph ();
	Int St;
	Dijkstra (scanf ("%d", &st));
	for (int i=0; i<n; i++) {printf ("[%d,%d]=%d\n", St, I, dist[i]); }
}

② Static adjacency table +SPFA

#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std;
#define MAXN #define MAXM #define INF 1<<30 queue<int> q;
int FIRST[MAXN], NEXT[MAXM]; struct Edge {int u, V, W;}
A[MAXM];
int DIST[MAXN], INQ[MAXN];

int n, m;
	void Spfa (int st) {for (int i=0; i<n; i++) dist[i] = i==st? 0:inf;
	memset (inq, 0, sizeof (INQ));
	Q.push (ST);					INQ[ST] = 1;
		I'll be out of the team anyway, this inq can do without while (!q.empty ()) {int u = q.front (); Q.pop ();
		Inq[u] = 0;
			for (int e=first[u]; e!=-1; e=next[e]) {int v = A[E].V;
				if (Dist[v] > DIST[U]+A[E].W) {dist[v] = DIST[U]+A[E].W;		if (!inq[v]) {Q.push (v); inq[v] = 1;}
			Inq=1!!!!
	}}}} void Read_graph () {cin>>n>>m;		memset (First,-1, sizeof (first));
		Don't forget to initialize the header for (int e=0; e<m; e++) {cin>>a[e].u>>a[e].v>>a[e].w;
		Next[e] = first[a[e].u];
	FIRST[A[E].U] = e;
	}} int main () {read_graph ();
	Int St;
cin>>st;	SPFA (ST);
	for (int i=0; i<n; i++) {printf ("[%d,%d]=%d\n", St, I, dist[i]);

 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.