019-dfs.bfs-Graph Traversal-"algorithmic design techniques and analysis" M.H.A study notes

Source: Internet
Author: User

Depth-First Search Dfs Deep Search Framework:

BOOL Dfs (int loc) {    Tagged state loc has been visited;    if (Loc is the target State) return true;    For (each possible operation) {a         new state is generated for the LOC application operation Nstat;         if (Nstat is valid and not accessed) {if (Dfs (NSTAT)) return true;         }    }    Revoke the LOC visited tag; This step to the specific problem specific analysis of    return false;}


Breadth First search BFS implementation method

1. First put the root node in the queue.

2. Remove the first node from the queue and verify that it is the target.

· If the target is found, end the search and return the result.

· Otherwise, it joins all the direct child nodes that have not yet been tested to the queue.

3. If the queue is empty, the whole picture is checked-that is, there is no target to search for. End Search and return " target not found ".

4. Repeat step 2.

Broad search Framework:

<pre name= "code" class= "CPP" >bool bfs (int init) {    que.enque (init);    while (queue que is not empty) {          int loc = Que.front ();   Que.deque ();          if (Loc is the target State) return true;          For (each possible operation) {a                new state is generated for the LOC application operation Nstat;                if (Nstat valid and not queued) {tag nstat is queued; Que.enque (Nstat);}}    return false;}


Add: Simplify your code:

In a class of search board, search map, search matrix position problem we will frequently encounter "from the current point to get neighbor" operation, in order to shorten the code, we can open a direction array dir[][2] for the direction, in 4 Direction for example, Then Dir[4][2] = {{0, 1}, {1, 0}, {0, 1}, {-1, 0}};

When used, there are:

for (int i = 0; i < 4; ++i) {int newx = oldx + Dir[i][0];int newy = OldY + dir[i][1];if (Newx,newy is legal and not accessed)//BFS or DFS phase Off Operation}

Record path:Guangzhou Search:

If the problem requires us to record the solution path, then for the wide search:

The struct tstat{int value;int pre;//records the position of its parent state in the queue, initialized to-1;};

Each time the new state is expanded, the pre of the new state is the subscript of the current state in the queue, and the solution path can be obtained by checking back to the first state after the target state is reached.

Deep Search:

For deep search, you can also open a stack to record the current legal state, each access to the new state before the current state into the stack, backtracking also corresponding to pop up the top of the stack, eventually the stack will be reversed to record the nodes on the path. The general practice is as follows:

stack<int> stk;bool dfs (int loc) {    Tagged state loc has been visited;    Stk.push (Loc);    For (each possible operation) {a         new state is generated for the LOC application operation Nstat;         if (Nstat valid and not accessed) {if (Dfs (NSTAT)) return true;         }    }    Stk.pop ();    Revoke the LOC visited tag;    return false;}

019-dfs.bfs-Graph Traversal-"algorithmic design techniques and analysis" M.H.A study notes

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.