Figure (2)

Source: Internet
Author: User
I. Graph traversal 1. concept: a vertex is used to access the rest of the vertices in the graph, each vertex is accessed only once (the graph traversal algorithm is the basis for solving graph connectivity problems, Topology Sorting, and key paths ).

I. Graph traversal 1. concept: a vertex is used to access the rest of the vertices in the graph, each vertex is accessed only once (the graph traversal algorithm is the basis for solving graph connectivity problems, Topology Sorting, and key paths ).

I. Graph Traversal

1. concept: a vertex is used to access the rest of the vertices in the graph, and each vertex is accessed only once (the graph traversal algorithm is the basis for solving graph connectivity problems, Topology Sorting, and key paths .)

2. Deep Priority Search (DFS)

1). Basic Ideas:

(1) After accessing a vertex v in the graph, start with v and access any of its adjacent vertex w1;
(2) access the vertex w2 that is adjacent to w1 but has not been accessed from w1;
(3) then start from w2 to perform similar access ,...
(4) proceed until all adjacent vertices are accessed.
(5) Next, return to the previously accessed vertex to see if there are other unaccessed adjacent vertex.
If yes, access this vertex, and then start from this vertex for access similar to the aforementioned;
If not, return to the search page. Repeat the above process until all vertices in the connected graph are accessed.

2) Algorithm Implementation (obviously requires (stack) recursion ):

Void DFSTraverse (Graph G, Status (* Visit) (int v) {// do depth-first traversal on Graph G for (v = 0; v
 
  
= 0; w = NextAdjVex (G, v, w)/* next node of w node starting with v node */) if (! Visited [w]) DFS (G, w); // recursively call DFS for the unaccessed adjacent vertex w of v}
 

3) DFS time complexity analysis:

(1) If the graph is represented by an adjacent matrix, each vertex in the traversal graph needs to scan the row where the vertex is located from the beginning. Therefore, the time required to traverse all vertices is O (n2 ).
(2) If an adjacent table is used to represent a graph, although there are 2e table nodes, you only need to scan e nodes to traverse the graph, and add the time to access n head nodes, therefore, the time complexity of traversing an image is O (n + e ).

3. breadth-first search (BFS)

1). Basic Ideas:

(1) starting from a vertex V0, and accessing this vertex in turn accesses all unaccessed adjacent contacts of V0, then, access the adjacent points in sequence based on the access order of these vertices until all vertices having the same path with V0 in the figure are accessed;
(2) If the vertex in the graph is not accessed (a non-connected graph), select an unaccessed vertex as the starting point;
(3) Repeat the above process until all vertices in the graph are accessed.

2). Algorithm Implementation (obviously queue is used)

Void BFSTraverse (Graph G, Status (* Visit) (int v) {// use the auxiliary queue Q and access flag array visited [v] for (v = 0; v
 
  
= 0; w = NextAdjVex (G, u, w) if (! Visited [w]) {// w is an unaccessed adjacent vertex of u. visited [w] = TRUE; Visit (w); EnQueue (Q, w );} // if} // while} if} // BFSTraverse
 
3) BFS time complexity analysis:

(1) If an adjacent table is used to represent a graph, the total time cost of BFS loop is d0 + d1 +... + Dn-1 = 2e = O (e) Where di is the degree of vertex I
(2) If an adjacent matrix is used, BFS cyclically detects a full row (n elements) in the matrix for each accessed vertex ), the total time cost is O (n2 ).


2. Graph connectivity problems:

1. Related Terms:

(1) vertex set of the connected component: the access sequence of the vertex obtained from a vertex of the connected component;
(2) spanning tree: A very small connected subgraph of a connected component (deep-first spanning tree and breadth-first spanning tree );
(3) generate a forest: a set of extremely small connected subgraphs of each connected component of a non-connected graph.

2. Minimal Spanning Tree:

1). Kruskal algorithm:

First, construct a forest with only n vertices, and add the edge to the forest from small to large in the connected network based on the weight, so that no loop is generated in the forest, until the forest changes to a tree (for detailed code, see the last article ).


