Directed Graph basic algorithm-Traversal Algorithm

Source: Internet
Author: User

1. Graph Representation

2. Directed Graph TraversalAlgorithm: Depth first

3. Directed Graph traversal algorithm: breadth first

4CodeReflection

5. Download

1. Graph Representation

1.1 graph Definition

Graph G is defined as the set G = {v, e} of V and E, where V represents the set of all vertices in the graph, and E represents the set of all edges in G. The graph is divided into Directed Graphs and undirected graphs based on whether the elements in E have directions.

1.2 Graph Representation

How do I represent a graph in a computer based on the mathematical definition given above? Generally, there are two methods: the adjacent table and the representation of the adjacent matrix.

The following is an undirected graph's list of neighboring tables and its matrix representation:

 

The following table lists the list of Directed Graphs and their adjacent matrices:

 

According to the preceding representation, the following defines the data structure (the adjacent table) of graph G. First, the vertex graphvertex of the graph is defined:

// The Public char symbol {Get; Set ;}// the current color of the vertex is public vertexcolor {Get; Set ;} // The distance between the vertex and the Start Node is public int distance {Get; set;} // The breadth traverses the parent node public graphvertex parent {Get; set ;} // start time public int starttime {Get; Set ;}// end time public int finishtime {Get; Set ;}// edge corresponding to the vertex in the deep Priority Search

Public list <graphedge> followedges {Get; set ;}

Define the data structure of the edge of graph G:

// Edge start vertex. It is not necessary to store public graphvertex from {Get; Set ;}// end vertex public graphvertex to {Get; set;} in the storage of the adjacent table ;} // Edge Weight

Public int weight {Get; set ;}

Definition diagram:

// Data member. Here we assume that the symbol of the vertex is a different private hashtable graph = new hashtable ();

Private int time = 0;

The overall structure is as follows:

 

2. digraph depth Priority Algorithm

2.1 Basic Algorithms

D indicates the time point when a node is first discovered. F indicates the time when all nodes starting from the node have been discovered.

 

2.2 design implementation

// Depth-first traversal algorithm public void depthfirstvisit (graphvertex v) {// just found, color: Gray console. writeline (v. symbol); V. color = vertexcolor. gray; this. time ++; // start time v. starttime = This. time; foreach (graphedge in v. followedges) {// If (edge. to. color = vertexcolor. white) {edge. to. parent = V; depthfirstvisit (edge. to) ;}} // if all edges have been found to be complete v. color = vertexcolor. black; this. time ++; V. finishtime = This. time;} public void depthfirsttravel () {// global time variable this. time = 0; // initialize graphvertex V; foreach (dictionaryentry e in this. graph) {v = (graphvertex) E. value; V. color = vertexcolor. white; V. parent = NULL;} // recursive call // foreach (dictionaryentry e in this. graph) {v = (graphvertex) E. value; // The vertex is white if (v. color = vertexcolor. white) {depthfirstvisit (v );}}

}

3. Directed Graph traversal algorithm: breadth first

3.1 Basic Algorithms

The color field indicates the status of the current node. If it is white, it indicates that the vertex has not been found, and gray indicates that the current vertex has been found, but the nodes departing from this node have not been found yet. The parent field defines the parent node of the search algorithm. The distance field indicates the Path Distance from node s to a detected node v.

3.2 design implementation

// The breadth-first traversal algorithm generates the breadth-first tree public void breadthfirsttravel (graphvertex s) {// initialize graphvertex V for all nodes; foreach (dictionaryentry e in this. graph) {v = (graphvertex) E. value; V. color = vertexcolor. white; V. distance = int. maxvalue; V. parent = NULL;} // found the first node S. color = vertexcolor. gray; S. distance = 0; S. parent = NULL; // initialize the queue context = new Queue (); context. enqueue (s); // If the queue is not empty, while (context. cou NT! = 0) {// the first element of the team. V = context. dequeue () as graphvertex; console. writeline (v. symbol); // traverse the V node foreach (graphedge item in v. followedges) {If (item. to. color = vertexcolor. white) {item. to. color = vertexcolor. gray; item. to. distance = v. distance + 1; item. to. parent = V; context. enqueue (item. to) ;}} v. color = vertexcolor. black ;}

}

4. Code reflection

The above search code structure is a typical search structure: first define the queue or stack to saveProgramRunning status. If the container is not empty, extract the elements and perform some processing on the elements.

5. Download Code

/Files/xuqiang/directedgraph1.rar

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.