Graph Traversal Algorithms - Breadth First Search

28
Analysis and Design of Algorithms BREADTH FIRST SEARCH, BRANCHING AND BOUNDING

Transcript of Graph Traversal Algorithms - Breadth First Search

Page 1: Graph Traversal Algorithms - Breadth First Search

Analysis and Design of Algorithms

BREADTH FIRST SEARCH, BRANCHING

AND BOUNDING

Page 2: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 2

InstructorProf. Amrinder [email protected] copy TA on emailsPlease feel free to call as well

Available for study sessionsScience and Engineering HallGWU

Algorithms

LOGISTICS

Textbook

Second Edition

Cognella Academic Publisher

www.cognella.com

ISBN: 978-1-63487-073-3

Page 3: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 3

Algorithms

AnalysisAsymptotic

NP-Completene

ss

Design

D&C

Greedy

DP

Graph

B&B

Applications

Algorithms

WHERE WE ARE

Done

Done

DFS, BFS

Done

Done

Page 4: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 4

Knowing that an object exists within a certain distance from us, in an infinite maze, how can we find it?

Algorithms

INFINITE MAZE

Page 5: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 5

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

BREADTH FIRST SEARCH (BFS)

Page 6: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 6

http://commons.wikimedia.org/wiki/File:Breadth-First-Search-Algorithm.gif

http://www.cs.sunysb.edu/~skiena/combinatorica/animations/anim/bfs.gif

Algorithms

BFS EXAMPLE

Page 7: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 7

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

BFS IMPLEMENTATION

Page 8: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 8

Procedure BFS(input: graph G)Queue Q; Integer s, xwhile (G has an unvisited node) dos := an unvisited nodevisit(s)Enqueue(s,Q)While (Q is not empty) dox := Dequeue(Q)For (unvisited neighbor y of x) dovisit(y)Enqueue(y,Q)

Algorithms

BFS PSEUDOCODE

Page 9: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 9

RoboticsOptimization problems using Branch and

Bound

Algorithms

BFS APPLICATIONS

Page 10: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 10

Hamiltonian CycleK-cliqueK-coloring

Algorithms

OTHER GRAPH PROBLEMS

Page 11: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 11

Algorithms

AnalysisAsymptotic

NP-Completene

ss

Design

D&C

Greedy

DP

Graph

B&B

Applications

Algorithms

WHERE WE ARE

Done

Done

Starting today..

Done

Done

Done

Page 12: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 12

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

BRANCH AND BOUND

Page 13: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & 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

B&B FOR JOB ASSIGNMENT

J1 J2 J3 J4 J5R1 2 4 5 3 11R2 3 5 6 4 8R3 4 3 5 7 9R4 3 4 3 8 12R5 4 2 6 4 9

Page 14: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 14

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

LOWER BOUND

Page 15: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 15

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

UPPER BOUND

Page 16: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 16

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

BRANCHING

Page 17: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 17

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

MORE.. (FROM WIKIPEDIA)

Page 18: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 18

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

B&B FOR MAXIMIZATION PROBLEMS

Page 19: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 19

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

0/1 KNAPSACK PROBLEM

Page 20: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 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

0/1 KNAPSACK EXAMPLE

Page 21: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 21

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

TRAVELING SALESPERSON PROBLEM (TSP)

Page 22: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 22

Input: n jobs, n employees, and an n x n matrix A where A i j 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

JOB ASSIGNMENT PROBLEM

Page 23: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 23

Do a Breadth First SearchEvaluate each node for upper and lower

boundsBranch into the “best” nodeTerminate if solution meets performance

parameters

Algorithms

BRANCH AND BOUND TEMPLATE

Page 24: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 24

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

BRANCH AND BOUND TEMPLATE (CONT.)

Page 25: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 25

Using a mixture of upper and lower bounds, we can find when might be a right time to terminate the search.

Algorithms

TERMINATION CRITERIA

Page 26: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 26

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

SUMMARY

Page 27: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 27

Algorithms

AnalysisAsymptotic

NP-Completene

ss

Design

D&C

Greedy

DP

Graph

B&B

Applications

Algorithms

WHERE WE ARE

Done

Done

Done

Done

Done

Done

Page 28: Graph Traversal Algorithms - Breadth First Search

Graph Traversal Techniques, Branch & Bound 28

Job Assignment problem, Branch and BoundReview slides again – Be ready to discuss

lower bound and upper boundshttp://en.wikipedia.org/wiki/Branch_and_boun

d“An Automatic Method of Solving Discrete

Programming Problems”http://rjlipton.wordpress.com/2012/12/19/bran

ch-and-bound-why-does-it-work/

Preview: NP completeness and lower bound for coins problem.

Algorithms

READING ASSIGNMENT/PREVIEW