Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.2 Graph Traversal.

Similar presentations


Presentation on theme: "3.2 Graph Traversal."— Presentation transcript:

1 3.2 Graph Traversal

2 Connected Component Connected component. Find all nodes reachable from s. Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.

3 Connectivity problems
s-t connectivity problem. Given two node s and t, is there a path between s and t? Reachability problem. Given node s, what are all the nodes reachable from s? Applications. Web crawling Friendster. Maze traversal. Kevin Bacon number. Data mining

4 Exploring unknown terrain with robots
Robot must locate the person by exploring the unknown terrain Constraints: Limited power: need to return to origin for recharging Tethered to the origin - power/data cable: movement restricted

5 Generic Connected Components Algorithm
Connected component. Find all nodes reachable from s. Theorem. Upon termination, R is the connected component containing s. Proof. By construction, each node in R has a path from s. Suppose there is a node w  R that has a path P in G from s. s  R, and w  R  there is a first node v  P, v s Immediate predecessor u of v on P must be in R So there is edge (u,v), with u  R and v  R Therefore, v should have been added to R R w s P u v

6 Finding all Connected Components
While there is an unvisited node s: Compute the connected component containing s Mark all nodes in this component as visited

7 Generic Connected Components Algorithm
Path from s to any node t can be recovered For every node v, record edge (u,v) that was considered when v was added Trace edges backwards from t Gives a “search tree” Choice of edge to grow R is unspecified. Two possible ways: Breadth First Search Depth First Search

8 Exploring a maze Robot with a wireless control:
Limited by battery, but can go pretty far ~ depth first search Exploring with a tethered robot: Cannot be more than distance L from origin at any time ~ Breath first search

9 Breadth First Search BFS intuition. Explore outward from s in all possible directions, adding nodes one "layer" at a time. BFS algorithm. L0 = { s }. L1 = all neighbors of L0. L2 = all nodes that do not belong to L0 or L1, and that have an edge to a node in L1. Li+1 = all nodes that do not belong to an earlier layer, and that have an edge to a node in Li. Construct BFS tree: put edge (u,v) if v was discovered from u Theorem. For each i, Li consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer. s L1 L2 L n-1

10 Breadth First Search Property. Let (x, y) be an edge of G. Then the level of x and y differ by at most 1. L0 L1 L2 L3

11 Depth First Search DFS intuition. Follow the first unexplored edge out of each node, and backtrack when a “dead end” is reached - a node that has no more unexplored edges out of it. Searching a Maze: Right-Hand Rule. Rooms are nodes and passages are edges Always follow the first passage on the right

12 DFS Algorithm Constructing a DFS Tree Make s the root
Make u the parent of v if u is responsible for the discovery of v

13 DFS on an example BFS Tree BFS Tree: short and bushy
DFS Tree: narrow and deep

14 Properties of DFS trees
For a recursive call DFS(u), all nodes that are marked “Explored” during this call are descendants of u Theorem. Let T be a DFS tree. Let x and y be nodes in T and let (x,y) be an edge of G that is not in T. Then one of x or y is an ancestor of the other Proof. Suppose x was explored before y. When (x,y) was examined, it was not added to T because y was marked “Explored” Therefore, y must be a descendant of x.

15 Implementing Graph Traversal using Queues and Stacks
Section 3.3 [KT]

16 Graph Representation: Adjacency Matrix
Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. Two representations of each edge. Space proportional to n2. Checking if (u, v) is an edge takes (1) time. Identifying all edges takes (n2) time. Examining all edges incident on u: (n) time Symmetric matrix for undirected graphs: A(u,v) = A(v,u) Even if u has few neighbors

17 Graph Representation: Adjacency List
Adjacency list. Node indexed array of lists. Two representations of each edge. Space O(m + n). Checking if (u, v) is an edge takes O(deg(u)) time. Identifying all edges takes (m + n) time. degree = number of neighbors of u 1 2 3 2 1 3 4 5 3 1 2 5 7 8 4 2 5 5 2 3 4 6 6 5 7 3 8 8 3 7

18 The Best Representation for Searching
Searching requires exploring all the edges incident on a node O(deg(v)) time for Adjacency List, instead of (n) time for Adjacency Matrix Adjacency List more suitable for Searching algorithms Need another data structure for storing nodes that are currently being explored

19 Queues and Stacks Queues.
Elements extracted in first-in, first-out (FIFO) order Can be implemented by a linked list Stacks. Elements extracted in last-in, first-out (LIFO) order Can be implemented by a doubly linked list or an array

20 Implementing Breadth-First Search
Algorithm can be run with a single list L, implemented as a queue, instead of separate lists L[i]

21 Breadth First Search: Analysis
Theorem. The above implementation of BFS runs in O(m + n) time if the graph is given by its adjacency representation. Pf. Easy to prove O(n2) running time: at most n lists L[i] each node occurs on at most one list; for loop runs  n times when we consider node u, there are  n incident edges (u, v), and we spend O(1) processing each edge Actually runs in O(m + n) time: when we consider node u, there are deg(u) incident edges (u, v) total time processing edges is uV deg(u) = 2m ▪ each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)

22 Implementing Depth-First Search
Order of insertion into S depends on Adjacency List Different Adjacency List orders can give different traversals

23 Constructing a DFS Tree
If Explored[v] = false Parent[v] = u DFS Tree formed by taking all edges (u, parent[u]) for All u ≠s

24 Running Time of DFS Implementation using Stacks
Main step: add/delete node in Stack: O(1) per operation How many such operations? deg(v) = degree of node v Node v is added to S each time one of its neighbors is explored Total number of nodes added to S ≤ ∑v deg(v)v = 2m The above implementation of DFS runs in time O(n+m), if the graph is given by an adjacency list representation.

25 3.5 Connectivity in Directed Graphs

26 Directed Graphs Directed graph. G = (V, E)
Edge (u, v) goes from node u to node v. Ex. Web graph - hyperlink points from one web page to another. Directedness of graph is crucial. Modern web search engines exploit hyperlink structure to rank web pages by importance.

27 Graph Search Directed reachability. Given a node s, find all nodes reachable from s. Directed s-t shortest path problem. Given two node s and t, what is the length of the shortest path between s and t? Graph search. BFS, DFS extend naturally to directed graphs. Web crawler. Start from web page s. Find all web pages linked from s, either directly or indirectly.

28 Strong Connectivity Def. Node u and v are mutually reachable if there is a path from u to v and also a path from v to u. Def. A graph is strongly connected if every pair of nodes is mutually reachable. Lemma. Let s be any node. G is strongly connected iff every node is reachable from s, and s is reachable from every node. Pf.  Follows from definition. Pf.  Path from u to v: concatenate u-s path with s-v path Path from v to u: concatenate v-s path with s-u path. ▪ Theorem. [Tarjan 1972] Can decompose a graph into its "strong components" in O(m + n) time. ok if paths overlap s u v

29 Strong Connectivity: Algorithm
Theorem. Can determine if G is strongly connected in O(m + n) time. Pf. Pick any node s. Run BFS from s in G. Run BFS from s in Grev. Return true iff all nodes reached in both BFS executions. Correctness follows immediately from previous lemma. ▪ reverse orientation of every edge in G Theorem. [Tarjan 1972] Can decompose a graph into its "strong components" in O(m + n) time. strongly connected not strongly connected


Download ppt "3.2 Graph Traversal."

Similar presentations


Ads by Google