1 6.001 SICP Search Algorithms Why search Data structures that support search Breadth first vs. depth first.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

College of Information Technology & Design
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
GRAPHS Trees Without Rules. Graph  A data structure that consists of a set of nodes (called vertices) and a set of edges that relate the nodes to each.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Advanced Data Structures
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
Jeffrey D. Ullman Stanford University Flow Graph Theory.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
P2P Course, Structured systems 1 Introduction (26/10/05)
Chapter Tow Search Trees BY HUSSEIN SALIM QASIM WESAM HRBI FADHEEL CS 6310 ADVANCE DATA STRUCTURE AND ALGORITHM DR. ELISE DE DONCKER 1.
Tree.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
A Level Computer Science Topic 9: Data Structures T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
1 Searching the dictionary ADT binary search binary search trees.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
1 University of Qom Information Retrieval Course Web Search (Spidering) Based on:
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
Binary Search Trees (BST)
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Brian Williams, Fall 041 Analysis of Uninformed Search Methods Brian C. Williams Sep 21 st, 2004 Slides adapted from: Tomas Lozano Perez,
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.
Simple Search Algorithm
Top 50 Data Structures Interview Questions
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Csc 2720 Instructor: Zhuojun Duan
COMP 103 Binary Search Trees.
i206: Lecture 13: Recursion, continued Trees
Data Structures and Algorithms for Information Processing
Spanning Trees Longin Jan Latecki Temple University based on slides by
The Metacircular Evaluator
Dictionaries < > = /9/2018 3:06 AM Dictionaries
Graphs Part 2 Adjacency Matrix
Lecture 12 CS203 1.
Binary Search Trees.
Dictionaries < > = /17/2019 4:20 PM Dictionaries
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
List and list operations (continue).
6.001 SICP Interpretation Parts of an interpreter
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Algorithm Course Dr. Aref Rashad
Presentation transcript:

SICP Search Algorithms Why search Data structures that support search Breadth first vs. depth first

2 Why search? Structuring data is only part of the issue Finding information distributed throughout complex data structures is often equally important

3 Examples of Search Look up a friend’s address state town type business Look up a business

4 Examples of search Find a document on the Web

5 Web search and Web spiders Web == (growing) collection of documents (unordered) Document == text (& other possible material) plus links to other documents Stages: Documents identified by URLs ( Using HTTP protocol, can retrieve document, plus encoding information (ASCII, HTML, GIF, JPG, MPEG) Program can extract links from document, hence URLs to other documents Spider starts within initial set of URLs, retrieves documents, adds links, and keeps going. Each time it does some work, e.g. index of documents (keywords)

6 Simple search Unordered collection of entries treat as a list (define (find1 compare query set) (cond ((null? Set) ‘not-here) ((compare query (car set)) (car set)) (else (find1 compare query (cdr set))))) Order of growth is linear – if not there –n; if there, n/2

7 Simple search Ordered collection < < << Can use ordering, assume less? Same? (define (find2 less? Same? Query set) (cond ((null? Set) ‘not-here) ((less? Query (car set)) ‘not-here) ((same? Query (car set)) (car set)) (else (find2 less? Same? Query (cdr set))))) Order of growth is linear – if not there -- n/2; if there -- n/2

8 Better search Use the ordering directly – binary tree leftright entry All entries to left of node are less than current entry All entries to right of node are greater than current entry

9 Searching a binary tree Assume an ADT Node: left, entry, right Tree: root node (define (find3 less? Same? Query tree) (cond ((null? Tree) ‘not-here) ((same? Query (entry tree)) (entry tree)) ((less? Query (entry tree)) (find3 less? Same? Query (left tree))) (else (find3 less? Same? Query (right tree))))) Order of growth – depends on the tree

10 Searching a binary tree leftright entry Log n Order of growth for balanced tree – logarithmic If not there – log n; if there – log n Worst case – linear list, reverts to linear growth

11 Hierarchical search What about our business look up example? Can use a linear list or a tree at each level E.g. 50 states, 100 towns, 100 types, 10 businesses, 1 lookup every 1 millisecond Not thereThere Unordered5000s2500s Ordered linear2500s Binary tree0.02s Hierarchical linear 300s Hierarchical tree 0.02s

12 Searching the Web Can we put an ordering on the documents on the Web? By title? – but then need to know name of document in which one wants to find information By keyword? – but then need multiple copies for each keyword How does one add new documents to the ordered data structure efficiently? The Web is really a directed graph! Nodes are documents Edges are embedded links to other documents

13 A new data structure Directed graph: Nodes Edges (with direction: from  to) Children == set of nodes reachable from current node in one step along an edge Cycle == collection of nodes s.t. can return to start node by sequence of steps along edges

14 Directed graph – an ADT ;;; Graph Abstraction ;;; ;;; Graph a collection of Graph-Entries ;;; Graph-Entry a node, outgoing children from the ;;; node, and contents for the node ;;; Node = symbol a symbol label or name for the node ;;; Contents = anytype the contents for the node ;; ;; Graph-Entry ; make-graph-entry: Node,list,Contents -> Entry (define (make-graph-entry node children contents) (list 'graph-entry node children contents)) (define (graph-entry? entry) ; anytype -> boolean (and (pair? entry) (eq? 'graph-entry (car entry)))) ; Get the node (the name) from the Graph-Entry (define (graph-entry->node entry) ; Graph-Entry -> Node (if (not (graph-entry? entry)) (error "object not entry: " entry) (first (cdr entry))))

