cs3000 algorithms data jonathan ullman
play

CS3000: Algorithms & Data Jonathan Ullman Lecture 9: - PowerPoint PPT Presentation

CS3000: Algorithms & Data Jonathan Ullman Lecture 9: Graphs Graph Traversals: DFS Topological Sort Feb 5, 2020 Whats Next Whats Next Graph Algorithms:


  1. CS3000: ¡Algorithms ¡& ¡Data Jonathan ¡Ullman Lecture ¡9: ¡ Graphs • Graph ¡Traversals: ¡DFS • Topological ¡Sort • Feb ¡5, ¡2020

  2. What’s ¡Next

  3. What’s ¡Next • Graph ¡Algorithms: • Graphs: Key ¡Definitions, ¡Properties, ¡Representations • Exploring ¡Graphs: Breadth/Depth ¡First ¡Search • Applications: ¡Connectivity, ¡Bipartiteness, ¡Topological ¡Sorting • Shortest ¡Paths: • Dijkstra • Bellman-­‑Ford ¡(Dynamic ¡Programming) • Minimum ¡Spanning ¡Trees: • Borůvka, ¡Prim, ¡Kruskal • Network ¡Flow: • Algorithms • Reductions ¡to ¡Network ¡Flow

  4. Graphs

  5. Graphs: ¡Key ¡Definitions • Definition: ¡ A ¡directed ¡graph 𝐻 = 𝑊, 𝐹 • 𝑊 is ¡the ¡set ¡of ¡nodes/vertices • 𝐹 ⊆ 𝑊×𝑊 is ¡the ¡set ¡of ¡edges • An ¡edge ¡is ¡an ¡ordered ¡ 𝑓 = 𝑣, 𝑤 “from ¡ 𝑣 to ¡ 𝑤 ” • Definition: An ¡undirected ¡graph 𝐻 = 𝑊, 𝐹 • Edges ¡are ¡unordered ¡ 𝑓 = 𝑣, 𝑤 “between ¡ 𝑣 and ¡ 𝑤 ” • Simple ¡Graph: • No ¡duplicate ¡edges • No ¡self-­‑loops ¡ 𝑓 = 𝑣, 𝑣

  6. Ask ¡the ¡Audience • How ¡many ¡edges ¡can ¡there ¡be ¡in ¡a ¡ simple directed/undirected graph?

  7. Graphs ¡Are ¡Everywhere • Transportation ¡networks • Communication ¡networks • WWW • Biological ¡networks • Citation ¡networks • Social ¡networks • …

  8. Paths/Connectivity • A ¡path is ¡a ¡sequence ¡of ¡consecutive ¡edges ¡in ¡ 𝐹 • 𝑄 = 𝑣, 𝑥 - , 𝑥 - , 𝑥 . , 𝑥 . , 𝑥 / , … , 𝑥 12- ,𝑤 • 𝑄 = 𝑣 − 𝑥 - − 𝑥 . − 𝑥 / − ⋯− 𝑥 12- − 𝑤 • The ¡length of ¡the ¡path ¡is ¡the ¡# ¡of ¡edges • An ¡undirected graph ¡is ¡connected if ¡for ¡every ¡two ¡ vertices ¡ 𝑣, 𝑤 ∈ 𝑊 , ¡there ¡is ¡a ¡path ¡from ¡ 𝑣 to ¡ 𝑤 • A ¡directed graph ¡is ¡strongly ¡connected if ¡for ¡every ¡ two ¡vertices ¡ 𝑣, 𝑤 ∈ 𝑊 , ¡there ¡are ¡paths ¡from ¡ 𝑣 to ¡ 𝑤 and ¡from ¡ 𝑤 to ¡ 𝑣

  9. Cycles • A ¡cycle is ¡a ¡path ¡ 𝑤 - − 𝑤 . − ⋯ − 𝑤 1 − 𝑤 - and ¡ 𝑤 - , …, 𝑤 1 are ¡distinct

  10. Ask ¡the ¡Audience • Suppose ¡an ¡undirected ¡graph ¡ 𝐻 is ¡connected • True/False? ¡ ¡ 𝐻 has ¡at ¡least ¡ 𝑜 − 1 edges

  11. Ask ¡the ¡Audience • Suppose ¡an ¡undirected ¡graph ¡ 𝐻 has ¡ 𝑜 − 1 edges • True/False? ¡ ¡ 𝐻 is ¡connected

  12. Trees • A ¡simple ¡undirected ¡graph ¡ 𝐻 is ¡a ¡tree if: • 𝐻 is ¡connected • 𝐻 contains ¡no ¡cycles • Theorem: any ¡two ¡of ¡the ¡following ¡implies ¡the ¡third • 𝐻 is ¡connected • 𝐻 contains ¡no ¡cycles • 𝐻 has ¡ = 𝑜 − 1 edges

  13. Trees • Rooted ¡tree: ¡choose ¡a ¡root ¡node ¡ 𝑠 and ¡orient ¡edges ¡ away ¡from ¡ 𝑠 • Models ¡ hierarchical ¡structure

  14. Phylogeny ¡Trees

  15. Parse ¡Trees if (A[x]==2) then (32 2 + (a*64 +12)/8) else fibonacci(n) if-­‑then-­‑else == fn-­‑call + / fibonacci n array ¡ref 2 power 8 + A x 32 2 * 12 a 64

  16. Representing ¡a ¡Graph

  17. Adjacency ¡Matrices • The ¡adjacency ¡matrix of ¡a ¡graph ¡ 𝐻 = 𝑊, 𝐹 with ¡ 𝑜 nodes ¡is ¡the ¡matrix ¡ 𝐵 1:𝑜 ¡, 1:𝑜 where A 1 2 3 4 1 0 1 1 0 𝐵 𝑗, 𝑘 = ¡>1 ¡ ¡ ¡ ¡ ¡ 𝑗, 𝑘 ∈ 𝐹 2 0 0 1 0 ¡0 ¡ ¡ ¡ ¡ ¡ 𝑗, 𝑘 ∉ 𝐹 3 0 0 0 0 4 0 0 1 0 Cost Space: ¡ Θ 𝑊 . 2 1 Lookup: ¡ Θ 1 time 3 4 List ¡Neighbors: ¡ Θ 𝑊 time

  18. Adjacency ¡Lists ¡(Undirected) • The ¡adjacency ¡list of ¡a ¡vertex ¡ 𝑤 ∈ 𝑊 is ¡the ¡list ¡ 𝐵[𝑤] of ¡all ¡ 𝑣 s.t. ¡ 𝑤, 𝑣 ∈ 𝐹 𝐵 1 = 2,3 𝐵 2 = 1,3 𝐵 3 = 1,2,4 𝐵 4 = 3 2 1 3 4

  19. Adjacency ¡Lists ¡(Directed) • The ¡adjacency ¡list of ¡a ¡vertex ¡ 𝑤 ∈ 𝑊 are ¡the ¡lists • 𝐵 GHI [𝑤] of ¡all ¡ 𝑣 s.t. ¡ 𝑤, 𝑣 ∈ 𝐹 • 𝐵 JK [𝑤] of ¡all ¡ 𝑣 s.t. ¡ 𝑣, 𝑤 ∈ 𝐹 𝐵 GHI 1 = 2,3 𝐵 JK 1 = ¡ 2 1 𝐵 GHI 2 = 3 𝐵 JK 2 = 1 𝐵 GHI 3 = ¡ 𝐵 JK 3 = 1,2,4 3 4 𝐵 GHI 4 = 3 𝐵 JK 4 = ¡

  20. Exploring ¡a ¡Graph

  21. Exploring ¡a ¡Graph • Problem: ¡ Is ¡there ¡a ¡path ¡from ¡ 𝑡 to ¡ 𝑢 ? • Idea: Explore ¡all ¡nodes ¡reachable ¡from ¡ 𝑡 . • Two ¡different ¡search ¡techniques: • Breadth-­‑First ¡Search: explore ¡nearby ¡nodes ¡before ¡ moving ¡on ¡to ¡farther ¡away ¡nodes • Depth-­‑First ¡Search: follow ¡a ¡path ¡until ¡you ¡get ¡stuck, ¡ then ¡go ¡back

  22. Exploring ¡a ¡Graph • BFS/DFS are ¡general ¡templates ¡for ¡graph ¡algorithms • Extensions ¡of ¡ Breadth-­‑First ¡Search : • 2-­‑Coloring ¡(Bipartiteness) • Shortest ¡Paths • Minimum ¡Spanning ¡Tree ¡(Prim’s ¡Algorithm) • Extensions ¡of ¡ Depth-­‑First ¡Search : • Topological ¡Sorting

  23. Depth-­‑First ¡Search ¡(DFS)

  24. Depth-­‑First ¡Search G = (V,E) is a graph explored[u] = 0 ∀ u u c DFS(u): explored[u] = 1 a b for ((u,v) in E): if (explored[v]=0): parent[v] = u DFS(v)

  25. Depth-­‑First ¡Search • Fact: The ¡parent-­‑child ¡edges ¡form ¡a ¡(directed) ¡tree • Each ¡edge ¡has ¡a ¡type: • Tree ¡edges: ¡ (𝑣, 𝑏),(𝑣, 𝑐), (𝑐, 𝑑) • These ¡are ¡the ¡edges ¡that ¡explore ¡new ¡nodes • Forward ¡edges: ¡ (𝑣, 𝑑) • Ancestor ¡to ¡descendant • Backward ¡edges: ¡ 𝑏, 𝑣 • Descendant ¡to ¡ancestor u c • Implies ¡a ¡directed ¡cycle! • Cross ¡edges: ¡ (𝑐, 𝑏) • No ¡ancestral ¡relation a b

  26. Ask ¡the ¡Audience • DFS ¡starting ¡from ¡node ¡ 𝑏 • Search ¡in ¡alphabetical ¡order • Label ¡edges ¡with ¡ {tree,forward,backward,cross} a b c d e f g h

  27. Post-­‑Ordering u c G = (V,E) is a graph explored[u] = 0 ∀ u a b DFS(u): explored[u] = 1 Vertex Post-­‑Order for ((u,v) in E): if (explored[v]=0): parent[v] = u DFS(v) post-visit(u) • Maintain ¡a ¡counter ¡ clock , ¡initially ¡set ¡ clock = 1 • post-visit(u): set postorder[u]=clock, clock=clock+1

  28. Pre-­‑Ordering u c G = (V,E) is a graph explored[u] = 0 ∀ u a b DFS(u): explored[u] = 1 Vertex Pre-­‑Order pre-visit(u) for ((u,v) in E): if (explored[v]=0): parent[v] = u DFS(v) • Maintain ¡a ¡counter ¡ clock , ¡initially ¡set ¡ clock = 1 • pre-visit(u): set preorder[u]=clock, clock=clock+1

  29. Ask ¡the ¡Audience • Compute ¡the ¡ post-­‑order of ¡this ¡graph • DFS ¡from ¡ 𝒃 , ¡search ¡in ¡alphabetical ¡order a b c d e f g h Vertex a b c d e f g h Post-­‑Order

  30. Ask ¡the ¡Audience • Compute ¡the ¡ post-­‑order of ¡this ¡graph • DFS ¡from ¡ 𝒃 , ¡search ¡in ¡alphabetical ¡order a b c d e f g h Vertex a b c d e f g h Post-­‑Order 8 7 5 4 6 1 2 3

  31. Ask ¡the ¡Audience • Observation: ¡ if ¡postorder[u] ¡< ¡postorder[v] ¡then ¡ (u,v) ¡is ¡a ¡backward ¡edge a b c d e f g h Vertex a b c d e f g h Post-­‑Order 8 7 5 4 6 1 2 3

  32. Ask ¡the ¡Audience • Observation: ¡ if ¡postorder[u] ¡< ¡postorder[v] ¡then ¡ (u,v) ¡is ¡a ¡backward ¡edge • DFS(u) ¡can’t ¡finish ¡until ¡its ¡children ¡are ¡finished • If ¡(u,v) ¡is ¡a ¡tree ¡edge, ¡then ¡postorder[u] ¡> ¡postorder[v] • If ¡(u,v) ¡is ¡a ¡forward ¡edge, ¡then ¡postorder[u] ¡> ¡postorder[v] • If ¡postorder[u] ¡< ¡postorder[v], ¡then ¡DFS(u) ¡finishes ¡ before ¡DFS(v), ¡thus ¡DFS(v) ¡is ¡not ¡called ¡by ¡DFS(u) • When ¡we ¡ran ¡DFS(u), ¡we ¡must ¡have ¡had ¡explored[v]=1 • Thus, ¡DFS(v) ¡started ¡before ¡DFS(u) • DFS(v) ¡started ¡before ¡DFS(u) ¡but ¡finished ¡after • Can ¡only ¡happen ¡for ¡a ¡backward ¡edge

  33. Topological ¡Ordering

  34. Directed ¡Acyclic ¡Graphs ¡(DAGs) • DAG: ¡ A ¡directed graph ¡with ¡no ¡directed ¡cycles • Can ¡be ¡much ¡more ¡complex ¡than ¡a ¡forest

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend