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

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

Lecture 3. Brute Force

CS 440 Theory of Algorithms /

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

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

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

Brute Force: Selection Sort

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

LECTURE 17 GRAPH TRAVERSALS

Graphs and Algorithms

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

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

CS/COE 1501 cs.pitt.edu/~bill/1501/ Graphs

CS Data Structures and Algorithm Analysis

Outline. Graphs. Divide and Conquer.

Trees. Arash Rafiey. 20 October, 2015

Graph: representation and traversal

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Module 11: Additional Topics Graph Theory and Applications

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

Chapter 9 Graph Algorithms

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

Module 5 Graph Algorithms

Introduction to Algorithms: Brute-Force Algorithms

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

CS/COE

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

Module 6 NP-Complete Problems and Heuristics

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

CS6402-DESIGN AND ANALYSIS OF ALGORITHM. TWO MARK QUESTION WITH ANSWERS Unit-I. Introduction

CS 310 Advanced Data Structures and Algorithms

Module 6 NP-Complete Problems and Heuristics

Module 6 NP-Complete Problems and Heuristics

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

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

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CSC 8301 Design and Analysis of Algorithms: Graph Traversal

Lecture 3: Graphs and flows

CS 161 Fall 2015 Final Exam

Chapter 9 Graph Algorithms

Homework Assignment #3 Graph

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

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Unit 2: Algorithmic Graph Theory

Graph. Vertex. edge. Directed Graph. Undirected Graph

Assignment No 2 (Group B)

Multidimensional Arrays & Graphs. CMSC 420: Lecture 3

Chapter 9 Graph Algorithms

PSD1A. DESIGN AND ANALYSIS OF ALGORITHMS Unit : I-V

CLASS: II YEAR / IV SEMESTER CSE CS 6402-DESIGN AND ANALYSIS OF ALGORITHM UNIT I INTRODUCTION

Introduction to Computer Science and Programming for Astronomers

Theorem 2.9: nearest addition algorithm

Figure 1: A directed graph.

CS6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK UNIT I

Graph Search Methods. Graph Search Methods

Worksheet for the Final Exam - Part I. Graphs

22.1 Representations of graphs

Exercise 1. D-ary Tree

Graphs. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. What is a Graph? b d f h j

Lecture 1. Introduction

Graph Representation

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

Problem Score Maximum MC 34 (25/17) = 50 Total 100

From NP to P Musings on a Programming Contest Problem

Technische Universität München, Zentrum Mathematik Lehrstuhl für Angewandte Geometrie und Diskrete Mathematik. Combinatorial Optimization (MA 4502)

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

22.3 Depth First Search

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Lecture 3, Review of Algorithms. What is Algorithm?

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:


Fundamental Algorithms

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

CSE 100: GRAPH ALGORITHMS

DESIGN AND ANALYSIS OF ALGORITHMS

Design and Analysis of Algorithms

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Chapter 14 Section 3 - Slide 1

Minimum Spanning Trees Ch 23 Traversing graphs

CS 491 CAP Introduction to Graphs and Search

CSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Elementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:

CSE 373: Data Structures and Algorithms

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Design and Analysis of Algorithms

Graph Algorithms. Definition

Graphs. Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. What is a Graph? b d f h j

Search means finding a path or traversal between a start node and one of a set of goal nodes. Search is a study of states and their transitions.

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

Introduction to Optimization

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

Info 2950, Lecture 16

Algorithm Design and Analysis

Transcription:

CSC 8301- Design and Analysis of Algorithms Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms Brute-Force Approach Brute force is a straightforward approach to solving a problem, usually directly based on the problem s statement and definitions of the concepts involved. Example 1: Computing a n (a > 0, n is a positive integer) Example 2: Searching for a given value in a list 1

Brute-Force Algorithm for Sorting Selection Sort Scan the array to find its smallest element and swap it with the first element. Then, starting with the second element, scan the elements to the right of it to find the smallest among them and swap it with the second element. Generally, on pass i (0 i n-2), find the smallest element in A[i..n-1] and swap it with A[i]: A[0]... A[i-1] A[i],..., A[min],..., A[n-1] in their final positions Example: 7 8 2 3 5 Analysis of Selection Sort Basic operation Summation for C(n) 2

Summation for C(n) C(n) = S 0 i n-1 S i+1 j n-1 1 Closest-Pair Problem The closest-pair problem is to find the two closest points in a set of n points in the Cartesian plane (with the distance between two points measured by the standard Euclidean distance formula). Brute-force algorithm for the closest-pair problem Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. 3

