0033 algorithm note-branch restriction method and single-source shortest path

Source: Internet
Author: User

1. Branch restriction Method

(1) Description:UseBreadth FirstThe node that generates the state space tree and uses the pruning function calledBranch limit method.

The so-called"Branch"Is to use the breadth-first policy to generate all branches of the extended node in sequence (I .e., the son node ).
The so-called"Restriction"In the process of node expansion, the upper bound (or lower bound) of the node is calculated, and some branches of the search tree are removed while searching, thus improving the search efficiency.

(2) Principles:According to the principle of breadth first, once a dynamic node becomes an extended node (e-node) R, the algorithm will generate all its child nodes in sequence, remove the sons that cause infeasible or non-optimal solutions, and add the other sons to the dynamic knots table. Then, a node is taken from the active node table as the current extension node. Repeat the above node expansion process until the problem is resolved or no solution is found.

(3) Branch restriction and backtracking

1) solution objective: the solution objective of the backtracking method is to find out the solutions that meet the constraints in the spatial tree.All solutionsWhile the goal of the branch limit method is to find outA solution that meets the constraintsOr find the optimal solution in a certain sense in the solution that meets the constraints. 
2) different search methods: the Backtracking Method UsesDepth firstThe method to search for the solution space tree, and the branch restriction rule isBreadth FirstOr useMinimum Cost first.

(4) Common branch Restriction Methods

1) FIFO branch restriction method (queue branch restriction method)

Basic Idea: select the next active node as the expansion node based on the first-in-first-out (FIFO) Principle of the queue.

Search policy: At the beginning, the root node is the only dynamic node, and the root node enters the queue. After the root node is extracted from the dynamic node team, it serves as the current extended node. For the current extended node, all its sons are generated from left to right, and all the sons meeting the constraint function are added to the dynamic node queue by checking the constraints. Then retrieve the first node of the team from the dynamic node table (the most advanced node in the Team) as the current expansion node ,......, Until an empty solution or dynamic node queue is found.

2) LC (least cost) Branch restriction method (priority queue branch restriction method)

Basic Idea: To accelerate the search process, we should effectively select dynamic nodes for expansion. Select the node with the highest priority as the current expansion node according to the priority set in the priority queue.

Search policy: Calculate a priority (function value of some information) for each dynamic node, and select the highest priority (most favorable) from the current dynamic node table) as an extension node, the search proceeds toward the branch where the solution tree has the optimal solution, so as to quickly find an optimal solution. The next highest priority node in the active node table is the current extended node ,......, Until an empty solution or dynamic node queue is found.

(5) search application example using the branch restriction Method

 1) 0-1 backpack problem. When n = 3, W = {16, 15, 15}, P = {45, 25, 25}, c = 30

Queue-based branch restriction method (processing rule: first-in-first-out): {}-> {A}-> {B, c}-> {c, d, e }(D Is An Infeasible solution, discard)-> {C, e}-> {e, f, g}-> {f, g, j, k }(J is An Infeasible solution, discard)-> {F, g, k}-> {g, k, l, m}-> {K, L, M, N, O}-> {}

Priority queue-based branch restriction method (processing rule: greater value preferred): {}-> {A}-> {B, c}-> {c, d, e}-> {c, e}-> {C, j, k}-> {c}-> {f, g}-> {g, l, m}-> {g, m}-> {g}-> {N, O}-> {o}-> {}

2) traveler goods sales problems

Queue-based branch restriction method (Node B starts): {}-{B}-{c, d, e}-{d, e, f, g}-{e, f, G, H, I}-{F, G, H, I, J, K}-{G, H, I, J, K, L}-{H, i, J, K, L, M}-{I, J, K, L, M, N}-{J, K, L, M, N, o}-{K, L, M, N, O, P}-{L, M, N, O, P, Q}-{M, N, O, P, q}-{N, O, P, Q}-{o, p, q}-{P, Q}-{q }-{}

Priority queue branch restriction method: the current cost of the node is priority: {}-{B}-{c, d, e}-{c, d, J, k}-{C, J, K, h, I}-{C, J, K, I, n}-{C, K, I, n, p}-{C, I, N, P, Q}-{C, N, P, Q, O}-{C, p, q, O}-{C, q, O}-{q, O, F, G}-{q, O, G, l}-{q, O, l, m}-{o, l, m}-{o, m }-{}

2. Single-source shortest path problem

Problem description

In the given directed graph G, each side has a non-negative edge weight. The shortest path between the Source Vertex s and the target vertex t of graph G is required.

It is the solution space tree generated by the single-source shortest path problem of Directed Graph G solved by the priority queue branch restriction method. The number next to each node indicates the current route length of the node.

Algorithm Design

The algorithm starts from the Source Vertex s of graph G and the empty priority queue. After node S is expanded, its son node is inserted into the heap sequentially. After that, the algorithm extracts the node with the minimum current path length from the heap as the current extension node, and checks all vertices adjacent to the current extension node in sequence. If edges are reachable from the current extended node I to vertex J and the path from vertex I to vertex J is smaller than the current optimum path length, insert the vertex into the active node priority queue as the active node. The expansion process of this node continues until the active node priority queue is empty.

