Lecture 11 CSS314 Parallel Computing

Slides:



Advertisements
Similar presentations
Lectures on Graph Algorithms: searching, testing and sorting
Advertisements

Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Types of Algorithms.
Part2 AI as Representation and Search
Branch & Bound Algorithms
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Recursion.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
MAE 552 – Heuristic Optimization Lecture 5 February 1, 2002.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Backtracking.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Lecture 10 CSS314 Parallel Computing
Tree Searching Breadth First Search Dept First Search.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Chapter 12 Recursion, Complexity, and Searching and Sorting
Fundamentals of Algorithms MCS - 2 Lecture # 7
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
JETT 2005 Session 5: Algorithms, Efficiency, Hashing and Hashtables.
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
BackTracking CS255.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Types of Algorithms.
Types of Algorithms.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Lectures on Graph Algorithms: searching, testing and sorting
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Types of Algorithms.
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
B-Trees.
Presentation transcript:

Lecture 11 CSS314 Parallel Computing Book: “An Introduction to Parallel Programming” by Peter Pacheco http://instructor.sdu.edu.kz/~moodle/ M.Sc. Bogdanchikov Andrey, Suleyman Demirel University

Content (Chapter 6) Tree Search Recursive depth-first search Nonrecursive depth-first search Data structures for the serial implementations Performance of the serial implementations

Tree Search Many problems can be solved using a tree search. As a simple example, consider the traveling salesperson problem, or TSP. In TSP, a salesperson is given a list of cities she needs to visit and a cost for traveling between each pair of cities. Her problem is to visit each city once, returning to her hometown, and she must do this with the least possible cost. A route that starts in her hometown, visits each city once and returns to her hometown is called a tour; thus, the TSP is to find a minimum-cost tour.

NP-complete problem Unfortunately, TSP is what’s known as an NP-complete problem. Exhaustive search means examining all possible solutions to the problem and choosing the best. The number of possible solutions to TSP grows exponentially as the number of cities is increased. For example, if we add one additional city to an n-city problem, we’ll increase the number of possible solutions by a factor of n-1. Thus, although there are only six possible solutions to a four-city problem, there are 4*6 = 24 to a five-city problem. In fact, a 100-city problem has far more possible solutions than the number of atoms in the universe!

Possible algorithm So how can we solve TSP? There are a number of clever solutions. However, let’s take a look at an especially simple one. It’s a very simple form of tree search. The idea is that in searching for solutions, we build a tree. The leaves of the tree correspond to tours, and the other tree nodes correspond to “partial” tours—routes that have visited some, but not all, of the cities. Each node of the tree has an associated cost, that is, the cost of the partial tour. We can use this to eliminate some nodes of the tree.

A four-city TSP

Search tree for four-city TSP

DFS Now, to find a least-cost tour, we should search the tree. There are many ways to do this, but one of the most commonly used is called depth-first search. In depth-first search, we probe as deeply as we can into the tree. After we’ve either reached a leaf or found a tree node that can’t possibly lead to a least-cost tour, we back up to the deepest “ancestor” tree node with unvisited children, and probe one of its children as deeply as possible.

How it works In our example, we’ll start at the root, and branch left until we reach the leaf labeled Then we back up to the tree node labeled 0 → 1, then Continuing, we’ll back up to the root and branch down to the node labeled 0 → 2. When we visit its child, labeled we’ll stop because we already have tour less than 21. Continuing in this fashion, we eventually find the least-cost tour

Recursive depth-first search Using depth-first search we can systematically visit each node of the tree that could possibly lead to a least-cost solution. The simplest formulation of depth-first search uses recursion. The algorithm makes use of several global variables: n: the total number of cities in the problem digraph: a data structure representing the input digraph hometown: a data structure representing vertex or city 0, the salesperson’s hometown best tour: a data structure representing the best tour so far

Pseudocode for a recursive solution to TSP

Nonrecursive depth-first search Since function calls are expensive, recursion can be slow. It also has the disadvantage that at any given instant of time only the current tree node is accessible. This could be a problem when we try to parallelize tree search by dividing tree nodes among the threads or processes. It is possible to write a nonrecursive depth-first search. The basic idea is modeled on recursive implementation. Recall that recursive function calls can be implemented by pushing the current state of the recursive function onto the run-time stack.

Pseudocode for an implementation of DFS without recursion

Alternative An alternative to this iterative version uses partial tours as stack records. This gives code that is closer to the recursive function. However, it also results in a slower version, since it’s necessary for the function that pushes onto the stack to create a copy of the tour before actually pushing it on to the stack. To emphasize this point, we’ve called the function Push copy. (What happens if we simply push a pointer to the current tour onto the stack?) The extra memory required will probably not be a problem. However, allocating storage for a new tour and copying the existing tour is time-consuming.

Pseudocode for a second solution to TSP without recursion

On the other hand On the other hand, this version has the virtue that the stack is more or less independent of the other data structures. Since entire tours are stored, multiple threads or processes can “help themselves” to tours, and, if this is done reasonably carefully it won’t destroy the correctness of the program. With the original iterative version, a stack record is just a city and it doesn’t provide enough information by itself to show where we are in the tree.

Data structures for the serial implementations Our principal data structures are the tour, the digraph, and, in the iterative implementations, the stack. The tour and the stack are essentially list structures. In problems that we’re likely to be able to tackle, the number of cities is going to be small— certainly less than 100—so there’s no great advantage to using a linked list to represent the tours and we’ve used an array that can store n-1 cities. We repeatedly need both the number of cities in the partial tour and the cost of the partial tour.

Structs Tours can be stored as array of cities Stack can be implements also by array, and implemented functions to push and pop. Digraph better to represent as adjacency matrix, so it is two-dimensional array nxn. Greater explanation Self-Study Chapter 6.2.3

Performance of the serial implementations The run-times of the three serial implementations are shown in Table. The input digraph contained 15 vertices (including the hometown), and all three algorithms visited approximately 95,000,000 tree nodes.

Homework 1. Exercise 6.17 2. Find and explain what is difference between DFS and BFS 3. Provide your own suggestion how to parallelize TSP problem. 4. Write short implementation of functions pop and push for stack data structure.

THE END Thank you