Basic Graph algorithms (breadth-first search and depth-first search)

Source: Internet
Author: User

Graph algorithms are vital to computer science. Hundreds of computing problems can eventually be reduced to graph theory problems. This article mainly sorts out the learning notes of graph algorithms in introduction to algorithms.

First, we will unify some expressions in the graph algorithm:Given the graph G = (V, E), when the runtime of an algorithm on the graph is expressed, we usually use the number of knots | V | and number of edges | E | as the input size. In addition, we use G.V to represent the node set of graph G and G.E to represent the edge set of graph G. That is to say, we regard the node and edge as the attributes of the graph.

Graph Representation

For Figure G = (V, E), two standard representation methods can be used. One representation is the combination of graphs as the adjacent linked list, and the other is the view of graphs as the adjacent matrix.

Adjacent linked list

An adjacent linked list is an array consisting of | V | linked lists. Each node has a linked list. For each node u, the adjacent linked list Adj [u] contains all nodes connected to the node u with edges. The adjacent linked list has an advantage in the representation of sparse graphs. A potential defect of the adjacent linked list is that you cannot quickly determine whether an edge (u, v) is an edge in the graph. The only way is to search for node v in the adjacent linked list (Adj [u. The adjacent matrix overcomes this defect, but the cost is higher storage space consumption.

Adjacent matrix

For the representation of the adjacent matrix, we usually compile the nodes in the graph G as 1, 2 ,...... , | V |, which can be any number. After numbering, the graph G's Adjacent matrix represents A | V | * | V | matrix A = (aij), which meets the following conditions:

Aij = 1 if (I, j) belongs to E; otherwise, aij = 0.

An example of two types of Graph Representation is as follows, where (B) is a linked list and (c) is a matrix representation.


Extended search

Breadth-first search is one of the simplest graph search algorithms and is also the prototype of many important graph algorithms. Prim's Minimum Spanning Tree Algorithm and Dijkstra's single-source shortest path algorithm both use the same idea of breadth-first search. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + o7o8L3A + signature/Signature + signature + O009S0veG143O1vcO/uPa/ybW9tO + 94 bXjtcS + signature + rPJ0ru/Signature/Lqsy/nT0L/fingerprint Authorization/authorization + 73hteOhozwvcD4KPHA + 1Nq547bI08XPyMvRy/e5/authorization/Iy9HL98vjt6jL + bzGy + Oz9rXEtNNztb20 73 XWrrzktcS + 4MDroaM8L3A + runtime + PHByZSBjbGFzcz0 = "brush: java;"> BFS (G, s) for each vertex u ε G.V-{s} u. color = WHITEu. d = infu. front = NILs. color = Grays. d = 0s. front = NILQ = empty set ENQUEUE (Q, s) while Q! = Empty set u = DEQUEUE (Q) for each v ε G. adj [u] if v. color = WHITEv. color = Grayv. d = u. d + 1v. front = uENQUEUE (Q, v) u. color = BLACK
Deep Priority Search

Deep Priority Search always explores the starting edge of node v that has been discovered recently, until all the starting edges of the node are discovered. Once all the start edges of node v are found, the search goes back to the start edges of node v to search for the start edges of the source node. This process continues until all nodes that can be reached from the source node are discovered. If no node is found, the deep-Priority Search selects one of the undiscovered nodes as the new source node and repeats the same search process.

Different from the breadth-first search, the breadth-first subgraphs form a tree, while the depth-first subgraphs may consist of multiple trees, because the search may be repeated from multiple source nodes. These subgraphs form a deep priority forest.

The depth Priority Algorithm stamps each node with a timestamp. Each node v has two timestamps, the first timestamp v. d record the time when node v was first discovered, and the second v. f records the time when the search completes the scanning of the adjacent linked list of v.

PseudocodeAs follows:

DFS(G)for each vertex u∈ G.Vu.color = WHITEu.front = NILtime = 0for each vertex u∈ G.Vif u.color == WHITEDFS-VISIT(G,u)DFS-VISIT(G,u)time = time+1u.d = timeu.color = Grayfor each vertex v∈G:Adj[u]if v.color == WHITEv.front = uDFS-VISIT(G,V)u.color = BLACKtime = time+1u.f = time

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.