Brute Force II.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Advertisements

Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
The Design and Analysis of Algorithms
COSC 3100 Brute Force and Exhaustive Search Instructor: Tanvir 1.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
RAIK 283: Data Structures & Algorithms
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Ch3 /Lecture #4 Brute Force and Exhaustive Search 1.
Exhaustive search Exhaustive search is simply a brute- force approach to combinatorial problems. It suggests generating each and every element of the problem.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Limitation of Computation Power – P, NP, and NP-complete
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Graphs Chapter 20.
Data Structures Lab Algorithm Animation.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Greedy Technique.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Graph Searching.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly.
CS120 Graphs.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 String Matching.
Heuristics Definition – a heuristic is an inexact algorithm that is based on intuitive and plausible arguments which are “likely” to lead to reasonable.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Can you get there from here?
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Subgraphs, Connected Components, Spanning Trees
3. Brute Force Selection sort Brute-Force string matching
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
3. Brute Force Selection sort Brute-Force string matching
Backtracking and Branch-and-Bound
A vertex u is reachable from vertex v iff there is a path from v to u.
Unit –VII Coping with limitations of algorithm power.
CSC 380: Design and Analysis of Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Analysis and design of algorithm
EMIS 8374 Search Algorithms Updated 12 February 2008
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Brute Force II

Lab 5: Mock Interview Work in pairs: 1 person is interviewer, 1 person is candidate Candidate writes pseudo-code on the board Interviewer asks questions to help interviewee improve their pseudo-code Problem: Opposites Don’t Attract You are writing an online dating match algorithm. Introduce Selves

What is Brute Force? “Brute Force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved.” Example: computing a^n by multiplying n times.

Brute Force Approaches to … Last Time: Sorting Searching String Matching Geometric Problems Closest Pair Today: Geometric Problems Convex Hull Combinatorial Problems Travelling Salesman Problem Knapsack Problem Assignment Problem Graph Problems

Convex Hull Given set of points S, find the subset of S that forms a convex hull enclosing all the points in S.

Brute Force Convex Hull Test each pair of points to see if line segment between them is part of convex hull. i.e. whether all other points lie to one side of it. Given two points (x1,y1) (x2,y2), line between them is: ax + by = c a = y2-y1, b = x1-x2, c = x1y2-y1x2 O(n3)

Combinatorial Problems

Combinatorial Problems Find an element with given property in domain that grows exponentially (or worse) with instance size. What would be a brute force approach? Common in Optimization problems.

Traveling Salesman (TSP) Find the shortest tour through n cities that visits each city exactly once before returning to the city where it started. a 2 b 7 5 3 8 c d 1

Traveling Salesman (TSP) What are all possible tours starting at a? How many are there (as function of n)? Which is the shortest? a 2 b 7 5 3 8 c d 1

Traveling Salesman (TSP) a →b→c→d→a len = 2 + 8 + 1 + 7 = 18 a →b→d→c→a len = 2 + 3 + 1 + 5 = 11 a →c→b→d→a len = 5 + 8 + 3 + 7 = 23 a →c→d→b→a len = 5 + 1 + 3 + 2 = 11 a →d→b→c→a len = 7 + 3 + 8 + 5 = 23 a →d→c→b→a len = 7 + 1 + 8 + 2 = 18 Notice the repetition -- one for each direction. a 2 b 7 5 3 8 c d 1

Traveling Salesman (TSP) a →b→c→d→a len = 2 + 8 + 1 + 7 = 18 a →b→d→c→a len = 2 + 3 + 1 + 5 = 11 a →c→b→d→a len = 5 + 8 + 3 + 7 = 23 a →c→d→b→a len = 5 + 1 + 3 + 2 = 11 a →d→b→c→a len = 7 + 3 + 8 + 5 = 23 a →d→c→b→a len = 7 + 1 + 8 + 2 = 18 a 2 b Choose 2 intermediate states, e.g. b,c and then force them to be in alphabetical order. Did the Big-O change? 7 5 3 8 c d 1

