Graph representation, depth-breadth traversal algorithm and its application

Source: Internet
Author: User

All the objects of the world can be transformed into nodes; all relations in the world can be transformed into a line between nodes, thus forming a graph like fleeting dream. The future of global must be the world of graphs.

First, the representation of the figure

The graph has the direction graph and the non-direction graph, the representation method generally has the adjacency table, the adjacency matrix and so on, the non-direction graph and the direction graph both can use these two methods to represent.

Figure 1. Example of a figure [1]

1. adjacency table

In the adjacency table, for each vertex u, use a list to string all the points adjacent to U and mark the set as adj (u). Give me a chestnut as follows:

Figure 2. Example of an adjacency table representation diagram [1]

In the real operation diagram of the experiment, usually also use the adjacency matrix, for example, to store the directed graph in Figure 1, you can directly use a CSV or TXT file, stored content as follows:

5,141,3,2,5,42,3,1,5,33,2,2,44,3,1,5,35,3,4,1,2

The above file is the first row stores the total number of nodes, the number of edges, and the next line is the ID of this vertex and the ID of the neighbor, and the 2nd digit stores the total number of neighbors nodes of the node; For further operation, the original node ID can be mapped from 0 to N, and no edge nodes are 0 added. This time the line number and ID are equal, and the access is faster. The advantage of this storage is that it takes less memory to complete and is easy to operate.

2. Adjacency Matrix

The adjacency matrix, as its name implies, is a n*n matrix that stores the relationships between nodes, spatial complexity $o (N^2) $, which is easier to store in some matrix operations, such as transpose. The adjacency matrix of the two graphs in Figure 1 is as follows:


Figure 3. Example of an adjacency matrix representation diagram [1]

Second, the graph travel algorithm 1, breadth first traversal

The breadth traversal of a graph is, as the name implies, a time to access the nodes in the graph, with precedence over breadth, that is, when a node A is accessed, the access order of all neighboring nodes of a is not required, and the order of access is defined in the specific question. Then continue to visit the neighbor's neighbor and look directly at the code:

#过程中主要是做三件事:颜色、距离、父节点BFS(G(V,E),s)    #初始化    for each vertex u in V-{s}        color[u] <- White        d[u]<-∞        pai[u]<-None    endfor    #处理开始节点    color[s]<-Gray    d[s]<-0    pai[s]<-None        #初始化队列    Q<-Queue()    Enqueue(Q,s)        #开始广度访问图    while not Q.empyt():        u<-Dequeue(Q)        for each v in Adj[u]:            if color[v] = White #检查颜色是否为白色,即没有被访问过                color[v] <- Gray #颜色,变颜色为灰色                d[v]<-d[u]+1 #距离                pai[v]<-u  #父节点            endif        endfor    color[u]<-Black    endwhile    End    

Application:
Non-direction graph two coloring problem, no odd circuit <=> can be two coloring <=> can be divided into two parts diagram

2. Depth-First traversal

The depth-first search is accessed from a vertex, and then accesses his neighbor, unlike BFs, when a depth-first search accesses his neighbor from a vertex, it chooses only one of them that has not been visited by the neighbor V, and then temporarily abandons the other neighbor of U, while the new son node v accesses the neighbor of V. When access to V departs is complete, DFS returns U, then accesses the second neighbor of U, and so on. There is an animation here, but I can't draw it.
Depth-first traversal, pseudo-code for iterative methods:

DFS(G(V,E))    #初始化访问控制的颜色,父节点    for each vetex u in V        color[u] = White        pai[u]=None    endfor    #最开始,时间为0    time <- 0        #对于每个节点进行一次深度优先遍历,防止图节点间不连通    for each vetex u in V:        if color[u]=While:            then DFS-visit(u)        endif    endfor    End#对节点s,进行深度优先遍历DFS-Visit(s):    color[s] <- Gray    time = time + 1    d[s]<-time    #如果访问到节点s的邻居节点,那么对其邻居节点迭代进行深度优先遍历    for each v in Adj[s]:        if color[v] = White            pai[v]<-s            DFS-Visit(v)        endif    endfor    color[s]<-Black    f[s]<-time<-time+1    End
2.1 interval set theorem: 2.2 White path theorem: 2.3 Topology sequencing: 2.4 The longest path problem in a non-loop, forward graph:

Some application problems need to find the shortest or longest simple path in the graph (without the path of the loop), the graph is often weighted. But for any intention, finding the longest path is the NPC problem, even if the graph is not weighted . However, only the graph is a non-circuit diagram, whether it is a graph or a graph, weighted or not weighted graph, can be in linear time $o (| v|+| e|) =o (n+m) $. Careful I may have found that no circuit diagram is a tree or a forest ah , two vertices are either not connected, or there is only one path.

2.5 Strong connected branches:

Definition 1: If any vertex in an undirected graph has a path to any other vertex, then this graph is called a strongly connected graph (strongly connected graph)
Definition 2: a directed graph G, the implicit graph $G ' $ ' means the direction of each side of G is removed after the resulting non-directed graph.
Definition 3: If an undirected graph $g ' $ that is implied by the graph g is a connected graph, then the graph G is called a weakly connected graph (weakly connected graph)
Definition 4: If the sub-diagram of a forward graph is a strongly connected graph, it becomes a strongly connected sub-graph (strongly connected subgraph)
Definition 5: If the strongly connected sub-graph of a forward graph is the largest, i.e. it cannot join any other vertex and is still strongly connected, then this sub-graph is called the strongly connected branch (strongly connected component)
Here the strongly connected branches are contained within the strongly connected sub-graph.
Definition 6: A strongly connected branching problem of a forward graph is the partitioning of a vertex of a graph into a number of strongly connected branches that do not intersect.

Iii. Reference Documents

[1] Shen Xiaojun. Computer Algorithm Fundamentals: Essentials of Computer algorithms[m]. Mechanical industry press, 2014.

Graph representation, depth-breadth traversal algorithm and its application

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.