Traversal algorithm for all paths between two points

Source: Internet
Author: User
Tags stack pop

Transferred from: http://blog.sina.com.cn/s/blog_5ff8e88e01013lot.html

These days, the use of this feature, but found on the internet, a piece of information, ideas written very clear, but the code is wrong. Therefore, I just follow this idea, I have compiled the code, now explained as follows: (referring to the former part of the idea, updated part of the code)

Traversal algorithm for all paths between two points

Ocean University School of Information Science and engineering Bear Building Liang Lei

absrtact : Firstly, this paper introduces the depth-first traversal algorithm of graphs, and then, according to the depth-first traversal algorithm of graph, find all the paths between two points in the connected graph and give the code.

keywords : graph, Depth-first traversal, algorithm

Abstract : This Arcicle introduces the Depth-first traversal method,then introduces a algorithm to find all roads from one point toanothe R point in a simple graph, and gives the codes withC + +.

Key Words : Graph , algorithm

First, depth-first traversal (Depth-first traversal)

1. Recursive definition of depth-first traversal of graphs

Suppose that the initial state of a given figure g is that all vertices have never been visited. If you select a vertex in G as the initial starting point (source point), then the depth-first traversal can be defined as follows: first access to the starting point V, and mark it as visited, and then start from V to search for the V of each adjacency point W. If W has not been accessed, the depth-first traversal is continued with W as the new starting point, until all vertices (also known as vertices reachable from the source point) in the graph that have a path to the source point v have been accessed. If you still have an unreachable vertex in the diagram, select another vertex that has not yet been accessed to repeat the process as the new source point until all the vertices in the diagram have been accessed.

The depth-first traversal of a graph is similar to a tree's pre-sequence traversal. The search method used is characterized by searching the depth direction as much as possible first. This search method is called depth-first search (Depth-first searches). Accordingly, traversing a graph with this method is naturally referred to as the depth-first traversal of the graph.

2, the depth of the first search process

Set X is the currently visited vertex, and after the access mark has been made to X, select an undetected Edge (x, y) that departs from X. If vertex y is found to have been visited, re-select an undetected edge from X, otherwise along the edge (x, y) to the never visited Y, access to Y and mark it as visited, and then search from Y until all the paths from y have been searched, that is, after all the vertices that are reachable from Y have been accessed, Back to Vertex x, and then select an undetected edge from X. The above process is until all edges from X have been detected. At this point, if X is not the source point, it goes back to the vertex that was visited before X, otherwise all vertices in the diagram that have a path to the source point (that is, all vertices reachable from the source point) have been accessed, and if figure G is a connected graph, then the traversal process ends, otherwise the selection of a vertex that has not yet been accessed as the new point is made.

Two, the algorithm of finding all paths between two points





Assuming that simple connectivity is shown in Figure 1, then it is shown in the adjacency table storage structure 2. Suppose we want to find all the paths of node 3 to node 6, then we set the node 3 as the starting point and the node 6 as the end point. The storage structure we need is a stack of saved paths, an array to hold the labeled nodes, then the steps to find all the paths to node 3 to node 6 are as follows:

1, we set up a storage node of the stack structure, the starting point 3 into the stack, the node 3 is labeled into the stack state;

2, starting from node 3, find the node 3 of the first non-stack state of the adjacent node 1, the node 1 is marked into the stack state;

3, starting from Node 1, find the node 1 of the first non-stack state of the adjacent node 0, the node 0 is marked into the stack state;

4, starting from node 0, find the node 0 of the first non-stack state of the adjacent node 2, the Node 2 is marked into the stack state;

5, starting from Node 2, find the Node 2 of the first non-stack state of the adjacent node 5, the node 5 is marked into the stack state;

6, starting from node 5, find the node 5 of the first non-stack state of the adjacent node 6, the node 6 is marked into the stack state;

7, stack top node 6 is the end point, then, we found a starting point to the end of the path, output this path;

8, from the top of the stack pop node 6, the 6 is marked as non-in-stack state;

9, now the top node of the stack is 5, the node 5 is not in addition to the end of the non-stack state node, so from the top of the Stack node 5 pops;

10, now the top node of the stack is 2, node 2 in addition to just out of the stack node 5, there are non-stack state node 6, then we will node 6 into the stack;

