Upcoming SlideShare
×

# Searching

583

Published on

Data Structures Searching

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total Views
583
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
51
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Searching

1. 1. Depth-First Search (DFS) <ul><li>DFS is another popular graph search strategy </li></ul><ul><ul><li>Idea is similar to pre-order traversal (visit children first) </li></ul></ul><ul><li>DFS can provide certain information about the graph that BFS cannot </li></ul><ul><ul><li>It can tell whether we have encountered a cycle or not </li></ul></ul>
2. 2. DFS Algorithm <ul><li>DFS will continue to visit neighbors in a recursive pattern </li></ul><ul><ul><li>Whenever we visit v from u, we recursively visit all unvisited neighbors of v. Then we backtrack (return) to u. </li></ul></ul><ul><ul><li>Note: it is possible that w2 was unvisited when we recursively visit w1, but became visited by the time we return from the recursive call. </li></ul></ul>u v w1 w2 w3
3. 3. DFS Algorithm Flag all vertices as not visited Flag yourself as visited For unvisited neighbors, call RDFS(w) recursively We can also record the paths using pred[ ].
4. 4. Example Adjacency List source Visited Table (T/F) Initialize visited table (all False) Initialize Pred to -1 Pred 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 F F F F F F F F F F - - - - - - - - - -
5. 5. Example Adjacency List source Visited Table (T/F) Mark 2 as visited Pred RDFS( 2 ) Now visit RDFS(8) 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 F F T F F F F F F F - - - - - - - - - -
6. 6. Example Adjacency List source Visited Table (T/F) Mark 8 as visited mark Pred[8] Pred RDFS( 2 ) RDFS(8) 2 is already visited, so visit RDFS(0) Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 F F T F F F F F T F - - - - - - - - 2 -
7. 7. Example Adjacency List source Visited Table (T/F) Mark 0 as visited Mark Pred[0] Pred RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbors, return to call RDFS(8) Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T F T F F F F F T F 8 - - - - - - - 2 -
8. 8. Example Adjacency List source Visited Table (T/F) Pred RDFS( 2 ) RDFS(8) Now visit 9 -> RDFS(9) Recursive calls Back to 8 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T F T F F F F F T F 8 - - - - - - - 2 -
9. 9. Example Adjacency List source Visited Table (T/F) Mark 9 as visited Mark Pred[9] Pred RDFS( 2 ) RDFS(8) RDFS(9) -> visit 1, RDFS(1) Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T F T F F F F F T T 8 - - - - - - - 2 8
10. 10. Example Adjacency List source Visited Table (T/F) Mark 1 as visited Mark Pred[1] Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) visit RDFS(3) Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T T T F F F F F T T 8 9 - - - - - - 2 8
11. 11. Example Adjacency List source Visited Table (T/F) Mark 3 as visited Mark Pred[3] Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit RDFS(4) Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T T T T F F F F T T 8 9 - 1 - - - - 2 8
12. 12. Example RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(4)  STOP all of 4’s neighbors have been visited return back to call RDFS(3) Adjacency List source Visited Table (T/F) Mark 4 as visited Mark Pred[4] Pred Recursive calls 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T T T T T F F F T T 8 9 - 1 3 - - - 2 8
13. 13. Example Adjacency List source Visited Table (T/F) Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit 5 -> RDFS(5) Recursive calls Back to 3 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T T T T T F F F T T 8 9 - 1 3 - - - 2 8
14. 14. Example Adjacency List source Visited Table (T/F) Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) 3 is already visited, so visit 6 -> RDFS(6) Recursive calls Mark 5 as visited Mark Pred[5] 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 T T T T T T F F T T 8 9 - 1 3 3 - - 2 8
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.