CHAPTER 4 Searching. Algorithm 4.1.1 Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found,

Slides:



Advertisements
Similar presentations
CHAPTER 7 Greedy Algorithms.
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
CMSC 341 Graphs 3. Adjacency Table Implementation Uses table of size |V|  |V| where each entry (i,j) is boolean –TRUE if there is an edge from vertex.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
5-1 Chapter 5 Tree Searching Strategies. 5-2 Satisfiability problem Tree representation of 8 assignments. If there are n variables x 1, x 2, …,x n, then.
Algorithms and Data Structures
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 3 The Greedy Method 3.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CHAPTER 10 P and NP. Algorithm Crossword Puzzle This algorithm solves a crossword puzzle, represented by a Boolean matrix D[i,j], 1 = i,j = n,
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Chapter 4  4 Basic Algorithms –Binary Search –Depth-First Search –Breadth-First Search –Topological Sort.
Backtracking.
Important Problem Types and Fundamental Data Structures
Data Structures Using C++ 2E Chapter 6 Recursion.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Announcements Exam Next week Review on Monday in class. Review on Wednesday in lab. DisjointSets lab program due tonight. BucketSort lab due next Wednesday.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dr. Jouhaina Chaouachi Siala
Data Structures Using C++ 2E Chapter 6 Recursion.
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,
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Graphs – Adjacency Matrix
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSE 373 Topological Sort Graph Traversals
CS120 Graphs.
Search Related Algorithms
CMSC 341 Graphs 3.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Backtracking and Branch-and-Bound
Chapter 16 1 – Graphs Graph Categories Strong Components
GRAPH TRAVERSAL.
Presentation transcript:

CHAPTER 4 Searching

