Presentation is loading. Please wait.

Presentation is loading. Please wait.

242-535 ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees.

Similar presentations


Presentation on theme: "242-535 ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees."— Presentation transcript:

1 242-535 ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees Algorithm Design and Analysis (ADA) 242-535, Semester 1 2014-2015 9. Graph Search

2 242-535 ADA: 9. Graph Search2 1.Graph Searching 2.Depth First Search (DFS) 3.Uses of DFS o cycle detection, reachability, topological sort 4.Breadth-first Search (BFS) 5.DFS vs. BFS 6.IP MulticastingOverview

3 242-535 ADA: 9. Graph Search3 Given: a graph G = (V, E), directed or undirected Goal: visit every vertex Often the end result is a tree built over the graph o called a spanning tree it visits every vertex, but not necessarily every edge o Pick any vertex as the root o Choose certain edges to produce a tree o Note: we might build a forest if the graph is not connected 1. Graph Searching

4 242-535 ADA: 9. Graph Search4Example search then build a spanning tree (or trees)

5 242-535 ADA: 9. Graph Search5 DFS is “depth first” because it always fully explores down a path away from a vertex v before it looks at other paths leaving v. Crucial DFS properties: uses recursion : essential for graph structures choice : at a vertex there may be a choice of several edges to follow to the next vertex backtracking : "return to where you came from" avoid cycles by grouping vertices into visited and unvisited 2. Depth First Search (DFS)

6 242-535 ADA: 9. Graph Search6 Directed Graph Example a d fe c b Graph G DFS works with directed and undirected graphs.

7 242-535 ADA: 9. Graph Search7 enum MARKTYPE {VISITED, UNVISITED}; struct cell { /* adj. list */ NODE nodeName; struct cell *next; }; typedef struct cell *LIST; struct graph { enum MARKTYPE mark; LIST successors; }; typedef struct graph GRAPH[NUMNODES]; Data Structures

8 242-535 ADA: 9. Graph Search8 void dfs(NODE u, GRAPH G) // recursively search G, starting from u { LIST p; // runs down adj. list of u NODE v; // node in cell that p points at G[u].mark = VISITED; // visited u p = G[u].successors; while (p != NULL) { // visit u’s succ’s v = p->nodeName; if (G[v].mark == UNVISITED) dfs(v, G); // visit v p = p->next; } } The dfs() Function

9 242-535 ADA: 9. Graph Search9 CallVisited d(a){a} d(a)-d(b){a,b} d(a)-d(b)-d(c){a,b,c} Skip b, return to d(b) d(a)-d(b)-d(d){a,b,c,d} Skip c d(a)-d(b)-d(d)-d(e){a,b,c,d,e} Skip c, return to d(d) Calling dfs(a,G) continued call it d(a) for short call it d(a) for short

10 242-535 ADA: 9. Graph Search10 d(a)-d(b)-d(d)-d(f){a,b,c,d,e,f} Skip c, return to d(d) d(a)-d(b)-d(d){a,b,c,d,e,f} Return to d(b) d(a)-d(b){a,b,c,d,e,f} Return to d(a) d(a){a,b,c,d,e,f} Skip d, return

11 242-535 ADA: 9. Graph Search11 Since nodes are marked, the graph is searched as if it were a tree: DFS Spanning Tree d/4 f/6e/5 c b/2 a/1 c/3 A spanning tree is a subgraph of a graph G which contains all the verticies of G.

12 242-535 ADA: 9. Graph Search12 Example 2 a b dc ef h g a b dc ef h g the tree generated by DFS is drawn with thick lines DFS

13 242-535 ADA: 9. Graph Search13 The time taken to search from a node is proportional to the no. of successors of that node. Total search time for all nodes = O(|V|) Total search time for all successors = time to search all edges = O(|E|) Total running time is O(V + E) dfs() Running Time continued

14 242-535 ADA: 9. Graph Search14 If the graph is dense, E >> V (E approaches V 2 ) then the O(V) term can be ignored o in that case, the total running time = O(E) or O(V 2 )

15 242-535 ADA: 9. Graph Search15 Finding cycles in a graph o e.g. for finding recursion in a call graph Searching complex locations, such as mazes Reachability detection o i.e. can a vertex v be reached from vertex u? o useful for e-mail routing; path finding Strong connectivity Topological sorting 3. Uses of DFS continued

16 242-535 ADA: 9. Graph Search16 The DFS algorithm is similar to a classic strategy for exploring a maze o mark each intersection, corner and dead end (vertex) as visited o mark each corridor (edge ) traversed o keep track of the path back to the previous branch points Maze Traversal Graphs16

17 Reachability DFS tree rooted at v: what are the vertices reachable from v via directed paths? A C E B D F A C ED A C E B D F start at C start at B

18 Strong Connectivity Each vertex can reach all other vertices Graphs18 a d c b e f g

19 Strong Connectivity Algorithm Pick a vertex v in G. Perform a DFS from v in G. o If there’s a vertex not visited, print “no”. Let G’ be G with edges reversed. Perform a DFS from v in G’. o If there’s a vertex not visited, print “no” If the algorithm gets here, print “yes”. Running time: O(V+E). 19 G: G’: a d c b e f g a d c b e f g

