Traveling Salesman Problem Parallel Distributed Tree Search

Similar documents
Tree Search for Travel Salesperson Problem Pacheco Text Book Chapt 6 T. Yang, UCSB CS140, Spring 2014

LECTURE 17 GRAPH TRAVERSALS

CSCE 350: Chin-Tser Huang. University of South Carolina

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

More NP-complete Problems. CS255 Chris Pollett May 3, 2006.

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

Graph definitions. There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs. An undirected graph

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Practice Final Exam 1

Chapter 9 STACK, QUEUE

L21: Putting it together: Tree Search (Ch. 6)!

Lecture 26: Graphs: Traversal (Part 1)

CSE 417 Branch & Bound (pt 4) Branch & Bound

CSE 100: GRAPH ALGORITHMS

TIE Graph algorithms

L20: Putting it together: Tree Search (Ch. 6)!

NP-complete Reductions

Dynamic Programming II

Outline. Graphs. Divide and Conquer.

Breadth First Search. cse2011 section 13.3 of textbook

- Logic and Algorithms - Graph Algorithms

Scribes: Romil Verma, Juliana Cook (2015), Virginia Williams, Date: May 1, 2017 and Seth Hildick-Smith (2016), G. Valiant (2017), M.

Graphs & Digraphs Tuesday, November 06, 2007

IV/IV B.Tech (Regular) DEGREE EXAMINATION. Design and Analysis of Algorithms (CS/IT 414) Scheme of Evaluation

Randomized Algorithms

Effective Tour Searching for Large TSP Instances. Gerold Jäger

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

Advanced Data Structures and Algorithms

Module 6 NP-Complete Problems and Heuristics

implementing the breadth-first search algorithm implementing the depth-first search algorithm

INSTITUTE OF AERONAUTICAL ENGINEERING

Communication Cost in Parallel Computing

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

Seach algorithms The travelling salesman problem The Towers of Hanoi Playing games. Comp24412: Symbolic AI. Lecture 4: Search. Ian Pratt-Hartmann

UNIT 5 GRAPH. Application of Graph Structure in real world:- Graph Terminologies:

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms

Notes for Lecture 24

9/17/2015 7:56 AM. CSCE 625 Programing Assignment #1 due: Tues, Sep 22 (by start of class) Objective

Parallel Programming with MPI and OpenMP

Graphs. Motivations: o Networks o Social networks o Program testing o Job Assignment Examples: o Code graph:

Graph: representation and traversal

Algorithm Design Techniques (III)

Module 6 P, NP, NP-Complete Problems and Approximation Algorithms

TIE Graph algorithms

The goal of this assignment is to implement and test Breadth-first search (BFS) on a simple navigation problem (path-finding).

Overview. H. R. Alvarez A., Ph. D.

Graphs and Algorithms

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

12 Abstract Data Types

Unit-5 Dynamic Programming 2016

Exam Sample Questions CS3212: Algorithms and Data Structures

Chapter 9 Graph Algorithms

SELF-BALANCING SEARCH TREES. Chapter 11

Copyright 2010, Elsevier Inc. All rights Reserved

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

Branch and Bound. Live-node: A node that has not been expanded. It is similar to backtracking technique but uses BFS-like search.

Lecture 3. Brute Force

Lecture 10. Graphs Vertices, edges, paths, cycles Sparse and dense graphs Representations: adjacency matrices and adjacency lists Implementation notes

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Chapter 9 Graph Algorithms

Computational problems. Lecture 2: Combinatorial search and optimisation problems. Computational problems. Examples. Example

Algorithms and Complexity

Assignment No 2 (Group B)

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Introduction to Computer Science and Programming for Astronomers

Module 11: Additional Topics Graph Theory and Applications

Sample Solutions to Homework #4

Graph. Vertex. edge. Directed Graph. Undirected Graph

Solutions to Problem Set 1

L3 Network Algorithms

56:272 Integer Programming & Network Flows Final Exam -- December 16, 1997

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

Data Structures and Algorithms Key to Homework Assignment 8

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

Floating Point Instructions

Backtracking and Branch-and-Bound

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

Brute Force: Selection Sort

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Introduction to Graph Theory

Parallel Traveling Salesman. PhD Student: Viet Anh Trinh Advisor: Professor Feng Gu.

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

A constant-factor approximation algorithm for the asymmetric travelling salesman problem

Understand graph terminology Implement graphs using

Design and Analysis of Algorithms

Dynamic programming. Trivial problems are solved first More complex solutions are composed from the simpler solutions already computed

Graph Algorithms Using Depth First Search

All-Pairs Shortest Paths - Floyd s Algorithm

Storage capacity of labeled graphs

Design and Analysis of Algorithms CS404/504. Razvan Bunescu School of EECS.

Questions... How does one show the first problem is NP-complete? What goes on in a reduction? How hard are NP-complete problems?

CS 2412 Data Structures. Chapter 9 Graphs

Topologies. Ned Nedialkov. McMaster University Canada. SE 4F03 March 2016

