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

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

CSC 8301 Design and Analysis of Algorithms: Exhaustive Search

CS 440 Theory of Algorithms /

Lecture 3. Brute Force

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

Brute Force: Selection Sort

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

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

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

Graphs and Algorithms

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

Outline. Graphs. Divide and Conquer.

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

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

CSCE 321/3201 Analysis and Design of Algorithms. Prof. Amr Goneid. Fall 2016

Design and Analysis of Algorithms

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.

Chapter 9 Graph Algorithms

Chapter 9 Graph Algorithms

Module 6 NP-Complete Problems and Heuristics

Theorem 2.9: nearest addition algorithm

Chapter 9 Graph Algorithms

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

Module 6 NP-Complete Problems and Heuristics

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

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

Chapter 14 Section 3 - Slide 1

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

P and NP CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Module 6 NP-Complete Problems and Heuristics

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

Notes for Lecture 24

Travelling Salesman Problem. Algorithms and Networks 2015/2016 Hans L. Bodlaender Johan M. M. van Rooij

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

Worksheet for the Final Exam - Part I. Graphs

P and NP CISC5835, Algorithms for Big Data CIS, Fordham Univ. Instructor: X. Zhang

Traveling Salesman Problem. Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij

Questions? You are given the complete graph of Facebook. What questions would you ask? (What questions could we hope to answer?)

Introduction to Combinatorial Algorithms

Assignment No 2 (Group B)

CS 373: Combinatorial Algorithms, Spring 1999

Lecture 1 (Part 1) Introduction/Overview

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

Design and Analysis of Algorithms

Introduction to Algorithms

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

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

PATH FINDING AND GRAPH TRAVERSAL

Module 5 Graph Algorithms

INSTITUTE OF AERONAUTICAL ENGINEERING

Chapter 6. The Traveling-Salesman Problem. Section 1. Hamilton circuits and Hamilton paths.

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

Introduction to Computer Science

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

Discussion. What problems stretch the limits of computation? Compare 4 Algorithms. What is Brilliance? 11/11/11

Chapter-6 Backtracking

DESIGN AND ANALYSIS OF ALGORITHMS

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

IE 102 Spring Routing Through Networks - 1

Algorithm Design Techniques (III)

Lecture 1. Introduction

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Best known solution time is Ω(V!) Check every permutation of vertices to see if there is a graph edge between adjacent vertices

Spanning Trees. Lecture 22 CS2110 Spring 2017

Graphs Introduction and Depth first algorithm


CSE 101 Final Exam. Spring 2017

DESIGN AND ANALYSIS OF ALGORITHMS

NP-Complete Problems

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2006

Analysis of Algorithms

Graphs. What is a Graph? Computer Science S-111 Harvard University David G. Sullivan, Ph.D.

10/31/18. About A6, Prelim 2. Spanning Trees, greedy algorithms. Facts about trees. Undirected trees

Introduction to Algorithms: Brute-Force Algorithms

Spanning Trees, greedy algorithms. Lecture 20 CS2110 Fall 2018

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Optimal tour along pubs in the UK

Exercise 1. D-ary Tree

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

(Refer Slide Time: 01:00)

CS 310 Advanced Data Structures and Algorithms

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

CS270 Combinatorial Algorithms & Data Structures Spring Lecture 19:

Math 3012 Combinatorial Optimization Worksheet

Traveling Salesperson Problem (TSP)

Data Structures (CS 1520) Lecture 28 Name:

Algorithm Design Techniques. Hwansoo Han

NP-Complete Problems

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

Introduction to Optimization

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

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

Traveling Salesman Problem Parallel Distributed Tree Search

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

Trees. Arash Rafiey. 20 October, 2015

L.J. Institute of Engineering & Technology Semester: VIII (2016)

Elements of Graph Theory

Approximation Algorithms

Transcription:

CSCE 350: Data Structures and Algorithms Chin-Tser Huang huangct@cse.sc.edu University of South Carolina

Announcement Homework 2 will be returned on Thursday; solution will be available on class website today Midterm Exam study guide has been passed out last Thursday HW score has been updated at CSE dropbox https://dropbox.cse.sc.edu 02/07/2012 2

Closest-Pair Problem Find the two closest points in a set of n points (in the two-dimensional Cartesian plane). Brute-force algorithm Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. 02/07/2012 3

Brute-Force Closest-Pair Algorithm Efficiency: Can we make it faster? 02/07/2012 4

Convex Hull We won t cover it here, but the book studies a brute-force algorithm for computing convex hulls P 6 P 7 P 8 P 2 P 3 P 4 P 5 P 1 02/07/2012 5

