Design and Analysis of Algorithms - Chapter 5

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Breadth-First and Depth-First Search
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.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
Theory of Algorithms: Decrease and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms – Chapter 41 Decrease and Conquer II Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 3 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 5: Decrease and Conquer
Chapter 4 Decrease-and-Conquer. Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend.
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Chapter 6: Transform and Conquer
Design and Analysis of Algorithms - Chapter 51 Decrease and Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Introduction to Algorithms
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
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.
RAIK 283: Data Structures & Algorithms
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Spring 2015 Lecture 10: Elementary Graph Algorithms
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,
1 Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance.
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.
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
RAIK 283: Data Structures & Algorithms
UNIT-II DECREASE-AND-CONQUER ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 5:
Chapter 5 Decrease-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
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.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
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++
1 Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting Yang,
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Properties and Applications of Depth-First Search Trees and Forests
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Now, Chapter 5: Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend.
Graph Traversal Text Weiss, § 9.6 Depth-First Search Think Stack Breadth-First Search Think Queue.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Breadth-First Search (BFS)
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review
Chapter 5 Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Chapter 5.
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Chapter 5 Decrease-and-Conquer
Decrease and Conquer Decrease and conquer technique Insertion sort
Analysis and design of algorithm
Presentation transcript:

Design and Analysis of Algorithms - Chapter 5 Decrease and Conquer Reduce problem instance to smaller instance of the same problem and extend solution Solve smaller instance Extend solution of smaller instance to obtain solution to original problem Also referred to as inductive or incremental approach Design and Analysis of Algorithms - Chapter 5

Examples of Decrease and Conquer Decrease by one: Insertion sort Graph search algorithms: DFS BFS Topological sorting Algorithms for generating permutations, subsets Decrease by a constant factor Binary search Fake-coin problems multiplication à la russe Josephus problem Variable-size decrease Euclid’s algorithm Selection by partition Design and Analysis of Algorithms - Chapter 5

Design and Analysis of Algorithms - Chapter 5 What’s the difference? Consider the problem of exponentiation: Compute an Brute Force: Divide and conquer: Decrease by one: Decrease by constant factor: Try to get the students involved in coming up with these: Brute Force: an= a*a*a*a*...*a n Divide and conquer: an= an/2 * an/2 Decrease by one: an= an-1* a (one hopes a student will ask what is the difference with brute force here there is none in the resulting algorithm, of course, but you can arrive at it in two different ways) Decrease by constant factor: an= (an/2)2 (again, if no student asks about it, be sure to point out the difference with divide and conquer. Here there is a significant difference that leads to a much more efficient algorithm – in divide and conquer we re-compute an/2) Design and Analysis of Algorithms - Chapter 5

Design and Analysis of Algorithms - Chapter 5 Graph Traversal Many problems require processing all graph vertices in systematic fashion Graph traversal algorithms: Depth-first search Breadth-first search Design and Analysis of Algorithms - Chapter 5

Design and Analysis of Algorithms - Chapter 5 Depth-first search Explore graph always moving away from last visited vertex Similar to preorder tree traversals Pseudocode for Depth-first-search of graph G=(V,E) DFS(G) count :=0 mark each vertex with 0 (unvisited) for each vertex v∈ V do if v is marked with 0 dfs(v) count := count + 1 mark v with count for each vertex w adjacent to v do if w is marked with 0 dfs(w) Design and Analysis of Algorithms - Chapter 5

Example – undirected graph b e f c d g h Traversal using alphabetical order of vertices. Work through this example in detail, showing the traversal by highlighting edges on the graph and showing how the stack evolves: 8 h 3 7 d 4 stack shown growing upwards 4 e 1 6 c 5 number on left is order that vertex was pushed onto stack 3 f 2 5 g 6 number on right is order that vertex was popped from stack 2 b 7 overlap is because after e, f are popped off stack, g and c 1 a 8 are pushed onto stack in their former locations. order pushed onto stack: a b f e g c d h order popped from stack: e f h d c g b a * show dfs tree as it gets constructed, back edges Depth-first traversal: Design and Analysis of Algorithms - Chapter 5

Design and Analysis of Algorithms - Chapter 5 Types of edges Tree edges: edges comprising forest Back edges: edges to ancestor nodes Forward edges: edges to descendants (digraphs only) Cross edges: none of the above Design and Analysis of Algorithms - Chapter 5