Introduction to Algorithms

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

Graphs. Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale Room - Faner 3131

Chapter 6 Backtracking Algorithms. Backtracking algorithms Branch-and-bound algorithms

Transcription:

Traveling Salesman Problem Parallel Distributed Tree Search Ned Nedialkov Dept. of Computing and Software McMaster University, Canada nedialk@mcmaster.ca March 2012

Outline Traveling salesman problem (TSP) Recursive depth-first search (DFS) Iterative DFS Parallelizing tree search Dynamic mapping Some MPI issues For more details, see Chapter 6, section 2 in the textbook Ned Nedialkov, McMaster University, 2012 2/24

Traveling Salesman Problem (TSP) Given a set of cities and the distances between them, determine the shortest path starting from a given city, passing through all the other cities and returning to the first city Symmetric TSP: the distance between two cities is the same in both directions TSP can be modeled as undirected weighted graph vertices denote cities edges connect them, and the weight of an edge is the distance between two cities Asymmetric TSP: paths may not exist in both directions, or the distances might be different in opposite directions. We have a directed graph Can arise e.g. with one-way streets or say air fares that are different in opposite directions Ned Nedialkov, McMaster University, 2012 3/24

This is NP-hard problem; no polynomial type algorithm exists We study how to do parallel distributed tree search Example 0,0 1 0 1, 1 0 2,3 0 3,8 7 0 8 3 1 5 4 6 18 1 2 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3,13 0 3 1,12 0 3 2, 20 3 12 2 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 10 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 (Figures 6.9 and 6.10 from P. Pacheco, An Introduction to Parallel Programming) Ned Nedialkov, McMaster University, 2012 4/24

Tree search Start at the origin, here city 0 Do depth-first search maintain the current best tour, that is minimum cost if a node is reached with cost larger than current minimum cost, do not go deeper 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1,21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0,34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 For example, going 0 2 1, we have cost 21 at 1, and we can stop, as 0 1 2 3 0 has cost 20 Ned Nedialkov, McMaster University, 2012 5/24

Serial depth-first search Cities are numbered 0,1,...,n 1 A tour contains number of cities, the cities in the tour, and the cost of it number of cities is citycount(tour) Initially, tour contains the first city 0 and cost 0 besttour(tour) checks if this is the best tour so far updatebesttour(tour) updates the best tour feasible (tour,city) checks if city has been visited, and if not, if it can be added to tour so that cost up to city < cost(best tour) add(tour, city) adds city to tour; city must be feasible removelast(tour, city) removes last city from tour We consider recursive and iterative depth first searches Ned Nedialkov, McMaster University, 2012 6/24

Recursive DFS Algorithm (Recursive DFS) DepthFirstSearch(tour) if citycount(tour) = n if besttour(tour) updatebesttour(tour) else for each neighboring city of last city in tour if feasible (city,tour) addcity(tour, city) DepthFirstSearch(tour) removelast(tour, city) Ned Nedialkov, McMaster University, 2012 7/24

Iterative DFS. Version I Use stack to avoid recursive calls push(stack,city) pushes city onto stack pop(stack) pops city from stack Use 1 to recognize when all children of a node are visited Stack contains only feasible cities, or 1 to mark the beginning of children nodes Ned Nedialkov, McMaster University, 2012 8/24

Algorithm (Iterative DFS. Version I) for city n 1 downto 0 push(stack, city) while stack is non empty city pop(stack) if city= 1 % no children to visit removelast(tour,city) else add(tour, city) if citycount(tour) = n if besttour(tour) updatebesttour(tour) removelast(tour,city) else push(stack, 1) % mark beginning of children list for b = n 1 downto 1 if feasible (tour,b) push(stack, b) Ned Nedialkov, McMaster University, 2012 9/24

Example 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 stack n 1,...,1,0, tour empty After first iteration: city = 0 tour = 0,0 stack n 1,...,1, 1,3,2,1 After second iteration: city = 1 tour = 0 1,1 stack n 1,...,1, 1,3,2, 1,3,2 After third iteration: city = 2 tour = 0 1 2,3 stack n 1,...,1, 1,3,2, 1,3, 1,3 Ned Nedialkov, McMaster University, 2012 10/24

Example 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 After third iteration: city = 2 tour = 0 1 2,3 stack n 1,...,1, 1,3,2, 1,3, 1,3 After fourth iteration: city = 3 tour = 0 1 2 3,20 best tour = 0 1 2 3 0,20 tour = 0 1 2,3 stack n 1,...,1, 1,3,2, 1,3, 1 After fifth iteration: city = 1 tour = 0 1,1 stack n 1,...,1, 1,3,2, 1,3 Ned Nedialkov, McMaster University, 2012 11/24

Iterative depth first search. Version II Push partial tours onto stack Code closer to recursive version, but slower than Version I Entire (partial) tours are in stack Multiple processes can get tours to work on Ned Nedialkov, McMaster University, 2012 12/24

