Graph algorithms - just "five steps" to get all paths between two nodes (non-recursively)
In implementing the "map" data structure, experience " get all paths between two points is " The algorithmic problems, most of the information online are achieved (see reference to the end of the article) recursive algorithm.
We know that it is easy to make the call stack overflow in JS using recursive algorithm, in order to be able to use in a production environment, you must use to achieve a non-recursive manner.
After some exploration, ideas realized mainly from the article " seek between two points traversal algorithm of all paths ", but the paper did not give a specific implementation details, so you need to achieve; and ultimately of this paper, similar to the " Algorithms - Scheduling field algorithm (Shunting Yard Mathimatics-Numerical algorithms) "mentioned in dual-stack to complete.
1, the algorithm process
In FIG calculated by the following example, the node 3 to 6-node all the paths of all possible paths to 8:
allpath
Specifically, we talk about how to get this process eight paths.
First prepared two stacks, it is called the main stack and the auxiliary stack :
- Main Stack : Each element is a single node (Vertex) , for storing the node on the current path;
- Auxiliary Stack : Each element for storing a corresponding element of the main stack neighbor node list (the Array Vertex) ; stack is used to assist the main stack having a length and a main stack of the same;
1 the STEP : built Stack
The v3
( Node 3 ) into the main stack, while the v3
adjacent node of the node list [v1, v7]
into the auxiliary stack:
First built in the stack
The main stack and the auxiliary stack pushed so that the stack length increase, I personally called to build the stack (build stack)
2 the STEP : to continue to build the stack
After the construction of the stack, we see the auxiliary stack, the stack is its node list [v1, v7]
:
View top of the stack
We extracted the first element of the list of nodes v1
, which is pressed into the main stack; while the remaining list of nodes [v7]
again pressed back auxiliary stack:
Meanwhile queries adjacent node list v1 is [v3, v0], due v3 node is already in the main stack, the need from this list excluded (this step is very important), will be excluded node list after [v0] pressed into the auxiliary stack :
This step also allows the main stack and the auxiliary stack length increased, it is also built stacks (build stack) process
Step 3: cut the stack
Continue to Step 2 of the process to build the stack until our main stack stack v7, this time auxiliary stack top of the stack is empty list []:
Since the auxiliary stack top of the stack is empty list [], so I can not continue to build the stack - this indicates that this path come to an end have not found the target node v6.
The situation went to that being the case, we need to start back to back, look at the other fork in the road had come.
We will v7 main stack stack of pop, but also the auxiliary stack empty list [] pops up:
This action causes the auxiliary stack and the main stack to reduce the length of the cutting process I personally call stack (cutdown stack).
Step 4: Get a first path
Repeat the above Step 2, Step 3, take the strategy:
- As long as the auxiliary stack stack is non-empty list, we will build stack
- As long as the auxiliary stack top of the stack is empty list, we will cut the stack
The top node until the main stack is the target node v6:
We were here, we stopped to see it for yourselves, found that the main contents of the stack is already a complete path from v3 to v6 of:
We output current stack array: [ 'v3', 'v1', 'v0', 'v2', 'v5', 'v6'], the array will represent v3 -> v1 -> v0 -> v2 -> v5 -> v6 this path.
Carried out so far, we finally get a path from v3 to v6 of.
We should drum for their efforts a palm, has seen the dawn of victory; next add a simple loop will be able to get all of the paths.
Step 5: Get all paths
Repeat Step 2 - Step 4 steps, take the following strategies:
- As long as the auxiliary stack stack is non-empty list, we will build stack
- As long as the auxiliary stack top of the stack is empty list, we will cut the stack
- As long as the main stack top of the stack is the target node, we output path, at the same time cutting stack
The process is repeated until the main stack is empty.
As the construction of the stack (build stack) and cut stack (cutdown stack) process, primary and secondary stack stack constantly changing, in this process of change we can continue to get the path from v3 to v6, and ultimately you can get all the paths.
2, code implementation
2.1, pseudocode
According to the above described procedure, it is the aspect of the text into pseudo-code:
BEGIN
初始化主栈
初始化辅栈
首次建栈
WHILE 主栈不为空 THEN
获取辅栈栈顶,为邻接节点列表
IF 邻接节点列表不为空 THEN
获取邻接节点列表首个元素
将该元素压入主栈,剩下列表压入辅栈
建栈
ELSE
削栈
CONTINUE
END IF
IF 主栈栈顶元素 === 目标节点 THEN
获取一条路径,保存起来
削栈
END IF
END WHILE
END
We do not take the above example is a directed graph, in fact, this algorithm is also suitable for a directed graph.
to sum up
In the recent review "map" this data structure, and gradually try to write code in the process to achieve a algorithm. Experience can get knowledge only after their own thinking and summarized in order to lay the foundation for subsequent mastery.
In the study summarized in this article, there are two experience a more profound impression:
- Use recursive problem can be solved, can generally be circulating + stack (Stack) way to solve.
- When not know how algorithm, more suitable for inductive learning method summary, which is to start gradually from simple demo scene, and so on and then set out to achieve groped after which the law.