Basic operation of graphs (based on adjacency matrix): Graph construction, deep search (DFS), wide search (BFS)

Source: Internet
Author: User

adjacency Matrix

An adjacency matrix is a representation of a graph Common storage representations. It uses two arrays to store information about the data element (vertex) and the relationship between the data elements (edges or arcs), respectively. The adjacency matrix A of graph G with order n is n*n. Label the vertex of G as v_1,v_2,..., v_n. if (V_i,v_j) \in E (G), a_{ij}=1, otherwise a_{ij}=0.


Depth-first-search

Is to traverse the tree's nodes, as deep as possible, to search the branches of the tree along the depth of the trees . When all the edges of node v have been explored, the search will go back to the starting node of the edge where node V was found . This process continues until all nodes that have been discovered from the source node are found. If there are still non-discovered nodes, select one as the source node and repeat the process, and the entire process repeats until all nodes are accessed. Belong to the blind search.


Search Result: 1 2 3 4 5 6 7 8 9Breadth-first-search

BFS, is a graph search algorithm. To put it simply, BFS is the node that traverses the tree as it starts from the root node and along the tree's width. If all nodes are accessed, the algorithm aborts.


Search Result: 1 2 3 4 5 6 7 8 9 10 11 12

Implementation
#include <iostream> #include <string> #include <queue> using namespace std;      #define MAXN-struct Graph {string VERTEX[MAXN];      int MATRIX[MAXN][MAXN];      int vertexnum;  int arcnum;    };              int Locate (Graph g,string str) {for (int i =0;i<g.vertexnum;i++) {if (str = = G.vertex[i])      return i;  } return-1;      } void Createdug (graph &g)//construct non-direction graph {string start,end;      cout << "Enter vertex and number of edges:" <<endl;        cin>>g.vertexnum>>g.arcnum;          for (int i = 0;i<g.vertexnum;i++) {cout<< "Please enter" <<i<< "vertices:" <<endl;      cin>>g.vertex[i]; } for (int i = 0;i<g.vertexnum;i++) {for (int j = 0;j<g.vertexnum;j++) {g          . matrix[i][j] = 0; }} for (int i = 0;i <g.arcnum;i++) {cout<< "Please enter the start and end vertices of the" <<i<< "Edge" <<e          Ndl Cin>>start>>end;          int m = Locate (G,start);            int n = Locate (g,end);          G.matrix[m][n] = 1;      G.MATRIX[N][M] = 1;      }} void Createudn (Graph &g)//constructed without net {string start,end;      int W;      cout << "Enter vertex and number of edges:" <<endl;        cin>>g.vertexnum>>g.arcnum;          for (int i = 0;i<g.vertexnum;i++) {cout<< "Please enter" <<i<< "vertices:" <<endl;      cin>>g.vertex[i]; } for (int i = 0;i<g.vertexnum;i++) {for (int j = 0;j<g.vertexnum;j++) {g          . matrix[i][j] = 0; }} for (int i = 0;i <g.arcnum;i++) {cout<< "Please enter the start and end vertices and weights of the" <<i<< "side" <&lt          ; Endl;            cin>>start>>end>>w;          int m = Locate (G,start);            int n = Locate (g,end);          G.matrix[m][n] = W;      G.matrix[n][m] = W; }} void Createdg (graph &g)//construct a graph {string start, end;      cout << "Enter vertex and number of edges:" <<endl;        cin>>g.vertexnum>>g.arcnum;          for (int i = 0;i<g.vertexnum;i++) {cout<< "Please enter" <<i<< "vertices:" <<endl;      cin>>g.vertex[i]; } for (int i = 0;i<g.vertexnum;i++) {for (int j = 0;j<g.vertexnum;j++) {g          . matrix[i][j] = 0; }} for (int i = 0;i <g.arcnum;i++) {cout<< "Please enter the start and end vertices of the" <<i<< "Edge" <<e          Ndl            cin>>start>>end;          int m = Locate (G,start);            int n = Locate (g,end);      G.matrix[m][n] = 1;      }} void Createdn (Graph &g)//Constructs a network {string start,end;      int W;      cout << "Enter vertex and number of edges:" <<endl;        cin>>g.vertexnum>>g.arcnum;          for (int i = 0;i<g.vertexnum;i++) {cout<< "Please enter" <<i<< "vertices:" <<endl;      cin>>g.vertex[i]; } foR (int i = 0;i<g.vertexnum;i++) {for (int j = 0;j<g.vertexnum;j++) {g.matrix[i][          J] = 0; }} for (int i = 0;i <g.arcnum;i++) {cout<< "Please enter the start and end vertices and weights of the" <<i<< "side" <&lt          ; Endl;            cin>>start>>end>>w;          int m = Locate (G,start);            int n = Locate (g,end);      G.matrix[m][n] = W; }} int Firstadjvex (Graph g,int v)//Returns the first adjacency vertex ordinal of v {for (int i = 0;i<g.vertexnum;i++) {if (G.mat      Rix[v][i] = = 1) return i;  } return-1;          } int Nextadjvex (Graph g,int v,int W)//Returns the next adjacency point of Vertex v relative to W ordinal {for (int i = w+1;i<g.vertexnum;i++) {      if (g.matrix[v][i] = = 1) return i;  } return-1;    } bool VISTED[MAXN];      void DFS (Graph g,int i) {cout <<g.vertex[i]<< "";      Visted[i] = true; for (int w = Firstadjvex (g,i); w>=0;w = Nextadjvex (g,i,w)) {if(!visted[i])      DFS (G,W);      }} void Dfstransfer (Graph g) {for (int i =0;i<g.vertexnum;i++) {visted[i] = false;      } for (int i = 0;i<g.vertexnum;i++) {if (!visted[i]) DFS (g,i);  } cout << Endl;          } void BFS (graph G,int v) {} void Bfstransfer (graph G) {for (int i =0;i<g.vertexnum;i++) {      Visted[i] = false;      } std::queue<int> que;              for (int i = 0;i<g.vertexnum;i++) {if (!visted[i]) {Que.push (i);              Visted[i] = true;                  while (!que.empty ()) {int k = Que.front ();                  Que.pop ();                  cout <<g.vertex[k]<< "";                      for (int i = Firstadjvex (g,k); i>=0;i = Nextadjvex (g,k,i)) {if (!visted[i])                   {Que.push (i);       Visted[i] = true;    }}}} cout <<endl;      } int main () {Graph g;      Createdug (g);      Dfstransfer (g);      Bfstransfer (g);  return 0;   }


Reference
    1. The basic algorithm of the 22nd chapter of the Introduction to algorithms P322
    2. Http://en.wikipedia.org/wiki/Breadth-first_search
    3. Http://en.wikipedia.org/wiki/Depth-first_search

Basic operation of graphs (based on adjacency matrix): Graph construction, deep search (DFS), wide search (BFS)

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.