Traversing Graph - summary graph theory algorithms (a)

Summary graph theory algorithms

I. Introduction

Data structure, primarily related to tree and is the ultimate difficulty and pain points, on algorithms, remember that name is very simple, take some time to remember the principle, how internalized into the knowledge itself, and, in the mind is thinking , with the take with the use of this memory requires a specific way. If it can not be used with that, and say that they can not understand this algorithm. I suggest that you, as the center of their own thinking, remember that logical thinking, but also keep in mind the idea of ​​code into their training within the algorithm.

Reference Bowen: https://liuchuo.net/archives/tag/dfs (there are some differences with the original Bowen, I suggest that you also write code according to their own ideas and way of thinking is not the same for everyone )

Second, the graph traversal

2.1. Dfs depth-first traversal FIG.

Access nodes that are not accessible to the depth-first fashion

2.1.1. Pseudo code
dfs(u){
    vis[u]=true;
    for(从u出发能到达的所有顶点v)
        if(vis[v]==false)
            dfs(v);
}
dfsTrave(G){
    for(G的所有节点u)
        if(vis[u]==false)
            dfs(u)
}
2.1.2.C language code (the adjacency matrix)
//邻接矩阵
void dfs(int u){
    vis[u]=1;
    printf("%d",u);
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==0&&path[u][i]==1) dfs(i);
}
void dfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==false) dfs(i);
}
2.1.3.C language code (the adjacency list)
void dfs(int u){
    vis[u]=1;
    printf("%d",n[u].val);
    for(int i=0;i<n[u].next.size();i++)
        if(vis[n[u].next[i]->val]==0) dfs(n[u].next[i]->val);
}
void dfsTrave(){
    for(int i=0;i<vertxNum;i++){
        if(vis[i]==0) dfs(i);
    }
}
2.1.4.0. Test Case (C ++)

2.1.4.1. Adjacency matrix

#include <iostream>
#define vertxNum 4

using namespace std;

int vetx[vertxNum]={0,1,2,3};
int path[vertxNum][vertxNum]={0};
bool vis[vertxNum]={0};

void dfs(int u){
    vis[u]=1;
    printf("%d",u);
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==0&&path[u][i]==1) dfs(i);
}
void dfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==false) dfs(i);
}
int main()
{
    /**build graph*/
    path[0][2]=1;path[2][0]=1;
    path[2][3]=1;path[3][2]=1;
    path[1][3]=1;path[3][1]=1;
    path[2][1]=1;path[1][2]=1;
    dfsTrave();
    system("pause");
    return 0;
}
2.1.4.2. Adjacency list
#include <iostream>
#include <vector>
#define vertxNum 4
using namespace std;
struct node{
    int val;
    vector<node*> next;
};
node n[vertxNum];
bool vis[vertxNum]={0};
void dfs(int u){
    vis[u]=1;
    printf("%d",n[u].val);
    for(int i=0;i<n[u].next.size();i++)
        if(vis[n[u].next[i]->val]==0) dfs(n[u].next[i]->val);
}
void dfsTrave(){
    for(int i=0;i<vertxNum;i++){
        if(vis[i]==0) dfs(i);
    }
}
int main(){
    /**build graph*/
    n[0].val=0;n[1].val=1;n[2].val=2;n[3].val=3;
    n[0].next.push_back(&n[2]);n[2].next.push_back(&n[0]);
    n[1].next.push_back(&n[2]);n[2].next.push_back(&n[1]);
    n[1].next.push_back(&n[3]);n[3].next.push_back(&n[1]);
    n[2].next.push_back(&n[3]);n[3].next.push_back(&n[2]);
    dfsTrave();
    system("pause");
    return 0;
}
2.2. Bfs breadth-first traversal map
2.2.1 pseudocode
bfs(u) {
  queue q;
  将u入队
  inq[u] = true;
  while(q非空) {
    for(从u出发到可到达的所有定点v) {
      if(inq[v] == false)
        将v入队
        inq[v] = true;
    }
  }
}
bfsTrave(G) {
  for(G的所有顶点u) {
    if(inq[u] == false)
      bfs(u);
  }
}
2.2.2.C language code (the adjacency matrix)
void bfs(int u){
    que.push(u);
    vis[u]=1;
    while(!que.empty()){
        u=que.front();
        printf("%d",u);
        que.pop();
        for(int i=0;i<vertxNum;i++)
            if(path[u][i]==1&&vis[i]==0){
                que.push(i);
                vis[i]=1;
            }
    }
}
void bfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vetx[i]==0) bfs(i);
}
2.2.3.C language code (the adjacency list)
void bfs(int u){
    que.push(u);
    vis[u]=1;
    while(!que.empty()){
        u=que.front();
        printf("%d",u);
        que.pop();
        for(int i=0;i<n[u].next.size();i++){
            if(vis[n[u].next[i]->val]==0){
                que.push(n[u].next[i]->val);
                vis[n[u].next[i]->val]=1;
            }
        }
    }
}
void bfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==0) bfs(n[i].val);
}
2.2.4.0. Test Case (C ++)
2.2.4.1. Adjacency matrix
#include <iostream>
#include <queue>
#define vertxNum 4

using namespace std;
int vetx[vertxNum]={0,1,2,3};
int path[vertxNum][vertxNum]={0};
queue<int> que;
bool vis[vertxNum]={0};

void bfs(int u){
    que.push(u);
    vis[u]=1;
    while(!que.empty()){
        u=que.front();
        printf("%d",u);
        que.pop();
        for(int i=0;i<vertxNum;i++)
            if(path[u][i]==1&&vis[i]==0){
                que.push(i);
                vis[i]=1;
            }
    }
}
void bfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vetx[i]==0) bfs(i);
}
int main()
{
    /**build graph*/
    path[0][2]=1;path[2][0]=1;
    path[0][3]=1;path[3][0]=1;
    path[1][3]=1;path[3][1]=1;
    path[2][1]=1;path[1][2]=1;
    bfsTrave();
    system("pause");
    return 0;
}
2.2.4.2. Adjacency list
#include <iostream>
#include <vector>
#include <queue>
#define vertxNum 4
using namespace std;
struct node{
    int val;
    vector<node*> next;
};
node n[vertxNum];
bool vis[vertxNum]={0};
queue<int> que;
void bfs(int u){
    que.push(u);
    vis[u]=1;
    while(!que.empty()){
        u=que.front();
        printf("%d",u);
        que.pop();
        for(int i=0;i<n[u].next.size();i++){
            if(vis[n[u].next[i]->val]==0){
                que.push(n[u].next[i]->val);
                vis[n[u].next[i]->val]=1;
            }
        }
    }
}
void bfsTrave(){
    for(int i=0;i<vertxNum;i++)
        if(vis[i]==0) bfs(n[i].val);
}
int main(){
    n[0].val=0;n[1].val=1;n[2].val=2;n[3].val=3;
    n[0].next.push_back(&n[2]);n[2].next.push_back(&n[0]);
    n[1].next.push_back(&n[2]);n[2].next.push_back(&n[1]);
    n[1].next.push_back(&n[3]);n[3].next.push_back(&n[1]);
    n[0].next.push_back(&n[3]);n[3].next.push_back(&n[0]);
    bfsTrave();
    system("pause");
    return 0;
}

Guess you like

Origin www.cnblogs.com/littlepage/p/11546548.html