Analysis and Design of Algorithms BREADTH FIRST SEARCH, BRANCHING AND BOUNDING
 Instructor Prof. Amrinder Arora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well   Available for study sessions Science and Engineering Hall GWU Algorithms Graph Traversal Techniques, Branch & Bound 2 LOGISTICS
Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph Traversal Techniques, Branch & Bound 3 WHERE WE ARE  Done  Done  DFS, BFS  Done  Done
Knowing that an object exists within a certain distance from us, in an infinite maze, how can we find it? Algorithms Graph Traversal Techniques, Branch & Bound 4 INFINITE MAZE
1. Select an unvisited node s, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node x in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of x. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat the previous step until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1. Algorithms Graph Traversal Techniques, Branch & Bound 5 BREADTH FIRST SEARCH (BFS)
 http://commons.wikimedia.org/wiki/File:Breadth- First-Search-Algorithm.gif  http://www.cs.sunysb.edu/~skiena/combinatorica/a nimations/anim/bfs.gif Algorithms Graph Traversal Techniques, Branch & Bound 6 BFS EXAMPLE
 Observations: The first node visited in each level is the first node from which to proceed to visit new nodes.  This suggests that a queue is the proper data structure to remember the order of the steps. Algorithms Graph Traversal Techniques, Branch & Bound 7 BFS IMPLEMENTATION
Procedure BFS(input: graph G) Queue Q; Integer s, x while (G has an unvisited node) do s := an unvisited node visit(s) Enqueue(s,Q) While (Q is not empty) do x := Dequeue(Q) For (unvisited neighbor y of x) do visit(y) Enqueue(y,Q) Algorithms Graph Traversal Techniques, Branch & Bound 8 BFS PSEUDOCODE
 Robotics  Optimization problems using Branch and Bound Algorithms Graph Traversal Techniques, Branch & Bound 9 BFS APPLICATIONS
 Hamiltonian Cycle  K-clique  K-coloring Algorithms Graph Traversal Techniques, Branch & Bound 10 OTHER GRAPH PROBLEMS
Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph Traversal Techniques, Branch & Bound 11 WHERE WE ARE  Done  Done  Starting today..  Done  Done  Done
 A systematic method for solving optimization problems  A general optimization technique that applies where the greedy method and dynamic programming fail.  Much slower – often leads to exponential time complexities in the worst case. If applied carefully, can lead to algorithms that run reasonably fast on average.  General idea of B&B is a BFS-like search for the optimal solution, but not all nodes get expanded.  Carefully selected criterion determines which node to expand and when  Another criterion tells the algorithm when an optimal solution has been found. Algorithms Graph Traversal Techniques, Branch & Bound 12 BRANCH AND BOUND
 Given jobs, resources, and cost matrix (cost if resource i does job j), how to assign jobs to resources, such that each job is done by a different resource, and the overall cost is minimum?  What is the right D&C strategy?  What is the right BFS strategy? Algorithms Graph Traversal Techniques, Branch & Bound 13 B&B FOR JOB ASSIGNMENT J1 J2 J3 J4 J5 R1 2 4 5 3 11 R2 3 5 6 4 8 R3 4 3 5 7 9 R4 3 4 3 8 12 R5 4 2 6 4 9
