### SlideShare for iOS

by Linkedin Corporation

FREE - On the App Store

Spring 2011 Algorithms

Spring 2011 Algorithms

- Total Views
- 400
- Views on SlideShare
- 366
- Embed Views

- Likes
- 0
- Downloads
- 4
- Comments
- 0

http://ohheum.tistory.com | 26 |

http://ohkwn.wordpress.com | 8 |

Uploaded via SlideShare as Adobe PDF

© All Rights Reserved

- 1. Part IV Graph AlgorithmsSaturday, April 30, 2011 1
- 2. 1 Undirected Graphs Undirected graph. G = (V, E) V = nodes. ! E = edges between pairs of nodes. ! Captures pairwise relationship between objects. ! Graph size parameters: n = |V|, m = |E|. ! V={1,2,3,4,5,6,7,8} V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = {E={(1,2),(1,3),(2,3),...,(7,8)}3-8, 4-5, 5-6 } 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, n=8 n=8 m = 11 m=11Saturday, April 30, 2011 3 2
- 3. Directed Graphshs 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 a ! Directedness of graph is crucial. ! Modern web search engines exploit hyperlink structur pages by importance. Saturday, April 30, 2011 3
- 4. 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. 1 if (i, j) ∈ E, A = (aij ), where aij = 0 otherwise. 1 2 3 4 5 6 7 8 1 0 1 1 0 0 0 0 0 2 1 0 1 1 1 0 0 0 3 1 1 0 0 1 0 1 1 4 0 1 0 1 1 0 0 0 5 0 1 1 1 0 1 0 0 6 0 0 0 0 1 0 0 0 7 0 0 1 0 0 0 0 1 8 0 0 1 0 0 0 1 0 7 8Saturday, April 30, 2011 4
- 5. Saturday, April 30, 2011 5
- 6. Saturday, April 30, 2011 6
- 7. Saturday, April 30, 2011 7
- 8. Paths and ConnectivityDef. A path in an undirected graph G = (V, E) is a sequence P of nodesv1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 isjoined by an edge in E.Def. A path is simple if all nodes are distinct.Def. An undirected graph is connected if for every pair of nodes u andv, there is a path between u and v. 10Saturday, April 30, 2011 8
- 9. Saturday, April 30, 2011 9
- 10. Breadth First Searchtion. Explore outward from s in all possible directions, adding "layer" at a time. s L1 L2 L ithm. n-1s }. neighbors of L0. nodes that do not belong to L0 or L1, and that have an edge de in L1. ll nodes that do not belong to an earlier layer, and that have e to a node in Li. For each i, Li consists of all nodes at distance exactly ihere is a path from s to t iff t appears in some layer. Saturday, April 30, 2011 18 10
- 11. Breadth First Searcht T be a BFS tree of G = (V, E), and let (x, y) be an edge ofevel of x and y differ by at most 1. s=1 √ 1. insert the start node into queue a queue 1 L0 L1 L2Saturday, April 30, 2011 11
- 12. Breadth First Searcht T be a BFS tree of G = (V, E), and let (x, y) be an edge ofevel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 1 2 3 L0 L1 L2Saturday, April 30, 2011 12
- 13. Breadth First Searcht T be a BFS tree of G = (V, E), and let (x, y) be an edge ofevel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 3 √ √ 2 3 4 5 L0 L1 L2Saturday, April 30, 2011 13
- 14. Breadth First Searcht T be a BFS tree of G = (V, E), and let (x, y) be an edge ofevel of x and y differ by at mostremove a node from queue 2. 1. s=1 √ 3. insert neighbors of it into the queue √ √ 4 5 √ √ 3 √ 4 5 6 L0 L1 L2Saturday, April 30, 2011 14
- 15. Saturday, April 30, 2011 15
- 16. Saturday, April 30, 2011 16
- 17. Saturday, April 30, 2011 17
- 18. Saturday, April 30, 2011 18
- 19. Saturday, April 30, 2011 19
- 20. 94 96 Figure 3.2 Exploring a graph is rather like navigating themaze. Figure 3.2. begin here Figure 3.4 The result of explore(A) on a graph of L A K D A B E B D F B E F G G H C F I J C I J C E H J K L Figure 3.5 Depth-ﬁrst search. I A procedure dfs(G) for all v ∈ V : visited(v) = false What parts of the graph are reachable from a given vertex? for all v ∈ V :Saturday, April 30, 2011 if not visited(v): explore(v) 20
- 21. Saturday, April 30, 2011 21
- 22. Saturday, April 30, 2011 22
- 23. Saturday, April 30, 2011 23
- 24. Saturday, April 30, 2011 24
- 25. Saturday, April 30, 2011 25
- 26. ➙ ➙ ➙Saturday, April 30, 2011 26
- 27. Def. A graph is strongly connected if every pair of nodes i reachable. Lemma. Let s be any node. G is strongly connected iff eve 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 pat ok if paths over s u vSaturday, April 30, 2011 27
- 28. 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. reverse orientation of every edge in G ! Run BFS from s in Grev. ! Return true iff all nodes reached in both BFS executions. ! Correctness follows immediately from previous lemma. ! strongly connected not strongly connected 37Saturday, April 30, 2011 28
- 29. strongly connected not strongly connected 37 Directed Acyclic Graphs Def. An DAG is a directed graph that contains no directed cycles. Pr Ex. Precedence constraints: edge (vi, vj) means vi must precede vj. Ap ! Def. A topological order of a directed graph G = (V, E) is an ordering ! of its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i < j. v2 v3 v6 v5 v4 v1 v2 v3 v4 v5 v6 v7 v7 v1 a DAG a topological ordering 39Saturday, April 30, 2011 29
- 30. O(n+m) with adjacent listSaturday, April 30, 2011 30
- 31. Saturday, April 30, 2011 31
- 32. b b ee a SCC d d g c c h h ffSaturday, April 30, 2011 32
- 33. b b ee a d d g c c h h ff more than one SCCSaturday, April 30, 2011 33
- 34. Saturday, April 30, 2011 34
- 35. Saturday, April 30, 2011 35
- 36. a SCC another SCC YES !! The SCCs partition nodes of graph.Saturday, April 30, 2011 36
- 37. b b ee a d d g c c h h ffSaturday, April 30, 2011 37
- 38. bb ee C2 C1 a a d d g g C4 c c h C3 h f fSaturday, April 30, 2011 38
- 39. b b ee C2 C1 a d d g C4 C3 c c h h ffSaturday, April 30, 2011 39
- 40. Saturday, April 30, 2011 40
- 41. Saturday, April 30, 2011 41
- 42. Saturday, April 30, 2011 42
- 43. 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6Saturday, April 30, 2011 43
- 44. 13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6Saturday, April 30, 2011 44
- 45. 13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6Saturday, April 30, 2011 45
- 46. 13/14 11/16 1/10 8/9 start here 12/15 3/4 2/7 5/6Saturday, April 30, 2011 46
- 47. 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6 start hereSaturday, April 30, 2011 47
- 48. 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6Saturday, April 30, 2011 48
- 49. Saturday, April 30, 2011 49
- 50. Saturday, April 30, 2011 50
- 51. Saturday, April 30, 2011 51

Full NameComment goes here.