Closest-Pair Brute Force Algorithm (cont.) Time efficiency: Noteworthy improvement: General Notes on Brute-Force Approach Strengths: wide applicability simplicity yields reasonable algorithms for some important problems (e.g., sorting, searching, matrix multiplication) Weaknesses: yields efficient algorithms very rarely some brute-force algorithms are unacceptably slow not as constructive as some other design techniues Note: Brute force can be a legitimate alternative in view of the human time vs. computer time costs 4

Exhaustive search Exhaustive search is a brute force approach to solving a problem that involves searching for an element with a special property, usually among combinatorial objects such permutations, combinations, or subsets of a set. Method: systematically construct all potential solutions to the problem (often using standard algorithms for generating combinatorial objects such as those in Sec. 4.3) evaluate solutions one by one, disualifying infeasible ones and, for optimization problems, keeping track of the best solution found so far when search ends, return the (best) solution found Example 1: Traveling salesman problem (TSP) Given n cities with known distances between each pair, find the shortest tour that passes through all the cities exactly once before returning to the starting city. Alternatively: Find shortest Hamiltonian circuit in a weighted connected graph. Example: 8 a 2 5 3 b 4 c 7 d 5

TSP by exhaustive search Tour Cost a b c d a 2+3+7+5 = 17 a b d c a 2+4+7+8 = 21 a c b d a 8+3+4+5 = 20 a c d b a 8+7+4+2 = 21 a d b c a 5+4+3+8 = 20 a d c b a 5+7+3+2 = 17 Fewer tours? Efficiency: Given n items with Example 2: Knapsack Problem weights: w 1 w 2 w n values: v 1 v 2 v n and a knapsack of capacity W, find most valuable subset of the items that fit into the knapsack Example: Knapsack capacity W=16 item weight value 1 2 $20 2 5 $30 3 10 $50 4 5 $10 6

Knapsack by exhaustive search Subset Total weight Total value {1} 2 $20 {2} 5 $30 {3} 10 $50 {4} 5 $10 {1,2} 7 $50 {1,3} 12 $70 {1,4} 7 $30 {2,3} 15 $80 {2,4} 10 $40 {3,4} 15 $60 {1,2,3} 17 not feasible {1,2,4} 12 $60 {1,3,4} 17 not feasible {2,3,4} 20 not feasible {1,2,3,4} 22 not feasible Efficiency: Comments on Exhaustive Search Typically, exhaustive search algorithms run in a realistic amount of time only on very small instances For some problems, there are much better alternatives shortest paths minimum spanning tree assignment problem In many cases, exhaustive search (or variation) is the only known way to solve problem exactly for all its possible instances TSP knapsack problem 7

Graph Traversal Many problems reuire processing all graph vertices (and edges) in systematic fashion, which can be considered exhaustive search algorithms Graph traversal algorithms: Depth-first search (DFS) Breadth-first search (BFS) Depth-First Search (DFS) Visits graph s vertices by always moving away from last visited vertex to unvisited one, backtracks if no adjacent unvisited vertex is available. Source: Wikipedia, https://en.wikipedia.org/ wiki/depth-first_search Redraws graph in tree-like fashion (with tree edges and back edges for undirected graph) 8

Depth-First Search (DFS) Uses a stack a vertex is pushed onto the stack first time is reached a vertex is popped off the stack when it becomes a dead end, i.e., when there is no adjacent unvisited vertex a b c d e f g h Example: DFS traversal of undirected graph Assumption: ties broken alphabetically a b c d i e f g h j DFS traversal stack: DFS forest: 9

Pseudocode of DFS 19 Notes on DFS DFS can be implemented with graphs represented as: adjacency matrices: Θ( V 2 ) adjacency lists: Θ( V + E ) Yields two distinct ordering of vertices: order in which vertices are first encountered (pushed onto stack) order in which vertices become dead-ends (popped off stack) Applications: checking connectivity, finding connected components checking acyclicity searching state-space of problems for solution (AI) 10

Breadth-first search (BFS) Visits graph vertices by moving across to all the neighbors of last visited vertex Source: Wikipedia, https://en.wikipedia.org/ wiki/breadth-first_search Redraws graph in tree-like fashion (with tree edges and cross edges for undirected graph) Pseudocode of BFS 22 11

Example of BFS traversal of undirected graph Instead of a stack, BFS uses a ueue Assumption: neighbors visited in alphabetical order a b c d i e f g h j BFS traversal ueue: BFS forest: Notes on BFS BFS has same efficiency as DFS and can be implemented with graphs represented as: adjacency matrices: Θ( V 2 ) adjacency lists: Θ( V + E ) Yields single ordering of vertices (order added/deleted from ueue is the same) Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges 12

Exercises 3.1: 4, 5, 8, 11 Exercises 3.4: 1, 5, 6 Exercises 3.5: 1, 2, 4, 6 Reading: Sec. 3.1, 3.4 and 3.5 Homework Next: Decrease-and-conuer (Chapter 4) 13