Parallel Algorithms Combinatorial Search Some Combinatorial Search Methods • Divide and Conquer
Total Page:16
File Type:pdf, Size:1020Kb
Parallel Programming Parallel algorithms Combinatorial Search Some Combinatorial Search Methods • Divide and conquer • Backtrack search • Branch and bound • Game tree search (minimax, alpha-beta) 2010@FEUP Parallel Algorithms - Combinatorial Search 2 Terminology • Combinatorial algorithm: computation performed on discrete structure • Combinatorial search: finding one or more optimal or suboptimal solutions in a defined problem space • Kinds of combinatorial search problem • Decision problem (exists (find 1 solution); doesn’t exist) • Optimization problem (the best solution) 2010@FEUP Parallel Algorithms - Combinatorial Search 3 Examples of Combinatorial Search • Laying out circuits in VLSI • Find the smallest area • Planning motion of robot arms • Smallest distance to move (with or without constraints) • Assigning crews to airline flights • Proving theorems • Playing games 2010@FEUP Parallel Algorithms - Combinatorial Search 4 Search Tree • Each node represents a problem or sub- problem • Root of tree: initial problem to be solved • Children of a node created by adding constraints (one move from the father) • AND node: to find solution, must solve problems represented by all children • OR node: to find a solution, solve any of the problems represented by the children 2010@FEUP Parallel Algorithms - Combinatorial Search 5 Search Tree (cont.) • AND tree • Contains only AND nodes • Divide-and-conquer algorithms • OR tree • Contains only OR nodes • Backtrack search and branch and bound • AND/OR tree • Contains both AND and OR nodes • Game trees 2010@FEUP Parallel Algorithms - Combinatorial Search 6 Divide and Conquer • Divide-and-conquer methodology • Partition a problem into subproblems • Solve the subproblems • Combine solutions to subproblems • Recursive: sub-problems may be solved using the divide-and-conquer methodology • Example: quicksort 2010@FEUP Parallel Algorithms - Combinatorial Search 7 Best for Centralized Multiprocessor • Unsolved subproblems kept in one shared stack • Processors needing work can access the stack • Processors with extra work can put it on the stack • Effective workload balancing mechanism • Stack can become a bottleneck as number of processors increases 2010@FEUP Parallel Algorithms - Combinatorial Search 8 Multicomputer Divide and Conquer • Subproblems must be distributed among memories of individual processors • Two designs • Original problem and final solution stored in memory of a single processor • Both original problem and final solution distributed among memories of all processors 2010@FEUP Parallel Algorithms - Combinatorial Search 9 Design 1 • Algorithm has three phases • Phase 1: problems divided and propagated throughout the parallel computer • Phase 2: processors compute solutions to their subproblems • Phase 3: partial results are combined • Maximum speedup limited by propagation and combining overhead 2010@FEUP Parallel Algorithms - Combinatorial Search 10 Design 2 • Both original problem and final solution are distributed among processors’ memories • Eliminates starting up and winding down phases of first design • Allows maximum problem size to increase with number of processors • Used this approach for parallel quicksort algorithms • Challenge: keeping workloads balanced among processors 2010@FEUP Parallel Algorithms - Combinatorial Search 11 Backtrack Search • Uses depth-first search to consider alternative solutions to a combinatorial search problem • Recursive algorithm • Backtrack occurs when • A node has no children (“dead end”) • All of a node’s children have been explored 2010@FEUP Parallel Algorithms - Combinatorial Search 12 Example: Crossword Puzzle Creation • Given • Blank crossword puzzle • Dictionary of words and phrases • Assign letters to blank spaces so that all puzzle’s horizontal and vertical “words” are from the dictionary • Halt as soon as a solution is found 2010@FEUP Parallel Algorithms - Combinatorial Search 13 Crossword Puzzle Problem Given a blank crossword puzzle and a dictionary ............. find a way to fill in the puzzle. 2010@FEUP Parallel Algorithms - Combinatorial Search 14 A Search Strategy • Identify longest incomplete word in puzzle (break ties arbitrarily) • Look for a word of that length • If cannot find such a word, backtrack • Otherwise, find longest incomplete word that has at least one letter assigned (break ties arbitrarily) • Look for a word of that length • If cannot find such a word, backtrack • Recurse until a solution is found or all possibilities have been attempted 2010@FEUP Parallel Algorithms - Combinatorial Search 15 State Space Tree Root of tree is initial, blank puzzle. Choices for word 1 Choices for word 2 Word 3 choices etc. 2010@FEUP Parallel Algorithms - Combinatorial Search 16 Backtrack Search ? ? ? ? ? ? ? 2010@FEUP Parallel Algorithms - Combinatorial Search 17 Backtrack Search T R ? ? O ? ? ? ? L L E Y 2010@FEUP Parallel Algorithms - Combinatorial Search 18 Backtrack Search T ? R ? C L O S E T S L ? L ? E ? Y ? 2010@FEUP Parallel Algorithms - Combinatorial Search 19 Backtrack Search T ? R ? C L O S E T S L ? L ? E ? Cannot find word. Must backtrack. Y ? 2010@FEUP Parallel Algorithms - Combinatorial Search 20 Backtrack Search T R ? ? O ? ? ? ? L L E Cannot find word. Must backtrack. Y 2010@FEUP Parallel Algorithms - Combinatorial Search 21 Backtrack Search T ? R ? C R O Q U E T L ? L ? E ? Y ? 2010@FEUP Parallel Algorithms - Combinatorial Search 22 Backtrack Search T T R R C R O Q U E T L M ? ? L ? P ? ? E E Y D 2010@FEUP Parallel Algorithms - Combinatorial Search 23 Time and Space Complexity • Suppose average branching factor in state space tree is b • Searching a tree of depth k requires examining bk1 b 1b b2 bk 1 (bk ) b 1 nodes in the worst case (exponential time) • Amount of memory usually required is (k) 2010@FEUP Parallel Algorithms - Combinatorial Search 24 Parallel Backtrack Search • First strategy: give each processor a subtree • Suppose p = bk • A process searches all nodes to depth k • It then explores only one of subtrees rooted at level k • If d (depth of search) > 2k, time required by each process to traverse first k levels of state space tree is negligible 2010@FEUP Parallel Algorithms - Combinatorial Search 25 Parallel Backtrack when p = bk Subtree Subtree Subtree Subtree Searched Searched Searched Searched by by by by Process 0 Process 1 Process 2 Process 3 2010@FEUP Parallel Algorithms - Combinatorial Search 26 What If p bk ? • A process can perform sequential search to level m (where bm > p) of state space tree • Each process explores its share of the subtrees rooted by nodes at level m • As m increases, there are more subtrees to divide among processes, which can make workloads more balanced • Increasing m also increases number of redundant computations 2010@FEUP Parallel Algorithms - Combinatorial Search 27 Maximum Speedup when p bk In this example 5 processors are exploring a state space tree with branching factor 3 and depth 10. 2010@FEUP Parallel Algorithms - Combinatorial Search 28 Disadvantage of Allocating One Subtree per Process • In most cases state space tree is not balanced • Example: in crossword puzzle problem, some word choices lead to dead ends quicker than others • Alternative: make sequential search go deeper, so that each process handles many subtrees (cyclic allocation) 2010@FEUP Parallel Algorithms - Combinatorial Search 29 Allocating Many Subtrees per Process b = 3; p = 4; m = 3; allocation rule (subtree nr) % p == rank 2010@FEUP Parallel Algorithms - Combinatorial Search 30 Backtrack Algorithm cutoff_count – nr of nodes at cutoff_depth cutoff_depth – depth at which subtrees are divided among processes depth – maximum search depth in the state space tree moves – records the path to the current node (moves made so far) p, id – number of processes, process rank Parallel_Backtrack(node, level) if (level == depth) if (node is a solution) Print_Solution(moves) else if (level == cutoff_depth) cutoff_count ++ if (cutoff_count % p != id) return possible_moves = Count_Moves(node) // nr of possible moves from current node for i = 1 to possible_moves node = Make_Move(node, i) moves[ level ] = i Parallel_Backtrack(node, level+1) node = Unmake_Move(node, i) return 2010@FEUP Parallel Algorithms - Combinatorial Search 31 Distributed Termination Detection • Suppose we only want to print one solution • We want all processes to halt as soon as one process finds a solution • This means processes must periodically check for messages • Every process calls MPI_Iprobe every time search reaches a particular level (such as the cutoff depth) • A process sends a message after it has found a solution 2010@FEUP Parallel Algorithms - Combinatorial Search 32 Simple (Incorrect) Algorithm • A process halts after one of the following events has happened: • It has found a solution and sent a message to all of the other processes • It has received a message from another process • It has completely searched its portion of the state space tree 2010@FEUP Parallel Algorithms - Combinatorial Search 33 Why Algorithm Fails • If a process calls MPI_Finalize before another active process attempts to send it a message, we get a run-time error • How this could happen? • A process finds a solution after another process has finished searching its share of the subtrees OR • A process finds a solution after another process has found a solution 2010@FEUP Parallel Algorithms - Combinatorial Search 34 Distributed Termination Problem • Distributed termination problem: Ensuring that • all processes are inactive AND • no