What is the lower bound? [In other words, how much will it cost at minimum to do the job assignment?] Each job must be done – so if we add minimum cost per job, then that must be minimum cost Each person must do a job – so if we add minimum cost per resource, then that must be minimum cost Taking the maximum of these two minimums, is a good “lower bound”. Algorithms Graph Traversal Techniques, Branch & Bound 14 LOWER BOUND
 Any random assignment can be an upper bound  Or, we can use a greedy algorithm for upper bound.  In the context of a minimization problem, upper bound means: “Here is a solution that I have already found. We are not interested in solutions that are more expensive than this.” So, in other words, this solution now defines the highest cost (the upper bound on cost) that we are willing to pay.  If we find an even better solution, then we will decrease our upper bound. Algorithms Graph Traversal Techniques, Branch & Bound 15 UPPER BOUND
 Assigning a job to a resource can be a node in the BFS tree  We need to think about the solution space as a tree, and define what a node in the tree means with respect to our problem (job assignment) Algorithms Graph Traversal Techniques, Branch & Bound 16 BRANCHING
 B&B is a general algorithm for finding optimal solutions of various optimization problems, especially in discrete and combinatorial optimization.  2 main steps  Branching  Bounding  (If bounding allows, then) Node Elimination Algorithms Graph Traversal Techniques, Branch & Bound 17 MORE.. (FROM WIKIPEDIA)
 We can consider a maximization problem just as easily in B&B as well.  In case of a maximization problem, the “lower bound” comes from a known solution. The “upper bound” comes from a theoretical/analytical solution that typically relaxes some constraint.  Lower bound signifies: A solution we already know exists  Upper bound signifies: Maximum value we could obtain, even if some constraints were relaxed.  An example of a maximization problem is a 0/1 knapsack problem. Algorithms Graph Traversal Techniques, Branch & Bound 18 B&B FOR MAXIMIZATION PROBLEMS
 Given a set of items, each with a weight and a value, determine which items to include in a collection so that the total weight is less than a given limit and the total value is as large as possible.  Different from fractional knapsack, can only take or not take an item.  No easy solution – greedy solution may not be optimal. Algorithms Graph Traversal Techniques, Branch & Bound 19 0/1 KNAPSACK PROBLEM
 3 Items. Knapsack can hold 50 lbs.  Item 1 weighs 10 lbs, worth 60$  Item 2 weighs 20 lbs, worth 100$  Item 3 weighs 30 lbs, worth 120$  Greedy solution: Item 1 + Item 2, worth = $160  Optimal solution: Item 3 + Item 2, worth = $220 Algorithms Graph Traversal Techniques, Branch & Bound 20 0/1 KNAPSACK EXAMPLE
 Given a complete graph with n vertices, the salesperson wishes to make a tour, visiting each city exactly once and finishing at the city he starts from. Cost of going from city i to city j = c(i,j). Algorithms Graph Traversal Techniques, Branch & Bound 21 TRAVELING SALESPERSON PROBLEM (TSP)
 Input: n jobs, n employees, and an n x n matrix A where Aij be the cost if person i performs job j.  Problem: Find a one-to-one matching of the n employees to the n jobs so that the total cost is minimized.  Formally, find a permutation f such that C(f), where C(f)=A1f(1) + A2f(2) + ... + Anf(n) is minimized. Algorithms Graph Traversal Techniques, Branch & Bound 22 JOB ASSIGNMENT PROBLEM
 Do a Breadth First Search  Evaluate each node for upper and lower bounds  Branch into the “best” node  Terminate if solution meets performance parameters Algorithms Graph Traversal Techniques, Branch & Bound 23 BRANCH AND BOUND TEMPLATE
 Branch and Bound depends upon being able to “bound” each node in the search tree, so that we can evaluate whether or not to expand it.  Lower bound and upper bound depend on the problem Algorithms Graph Traversal Techniques, Branch & Bound 24 BRANCH AND BOUND TEMPLATE (CONT.)
 Using a mixture of upper and lower bounds, we can find when might be a right time to terminate the search. Algorithms Graph Traversal Techniques, Branch & Bound 25 TERMINATION CRITERIA
 B&B is an interesting technique that uses BFS and bounding to eliminate the nodes in the search tree.  Can solve problems that are intrinsically harder.  Time complexity may not be polynomial, but may be practical.  We can return an approximate, usable solution with some bound on performance.  Developing interesting bounds depends upon the problem and uses problem specific insights. Algorithms Graph Traversal Techniques, Branch & Bound 26 SUMMARY
Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph Traversal Techniques, Branch & Bound 27 WHERE WE ARE  Done  Done  Done  Done  Done  Done
 Job Assignment problem, Branch and Bound  Review slides again – Be ready to discuss lower bound and upper bounds  http://en.wikipedia.org/wiki/Branch_and_bound  “An Automatic Method of Solving Discrete Programming Problems”  http://rjlipton.wordpress.com/2012/12/19/branch- and-bound-why-does-it-work/  Preview: NP completeness and lower bound for coins problem. Algorithms Graph Traversal Techniques, Branch & Bound 28 READING ASSIGNMENT/PREVIEW