Algorithm Binary Search This algorithm searches for the value key in the nondecreasing array L[i],..., L[j]. If key is found, the algorithm returns an index k such that L[k] equals key. If key is not found, the algorithm returns -1, which is assumed not to be a valid index. Input Parameters: L,i,j,key Output Parameters: None bsearch(L,i,j,key) { while (i = j) { k = (i + j)/2 if (key == L[k]) // found return k if (key < L[k]) // search first part j = k - 1 else // search second part i = k + 1 } return -1 // not found }

Algorithm Depth-First Search This algorithm executes a depth-first search beginning at vertex start in a graph with vertices 1,..., n and outputs the vertices in the order in which they are visited. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i, and next, the next node in the linked list or null, for the last node in the linked list. To track visited vertices, the algorithm uses an array visit; visit[i] is set to true if vertex i has been visited or to false if vertex i has not been visited.

Input Parameters: adj,start Output Parameters: None dfs(adj,start) { n = adj.last for i = 1 to n visit[i] = false dfs_recurs(adj,start) } dfs_recurs(adj,start) { println(start) visit[start] = true trav = adj[start] while (trav != null) { v = trav.ver if (!visit[v]) dfs_recurs(adj,v) trav = trav.next }

Algorithm Breadth-First Search This algorithm executes a breadth-first search beginning at vertex start in a graph with vertices 1,..., n and outputs the vertices in the order in which they are visited. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i, and next, a reference to the next node in the linked list or null, for the last node in the linked list. To track visited vertices, the algorithm uses an array visit; visit[i] is set to true if vertex i has been visited or to false if vertex i has not been visited. The algorithm uses an initially empty queue q to store pending current vertices. The expression q.enqueue(val) adds val to q. The expression q.front() returns the value at the front of q but does not remove it. The expression q.dequeue() removes the item at the front of q. The expression q.empty() returns true if q is empty or false if q is not empty.

Input Parameters: adj,start Output Parameters: None bfs(adj,start) { n = adj.last for i = 1 to n visit[i] = false visit[start] = true println(start) q.enqueue(start) // q is an initially empty queue while (!q.empty()) { current = q.front() q.dequeue() trav = adj[current] while (trav != null) { v = trav.ver if (!visit[v]) { visit[v] = true println(v) q.enqueue(v) } trav = trav.next }

Algorithm Finding Shortest Path Lengths Using Breadth-First Search This algorithm finds the length of a shortest path from the start vertex start to every other vertex in a graph with vertices 1,..., n. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i, and next, a reference to the next node in the linked list or null, for the last node in the linked list. In the array length, length[i] is set to the length of a shortest path from start to vertex i if this length has been computed or to ∞ if the length has not been computed. If there is no path from start to i, when the algorithm terminates, length[i] is ∞.

Input Parameters: adj,start Output Parameters: length shortest_paths(adj,start,length) { n = adj.last for i = 1 to n length[i] = ∞ length[start] = 0 q.enqueue(start) // q is an initially empty queue while (!q.empty()) { current = q.front() q.dequeue() trav = adj[current] while (trav != null) { v = trav.ver if (length[v] == ∞) { length[v] = 1 + length[current] q.enqueue(v) } trav = trav.next }

Algorithm Topological Sort This algorithm computes a topological sort of a directed acyclic graph with vertices 1,..., n. The vertices in the topological sort are stored in the array ts. The graph is represented using adjacency lists; adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i. Each node has members ver, the vertex adjacent to i, and next, the next node in the linked list or null, for the last node in the linked list. To track visited vertices, the algorithm uses an array visit; visit[i] is set to true if vertex i has been visited or to false if vertex i has not been visited.

Input Parameters: adj Output Parameters: ts top_sort(adj,ts) { n = adj.last // k is the index in ts where the next vertex is to be // stored in topological sort. k is assumed to be global. k = n for i = 1 to n visit[i] = false for i = 1 to n if (!visit[v]) top_sort_recurs(adj,i,ts) } top_sort_recurs(adj,start,ts) { visit[start] = true trav = adj[start] while (trav != null) { v = trav.ver if (!visit[v]) top_sort_recurs(adj,v,ts) trav = trav.next } ts[k] = start k = k - 1 }

Algorithm n-Queens, Initial Version The n-queens problem is to place n queens on an n × n board so that no two queens are in the same row, column, or diagonal. Using backtracking, this algorithm outputs all solutions to this problem. We place queens successively in the columns beginning in the left column and working from top to bottom. When it is impossible to place a queen in a column, we return to the previous column and move its queen down.

n_queens(n) { rn_queens(1,n) } rn_queens(k,n) { for row[k] = 1 to n if (position_ok(k,n)) if (k == n) { for i = 1 to n print(row[i] + “ ”) println() } else rn_queens(k + 1,n) } position_ok(k,n) for i = 1 to k - 1 // abs is absolute value if (row[k] == row[i] || abs(row[k] - row[i]) == k - i) return false return true }

Algorithm Solving the n-Queens Problem Using Backtracking The n-queens problem is to place n queens on an n × n board so that no two queens are in the same row, column, or diagonal. Using backtracking, this algorithm outputs all solutions to this problem. We place queens successively in the columns beginning in the left column and working from top to bottom. When it is impossible to place a queen in a column, we return to the previous column and move its queen down.

The value of row[k] is the row where the queen in column k is placed. The algorithm begins when n_queens calls rn_queens(1,n). When rn_queens(k, n) is called, queens have been properly placed in columns 1 through k - 1, and rn_queens(k,n) tries to place a queen in column k. If it is successful and k equals n, it prints a solution. If it is successful and k does not equal n, it calls rn_queens(k + 1,n). If it is not successful, it backtracks by returning to its caller rn_queens(k - 1,n). The value of row_used[r] is true if a queen occupies row r and false otherwise. The value of ddiag_used[d] is true if a queen occupies ddiag diagonal d and false otherwise. According to the numbering system used, the queen in column k, row r, is in ddiag diagonal n - k + r. The value of udiag_used[d] is true if a queen occupies udiag diagonal d and false otherwise. According to the numbering system used, the queen in column k, row r, is in udiag k + r - 1. The function position_ok(k,n) assumes that queens have been placed in columns 1 through k - 1. It returns true if the queen in column k does not conflict with the queens in columns 1 through k- 1 or false if it does conflict.

Input Parameter: n Output Parameters: None n_queens(n) { for i = 1 to n row_used[i] = false for i = 1 to 2 * n - 1 ddiag_used[i] = udiag_used[i] = false rn_queens(1,n) }...

// When rn_queens(k,n) is called, queens have been // properly placed in columns 1 through k - 1. rn_queens(k,n) { for row[k] = 1 to n if (position_ok(k,n)) row_used[row[k]] = true ddiag_used[n - k + row[k]] = true udiag_used[k + row[k] - 1] = true if (k == n) { // Output a solution. Stop if only one // solution is desired. for i = 1 to n print(row[i] + “ ”) println() } else rn_queens(k + 1,n) row_used[row[k]] = false ddiag_used[n - k + row[k]] = false udiag_used[k + row[k] - 1] = false }...

// position_ok(k,n) returns true if the queen in column k // does not conflict with the queens in columns 1 // through k - 1 or false if it does conflict. position_ok(k,n) return !(row_used[row[k]] || ddiag_used[n - k + row[k]] || udiag_used[k + row[k] - 1 ]) }

Form of Backtracking Algorithm Suppose that we solve a problem using backtracking as in Algorithm in which the solution is of the form x[1],..., x[n]. Suppose also that the values of x[i] are in the set S (e.g., in Algorithm 4.5.2, S = {1,..., n}). We require a function bound(k) with the following property. Whenever x[1],..., x[k - 1] is a partial solution and x[k] has been assigned a value, then bound(k) has to return true if x[1],..., x[k] is a partial solution and false otherwise. The key to writing a useful back- tracking algorithm is to write an efficient bound function that eliminates many potential nodes from the search tree.

The general form of a backtracking algorithm is backtrack(n) { rbacktrack(1,n) } rbacktrack(k,n) { for each x[k]  S if (bound(k)) if (k == n) {// Output a solution. Stop if only for i = 1 to n// one solution is desired. print(x[i] + “ ”) println() } else rbacktrack(k + 1, n) }

Algorithm Searching for a Hamiltonian Cycle This algorithm inputs a graph with vertices 1,..., n. The graph is represented as an adjacency matrix adj; adj[i][j] is true if (i,j) is an edge or false if it is not an edge. If the graph has a Hamiltonian cycle, the algorithm computes one such cycle (x[1],..., x[n], x[1]). If the graph has no Hamiltonian cycle, the algorithm returns false and the contents of the array x are not specified. In the array used, used[i] is true if i has been selected as one of the vertices in a potential Hamiltonian cycle or false if i has not been selected. The function path_ok(adj,k,x) assumes that (x[1],..., x[k - 1]) is a path from x[1] to x[k - 1] and that the vertices x[1],..., x[k - 1] are distinct. It then checks whether x[k] is different from each of x[1],..., x[k - 1] and whether (x[k - 1], x[k]) is an edge. If k = n, path_ok also checks whether (x[n], x[1]) is an edge.

Input Parameter: adj Output Parameter: x hamilton(adj,x) { n = adj.last x[1] = 1 used[1] = true for i = 2 to n used[i] = false rhamilton(adj,2,x) } rhamilton(adj,k,x) { n = adj.last for x[k] = 2 to n if (path_ok(adj,k,x)) { used[x[k]] = true if (k == n || rhamilton(adj,k + 1,x)) return true used[x[k]] = false } return false }...

path_ok(adj,k,x) { n = adj.last if (used[x[k]]) return false if (k < n) return adj[x[k - 1]][x[k]] else return adj[x[n - 1]][x[n]] && adj[x[1]][x[n]] }