2) Prim algorithm (or understanding ):

Assume that all the original node sets are V, and the node set of the generated Minimum Spanning Tree is U. First, add the starting point V1 to U, and then compare all adjacent edges of V1, add the smallest V3 node to the set U,

Then, let's take a look at the distance between the remaining v-U nodes and the nodes in the U, and select the smallest... until the number of edges is n-1.

Algorithm design ideas:

Add an auxiliary array Closedge [n]. Each array component has two fields:

Requirement: Minimum Colsedge [I]. lowcost

3. Comparison of the two algorithms:

(1) the time complexity of the prim algorithm is O (n2), which is independent of the number of edges in the net and suitable for dense graphs;
(2) The cruise Karl algorithm needs to sort the e-edge by the weight value. The time complexity is O (eloge), and e is the number of edges in the network, which is suitable for sparse graphs.

4. Implement the following two algorithms:

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define MAX_VERTEX_NUM 20 # define OK 1 # define ERROR 0 # define MAX 1000 typedef struct Arcell {double adj; // vertex type} Arcell, adjMatrix [MAX_VERTEX_NUM] [MAX_VERTEX_NUM]; typedef struct {char vexs [MAX_VERTEX_NUM]; // node array, AdjMatrix arcs; // adjacent matrix int vexnum, arcnum; // current node count and arc count of the graph} MGraph; typedef struct Pnode // used for the split algorithm {char adjvex; // node double lowcost; // weight} Pnode, closedge [MAX_VERTEX_NUM]; // record the secondary array of edges with minimal cost from vertex set U to V-U define typedef struct Knode // used to store an edge and its corresponding 2 nodes in the cruise Karl algorithm {char trim; // Node 1 char ch2; // Node 2 double value; // weight} Knode, Dgevalue [MAX_VERTEX_NUM]; // vertex int CreateUDG (MGraph & G, dgevalue & dgevalue); int LocateVex (MGraph G, char ch); int Minimum (MGraph G, Closedge closedge); void MiniSpanTree_PRIM (MGraph G, char u ); void Sortdge (Dgevalue & dgevalue, MGraph G); // vertex int CreateUDG (MGraph & G, Dgevalue & dgevalue) // constructs the undirected Weighted Graph's Adjacent matrix {int I, j, k; cout <"Enter the number of nodes in the graph and the number of edges/arcs:"; cin> G. vexnum> G. arcnum; cout <"Enter the node:"; for (I = 0; I
    
     
> G. vexs [I]; for (I = 0; I
     
      
> Dgevalue [k]. usage> dgevalue [k]. ch2> dgevalue [k]. value; I = LocateVex (G, dgevalue [k]. vertex); j = LocateVex (G, dgevalue [k]. ch2); G. arcs [I] [j]. adj = dgevalue [k]. value; G. arcs [j] [I]. adj = G. arcs [I] [j]. adj;} return OK;} int LocateVex (MGraph G, char ch) // determine the node ch in the graph G. position in vexs {int a; for (int I = 0; I
      
        Dgevalue [j]. value) {temp = dgevalue [I]. value; dgevalue [I]. value = dgevalue [j]. value; dgevalue [j]. value = temp; Signature = dgevalue [I]. vertex; dgevalue [I]. vertex = dgevalue [j]. vertex; dgevalue [j]. vertex = vertex; ch2 = dgevalue [I]. ch2; dgevalue [I]. ch2 = dgevalue [j]. ch2; dgevalue [j]. ch2 = ch2 ;}}} void main () {int I, j; MGraph G; char u; Dgevalue dgevalue; CreateUDG (G, dgevalue ); cout <"the adjacent matrix of the graph is:" <
       
         > U; cout <"the edge set of the minimum cost Spanning Tree is \ n"; MiniSpanTree_PRIM (G, u ); cout <"============= cruise Cole algorithm ===================\ n "; cout <"the edge set that constitutes the minimum cost Spanning Tree is: \ n"; MiniSpanTree_KRSL (G, dgevalue );}
       
      
     
    
   
  
 
Running result:

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.