1 / 15

SEARCHING GRAPHS

SEARCHING GRAPHS. Chapter 16.2. INTRO. in our tree discussion, learned three ways of traversing a graph: Preorder (prefix) Inorder (infix) Postorder (postorder) traversing easy if we begin at root (can get to every other node from there)

gaston
Download Presentation

SEARCHING GRAPHS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SEARCHING GRAPHS Chapter 16.2

  2. INTRO • in our tree discussion, learned three ways of traversing a graph: • Preorder (prefix) • Inorder (infix) • Postorder (postorder) • traversing easy if we begin at root (can get to every other node from there) • in a graph, however, some nodes may not be reachable from a given node, making it impossible to traverse the whole graph • graphs are used to model many real-world systems • ex: towns and connecting railways • some towns may not be reachable on the graph • if there is no rail service to that town • two basic search algorithms for graphs: • 1) breadth-first • 2) depth-first

  3. DEPTH-FIRST SEARCH (TREE) • Consider the tree: • Perform depth-first: • Visit each node • Look at its children • Start at 10: • Look at 5 • Now children of 5: • Look at 2 • Child of 2: • Look at 1 • backtrack to 5 • Look at 7 • Backtrack to 10 • Repeat for right child of 10 • Handle backtracking with a STACK 10 5 12 2 7 11 14 17 1

  4. DEPTH-FIRST (GRAPH) • same concept as with a tree: • Algorithm: • 1) start at a node A • 2) go to an adjacent unvisited vertex B: • a) mark it as visited • b) push it onto the stack • 3) repeat step 2 until no more adjacent nodes to B remain unvisited • a) pop current node off stack • b) go to next adjacent unvisited vertex, and begin process again • 4) if no more unvisited nodes in graph remain, search is done

  5. DEPTH-FIRST (GRAPH) • Example: • Start at A: VisitStack A A B AB F ABF C ABFC BACKTRACK ABF H ABFH Backtrack ABF Backtrack AB Backtrack A D AD B F H A C G I D E

  6. DEPTH-FIRST (cont.) • Example (cont.) Visit Stack E ADE Backtrack AD G ADG I ADGI Backtrack ADG Backtrack AD Backtrack A Backtrack empty B F H A C G I D E

  7. DEPTH-FIRST (cont.) • the algorithm can easily be written as a recursive function and by traversing the adjacency matrix/list for the graph • from previous example: adjacency list: • A-->B-->C-->D-->0 • B-->F-->0 • C-->B-->0 • D-->E-->G-->0 • E-->0 • F-->C-->H0 • G-->I0 • H-->0 • I-->0

  8. DEPTH-FIRST (cont.) • Algorithm (adjacency list): • start: • mark all nodes as unvisited • depthfirstsearch(A): • for each vertex u in A's list • mark it as visited • depthfirstsearch(u) • once all A's list has been exhausted, move to next UNVISITED vertex: • depthfirstsearch(D) • repeat until all nodes have been visited

  9. DEPTH-FIRST (cont.) • Comments: • Disadv: • is possible that some nodes may still not get visited by depth-search (for example, start at B) • Adv: • good algorithm because it can yield lots of information about the structure of the graph

  10. BREADTH-FIRST SEARCH (TREE) • Again consider: • we perform breadth-first by starting at a node, then looking at all children of that node: • Start 10: • Look 5 12 • Look at their children: • Look 2,7,11,14 • And their children: • Look 1, 17 • When no more children, search done • Since we track where we look first, a queue is used 10 5 12 2 7 11 14 17 1

  11. BREADTH-FIRST (GRAPH) • again, similar to that of a tree • use a queue to store vertices as they are visited • Algorithm • Mark all vertices as UNVISITED • 1) start at a vertex A • 2) visit the next UNVISITED vertex that is adjacent to the current one • a) Mark it as VISITED • b) Insert it into the queue • c) repeat 2 until no more unvisited vertices adjacent to current one • 3) Remove next vertex from queue and make it the current vertex • 4) Repeat step 2 until no more vertices are unvisited • 5) If no unvisited vertices remain (queue is empty) you are done

  12. Breath-First (Graph) • Again consider: • Start at A: VisitQueue A empty B (A->B) B C (A->C) BC D (A->D) BCD no more children of A: remove B CD F CDF no more children of B: remove C DF B already visited, no more children: remove D F B F H A C G I D E

  13. BREADTH-FIRST (cont.) • Example (cont.) VisitQueue E FE G FEG remove F EG visit H EGH remove E GH remove G H I HI remove H I remove I queue empty no children of I Queue Empty - Search Done B F H A C G I D E

  14. BREADTH-FIRST (discussion) • Implementation: • can easily implement this algorithm with an adjacency list by simply searching the linked list for each node in the list (how would you write it?) • Comments: • again, may be situations where not all vertices are searched, depending on where you start (i.e, starting again at B) • still very good algorithm to use due to its ease of implementation

  15. QUESTIONS? • MORE GRAPHS • P. 735 - 744

More Related