Example – directed graph b c d e f g h Depth-first traversal: Design and Analysis of Algorithms - Chapter 5

Depth-first search: Notes DFS can be implemented with graphs represented as: Adjacency matrices: Θ(V2) Adjacency linked lists: Θ(V+E) Yields two distinct ordering of vertices: preorder: as vertices are first encountered (pushed onto stack) postorder: as vertices become dead-ends (popped off stack) Applications: checking connectivity, finding connected components checking acyclicity searching state-space of problems for solution (AI) Design and Analysis of Algorithms - Chapter 5

Design and Analysis of Algorithms - Chapter 5 Breadth-first search Explore graph moving across to all the neighbors of last visited vertex Similar to level-by-level tree traversals Instead of a stack, breadth-first uses queue Applications: same as DFS, but can also find paths from a vertex to all other vertices with the smallest number of edges Design and Analysis of Algorithms - Chapter 5

Breadth-first search algorithm BFS(G) count :=0 mark each vertex with 0 for each vertex v∈ V do bfs(v) bfs(v) count := count + 1 mark v with count initialize queue with v while queue is not empty do a := front of queue for each vertex w adjacent to a do if w is marked with 0 mark w with count add w to the end of the queue remove a from the front of the queue Design and Analysis of Algorithms - Chapter 5

Example – undirected graph b e f c d g h Breadth-first traversal: Design and Analysis of Algorithms - Chapter 5

Example – directed graph b c d e f g h Breadth-first traversal: Design and Analysis of Algorithms - Chapter 5

Breadth-first search: Notes BFS has same efficiency as DFS and can be implemented with graphs represented as: Adjacency matrices: Θ(V2) Adjacency linked lists: Θ(V+E) Yields single ordering of vertices (order added/deleted from queue is the same) Design and Analysis of Algorithms - Chapter 5

Directed acyclic graph (dag) A directed graph with no cycles Arise in modeling many problems, eg: prerequisite structure food chains Imply partial ordering on the domain Design and Analysis of Algorithms - Chapter 5

Topological sorting Problem: find a total order consistent with a partial order Example: tiger Order them so that they don’t have to wait for any of their food (i.e., from lower to higher, consistent with food chain) human fish sheep shrimp The significance of topological sorting is not brought out in this whimsical example, so it is important to introduce it with a sort of disclaimer and point out the importance of the topological sorting problem. On the other hand, it is more fun to go over this kind of example in class, because you can’t help feeling involved when your turn of getting eaten is being deliberated! It is a good idea to go over the solution using both algorithms and show that you get a different solution. Using alphabetical order to break ties: dfs-based algorithm: traversal yields the components (order popped from stack): tiger- human- fish shrimp-plankton sheep wheat reversing the order we obtain the topological sorting: wheat sheep plankton shrimp fish human tiger 2) source removal algorithm: plankton shrimp fish wheat sheep human tiger NB: problem is solvable iff graph is dag plankton wheat Design and Analysis of Algorithms - Chapter 5

Topological sorting Algorithms DFS-based algorithm: DFS traversal noting order vertices are popped off stack Reverse order solves topological sorting Back edges encountered?→ NOT a dag! Source removal algorithm Repeatedly identify and remove a source vertex, ie, a vertex that has no incoming edges Both Θ(V+E) using adjacency linked lists Design and Analysis of Algorithms - Chapter 5

Variable-size-decrease: Binary search trees Arrange keys in a binary tree with the binary search tree property: Example 1: 5, 10, 3, 1, 7, 12, 9 Example 2: 4, 5, 7, 2, 1, 3, 6 k <k >k What about repeated keys? Design and Analysis of Algorithms - Chapter 5

Searching and insertion in binary search trees Searching – straightforward Insertion – search for key, insert at leaf where search terminated All operations: worst case # key comparisons = h + 1 lg n ≤ h ≤ n – 1 with average (random files) 1.41 lg n Thus all operations have: worst case: Θ(n) average case: Θ(lgn) Bonus: inorder traversal produces sorted list (treesort) 1.41 lg n = 2 ln n Design and Analysis of Algorithms - Chapter 5