CSC 8301 Design and Analysis of Algorithms: Graph Traversal Professor Henry Carter Fall 2016
Exhaustive Search Combinatorial/Optimization problems often require exhaustive search Exhaustive search is critical in many other problems The algorithm for search can be optimized based on data structure and application 2
Exhaustive (Graph) Search Graphs are a very common construction in computer science Specially structured graphs called trees are a common way to store data How do we search graph vertices in an ordered fashion? 3
Depth-First Search Searches vertices by moving as far as possible from the starting node Produces a set of trees containing the searched vertices called a Depth-First Search Forest Implemented with a stack (last in first out) data structure (or a recursive call stack) 4
Breadth-First Search Searches vertices by staying as near as possible from the starting node Also outputs a search forest Implemented with a queue (first in first out) data structure 5
BFS Algorithm 6
BFS: Example Graph 7
BFS: Analysis Matrix: List 8
What s the point? Mathematical: acyclicity, minimum-edge paths Networks (communication, social) are often represented as trees Breadth first search finds nearby neighbors (e.g., closest social connection, closest BitTorrent peer) Also used in garbage collection (e.g., Java) 9
Ordering In some application, the order of traversal matters BFS yields a unique ordering (objects enter/exit the queue in one order) DFS yields two orderings depending on when vertices are recorded Pre-order Post-order 10
BFS Order 11
DFS Pre-order 12
DFS Post-order 13
Practice f c b e d a g 14
Decrease-and-Conquer Previous chapters focus on solving the full-sized problem Brute force: not efficient Some problems can (should) be broken into smaller versions 15
Decrease-and-Conquer Solutions that use relations between the problem instance and a smaller instance Comes in three delicious flavors: Decrease by a constant Decrease by a constant factor Variable decrease 16
Decrease by a constant Reduces problem size by an additive constant (like 1) Can be iterative OR recursive Example: Exponentiation 17
Decrease by a constant factor Divide problem size by a constant (like 2) Common in recursion (divide-and-conquer) Example: Exponentiation v. 2 18
Variable decrease Reduction depends on problem and input Includes problems that aren t clearly reduce-by-constant or reduce-by-constant-factor Example: Euclid s algorithm 19
Applying decrease-and-conquer: sorting Insertion sort iteratively inserts an element into a presorted array Differs from previous algorithms that do not assume any previous sorting Iterative application allows for sorting a completely unsorted list 20
Insertion Sort Algorithm Insertion Sort(A[0..n-1]) for i 1 to n-1 do v A[i]; j i - 1 while j 0 and A[j] > v do A[j+1] A[j] j j - 1 A[j+1] v 21
Insertion Sort Example 22
Insertion Sort Analysis C worst (n) = C best (n) = C avg (n) 23
Insertion Sort Facts Good elementary sorting algorithm (very good on nearly-sorted data) Can be extended in many ways (e.g., Shellsort) Equivalent order of growth to selection sort in worst/average cases 24
Ferrying Soldiers A detachment of n soldiers must cross a wide and deep river with no bridge in sight. They notice two 12-yearold boys playing in a rowboat by the shore. The boat is so tiny, however, that it can only hold two boys or one soldier. How can the soldiers get across the river and leave the boys in joint possession of the boat? How many times need the boat pass from shore to shore? 25
Recap Decrease-and-conquer solutions relate a problem to a smaller instance Three varieties: Decrease-by-constant Decrease-by-constant-factor Variable Decrease Insertion sort iteratively inserts elements into a previously sorted list Great for nearly-sorted data 26
Next Time... Levitin Chapter 4.2, 4.4 Remember, you need to read it BEFORE you come to class! Homework 3.5: 4, 6, 7 4.1: 2, 4, 5, 9, 11 27