Graph Traversal Algorithms - Breadth First Search

  • 1.
    Analysis and Design of Algorithms BREADTHFIRST SEARCH, BRANCHING AND BOUNDING
  • 2.
     Instructor Prof. AmrinderArora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well   Available for study sessions Science and Engineering Hall GWU Algorithms Graph Traversal Techniques, Branch & Bound 2 LOGISTICS
  • 3.
    Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph TraversalTechniques, Branch & Bound 3 WHERE WE ARE  Done  Done  DFS, BFS  Done  Done
  • 4.
    Knowing that anobject exists within a certain distance from us, in an infinite maze, how can we find it? Algorithms Graph Traversal Techniques, Branch & Bound 4 INFINITE MAZE
  • 5.
    1. Select anunvisited node s, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node x in the current level, in the order in which the level nodes were visited, visit all the unvisited neighbors of x. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat the previous step until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1. Algorithms Graph Traversal Techniques, Branch & Bound 5 BREADTH FIRST SEARCH (BFS)
  • 6.
  • 7.
     Observations: Thefirst node visited in each level is the first node from which to proceed to visit new nodes.  This suggests that a queue is the proper data structure to remember the order of the steps. Algorithms Graph Traversal Techniques, Branch & Bound 7 BFS IMPLEMENTATION
  • 8.
    Procedure BFS(input: graphG) Queue Q; Integer s, x while (G has an unvisited node) do s := an unvisited node visit(s) Enqueue(s,Q) While (Q is not empty) do x := Dequeue(Q) For (unvisited neighbor y of x) do visit(y) Enqueue(y,Q) Algorithms Graph Traversal Techniques, Branch & Bound 8 BFS PSEUDOCODE
  • 9.
     Robotics  Optimizationproblems using Branch and Bound Algorithms Graph Traversal Techniques, Branch & Bound 9 BFS APPLICATIONS
  • 10.
     Hamiltonian Cycle K-clique  K-coloring Algorithms Graph Traversal Techniques, Branch & Bound 10 OTHER GRAPH PROBLEMS
  • 11.
    Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph TraversalTechniques, Branch & Bound 11 WHERE WE ARE  Done  Done  Starting today..  Done  Done  Done
  • 12.
     A systematicmethod for solving optimization problems  A general optimization technique that applies where the greedy method and dynamic programming fail.  Much slower – often leads to exponential time complexities in the worst case. If applied carefully, can lead to algorithms that run reasonably fast on average.  General idea of B&B is a BFS-like search for the optimal solution, but not all nodes get expanded.  Carefully selected criterion determines which node to expand and when  Another criterion tells the algorithm when an optimal solution has been found. Algorithms Graph Traversal Techniques, Branch & Bound 12 BRANCH AND BOUND
  • 13.
     Given jobs,resources, and cost matrix (cost if resource i does job j), how to assign jobs to resources, such that each job is done by a different resource, and the overall cost is minimum?  What is the right D&C strategy?  What is the right BFS strategy? Algorithms Graph Traversal Techniques, Branch & Bound 13 B&B FOR JOB ASSIGNMENT J1 J2 J3 J4 J5 R1 2 4 5 3 11 R2 3 5 6 4 8 R3 4 3 5 7 9 R4 3 4 3 8 12 R5 4 2 6 4 9
  • 14.
    What is thelower bound? [In other words, how much will it cost at minimum to do the job assignment?] Each job must be done – so if we add minimum cost per job, then that must be minimum cost Each person must do a job – so if we add minimum cost per resource, then that must be minimum cost Taking the maximum of these two minimums, is a good “lower bound”. Algorithms Graph Traversal Techniques, Branch & Bound 14 LOWER BOUND
  • 15.
     Any randomassignment can be an upper bound  Or, we can use a greedy algorithm for upper bound.  In the context of a minimization problem, upper bound means: “Here is a solution that I have already found. We are not interested in solutions that are more expensive than this.” So, in other words, this solution now defines the highest cost (the upper bound on cost) that we are willing to pay.  If we find an even better solution, then we will decrease our upper bound. Algorithms Graph Traversal Techniques, Branch & Bound 15 UPPER BOUND
  • 16.
     Assigning ajob to a resource can be a node in the BFS tree  We need to think about the solution space as a tree, and define what a node in the tree means with respect to our problem (job assignment) Algorithms Graph Traversal Techniques, Branch & Bound 16 BRANCHING
  • 17.
     B&B isa general algorithm for finding optimal solutions of various optimization problems, especially in discrete and combinatorial optimization.  2 main steps  Branching  Bounding  (If bounding allows, then) Node Elimination Algorithms Graph Traversal Techniques, Branch & Bound 17 MORE.. (FROM WIKIPEDIA)
  • 18.
     We canconsider a maximization problem just as easily in B&B as well.  In case of a maximization problem, the “lower bound” comes from a known solution. The “upper bound” comes from a theoretical/analytical solution that typically relaxes some constraint.  Lower bound signifies: A solution we already know exists  Upper bound signifies: Maximum value we could obtain, even if some constraints were relaxed.  An example of a maximization problem is a 0/1 knapsack problem. Algorithms Graph Traversal Techniques, Branch & Bound 18 B&B FOR MAXIMIZATION PROBLEMS
  • 19.
     Given aset of items, each with a weight and a value, determine which items to include in a collection so that the total weight is less than a given limit and the total value is as large as possible.  Different from fractional knapsack, can only take or not take an item.  No easy solution – greedy solution may not be optimal. Algorithms Graph Traversal Techniques, Branch & Bound 19 0/1 KNAPSACK PROBLEM
  • 20.
     3 Items.Knapsack can hold 50 lbs.  Item 1 weighs 10 lbs, worth 60$  Item 2 weighs 20 lbs, worth 100$  Item 3 weighs 30 lbs, worth 120$  Greedy solution: Item 1 + Item 2, worth = $160  Optimal solution: Item 3 + Item 2, worth = $220 Algorithms Graph Traversal Techniques, Branch & Bound 20 0/1 KNAPSACK EXAMPLE
  • 21.
     Given acomplete graph with n vertices, the salesperson wishes to make a tour, visiting each city exactly once and finishing at the city he starts from. Cost of going from city i to city j = c(i,j). Algorithms Graph Traversal Techniques, Branch & Bound 21 TRAVELING SALESPERSON PROBLEM (TSP)
  • 22.
     Input: njobs, n employees, and an n x n matrix A where Aij be the cost if person i performs job j.  Problem: Find a one-to-one matching of the n employees to the n jobs so that the total cost is minimized.  Formally, find a permutation f such that C(f), where C(f)=A1f(1) + A2f(2) + ... + Anf(n) is minimized. Algorithms Graph Traversal Techniques, Branch & Bound 22 JOB ASSIGNMENT PROBLEM
  • 23.
     Do aBreadth First Search  Evaluate each node for upper and lower bounds  Branch into the “best” node  Terminate if solution meets performance parameters Algorithms Graph Traversal Techniques, Branch & Bound 23 BRANCH AND BOUND TEMPLATE
  • 24.
     Branch andBound depends upon being able to “bound” each node in the search tree, so that we can evaluate whether or not to expand it.  Lower bound and upper bound depend on the problem Algorithms Graph Traversal Techniques, Branch & Bound 24 BRANCH AND BOUND TEMPLATE (CONT.)
  • 25.
     Using amixture of upper and lower bounds, we can find when might be a right time to terminate the search. Algorithms Graph Traversal Techniques, Branch & Bound 25 TERMINATION CRITERIA
  • 26.
     B&B isan interesting technique that uses BFS and bounding to eliminate the nodes in the search tree.  Can solve problems that are intrinsically harder.  Time complexity may not be polynomial, but may be practical.  We can return an approximate, usable solution with some bound on performance.  Developing interesting bounds depends upon the problem and uses problem specific insights. Algorithms Graph Traversal Techniques, Branch & Bound 26 SUMMARY
  • 27.
    Algorithms Analysis Asymptotic NP- Completeness Design D&C Greedy DP Graph B&B Applications Algorithms Graph TraversalTechniques, Branch & Bound 27 WHERE WE ARE  Done  Done  Done  Done  Done  Done
  • 28.
     Job Assignmentproblem, Branch and Bound  Review slides again – Be ready to discuss lower bound and upper bounds  http://en.wikipedia.org/wiki/Branch_and_bound  “An Automatic Method of Solving Discrete Programming Problems”  http://rjlipton.wordpress.com/2012/12/19/branch- and-bound-why-does-it-work/  Preview: NP completeness and lower bound for coins problem. Algorithms Graph Traversal Techniques, Branch & Bound 28 READING ASSIGNMENT/PREVIEW