EMIS 8374 Search Algorithms Updated 9 February 2004

Slides:



Advertisements
Similar presentations
Chapter 5 Shortest Paths: Label-Correcting Algorithms
Advertisements

15.082J, 6.855J, and ESD.78J Sept 16, 2010 Lecture 3. Graph Search Breadth First Search Depth First Search Intro to program verification Topological Sort.
Graph.
Graph & BFS.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Chapter 4 Shortest Path Label-Setting Algorithms Introduction & Assumptions Applications Dijkstra’s Algorithm.
COSC 2007 Data Structures II
15.082J & 6.855J & ESD.78J September 30, 2010 The Label Correcting Algorithm.
EMIS 8374 The Ford-Fulkerson Algorithm (aka the labeling algorithm) Updated 4 March 2008.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Breadth-First Search (BFS)
Graphs A New Data Structure
Graphs Chapter 20.
Chapter 22 Elementary Graph Algorithms
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Graph Algorithms BFS, DFS, Dijkstra’s.
Spanning Trees.
CSC317 Graph algorithms Why bother?
CSE 2331/5331 Topic 9: Basic Graph Alg.
Lecture 12 Graph Algorithms
Discrete Optimization Lecture 1
Depth-First Search.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Unweighted Shortest Path Neil Tang 3/11/2010
EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008
CS120 Graphs.
Graph Algorithm.
CSE 421: Introduction to Algorithms
Graph & BFS.
Graphs Representation, BFS, DFS
Graph Algorithm.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Spanning Trees.
Lecture 4 Graph Search.
Lectures on Graph Algorithms: searching, testing and sorting
CS 583 Analysis of Algorithms
Chapter 11 Graphs.
EMIS 8374 Shortest Path Problems: Introduction Updated 9 February 2008
Shortest Path Problems
Network Optimization Depth First Search
Introduction to Algorithms
The Ford-Fulkerson Algorithm
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs.
Graphs.
UNINFORMED SEARCH -BFS -DFS -DFIS - Bidirectional
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
and 6.855J Depth First Search
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Breadth first search animation
Graphs.
OPIM 915 Fall 2010 Data Structures 23-38,
Algorithm Course Dr. Aref Rashad
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Richard Anderson Winter 2019 Lecture 5
Graph Algorithm.
EMIS 8374 Search Algorithms Updated 12 February 2008
EMIS 8374 Search Algorithms: DFS Updated 12 February 2004
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

EMIS 8374 Search Algorithms Updated 9 February 2004

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.

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

Search Example 1: Search Tree 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

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

A Connected Graph 2 4 1 6 3 5

A Tree 2 4 1 3 5 6

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.

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.

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

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)

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.

Adjacency List Implementation 1 2 3 2 5 4 3 5 5 6 4 6 3 6

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

Mark Node 6 1 2 3 2 5 4 3 5 5 6 4 6 3 6

Advance Current Arc for Node 4 1 2 3 2 5 4 3 5 5 6 4 6 3 6

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

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

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

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

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

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

LIST = {} 1 2 3 2 5 4 3 5 5 6 4 6 3 6

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Problems Solved by BFS Shortest Path problem when all arcs have the same cost. s-t shortest path all-pairs shortest path problem Determining strong connectivity Is there a directed path between all pairs of nodes?

Levels of a BFS Tree Root node is at level 0 Descendants of the root are at level 1 IF node i is at level k Then the children of i are at level k+1

BFS Solves s-t Shortest Path 2 4 1 6 3 5 Level 0 Level 1 Level 2 Level 3

Unweighted Shortest Path Problems cij = 1 for all (i,j) BFS solves s-t shortest path problem in O(m) time. BFS solves all-pairs shortest path problem in O(nm) time. BFS determines strong connectivity in O(nm) time.

Depth-First Search Always pick the last (most recently added) node in the LIST Pick marked nodes in a last-in, first-out order (LIFO) Breadth-First Search uses a FIFO order Makes a “deep” probe, creating as long a path as possible Backs up when it can’t mark a new node

Depth-First Search 1 2 4 3 5 6

Depth-First Search 5 2 1 2 4 3 5 6 4 6 3 List = {} List = {1}

Depth-First Search Tree 5 2 2 4 1 6 1 4 3 5 6 3

Properties of a DFS Tree If node j is a descendant of node i, then order(j) > order(i) All descendants of any node are ordered consecutively