11, now the top of the stack is node 6, that is, find the second path, output the entire stack, that is, the second path

12, Repeat step 2-11, you can find all the paths from the starting point 3 to the end 6;

13, the stack is empty, the algorithm ends.


Third, the algorithm C + + code is as follows:

#include <iostream>
#include <map>
using namespace Std;

Class node
{
Public
int number;
Node *next;
Node (int a,node *b)
{
Number=a;
Next=b;
}
};

Class stacks
{
Public
node * TOP;
Stacks (node * a=null)
{
Top=null;
}
void push (int a)
{
if (top==null)
Top =new node (a,null);
else top=new node (a,top);
}
void Pop ()
{
Node *b=top;
top=top->next;
Delete B;
}

}; To save a stack that has joined a path node

int cur_node;//The current node, which is the node at the top of the stack.
int next_node=8;//The next adjacency point of the current node, which is the node that just popped from the top of the stack, is initialized to 8.
Map<int,int> map_next;//the next adjacency point for each node, which is the node just popped from the top of the stack.
int start=3;
int end=6;//starts at 3 and ends at 6
Stacks aray[8]={stacks (null), stacks (null), stacks (null), stacks (null), stacks (null), stacks (null), stacks (null), Stacks (NULL)};
Stacks stack (NULL);
int states[8];//An array that holds the state of the node
int main ()
{
Initialize Map_next
for (int i=0;i<=7;i++)
{
Map_next[i] =-1;
}
Aray[0].push (2);
Aray[0].push (1);
Aray[1].push (4);
Aray[1].push (3);
Aray[1].push (0);
Aray[2].push (6);
Aray[2].push (5);
Aray[2].push (0);
Aray[3].push (7);
Aray[3].push (1);
Aray[4].push (7);
Aray[4].push (1);
Aray[5].push (6);
Aray[5].push (2);
Aray[6].push (5);
Aray[6].push (2);
Aray[7].push (4);
Aray[7].push (3);
node* neighbour (int a);//, int b
Stack.push (start);//Put the starting point into the stack
states[start]=1;//mark the starting point as a stack state
while (NULL! = stack.top)//stack is not empty
{
if (stack.top->number==end)
{
cout<< "End";
node* Abc=stack.top;
while (abc->number! = start)
{
cout<<abc->number<< ",";
abc=abc->next;
}
cout << "Start" <<endl;//output the path that was found
Stack.pop ();//Eject the top node of the stack.
states[end]=0;//clears the status of the end point
Map_next[end]=-1;
}
Else
{
cur_node=stack.top->number;
if (neighbour (cur_node) = NULL)//neighbor is not empty
{
Node *d =neighbour (Cur_node);
Map_next[cur_node] = d->number;
cur_node=d->number;
Stack.push (Cur_node);
States[cur_node]=1;
}
Else
{
Stack.pop ();
states[cur_node]=0;
Map_next[cur_node] =-1;
}
}
}
return 0;
}

node* neighbour (int a)//,int b
{
Node *abc=aray[a].top;
while ((NULL!=ABC))//node ABC not empty
{
if (states[abc->number]==1)//is already in the stack stack
{
abc=abc->next;
}
else//not in stack stack.
{
if ( -1 = = Map_next[a])//ABC as the return value
{
while (NULL!=ABC && states[abc->number]==1)
{
ABC = abc->next;
}
Return ABC;

}
else if (Abc->number = = Map_next[a])
{
abc=abc->next;
while (NULL!=ABC && states[abc->number]==1)
{
ABC = abc->next;
}
Return ABC; Return the next node of ABC
}
Else
{
abc=abc->next;
}
}
}
return NULL;
}


Iv. Summary

This algorithm uses the adjacency table storage structure of undirected graphs to find all the paths between two points in the connected graph through depth-first traversal. The algorithm is not complex and has high efficiency. Since the graph can also be stored with adjacency tables, the algorithm is also applicable to the graph.

Five, bibliography

[1] Min, 聯繫. Data structure (C language version) [M]. Beijing: Tsinghua University published by DU, 1997

[2] Rectification C + + object-oriented programming Beijing: Tsinghua University published by DU, 2006

[3] Larry Nyhoff He data structure and algorithm analysis: C + + language description Tsinghua University Press 2006

Traversal algorithm for all paths between two points

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.