In the process of algorithm node expansion,Once it is found that the lower bound of a node is no less than the shortest path length found, the algorithm deletes the subtree with the node as the root..
In the algorithm, the inter-node control relationship is used for pruning. Starting from the Source Vertex s, two different paths reach the same vertex of graph G. Because the path lengths of the two paths are different, you can cut the node in the tree corresponding to the long path as the root subtree.

The algorithm code is as follows:

1. minheap2.h


#include <iostream>template<class Type>class Graph;template<class T> class MinHeap { template<class Type>friend class Graph;public: MinHeap(int maxheapsize = 10); ~MinHeap(){delete []heap;} int Size() const{return currentsize;} T Max(){if(currentsize) return heap[1];} MinHeap<T>& Insert(const T& x); MinHeap<T>& DeleteMin(T &x); void Initialize(T x[], int size, int ArraySize); void Deactivate(); void output(T a[],int n);private: int currentsize, maxsize; T *heap; }; template <class T> void MinHeap<T>::output(T a[],int n) { for(int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl; } template <class T> MinHeap<T>::MinHeap(int maxheapsize) { maxsize = maxheapsize; heap = new T[maxsize + 1]; currentsize = 0; } template<class T> MinHeap<T>& MinHeap<T>::Insert(const T& x) { if(currentsize == maxsize) { return *this; } int i = ++currentsize; while(i != 1 && x < heap[i/2]) { heap[i] = heap[i/2]; i /= 2; } heap[i] = x; return *this; } template<class T> MinHeap<T>& MinHeap<T>::DeleteMin(T& x) { if(currentsize == 0) { cout<<"Empty heap!"<<endl; return *this; } x = heap[1]; T y = heap[currentsize--]; int i = 1, ci = 2; while(ci <= currentsize) { if(ci < currentsize && heap[ci] > heap[ci + 1]) { ci++; } if(y <= heap[ci]) { break; } heap[i] = heap[ci]; i = ci; ci *= 2; } heap[i] = y; return *this; } template<class T> void MinHeap<T>::Initialize(T x[], int size, int ArraySize) { delete []heap; heap = x; currentsize = size; maxsize = ArraySize; for(int i = currentsize / 2; i >= 1; i--) { T y = heap[i]; int c = 2 * i; while(c <= currentsize) { if(c < currentsize && heap[c] > heap[c + 1]) c++; if(y <= heap[c]) break; heap[c / 2] = heap[c]; c *= 2; } heap[c / 2] = y; } } template<class T> void MinHeap<T>::Deactivate() { heap = 0; } 

2. 6d2. cpp



// Solve the single-source shortest path problem by using the branch restriction method # include "stdafx. H "# include" minheap2.h "# include <iostream> # include <fstream> using namespace STD; ifstream fin (" 6d2.txt "); template <class type> class graph {friend int main (); Public: void shortespaths (INT); Private: INTN, // Number of vertices of graph G * Prev; // type ** C, // The receiving matrix * Dist of graph G; // the Shortest Path array }; template <class type> class minheapnode {friend graph <type>; public: Operator int () const {return length ;} PRIVATE: int I; // vertex number type length; // current path length}; Template <class type> void graph <type >:: shortespaths (INT V) // The priority queue branch restriction Method for Single-source shortest path problem {minheap <minheapnode <type> H (1000); minheapnode <type> E; // define the source as the initial extension node E. I = V; E. length = 0; Dist [v] = 0; while (true) // space for solving the search problem {for (Int J = 1; j <= N; j ++) if (C [E. i] [J]! = 0) & (E. length + C [E. i] [J] <Dist [J]) {// vertex I is reachable to vertex J and satisfies the control constraint Dist [J] = E. length + C [E. i] [J]; Prev [J] = E. i; // Add minheapnode to the active node priority queue <type> N; N. I = J; N. length = DIST [J]; H. insert (n);} 'try {H. deletemin (E); // remove an extension node} catch (INT) {break;} If (H. currentsize = 0) // The priority queue is null {break ;}} int main () {int n = 11; int Prev [12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int Dist [12] = {,}; cout <"the adjacent matrix of a single source image is as follows: "<Endl; int ** c = new int * [n + 1]; for (INT I = 1; I <= N; I ++) {c [I] = new int [n + 1]; for (Int J = 1; j <= N; j ++) {fin> C [I] [J]; cout <C [I] [J] <";}cout <Endl ;}int v = 1; graph <int> G; G. N = N; G. C = C; G. dist = DIST; G. prev = Prev; G. shortespaths (V); cout <"the shortest path length from S to T is:" <Dist [11] <Endl; For (INT I = 2; I <= N; I ++) {cout <"Prev (" <I <") = "<Prev [I] <" "<Endl ;}for (INT I = 2; I <= N; I ++) the shortest path length of {cout <"from 1 to" <I <"is:" <Dist [I] <Endl ;}for (INT I = 1; I <= N; I ++) {Delete [] C [I];} Delete [] C; C = 0; return 0 ;}

Program running result



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.