Exhaustive Search A brute-force approach to combinatorial problem Generate each and every element of the problem s domain Then compare and select the desirable element that satisfies the set constraints Involve combinatorial objects such as permutations, combinations,, and subsets of a given set The time efficiency is usually bad usually the complexity grows exponentially with the input size Three examples Traveling salesman problem Knapsack problem Assignment problem 02/07/2012 6

Example 1: Traveling Salesman Problem Find the shortest tour through a given n cities that visits each city exactly once before returning to the starting city Alternatively, finding shortest Hamiltonian Circuit a cycle that passes through all the vertices of the graph exactly once Exhaustive search: List all the possible Hamiltonian circuits (starting from any vertex) Ignore the direction How many candidate circuits do we have? (n-1)!/2 Very high complexity 02/07/2012 7

TSP Example a 2 b 5 3 8 7 Tour c 1 d Length a ---> b ---> c --->d ---> a a ---> b---> d ---> c ---> a a ---> c ---> b ---> d --->a a ---> c ---> d ---> b ---> a a---> d ---> b ---> c ---> a l = 2 + 8 + 1 + 7 = 18 l = 2 + 3 + 1 + 5 = 11 l = 5 + 8 + 3 + 7 = 23 l = 5 + 1 + 3 + 2 = 11 l =7+3+8+5=23 + + = optimal optimal a ---> d ---> c ---> b ---> a l = 7 + 1 + 8 + 2 = 18 02/07/2012 8

Example 2: Knapsack Problem Given n items of known weights w 1, w 2,, w n and values v 1, v 2,, v n and a knapsack of capacity W, find the most valuable subset of the items that fit into the knapsack. Exhaustive search: Find all 2 n subset of the n items each item can be selected or not selected Select the one with the largest value while satisfies the capacity constraint. Complexity 2 n is very high. 02/07/2012 9

Knapsack Example 10 w = 7 1 w 2= 3 w 3= 4 w 4= 5 v 1 = $42 v 2= $12 v 3= $40 v 4= $25 Knapsack item 1 item 2 item 3 item 4 02/07/2012 10

Knapsack Example 02/07/2012 11

Complexity of TSP and Knapsack Both TSP and Knapsack are so-called NP-hard problem. No polynomial-time algorithm is known for any NP-hard problem. 1 constant log n n n log n n 2 n 3 logarithmic linear n log n quadratic cubic 2 n exponential n! factorial 02/07/2012 12

Example 3: Assignment Problem n people to be assigned to execute n jobs, one person per job. C[i,j] is the cost if person i is assigned to job j. Find an assignment with the smallest total cost Job 1 Job 2 Job 3 Job 4 Person 1 9 2 7 8 Person 2 6 4 3 7 Person 3 5 8 1 8 Person 4 7 6 9 4 Exhaustive search: How many kinds of different assignments? The permutation of n persons n! Very high h complexity Hungarian method much more efficient polynomial 02/07/2012 13

Assignment Problem by Exhaustive Search 02/07/2012 14

Comments on Exhaustive Search Exhaustive-search algorithms run in a realistic amount of time only on very small instances In some cases, there are much better alternatives! Euler circuits shortest paths minimum spanning tree assignment problem In many cases, exhaustive search or its variation is the only known way to get exact solution 02/07/2012 15

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. Uses a stack a vertex is pushed onto the stack when it s reached for the first time a vertex is popped off the stack when it becomes a dead end, i.e., when there is no adjacent unvisited vertex Redraws graph in tree-like fashion (with tree edges and back edges for undirected graph) 02/07/2012 16

Pseudocode of DFS 02/07/2012 17

DFS Example Undirected Graph g j a d c f e b h i d c 3,1 2,5 e b f 6,2 5,3 4,4 j i h a 1 g,6 10,7 9,8 8,9 7,10 a c d f b e g h i j Input Graph Stack push/pop DFS forest (Tree edge / Back edge) 02/07/2012 18

Applications of DFS To check the connectivity of a graph, check whether all the graph s vertices have been visited iitdafter the dfs procedure halts hlt If so, the graph is connected; otherwise not connected Can continue to find out each connected components To check the acyclicity of a graph, check if the DFS forest has any back edge If not, the graph is acyclic; otherwise, the graph has acyce cycle 02/07/2012 19

Notes on DFS DFS can be implemented with graphs represented as: Adjacency matrices: Θ( V 2 ) Adjacency linked lists: Θ( V + E ) Yields two distinct ordering of vertices: preorder: as vertices are first encountered (pushed onto stack) postorder: as vertices become dead-ends ends (popped off stack) 02/07/2012 20