Implementation and traversing the data structure of FIG. (DFS, BFS)

// storage structure of FIG:
const int the MAXSIZE = 10;
// adjacency matrix
Template <class T>
class MGraph {
public:
    MGraph (A T [], n-int, int E);
    void the DFS (V int);
    void the BFS (V int);
Private: // edge edge is used to indicate an undirected graph, arc used to indicate an arc directed graph, vertex is a vertex
    T vertex [the MAXSIZE];
    int aRC [the MAXSIZE] [the MAXSIZE];
    int edge [ the MAXSIZE] [the MAXSIZE];
    int vNum, arcNum, ENUM;
    BOOL * visited; // use the DFS
};
Template <class T>
MGraph <T> :: MGraph (A T [], V int, int E) {// n is the number of vertices, e is the number of edges, a [] is the value of each vertex
    vNum = v; // the number of vertices, the number of edges, vertices class attribute value into
    arcNum = E;
    visited new new = BOOL [V];
    Memset (visited, to false, the sizeof (BOOL) V *);
    for (int K = 0; K <V; K ++) {    
        Vertex [K] = A [K];
    }
    for (int K = 0; K <V; K ++) {// first adjacency matrix initialization
        for (int j = 0; J <V; J ++) {
            ARC [K] [J] = 0;
        }
    }
    for (int K = 0, I = 0, J = 0; K <E; K ++) {// this is undirected FIG adjacency matrix, two vertices adjacent input
        CIN I >> >> J;
        ARC [-J. 1] [I-. 1] = ARC [. 1-I] [J-. 1] =. 1;
    }
}

// abutment tables, likewise as an example to non-view side is represented by ARC
struct ArcNode {// edge data structure, and stored information at a side edge
    int adjvex; // adjacent vertices
    ArcNode * nextarc;
};
struct {VertexNode / / vertex data structure stores the vertex information and the next edge
    int vertex;
    ArcNode * firstarc;
};
Template <class T>
ALGraph {class
public:
    ALGraph (A T [], n-int, int E);
    // ~ ALGraph ();
    void the DFS (V int);
    void the BFS (V int);
Private:
    VertexNode adjlist [the MAXSIZE];
    int vNum , arcNum;
    BOOL * visited;
};
Template <class T>
ALGraph <T> :: ALGraph (A T [], V int, int E) {
    vNum = V;
    arcNum = E;
    visited BOOL new new = [V];
    Memset (visited, to false, the sizeof (BOOL) * V);
    for (int I = 0; I <V; I ++) {// initialize a table of vertices
        adjlist [I] .vertex = A [I];
        adjlist [I ] .firstarc = NULL;
    }
    for (int K = 0, i = 0, j = 0; K <E; K ++) {// this is undirected graph, a directed graph, then if, for the vertex i to point j
        cin >> i >> j; // e note here is the sum of each vertex edge connection, as there will be no to have the double arrow (not actually fit undirected graph represented)
        ArcNode * = S new new ArcNode ;
        S-> J = adjvex; // used herein is the head insertion method
        S-> nextarc = adjlist [-I. 1] .firstarc;
        adjlist [-I. 1] .firstarc = S;
    }
}
// NOTE: adjacency table suitable directed graph indicates, there is a reverse adjacency list representation, and opposite to the pointing direction of the adjacent table, the inverse table is similar to the adjacent edge list

// orthogonal list, only the structure of the storage node
struct VertexNodeC {
    int Vertex;
    * firstin ArcNode;
    ArcNode first out, a *;
};
struct ArcNodeC {
    int headvex, tailvex;
    ArcNode * HLINK;
};

// set array side, more commonly, the use of a two-dimensional array is stored, a storage array of vertices, stored in an array starting point , end weights
struct ArcNodeE {
    int headvex;
    int tailvex;
    weight int;
};

// traverse the graph: DFS and the BFS
// the DFS depth-first search, similar to a preorder traversal of a binary tree, a branch start to the end, and then a step back has not visited nodes to find
// dfs adjacency list and the adjacency matrix two storage structures
// dfs two storage structure comparison
time complexity adjacency matrix // n
// adjacency list of the time complexity of n + e n stack depth and space complexity n
Template <class T>
void MGraph <T> :: the DFS (V int) {
    COUT << vertex [V];
    visited [V] = to true; // vertex flag to the accessed visited
    for (int j = 0; j < vNum; j ++) {// accordance with the characteristics of the adjacency matrix, the marking depth to search
        IF (ARC [V] [J] ==. 1 && visited [J] == to false) {
            the DFS (J);
        }
    }
}
Template < T class>
void ALGraph <T> :: the DFS (V int) {// the connecting adjoining table into the vertex node from the first edge vertex node, and whether the access flag, and a step back to see if there are any access neighbor node
    cout << adjlist [v] .vertex;
    visited[v] = true;
    * P = adjlist ArcNode [V] .firstarc;
    the while (P) {
        IF (visited [p-> adjvex] == to false) {
            the DFS (p-> adjvex);
        }
        P = p-> nextarc;
    }
}

// BFS BFS, similar tree traversal sequence, queue logic structure using
@ and respectively abutting adjacency matrix table describes
// n-time complexity adjacency matrix
// time complexity adjacency table space complexity n + e n-
Template <class T>
void MGraph <T> :: the BFS (V int) {
    int Queue [the MAXSIZE];
    int F = 0, 0 = R & lt;
    COUT << Vertex [V];
    visited [V] = to true;
    Queue [+ R & lt +] = V;
    the while (! F = R & lt) {// is equal to the head end of the queue is empty, the queue is empty expressed
        V = queue [F ++];
        for (int J = 0; J <vNum; J ++) {
            IF (ARC [V] [J] ==. 1 && visited [J] == to false) {
                cout << vertex[j];
                visited[j] = true;
                queue[++r] = j;
            }
        }
    }
}
template<class T>
void ALGraph<T>::BFS(int v) {
    int queue[MAXSIZE];
    int f = 0, r = 0;
    cout << adjlist[v].vertex;
    visited[v] = true;
    queue[++r] = v;
    while (r!=f) {
        v = queue[++f];
        ArcNode *p = adjlist[v].firstarc;
        while (p) {
            int j = p->adjvex;
            if (visited[j] == false) {
                cout << adjlist[j].vertex;
                visited[j] = true;
                Queue [R & lt ++] = J;
            }
            P = p-> nextarc;
        }
    }
}
. 4 on the basic algorithm of FIG in a separate one, Prim, Kruskal, Floyd, Dijkstra

Guess you like

Origin www.cnblogs.com/cimuhuashuimu/p/12134076.html