15 Directed graph – an ADT ; Get the children (a list of outgoing node names) ; from the Graph-Entry (define (graph-entry->children entry) ; Graph-Entry -> list (if (not (graph-entry? entry)) (error "object not entry: " entry) (second (cdr entry)))) ; Get the contents from the Graph-Entry (define (graph-entry->contents entry) ; Graph-Entry -> Contents (if (not (graph-entry? entry)) (error "object not entry: " entry) (third (cdr entry))))

16 Directed graph – an ADT (define (make-graph entries) ; list -> Graph (cons 'graph entries)) (define (graph? graph) ; anytype -> boolean (and (pair? graph) (eq? 'graph (car graph)))) (define (graph-entries graph ; Graph -> list (if (not (graph? graph)) (error "object not a graph: " graph) (cdr graph))) (define (graph-root graph) ; Graph -> Node|null (let ((entries (graph-entries graph))) (if (null? entries) #f (graph-entry->node (car entries)))))

17 Searching a directed graph Start at some node Traverse graph, looking for a particular goal node Key choice is in how to traverse the graph Breadth first Depth first

18 Depth first search A LK J D IHGFE C B

19 Breadth first search A LK J D IHGFE C B

20 General search framework ;; search: Seeker, Node  symbol (define (search next-place start-node) (define (loop node) (let ((next-node (next-place node))) (cond ((eq? Next-node #t) ‘FOUND) ((eq? Next-node #f) ‘NOT-FOUND) (else (loop next-node))))) (loop start-node)) A seeker captures knowledge about the order in which to search the nodes of a graph, and how to detect if reached goal: Returns #t if current node is goal Returns #f if no more nodes to visit Otherwise returns next node to visit

21 Strategies for creating search seekers Strategy should use: A graph A goal? Procedure: goal?: Node  boolean A children procedure, where children: (Graph, Node)  list Children should deliver the nodes that can be reached by a directed edge from a given node

22 A depth-first strategy (define (depth-first-strategy graph goal? Children) (let ((*to-be-visited* ‘())) (define (where-next? Here) (set! *to-be-visited* (append (children graph here) *to-be-visited*)) (cond ((goal? Here) #t) ((null? *to-be-visited*) #f) (else (let ((next (car *to-be-visited*))) (set! *to-be-visited* (cdr *to-be-visited*)) next)))) where-next?))

23 Tracking the depth first seeker Loop starts at *to-be-visited* where-next? checks returns A (B C D)A (C D) B B (E F G H C D)B (F G H C D) E E E (G H C D) F G (K L H C D)G (L H C D) K F (G H C D)F (H C D) G

24 A Breadth-first strategy (define (breadth-first-strategy graph goal? Children) (let ((*to-be-visited* ‘())) (define (where-next? Here) (set! *to-be-visited* (append *to-be-visited* (children graph here))) (cond ((goal? Here) #t) ((null? *to-be-visited*) #f) (else (let ((next (car *to-be-visited*))) (set! *to-be-visited* (cdr *to-be-visited*)) next)))) where-next?))

25 Tracking the breadth first seeker Loop starts at *to-be-visited* where-next? checks returns A (B C D)A (C D) B B (C D E F G H)B (D E F G H) C C (D E F G H I J)C (E F G H I J) D E (F G H I J)E (G H I J) F D (E F G H I J)D (F G H I J) E

26 Dealing with cycles and sharing We “cheated” in our earlier version – only works on trees Extend by marking where we’ve been (node-visited! Node) remembers that we’ve seen this node (deja-vu? Node) tests whether we’ve visited this node These procedures will need some internal state to keep track of visited nodes.

27 A better search method (define (depth-first-strategy graph goal? Children) (let ((mark-procedures (make-mark-procedures))) (let ((deja-vu? (car mark-procedures)) (node-visited! (cadr mark-procedures)) (*to-be-visited* ‘())) (define (try-node candidates) (cond ((null? Candidates) #f) ((deja-vu? (car candidates)) (try-nodes (cdr candidates))) (else (set! *to-be-visited* (cdr candidates)) (car candidates)))) (define (where-next? Here) (node-visited! Here) (set! *to-be-visited* (append (children graph here) *to-be-visited*)) (if (goal? Here) #t (try-node *to-be-visited*))) where-next?)))

28 Variations on a search theme A central issue is deciding how to order the set of nodes to be searched Suppose you could guess at how close you were to the goal Then you could use that to reorder the set of nodes to be searched, trying the better ones first E.g., measure closeness of document to query, or how often keywords show up in document, and assume that connected documents are similar