Knapsack Problem You have a set of n items with weights w1, w2, …, wn and values v1, v2, …, vn. You have a knapsack that can hold a total weight of W. What’s the most valuable subset of items you can fit in the knapsack? W = 10 w1 = 7 v1 = $42 w4 = 5 v4 = $25 w3 = 4 v3 = $40 w2 = 3 v2 = $12

Knapsack Problem Subset Total Weight Total Value { } $0 {1} 7 $42 {2} $0 {1} 7 $42 {2} 3 $12 {3} 4 $40 {4} 5 $25 {1,2} 10 $54 {1,3} 11 not feasible {1,4} 12 {2,3} $52 {2,4} 8 $37 {3,4} 9 $65 {1,2,3} 14 {1,2,4} 15 {1,3,4} 16 {2,3,4} {1,2,3,4} 19 Knapsack Problem

Knapsack Problem How many subsets exist in a set of n items? Imagine representing subset with bit string – 1 means item is in subset, 0 means item is not in subset. How many numbers can be represented with n bits of a binary representation? 1 2 3 4

Knapsack Problem How many subsets exist in a set of n items? Imagine representing subset with bit string – 1 means item is in subset, 0 means item is not in subset. How many numbers can be represented with n bits of a binary representation? 2n Example of NP-Hard problem 1 2 3 4

Longer than age of universe!! Remember! log2n n n log2n n2 n3 2n n! 10 3.3 33 102 103 106 6.6 660 104 1.3 ∙ 1030 9.3 ∙ 10157 109 13 1.3 ∙ 105 108 1012 105 17 1.7 ∙ 106 1010 1015 20 2.0 ∙ 107 1018 Longer than age of universe!!

Exhaustive Search Brute Force approach to Combinatorial Problems Generate each and every element in problem domain. Select those that satisfy the required constraints. Choose the one that optimizes the relevant objective function. Depth-First and Breadth-First Search Useful for graph problems in AI and OR

Depth First Search (DFS) View graph as a tree, rooted at arbitrary starting node: a c b e d f

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d e f c

Depth First Search (DFS) Keep track of nodes to explore on a stack (LIFO) b d e f c

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d d b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d e e b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d e f f e b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d e f e b a

Depth First Search (DFS) Start at arbitrary vertex Each iteration, proceed to an unvisited vertex – if there are multiple, break tie arbitrarily Upon reaching a dead end, back up. If back up to start node, no solution exists. b d e c f e b c a

DFS Algorithm # Stack Structure Implicit: Uses Call Stack count ← 0 // global var DFS(G) // Input: graph G = <V,E> mark each vertex in V with 0 // unvisited for each vertex v in V do if v is marked with 0 dfs(v) count ← count + 1 mark v with count for each vertex w adjacent to v do if w is marked with 0 dfs(w)

DFS Properties Efficiency: a O( |V|2 ) for adjacency matrix O( |V| + |E| ) for adjacency list b d e f c

DFS Properties Categorize Edges: Graph properties: connected? acyclic? tree edge: one used to arrive at vertex (solid) back edge: edge to other ancestor (dotted) Graph properties: connected? acyclic? How relate tree vs. back edges? b d e f c

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … Best represented as a queue (FIFO) d b c e f

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c a b c d

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c e b c d e

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c e f c d e f

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c e f d e f

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c e f e f

Breadth First Search (BFS) Start at arbitrary vertex Visit all nodes adjacent to starting node … Then all unvisited nodes two hops from starting node … Then three … d b c e f f

BFS Algorithm count ← 0 q ← [] // empty queue BFS(G) mark each vertex in V with 0 for each vertex v in V do if v is marked with 0 bfs(v) count ← count + 1 mark v with count and q.enqueue(v) while q is not empty do for each vertex w adjacent to v do if w is marked with 0 mark w with count q.enqueue(w) q.dequeue()

BFS Properties Efficiency: a O( |V|2 ) for adjacency matrix O( |V| + |E| ) for adjacency list d b c e f

BFS Properties Graph Properties: a Can also detect connected, acyclic Can find shortest path between root and other vertex. d b c e f