1 / 60

Generic Search Algorithm - Find Reachable Nodes

This algorithm finds all nodes reachable via a directed path from a source node. It marks and adds nodes to a list until all reachable nodes are found.

ridlon
Télécharger la présentation

Generic Search Algorithm - Find Reachable Nodes

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. EMIS 8374Search AlgorithmsUpdated 9 February 2004

  2. Generic Search Algorithm (pg 74) • Find all nodes reachable via a directed path from source node s • All nodes are either marked or unmarked • Arc (i,j) is admissible if i is marked and j is not marked; and inadmissible, otherwise.

  3. Initialization unmark all nodes in N; mark source node s; pred(s):= 0; next := 1; order(s) := 1 LIST :={s};

  4. Main Loop while LIST is not empty do begin select a node i from LIST; if there is an admissible arc (i, j) then begin mark node j; pred(j) := i, next := next + 1, order(j) := next; add node j to LIST; end else delete node i from LIST; end

  5. Search Example 1: s = 4 2 4 1 6 3 5

  6. Search Example 1: Initialization 2 4 1 6 3 5 next = 1 LIST ={4} pred[4] = 0 order[4] = 1

  7. Search Example 1: While Loop 2 4 1 6 3 5 next = 1 LIST ={4} pred[4] = 0 order[4] = 1 i = 4. Find an admissible arc. j = 6.

  8. Search Example 1: Mark Node 6 2 4 1 6 3 5 next = 2 LIST ={4, 6} pred[6] = 4 order[6] = 2

  9. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={4, 6}. i = 6. Find an admissible arc. LIST = {4}.

  10. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={4} i = 4. Find an admissible arc. j = 3.

  11. Search Example 1: Mark Node 3 2 4 1 6 3 5 next = 3 LIST ={4, 3} pred[3] = 4 order[3] = 3

  12. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={4, 3}. i = 4. Find an admissible arc. LIST = {3}.

  13. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={3}. i = 3. Find an admissible arc. j = 5.

  14. Search Example 1: Mark Node 5 2 4 1 6 3 5 next = 4 LIST ={5, 3} pred[5] = 3 order[5] = 4

  15. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={5, 3}. i = 3. Find an admissible arc. LIST = {5}.

  16. Search Example 1: While Loop 2 4 1 6 3 5 LIST ={5}. i = 5. Find an admissible arc. LIST = {}.

  17. Search Example 1: Search Tree 1 2 4 2 1 6 4 3 3 5 order[3] = 3, order[4] = 1, order[5] = 4, order [6] = 2 Pred[4] = 0, Pred[6] = 1, Pred[5] = 3. Pred[3] =4

  18. Trees • An undirected graph is connected if there is a path between an pair of nodes • A tree T=(N,A) is a connected graph with with n-1 arcs (edges) • A graph with n nodes must have at least n-1 arcs to be connected

  19. A Connected Graph 2 4 1 6 3 5

  20. 2 4 1 6 3 5 A Tree

  21. Complexity of Generic Search • Each iteration of the While loop either adds a new node to LIST or removes a node from LIST. • A given node can be added at most once and removed at most once. • Thus, the loop runs at most 2n times.

  22. Complexity of Generic Search: Work per iteration of the While Loop • Selecting a node in LIST is O(1). • Deleting a node from LIST is O(1). • Marking a node (updating pred, next, and order, etc) is O(1). • Therefore, the work per iteration is O(1) + work required finding an admissible arc.

  23. Finding an Admissible Arc Incident to Node i: Naïve Approach found := 0; for { (i, j) in A(i) } if j is unmarked then begin found := 1; break; end if found = 1 then mark node j; pred(j) := i; etc … else delete node i from LIST

  24. Finding an Admissible Arc Incident to Node i: Naïve Approach • Worst-Case: • when there are no admissible arcs incident to node i this approach will check all of node i’s neighbors to see if they are marked. • O(n) • Overall complexity of generic search: 2n x n = O(n2)

  25. Finding an Admissible Arc Incident to Node i: Efficient Approach • Use an adjacency list representation of the network • Maintain a current arc data structure for each node. • Indicates the next candidate arc in the adjacency list for node i. • The next time node i is selected from LIST check the current arc first. • If the current arc points to a marked node we move to the next arc in the adjacency list. • If the we reach the end of the adjacency list for a given node we remove it from LIST.

  26. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 Adjacency List Implementation

  27. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 Search from Node 4: LIST = {4}

  28. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 Mark Node 6

  29. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 Advance Current Arc for Node 4

  30. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {4, 6}, i = 6

  31. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {4}, i = 4, mark 3

  32. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {4,3}, i = 4

  33. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {3}, i = 3, mark 5

  34. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {3, 5}, i = 3

  35. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {5}, i = 5

  36. 1 2 3 5 4 6 6 3 0 0 0 2 4 6 0 3 5 0 5 0 LIST = {}

  37. Complexity with Adjacency List • For each node i marked by the search, we check update/test the current arc parameter exactly once for each arc in A(i). • The total number of operations required by the search algorithm to find admissible arcs is proportional to the sum of |A(i)| for all the nodes it marks. • Overall complexity is O(n) for marking nodes, updating pred vector etc, plus O(m) for finding admissible arcs = O(n + m) = O(m)

  38. Breadth-First Search • Implement LIST as an ordered list • Always pick the first node in the list • Always add newly marked nodes to the end of the list • Level parameter • Initialize level(s) := 0 • Set level(i) := level(pred[i]) + 1 • Level(i) = number of arcs in path from s to i

  39. Breadth-First Search: s = 1 2 4 1 6 1 3 5

  40. Breadth-First Search: s = 1 2 2 4 1 6 1 3 5 LIST = {1, 2}

  41. Breadth-First Search: s = 1 2 2 4 1 6 1 3 5 3 LIST = {1, 2, 3}

  42. Breadth-First Search: s = 1 2 2 4 1 6 1 3 5 3 LIST = {2, 3}

  43. Breadth-First Search: s = 1 2 2 4 1 6 1 3 5 3 LIST = {2, 3}

  44. Breadth-First Search: s = 1 2 4 2 4 1 6 1 3 5 3 LIST = {2, 3, 4}

  45. Breadth-First Search: s = 1 2 4 2 4 1 6 1 3 5 3 5 LIST = {2, 3, 4, 5}

  46. Breadth-First Search: s = 1 2 4 2 4 1 6 1 3 5 3 5 LIST = {3, 4, 5}

  47. Breadth-First Search: s = 1 2 4 2 4 1 6 1 3 5 3 5 LIST = {4, 5}

  48. Breadth-First Search: s = 1 2 4 2 4 6 1 6 1 3 5 3 5 LIST = {4, 5, 6}

  49. Breadth-First Search: s = 1 2 4 2 4 6 1 6 1 3 5 3 5 LIST = {5, 6}

  50. Breadth-First Search: s = 1 2 4 2 4 6 1 6 1 3 5 3 5 LIST = {6}

More Related