A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch. 1 Chapter 2.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
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.
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Design and Analysis of Algorithms - Chapter 51 Decrease and Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller.
The Design and Analysis of Algorithms
Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.
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.
Analysis & Design of Algorithms (CSCE 321)
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Design and Analysis of Algorithms - Chapter 5
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
RAIK 283: Data Structures & Algorithms
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
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,
Representing and Using Graphs
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Lecture 7 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
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.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
Introduction to Algorithms: Brute-Force Algorithms.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force II.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Graphs Chapter 20.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
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 – based directly.
Chapter 5.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 String Matching.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
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.
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
3. Brute Force Selection sort Brute-Force string matching
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
3. Brute Force Selection sort Brute-Force string matching
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis and design of algorithm
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1 Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples – based directly on definitions: 1. Computing a n (a > 0, n a nonnegative integer) 2. Computing n! 3. Multiplying two matrices 4. Searching for a key of a given value in a list

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2 Sorting by Brute Force Use definition of sorted and obvious algorithm? 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 elements. 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 in their final positions Example:

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3 Analysis of Selection Sort

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4 String Matching by Brute Force b pattern: a string of m characters to search for b text: a (longer) string of n characters to search in b problem: find first substring in text that matches pattern Brute-force: Scan text LR, compare chars, looking for pattern, Step 1 Align pattern at beginning of text Step 2 Moving from left to right, compare each character of pattern to the corresponding character in text until –all characters are found to match (successful search); or –a mismatch is detected Step 3 While pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat Step 2

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 Examples of Brute-Force String Matching Pattern: T ext: Pattern: happy Text: It is never too late to have a happy childhood.

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6 Pseudocode and Efficiency Efficiency: Decisions??

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7 Brute-Force Polynomial Evaluation p  0.0 for i  n downto 0 do power  1 power  1 for j  1 to i do//compute x i for j  1 to i do//compute x i power  power  x power  power  x p  p + a[i]  power p  p + a[i]  power returnp return p

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8 Polynomial Evaluation: Improvement p  a[0] power  1 for i  1 to n do power  power  x power  power  x p  p + a[i]  power p  p + a[i]  power return p

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 9 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 Compute the distance between every pair of distinct points and return the indexes of the points for which the distance is the smallest. and return the indexes of the points for which the distance is the smallest.

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10 Closest-Pair Brute-Force Algorithm (cont.) Efficiency: Efficiency:Advantage: How to make it faster? Convex Hull How to make it faster? Convex Hull

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 11 Exhaustive Search Brute force Closest Pair is an example of Exhaustive Search Enumerate and evaluate all solutions, and choose solution that meets some criteria (eg smallest) Exhaustively search solutions (ie search until list of possible solutions is exhausted) Frequently the obvious solution Slow

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12 Exhaustive Search A brute force solution to a problem involving search for an element with a special property, usually among combinatorial objects such as permutations, combinations, or subsets of a set. Method: generate a list of all potential solutions to the problem in a systematic manner (see algorithms in Sec. 5.4)generate a list of all potential solutions to the problem in a systematic manner (see algorithms in Sec. 5.4) evaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so farevaluate potential solutions one by one, disqualifying infeasible ones and, for an optimization problem, keeping track of the best one found so far when search ends, announce the solution(s) foundwhen search ends, announce the solution(s) found

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 13 Exhaustive Search - Examples Consider several more Exhaustive Search Algorithms Traveling Salesman Problem (TSP) Knapsack Problem Assignment Problem Graph algorithms: Depth First Search (DFS) Breadth First Search (BFS) Better algorithms may exist

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14 GRAPHS Many problem solutions use a graph to represent the data: - TSP - Cities and roads - Network nodes and connections among them - People and friends Graph: two sets: - Set of vertices - Set of edges - Edges may have weights and directions - Undirected graph: edges have no directions We solve problems with graphs, and examine some standard graph algorithms (eg DFS and BFS in this chapter)

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15 Example 1: Traveling Salesman Problem b 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 b More formally: Find shortest Hamiltonian circuit in a weighted connected graph b Example: ab cd

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16 TSP by Exhaustive Search Tour Cost Tour Cost a→b→c→d→a = 17 a→b→d→c→a = 21 a→c→b→d→a = 20 a→c→d→b→a = 21 a→d→b→c→a = 20 a→d→c→b→a = 17 Have we considered all tours? Do we need to consider more? Any way to consider fewer tours? Efficiency: Number of tours = number of …

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 17 Example 2: Knapsack Problem Given n items: weights: w 1 w 2 … w nweights: w 1 w 2 … w n values: v 1 v 2 … v nvalues: v 1 v 2 … v n a knapsack of capacity Wa 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 $ $ $ $10

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 18 Knapsack: Exhaustive Search Subset Total weight Total value {1} 2 $20 {1} 2 $20 {2} 5 $30 {2} 5 $30 {3} 10 $50 {3} 10 $50 {4} 5 $10 {4} 5 $10 {1,2} 7 $50 {1,2} 7 $50 {1,3} 12 $70 {1,3} 12 $70 {1,4} 7 $30 {1,4} 7 $30 {2,3} 15 $80 {2,3} 15 $80 {2,4} 10 $40 {2,4} 10 $40 {3,4} 15 $60 {3,4} 15 $60 {1,2,3} 17 not feasible {1,2,3} 17 not feasible {1,2,4} 12 $60 {1,2,4} 12 $60 {1,3,4} 17 not feasible {1,3,4} 17 not feasible {2,3,4} 20 not feasible {2,3,4} 20 not feasible {1,2,3,4} 22 not feasible Efficiency: how many subsets?

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19 Example 3: The Assignment Problem There are n people who need to be assigned to n jobs, one person per job. The cost of assigning person i to job j is C[i,j]. Find an assignment that minimizes the total cost. Job 0 Job 1 Job 2 Job 3 Job 0 Job 1 Job 2 Job 3 Person Person Person Person Algorithmic Plan: Generate all legitimate assignments, compute their costs, and select the cheapest one. How many assignments are there? Pose the problem as the one about a cost matrix:

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Assignment (col.#s) Total Cost Assignment (col.#s) Total Cost 1, 2, 3, =18 1, 2, 3, =18 1, 2, 4, =30 1, 2, 4, =30 1, 3, 2, =24 1, 3, 2, =24 1, 3, 4, =26 1, 3, 4, =26 1, 4, 2, =33 1, 4, 2, =33 1, 4, 3, =23 1, 4, 3, =23 etc. etc. (For this particular instance, the optimal assignment can be found by exploiting the specific features of the number given. It is: ) Assignment Problem: Exhaustive Search C =

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 21 Final Comments on Exhaustive Search b Exhaustive-search algorithms run in a realistic amount of time only on very small instances b In some cases, there are much better alternatives! Euler circuitsEuler circuits shortest pathsshortest paths minimum spanning treeminimum spanning tree assignment problemassignment problem b In many cases, exhaustive search or its variation is the only known way to get exact solution

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22 Graph Traversal Algorithms Many problems require processing all graph vertices (and edges) in systematic fashion Graph traversal algorithms: Depth-first search (DFS): Visit children firstDepth-first search (DFS): Visit children first Breadth-first search (BFS): Visit siblings firstBreadth-first search (BFS): Visit siblings first

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23 GRAPHS (revisited) Graph definition: two sets: - Set of vertices - Set of edges - Edges may have weights and directions - Undirected graph: edges have no directions - Directed graph: edges have direction We look at graphs in 2 ways: - solve problems using a graph to represent data - eg TSP and topological sort in this and next chapter - examine some standard graph algorithms - eg DFS and BFS in this chapter

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 24 GRAPH TERMS Tree: - acyclic graph - n nodes, n-1 edges Forest: set of trees Connected graph: - every pair of vertices has a path between them Complete graph: - every pair of vertices has an edge between them

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25 Graph Implementation Adjacency matrix: - Row and column for each vertix - 0/1 for edge or not - Undirected graph means what kind of matrix Adjacency lists - Each node has a list of adjacent nodes

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26 Depth-First Search (DFS) b Visits graph’s vertices by always moving away from last visited vertex to unvisited one Backtracks if no adjacent unvisited vertex is available. Backtracks if no adjacent unvisited vertex is available. b Uses a stack a vertex is pushed when it’s reached for the first timea vertex is pushed when it’s reached for the first time a vertex is popped when it becomes a dead end, i.e., when there is no adjacent unvisited vertexa vertex is popped when it becomes a dead end, i.e., when there is no adjacent unvisited vertex b Marks edges in tree-like fashion (mark edges as tree edges and back edges [goes back to already discovered ancestor vertex]. In a DFS of an undirected graph, each edge becomes either a tree edge or a back edge In a DFS of an undirected graph, each edge becomes either a tree edge or a back edge

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27 Pseudocode of DFS

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 28 Example: DFS traversal of undirected graph ab ef cd gh DFS traversal stack: DFS tree:

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29 Example: DFS traversal of undirected graph ab ef cd gh Nodes pushed: a b f e g c d h Nodes popped: e f h d c g b a Complexity???

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30 Notes on DFS b DFS can be implemented with graphs represented as: adjacency matrices: Θ(V 2 )adjacency matrices: Θ(V 2 ) adjacency lists: Θ(|V|+|E|)adjacency lists: Θ(|V|+|E|) b Yields two distinct ordering of vertices: order in which vertices are first encountered (pushed onto stack)order in which vertices are first encountered (pushed onto stack) order in which vertices become dead-ends (popped off stack)order in which vertices become dead-ends (popped off stack) Orderings and edges used by various algorithmsOrderings and edges used by various algorithms –(eg scheduling / topological sort) b Applications: checking connectivity, finding connected componentschecking connectivity, finding connected components checking acyclicitychecking acyclicity finding articulation points and biconnected componentsfinding articulation points and biconnected components searching state-space of problems for solution (AI)searching state-space of problems for solution (AI)

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 31 Breadth-first search (BFS) b Visits graph vertices by moving across to all the neighbors of last visited vertex b BFS uses a queue (not a stack like DFS) b Similar to level-by-level tree traversal b Marks edges in tree-like fashion (mark tree edges and cross edges [goes across to an already discovered sibling edge] In a BFS of an undirected graph, each edge becomes either a tree edge or a cross edgeIn a BFS of an undirected graph, each edge becomes either a tree edge or a cross edge

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 32 Pseudocode of BFS

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 33 Example of BFS traversal of undirected graph BFS traversal queue: ab ef cd gh BFS tree:

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 34 Example of BFS traversal of undirected graph BFS traversal queue: a, b e f, g, c h, d Level: 1, 2 2 2, 3, 4 4, 5 ab ef cd gh

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 35 Notes on BFS b BFS has same efficiency as DFS and can be implemented with graphs represented as: adjacency matrices: Θ(V 2 )adjacency matrices: Θ(V 2 ) adjacency lists: Θ(|V|+|E|)adjacency lists: Θ(|V|+|E|) b Yields single ordering of vertices (order added/deleted from queue is the same) b Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges [How: mark depth from root]

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 36 Brute Force: Review b Based on problem statement and definitions b Typically slow, but may be only known algorithm b Useful to consider first better algorithm frequently knownbetter algorithm frequently known b Examples: Sorting and SearchingSorting and Searching Exhaustive Search:Exhaustive Search: –Pattern Match, TSP, Knapsack, Assignment, Graph (DFS, BFS)Graph (DFS, BFS)

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 37 Brute-Force Strengths and Weaknesses b Strengths wide applicabilitywide applicability simplicitysimplicity yields reasonable algorithms for some important problems (e.g., matrix multiply, sorting, searching, string matching)yields reasonable algorithms for some important problems (e.g., matrix multiply, sorting, searching, string matching) Algorithm may be good enough for small problemAlgorithm may be good enough for small problem Improvement may be too hardImprovement may be too hard Provides yardstick for comparisonProvides yardstick for comparison b Weaknesses rarely yields efficient algorithmsrarely yields efficient algorithms some brute-force algorithms are unacceptably slowsome brute-force algorithms are unacceptably slow not as constructive as some other design techniquesnot as constructive as some other design techniques