20 Strongly Connected Components List all the subgraphs where each vertex can reach all the other vertices in that subgraph. Can also be done in O(V+E) time using DFS. 20 { a, c, g } { f, d, e, b } a d c b e f g

21 242-535 ADA: 9. Graph Search21 Topological sort of a directed acyclic graph ( DAG ): o linearly order all the vertices in a graph G such that vertex u comes before vertex v if edge (u, v)  G o a DAG is a directed graph with no directed cycles Topological Sort

22 242-535 ADA: 9. Graph Search22 Example: Getting Dressed UnderwearSocks ShoesTrousers Belt Shirt Watch Tie Jacket SocksUnderwear Trousers ShoesWatchShirtBeltTieJacket one topological sort (not unique)

23 242-535 ADA: 9. Graph Search23 Topological-Sort() { Run DFS; When a vertex is finished, output it; Vertices are output in reverse topological order; } Time: O(V+E) Topological Sort Algorithm

24 242-535 ADA: 9. Graph Search24 Process all the verticies at a given level before moving to the next level. Example graph G (again): 4. Breadth-first Search (BFS) a b dc ef h g

25 242-535 ADA: 9. Graph Search25 1) Put the verticies into an ordering o e.g. {a, b, c, d, e, f, g, h} 2) Select a vertex, add it to the spanning tree T: e.g. a 3) Add to T all edges (a,X) and X verticies that do not create a cycle in T o i.e. (a,b), (a,c), (a,g) T = {a, b, c, g} Informal Algorithm continued a bc g

26 242-535 ADA: 9. Graph Search26 Repeat step 3 on the verticies just added, these are on level 1 o i.e. b: add (b,d) c: add (c,e) g: nothing T = {a,b,c,d,e} Repeat step 3 on the verticies just added, these are on level 2 o i.e. d: add (d,f) e: nothing T = {a,b,c,d,e,f} a bc g de a bc g de f continued level 1 level 2

27 242-535 ADA: 9. Graph Search27 Repeat step 3 on the verticies just added, these are on level 3 o i.e. f: add (f,h) T = {a,b,c,d,e,f,h} Repeat step 3 on the verticies just added, these are on level 4 o i.e. h: nothing, so stop a bc g de f h continued level 3

28 242-535 ADA: 9. Graph Search28 Resulting spanning tree: a b dc ef h g a different spanning tree from the earlier solution a different spanning tree from the earlier solution

29 242-535 ADA: 9. Graph Search29 Example 2

30 242-535 ADA: 9. Graph Search30 Algorithm Graphically start node pre-built adjency list

31 242-535 ADA: 9. Graph Search31

32 242-535 ADA: 9. Graph Search32

33 242-535 ADA: 9. Graph Search33 boolean marked[]; // visited this vertex? int edgeTo[]; // vertex number going to this vertex void bfs(Graph graph, int start) { Queue q = new Queue(); marked[start] = true; q.add(start); // add to end of queue while (!q.isEmpty()) { int v = q.remove(); // get from start of queue for (int w : graph.adjacentTo(v)) // v --> w if (!marked[w]) { edgeTo[w] = v; // save last edge on a shortest path marked[w] = true; q.add(w); // add to end of queue } } // end of bfs() BFS Code

34 242-535 ADA: 9. Graph Search34 5. DFS vs. BFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components  see part 11

35 242-535 ADA: 9. Graph Search35 DFS is like one person exploring a maze o do down a path to the end, get to a dead-end, backtrack, and try a different path BFS is like a group of searchers fanning out in all directions, each unrolling a ball of string. o at a branch point, the searchers split up to explore all the branches at once o if two groups meet up, they join forces (using the ball of string of the group that got there first) o the group that gets to the exit first has found the shortest path DFS and BFS as Maze Explorers

36 242-535 ADA: 9. Graph Search36 BFS Maze Graphically Also called flood filling; used in paint software.

37 242-535 ADA: 9. Graph Search37 The BFS "fanning out" algorithm is best implemented in a parallel language, where each "group of explorers" is a separate thread of execution. o e.g. use fork and join in Java The earlier implementation uses a queue to implement the fanning out as a sequential algorithm. DFS is inherently a sequential algorithm. Sequential / Parallel

38 242-535 ADA: 9. Graph Search38 A network of computers and routers: 6. IP Multicasting source computer router continued

39 242-535 ADA: 9. Graph Search39 How can a packet (message) be sent from the source computer to every other computer? The inefficient way is to use broadcasting o send a copy along every link, and have each router do the same o each router and computer will receive many copies of the same packet o loops may mean the packet never disappears! continued

40 242-535 ADA: 9. Graph Search40 IP multicasting is an efficient solution o send a single packet to one router o have the router send it to 1 or more routers in such a way that a computer never receives the packet more than once This behaviour can be represented by a spanning tree. Can use either BFS or DFS, but BFS will usually produce shorter paths o i.e. BFS is a better choice continued

41 242-535 ADA: 9. Graph Search41 One spanning tree for the network: source computer router the tree is drawn with thick lines the tree is drawn with thick lines


Download ppt "242-535 ADA: 9. Graph Search1 Objective o describe and compare depth-first and breadth- first graph searching, and look at the creation of spanning trees."

Similar presentations


Ads by Google