CSCE 411 Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Dr. J. Michael Moore Fall 2014 CSCE 411, Fall 2014: Set 2 1 Based primarily.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Graph Algorithms
Advertisements

Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
1 Chapter 22: Elementary Graph Algorithms IV. 2 About this lecture Review of Strongly Connected Components (SCC) in a directed graph Finding all SCC (i.e.,
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
CSCE 411H Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 2 1.
Tirgul 8 Graph algorithms: Strongly connected components.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Data Structures DFS, Topological Sort Dana Shapira.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
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.
Depth-First Search Idea: Keep going forward as long as there are unseen nodes to be visited. Backtrack when stuck. v G G G G is completely traversed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Topological Sort (an application of DFS) CSC263 Tutorial 9.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
 2004 SDU 1 Lecture5-Strongly Connected Components.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Graph Algorithms – 2. graphs Parenthesis Theorem Theorem 22.7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or.
Introduction to Algorithms
Chapter 22 Elementary Graph Algorithms
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Topological Sort (an application of DFS)
CSCE 411 Design and Analysis of Algorithms
Graph Algorithms – 2 DAGs Topological order
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Applications of DFS Topological sort (for directed acyclic graph)
Topological Sort (an application of DFS)
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

CSCE 411 Design and Analysis of Algorithms Set 2: Brute Force & Exhaustive Search Dr. J. Michael Moore Fall 2014 CSCE 411, Fall 2014: Set 2 1 Based primarily on slides by Prof. Jennifer Welch

Brute Force & Exhaustive Search Straightforward way to solve a problem, based on the definition of the problem itself; often involves checking all possibilities Pros: widely applicable easy good for small problem sizes Con: often inefficient for large inputs CSCE 411, Fall 2014: Set 22

Brute Force Sorting Selection sort scan array to find smallest element scan array to find second smallest element etc. Bubble sort scan array, swapping out-of-order neighbors continue until no swaps are needed Both take Θ(n 2 ) time in the worst case. CSCE 411, Fall 2014: Set 23

Brute Force Searching Sequential search: go through the entire list of n items to find the desired item Takes Θ(n) time in the worst case CSCE 411, Fall 2014: Set 24

Brute Force Searching in a Graph (Review graph terminology and basic algorithms) Breadth-first search: go level by level in the graph Depth-first search: go as deep as you can, then backtrack Both take Θ(V+E) time, where |V| is the number of vertices and |E| is the number of edges CSCE 411, Fall 2014: Set 25

Brute Force for Combinatorial Problems Traveling Salesman Problem (TSP): given a set of n cities and distances between all pairs of cities, determine order for traveling to every city exactly once and returning home with minimum total distance Solution: Compute distance for all “tours” and choose the shortest. Takes Θ(n!) time (terrible!) CSCE 411, Fall 2014: Set 26

TSP Example CSCE 411, Fall 2014: Set 27 c d 2 1 a b a,b,c,d,a -> 18 a,b,d,c,a -> 11 a,c,b,d,a -> 23 a,d,b,c,a -> 23 a,d,c,b,a -> 18 Do we need to consider more tours? Something odd about the “distances”?

TSP Applications transportation and logistics (school buses, meals on wheels, airplane schedules, etc.) drilling printed circuit boards analyzing crystal structure overhauling gas turbine engines clustering data tsp.gatech.edu/apps/index.html iris.gmu.edu/~khoffman/papers/trav_salesman.html CSCE 411, Fall 2014: Set 28

Brute Force for Combinatorial Problems Knapsack Problem: There are n different items in a store Item i weighs w i pounds and is worth $v i A thief breaks in He can carry up to W pounds in his knapsack What should he take to maximize his haul? Solution: Consider every possible subset of items, calculate total value and total weight and discard if more than W; then choose remaining subset with maximum total value. Takes Ω(2 n ) time CSCE 411, Fall 2014: Set 29

Knapsack Applications Least wasteful way to use raw materials selecting capital investments and financial portfolios generating keys for the Merkle-Hellman cryptosystem Knapsack Problems, H. Kellerer, U. Pferschy, D. Pisinger, Springer, CSCE 411, Fall 2014: Set 210

Knapsack Example item 1: 7 lbs, $42 item 2: 3 lbs, $12 item 3: 4 lbs, $40 item 4: 5 lbs, $25 W = 10 need to check 16 possibilities subsettotal weighttotal value Ø0$0 {1}7$42 {2}3$12 {3}4$40 {4}5$25 {1,2}10$54 {1,3}11infeasible {1,4}12infeasible {2,3}7$52 {2,4}8$37 etc. CSCE 411, Fall 2014: Set 211

Brute Force For Closest Pair Closest-Pair Problem: Given n points in d-dimensional space, find the two that are closest Applications: airplanes close to colliding which post offices should be closed which DNA sequences are most similar CSCE 411, Fall 2014: Set 212

Brute Force For Closest Pair Brute-force Solution (for 2-D case): compute distances between all pairs of points sqrt((x i – x j ) 2 + (y i – y j ) 2 ) scan all distances to find smallest Running time: Θ(n 2 ), assuming each numerical operation is constant time (including square root?) Improvements: drop the square root don’t compute distance for same 2 points twice CSCE 411, Fall 2014: Set 213

Brute Force For Convex Hull Convex Hull Problem: Given a set of points in 2-D, find the smallest convex polygon s.t. each point in the set is enclosed by the polygon polygon: sequence of line segments that ends where it begins convex: all points on a line segment between 2 points in the polygon are also in the polygon CSCE 411, Fall 2014: Set 214

Convex Hull Applications In computer graphics or robot planning, a simple way to check that two (possibly complicated) objects are not colliding is to compute their convex hulls and then check if the hulls intersect Estimate size of geographic range of a species, based on observations (geocat.kew.org/about) CSCE 411, Fall 2014: Set 215

Brute Force For Convex Hull Key idea for solution: line passing through (x i,y i ) and (x j,y j ) is: ax + by = c where a = (y j – y i ), b = (x i – x j ), c = x i y j – y i x j The 2 pts are on the convex hull iff all other pts are on same side of this line: CSCE 411, Fall 2014: Set 216

Brute Force For Convex Hull For each (distinct) pair of points in the set, compute a, b, and c to define the line ax + by = c. For each other point, plug its x and y coordinates into the expression ax + by – c. If they all have the same sign (all positive or all negative), then this pair of points is part of the convex hull. Takes Θ(n 3 ) time. CSCE 411, Fall 2014: Set 217

Brute Force for Two Numeric Problems Problem: Compute a n Solution: Multiply a by itself n-1 times Takes Θ(n) time, assuming each multiplication takes constant time. Problem: multiply two nxn matrices A and B to create product matrix C Solution: Follow the definition, which says the (i,j) entry of C is Σ a ik *b kj, k = 1 to n Takes Θ(n 3 ) time, assuming each basic operation takes constant time CSCE 411, Fall 2014: Set 2 18

Brute Force/Exhaustive Search Summary sorting: selection sort, bubble sort searching: sequential search graphs: BFS, DFS combinatorial problems: check all possibilities for TSP and knapsack geometric: check all possibilities for closest pair and for convex hull numerical: follow definition to compute a n or matrix multiplication CSCE 411, Fall 2014: Set 219

Applications of DFS Now let’s go more in depth on two applications of depth-first search topological sort finding strongly connected components of a graph CSCE 411, Fall 2014: Set 220

CSCE 411, Fall 2014: Set 221 Depth-First Search Input: G = (V,E) for each vertex u in V do mark u as unvisited parent[u] := nil time := 0 for each unvisited vertex u in V do parent[u] := u // a root call recursive DFS(u) recursiveDFS(u): mark u as visited time++ disc[u] := time for each unvisited (o-g) neighbor v of u do parent[v] := u call recursiveDFS(v) time++ fin[u] := time

CSCE 411, Fall 2014: Set 222 Nested Intervals Let interval for vertex v be [disc[v],fin[v]]. Fact: For any two vertices, either one interval precedes the other or one is enclosed in the other. because recursive calls are nested Corollary: v is a descendant of u in the DFS forest if and only if v's interval is inside u's interval.

CSCE 411, Fall 2014: Set 223 Classifying Edges Consider edge (u,v) in directed graph G = (V,E) w.r.t. DFS forest tree edge: v is a child of u back edge: v is an ancestor of u forward edge: v is a descendant of u but not a child cross edge: none of the above

CSCE 411, Fall 2014: Set 224 Example of Classifying Edges b e a c f d in DFS forest not in DFS forest tree forward back cross

CSCE 411, Fall 2014: Set 225 DFS Application: Topological Sort Given a directed acyclic graph (DAG), find a linear ordering of the vertices such that if (u,v) is an edge, then u precedes v. DAG indicates precedence among events: events are graph vertices, edge from u to v means event u has precedence over event v Partial order because not all events have to be done in a certain order

CSCE 411, Fall 2014: Set 226 Precedence Example Tasks that have to be done to eat breakfast: get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat. Certain events must happen in a certain order (ex: get bowl before pouring milk) For other events, it doesn't matter (ex: get bowl and get spoon)

CSCE 411, Fall 2014: Set 227 Precedence Example get glass pour juice get bowl pour cereal pour milk get spoon eat breakfast Order: glass, juice, bowl, cereal, milk, spoon, eat.

CSCE 411, Fall 2014: Set 228 Why Acyclic? Why must directed graph by acyclic for the topological sort problem? Otherwise, no way to order events linearly without violating a precedence constraint.

CSCE 411, Fall 2014: Set 229 Idea for Topological Sort Alg. eat juice glass milk cereal bowl spoon consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat Run DFS on the input graph

CSCE 411, Fall 2014: Set 230 Topological Sort Algorithm input: DAG G = (V,E) 1. call DFS on G to compute finish[v] for all vertices v 2. when each vertex's recursive call finishes, insert it on the front of a linked list 3. return the linked list Running Time: O(V+E)

CSCE 411, Fall 2014: Set 231 Correctness of T.S. Algorithm Show that if (u,v) is an edge, then v finishes before u finishes. Thus the algorithm correctly orders u before v. Case 1: u is discovered before v is discovered. By the way DFS works, u does not finish until v is discovered and v finishes. Then v finishes before u finishes. disc(u) fin(u) disc(v)fin(v) u v u v

CSCE 411, Fall 2014: Set 232 Correctness of T.S. Algorithm Show that if (u,v) is an edge, then v finishes before u finishes. Thus the algorithm correctly orders u before v. Case 2: v is discovered before u is discovered. Suppose u finishes before v finishes (i.e., u is nested inside v). Show this is impossible… disc(v) fin(v) disc(u)fin(u) u v u v

CSCE 411, Fall 2014: Set 233 Correctness of T.S. Algorithm v is discovered but not yet finished when u is discovered. Then u is a descendant of v. But that would make (u,v) a back edge and a DAG cannot have a back edge (the back edge would form a cycle). Thus v finishes before u finishes.

CSCE 411, Fall 2014: Set 234 DFS Application: Strongly Connected Components Consider a directed graph. A strongly connected component (SCC) of the graph is a maximal set of vertices with a (directed) path between every pair of vertices Problem: Find all the SCCs of the graph.

CSCE 411, Fall 2014: Set 235 What Are SCCs Good For? Packaging software modules: Construct directed graph of which modules call which other modules A SCC is a set of mutually interacting modules Pack together those in the same SCC Solving the “2-satisfiability problem”, which in turn is used to solve various geometric placement problems (graph labeling, VLSI design), as well as data clustering and scheduling wikipedia

CSCE 411, Fall 2014: Set 236 SCC Example h fae g cbd four SCCs

CSCE 411, Fall 2014: Set 237 How Can DFS Help? Suppose we run DFS on the directed graph. All vertices in the same SCC are in the same DFS tree. But there might be several different SCCs in the same DFS tree. Example: start DFS from vertex h in previous graph

CSCE 411, Fall 2014: Set 238 Main Idea of SCC Algorithm DFS tells us which vertices are reachable from the roots of the individual trees Also need information in the "other direction": is the root reachable from its descendants? Run DFS again on the "transpose" graph (reverse the directions of the edges)

CSCE 411, Fall 2014: Set 239 SCC Algorithm input: directed graph G = (V,E) 1. call DFS(G) to compute finishing times 2. compute G T // transpose graph 3. call DFS(G T ), considering vertices in decreasing order of finishing times 4. each tree from Step 3 is a separate SCC of G

CSCE 411, Fall 2014: Set 240 SCC Algorithm Example h fae g cbd input graph - run DFS

CSCE 411, Fall 2014: Set 241 After Step 1 c bg a f d e h fin(c) fin(d)fin(b)fin(e)fin(a)fin(h)fin(g) fin(f) Order of vertices for Step 3: f, g, h, a, e, b, d, c

CSCE 411, Fall 2014: Set 242 After Step 2 h fae g cbd transposed input graph - run DFS with specified order of vertices

CSCE 411, Fall 2014: Set 243 After Step 3 g he f a d SCCs are {f,h,g} and {a,e} and {b,c} and {d}. b c

CSCE 411, Fall 2014: Set 244 Running Time of SCC Algorithm Step 1: O(V+E) to run DFS Step 2: O(V+E) to construct transpose graph, assuming adjacency list rep. Step 3: O(V+E) to run DFS again Step 4: O(V) to output result Total: O(V+E)

CSCE 411, Fall 2014: Set 245 Correctness of SCC Algorithm Proof uses concept of component graph, G SCC, of G. Vertices are the SCCs of G; call them C 1, C 2, …, C k Put an edge from C i to C j iff G has an edge from a vertex in C i to a vertex in C j

CSCE 411, Fall 2014: Set 246 Example of Component Graph {a,e}{f,h,g} {d} {b,c} based on example graph from before

CSCE 411, Fall 2014: Set 247 Facts About Component Graph Claim: G SCC is a directed acyclic graph. Why? Suppose there is a cycle in G SCC such that component C i is reachable from component C j and vice versa. Then C i and C j would not be separate SCCs.

CSCE 411, Fall 2014: Set 248 Facts About Component Graph Consider any component C during Step 1 (running DFS on G) Let d(C) be earliest discovery time of any vertex in C Let f(C) be latest finishing time of any vertex in C Lemma: If there is an edge in G SCC from component C' to component C, then f(C') > f(C).