Algorithm (Iterative depth-first-search. Version II) push(stack, tour) while stack is non empty tour pop(stack) if citycount(tour) = n if besttour(tour) updatebesttour(tour) else for b = n 1 downto 1 if feasible (tour,b) add(city,tour) push(stack, tour) removelast(tour, city) Ned Nedialkov, McMaster University, 2012 13/24

Example Initially stack contains 0,0 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 Iteration 1 of while loop tour = 0,0 b = 3, tour = 0 3,8 stack 0 3,8 b = 2, tour = 0 2,3 stack 0 3,8 0 2,3 b = 1, tour = 0 1,1 stack 0 3,8 0 2,3 0 1,1 Ned Nedialkov, McMaster University, 2012 14/24

Example 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 stack 0 3,8 0 2,3 0 1,1 Iteration 2 of while loop pop tour = 0 1,1 stack 0 3,8 0 2,3 b = 3, tour = 0 1 3,7 stack 0 3,8 0 2,3 0 1 3,7 b = 2, tour = 0 1 2,3 b = 1, not feasible stack 0 3,8 0 2,3 0 1 3,7 0 1 2,3 Ned Nedialkov, McMaster University, 2012 15/24

Parallelizing tree search Recursive and Version I DFS are difficult (if not impossible) to parallelize. Why? 0, 0 0 1, 1 0 2, 3 0 3,8 0 1 2, 3 0 1 3, 7 0 2 1, 21 0 2 3, 13 0 3 1,12 0 3 2, 20 0 1 2 3 0, 20 0 2 1 3 0, 34 0 3 1 2 0, 15 0 1 3 2 0, 20 0 2 3 1 0, 22 0 3 2 1 0, 43 stack after iteration 2 0 3,8 0 2,3 0 1 3,7 0 1 2,3 A process can get a stack record and work with it independently! Ned Nedialkov, McMaster University, 2012 16/24

Mapping Assume p processes One process could run Version II until there are p tours in the stack Assign them to processes Best tour Processes work independently until each finds it local best tour Do global reduction on process 0 to find the best tour Simple, but a process may search through partial tours that cannot lead to global best tour Ned Nedialkov, McMaster University, 2012 17/24

Dynamic mapping When a process runs out of work, get more work In version II, each stack entry is partial tour A process can get a partial tour and work on it The order in which nodes are visited does not matter Ned Nedialkov, McMaster University, 2012 18/24

Graph representation Represent the graph using adjacency matrix Entry (i,j) is the weight of edge from vertex i to vertex j Process 0 sends this matrix to each process Since the adjacency matrix is not large (e.g. 100 100) each process can store it We have to partition the tree check and update best tour get best tour on process 0 Ned Nedialkov, McMaster University, 2012 19/24

Dynamic mapping When a process runs out of work, it can busy-wait for more work or notification that program is terminating A process with work can send part of it to an idle process Alternatively, a process without work can request such form other process(es) How can we do this? Ned Nedialkov, McMaster University, 2012 20/24

Terminated algorithm A process checks if it has at least two tours If it has received request for work, it splits its stack and sends work to the requesting process If it cannot send, it sends no work reply Setup: a process has its own my stack fulfill request(my stack) checks if a request for work is received; if so it splits the stack and sends work send rejects checks for work requests and sends no work reply If no requests, it simply returns Ned Nedialkov, McMaster University, 2012 21/24

Algorithm (Terminated) if number of tours in my stack 2 fulfill request(my stack) return false else send rejects if my stack is non empty % 1 tour return false else % empty stack if only one process left return true announce out of work workrequest = false while (1) if no work left return true Algorithm (Terminated Cont.) else if workrequest = false send work request workrequest = true else check for work if work available receive it in my stack return false Ned Nedialkov, McMaster University, 2012 22/24

MPI issues Process 0 generates and sends partial tours to p processes Use MPI Scaterv When a process finds a best tour, it sends its cost to all other processes MPI Bcast cannot be used, as processes will block We can do for ( dest =0; dest < p; dest++) if (dest!=my rank) MPI Send(&new best cost,1,mpi INT, dest, NEW COST TAG,comm); NEW COST TAG tells the receiving process the message contains new cost Destination can check periodically using MPI Recv(&received cost,1,mpi INT,MPI ANY SOURCE, NEW COST TAG,comm,&status ); But receiving process will block Ned Nedialkov, McMaster University, 2012 23/24

Can use int MPI Iprobe(int src,int tag,mpi Comm comm, int msg, MPI Status status Checks if a message from src with tag in communicator comm is available If such is available msg is 1 and status >MPI SOURCE contains the source; otherwise msg is 0 To check if there is a message from any source MPI Iprobe(MPI ANY SOURCE,NEW COST TAG, &msg, &status) If msg=1, we can receive with MPI Recv(&received cost,1,mpi INT,status.MPI SOURCE, NEW COST TAG,comm,MPI STATUS IGNORE); Ned Nedialkov, McMaster University, 2012 24/24