Deep understanding of chain forward star
The chained forward star is composed of a structure (including the target point, the edge weight and the next edge with the same starting point) and the head array (used to store the first outgoing edge of a point)
If necessary, you can also add an array of in-degree statistics, because the BFS DFS is performed by relying on the out-degree of the point and the adjacency of the out-edge. If the in-degree of more than one point is 0, it will only traverse to one of the points and beyond.
For the chained forward star: the head array will be updated every time the chained forward star adds an edge, so that the head array always stores the outgoing edge of the newly added point, and the next of this outgoing edge always points to the one stored before the head array. The serial number of the outgoing edge.
The order of our input edges is:
1 2
2 3
3 4
1 3
4 1
1 5
4 5
We create the edge structure as:
struct Edge { int next; int to; int w; };
where edge[i].to represents the end point of the i-th edge
edge[i].next indicates the storage location of the previous edge that has the same origin as the i-th edge
edge[i].w is the edge weight
There is also an array head[], which is used to represent the location where the new edge starting from i is stored
In fact, you will find that the location of the new edge stored here is actually the last input number of all edges starting from i
The head[] array is generally initialized to -1, and the add function for the edge is like this:
void add(int u,int v,int w) { edge[cnt].w = w; edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; }
Initialize cnt = 0, so, now we still simulate it according to the above diagram and input:
edge[0].to = 2; edge[0].next = -1; head[1] = 0;
edge[1].to = 3; edge[1].next = -1; head[2] = 1;
edge[2].to = 4; edge[2],next = -1; head[3] = 2;
edge[3].to = 3; edge[3].next = 0; head[1] = 3;
edge[4].to = 1; edge[4].next = -1; head[4] = 4;
edge[5].to = 5; edge[5].next = 3; head[1] = 5;
edge[6].to = 5; edge[6].next = 4; head[4] = 6;
Use template:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #define mae 10010 // 最大边数 #define mav 110 // 最大顶点数 using namespace std; struct EdgeNode { int v; int w; int next; } g [is]; int head[mae]; int t = 1;//edgenum 作用 int n, m; bool visdfs[mav]; bool visbfs[mav]; void init() // initialize { memset(head, 0, sizeof(head)); memset(visbfs, 0, sizeof(visbfs)); memset(visdfs, 0, sizeof(visdfs)); t = 1; } void add(int a, int b, int c) //加边 { g[t].v = b; // The current node a points to b g[t].w = c; // The weight of the a->b edge is c g[t].next = head[a]; / / next points to the previous edge starting from a head[a] = t; // head[a] represents the number t++ of the last input edge starting from a; // assign a number to each edge built with ( edgenum) } void Print() { int k, i; for(i = 1; i <= n; i++) { if(head[i])//找边 { for(k = head[i]; k != 0; k = g[k].next) { printf("%d->%d %d\n", i, g[k].v, g[k].w); } } } } void DFS(int x) { visdfs[x] = true; printf("%d\n", x); for(int i = head[x]; i != 0; i = g[i].next) { if(!visdfs[g[i].v]) { DFS(g[i].v); } } } void BFS(int x) { int q[mav]; // Queue int jin = 0 , chu = 0 , st; q[jin ++] = x; visbfs [x] = true ; // Mark while (chu < jin) { st = q[chu++]; printf("%d\n", st); for(int k = head[st]; k != 0; k = g[k].next) { if(!visbfs[g[k].v]) { visbfs[g[k].v] = true ; // mark q[jin++] = g[k].v; } } } } intmain () { int U, V, W, in; while(~scanf("%d%d", &n, &m)) { init(); for(int i = 1; i <= m; i++) { scanf("%d%d%d", &U, &V, &W); add(U, V, W); } in = 1 ; // Here, take 1 as an example to start searching for puts( " Mapping as " ); Print(); puts( " dfs access result: " ); DFS(in); printf("-----------\n"); puts( " bfs access result: " ); BFS(in); } return 0; }
Reference blog:
One of the mapping methods "forward star" BFS&&DFS simple application