CSCE 411, Fall 2014: Set 249 Proof of Lemma Case 1: d(C') < d(C). Suppose x is first vertex discovered in C'. By the way DFS works, all vertices in C' and C become descendants of x. Then x is last vertex in C' to finish and finishes after all vertices in C. Thus f(C') > f(C). C'C

CSCE 411, Fall 2014: Set 250 Proof of Lemma Case 2: d(C') > d(C). Suppose y is first vertex discovered in C. By the way DFS works, all vertices in C become descendants of y. Then y is last vertex in C to finish. Since C'  C, no vertex in C' is reachable from y, so y finishes before any vertex in C' is discovered. Thus f(C') > f(C). C'C

CSCE 411, Fall 2014: Set 251 SCC Algorithm is Correct Prove this theorem by induction on number of trees found in Step 3 (running DFS on G T ). Hypothesis is that the first k trees found constitute k SCCs of G. Basis: k = 0. No work to do !

CSCE 411, Fall 2014: Set 252 SCC Algorithm is Correct Induction: Assume the first k trees constructed in Step 3 (running DFS on G T ) correspond to k SCCs; consider the (k+1)st tree. Let u be the root of the (k+1)st tree. u is part of some SCC, call it C. By the inductive hypothesis, C is not one of the k SCCs already found and all so vertices in C are unvisited when u is discovered. By the way DFS works, all vertices in C become part of u's tree

CSCE 411, Fall 2014: Set 253 SCC Algorithm is Correct Show only vertices in C become part of u's tree. Consider an outgoing edge from C. w z u C' C GT:GT: w z u C G:

CSCE 411, Fall 2014: Set 254 SCC Algorithm is Correct By lemma, in Step 1 (running DFS on G) the last vertex in C' finishes after the last vertex in C finishes. Thus in Step 3 (running DFS on G T ), some vertex in C' is discovered before any vertex in C is discovered. Thus in Step 3, all of C', including w, is already visited before u's DFS tree starts w z u C' C G: