lecture 7 data structures dat037
play

Lecture 7 Data Structures (DAT037) Ramona Enache - PowerPoint PPT Presentation

Lecture 7 Data Structures (DAT037) Ramona Enache (with slides from Nick Smallbone and Nils Anders Danielsson) Directed Acyclic Graphs A


  1. Lecture ¡7 ¡ Data ¡Structures ¡(DAT037) ¡ ¡ ¡ ¡ Ramona ¡Enache ¡ (with ¡slides ¡from ¡Nick ¡Smallbone ¡and ¡ Nils ¡Anders ¡Danielsson) ¡

  2. Directed ¡Acyclic ¡Graphs ¡ ¡ A ¡ DAG ¡is ¡a ¡directed ¡graph ¡without ¡cycles. ¡ ¡ ¡ That ¡means: ¡ once ¡you ¡ ¡follow ¡an ¡edge ¡there ¡is ¡no ¡way ¡back ¡to ¡the ¡source ¡node ¡ ¡ ¡ ¡ (we ¡can ¡say ¡that ¡one ¡node ¡ ¡ Is ¡aHer ¡another ¡in ¡the ¡graph) ¡

  3. Topologic ¡Sort ¡ ¡ ¡ ¡ An ¡example: ¡ ¡ • nodes ¡are ¡tasks, ¡and ¡an ¡edge ¡(u, ¡v) ¡means ¡“task ¡u ¡must ¡be ¡done ¡ before ¡task ¡v” ¡ • the ¡graph ¡is ¡a ¡DAG, ¡ ¡ ¡ It ¡means ¡that ¡there ¡are ¡no ¡impossible ¡dependencies ¡between ¡tasks ¡ A ¡topological ¡sort ¡gives ¡a ¡valid ¡order ¡to ¡do ¡the ¡tasks ¡in ¡

  4. ImplemenNng ¡Topologic ¡Sort ¡ ¡ r ¡= ¡new ¡empty ¡list ¡ ¡ while ¡V ¡≠ ¡ ∅ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ any ¡v ¡ ∈ ¡V ¡with ¡indegree(v) ¡= ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ then ¡ ¡r.add-­‑last(v) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡remove ¡v ¡from ¡G ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡error: ¡cycle ¡found ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡

  5. ImplemenNng ¡Topologic ¡Sort ¡ ¡ r ¡= ¡new ¡empty ¡list ¡ ¡ while ¡V ¡≠ ¡ ∅ ¡ How ¡can ¡we ¡avoid ¡ ¡ ¡ do ¡ removing ¡v ¡from ¡G ¡? ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ any ¡v ¡ ∈ ¡V ¡with ¡indegree(v) ¡= ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ then ¡ ¡r.add-­‑last(v) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡remove ¡v ¡from ¡G ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡error: ¡cycle ¡found ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡

  6. ImplemenNng ¡Topologic ¡Sort ¡ ¡ r ¡= ¡new ¡empty ¡list ¡ ¡ d ¡= ¡map ¡from ¡verNces ¡to ¡their ¡indegrees ¡// ¡null ¡for ¡nodes ¡in ¡r. ¡ repeat ¡|V| ¡Nmes ¡ ¡ ¡ ¡ ¡ if ¡d[v] ¡== ¡0 ¡for ¡some ¡v ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then ¡ ¡ r.add-­‑last(v) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡d[v] ¡= ¡null ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡each ¡direct ¡successor ¡v' ¡of ¡v ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡decrease ¡d[v'] ¡by ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡error: ¡cycle ¡found ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡

  7. ImplemenNng ¡Topologic ¡Sort ¡ ¡ Complexity ¡Analysis ¡ ¡ (Need ¡more ¡info ¡because ¡of ¡pseudocode ¡representaNon). ¡ ¡ -­‑ Nodes: ¡0, ¡1,…|V|-­‑1 ¡ -­‑ adjacent : ¡array ¡with ¡V ¡posiNons ¡ adjacent[i] ¡– ¡contains ¡list ¡of ¡neighbours ¡for ¡node ¡I ¡ -­‑ r : ¡dynamic ¡array, ¡ -­‑ ¡ ¡ ¡ d : ¡array. ¡ ¡

  8. ImplemenNng ¡Topologic ¡Sort ¡ ¡ // ¡d ¡(indegree) ¡is ¡a ¡map ¡from ¡nodes ¡to ¡the ¡indegrees ¡that ¡it ¡would ¡have ¡ ¡ // ¡easier ¡for ¡deleNng ¡a ¡node ¡from ¡the ¡graph ¡– ¡indegree ¡would ¡be ¡null ¡ ¡ d ¡= ¡new ¡array ¡of ¡size ¡|V| ¡ ¡ for ¡i ¡in ¡[0,...,|V|-­‑1] ¡ ¡ O(|V|) ¡ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ ¡d[i] ¡= ¡0 ¡ for ¡i ¡in ¡[0,...,|V|-­‑1] ¡ ¡ O(|E|) ¡ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ for ¡each ¡direct ¡successor ¡j ¡of ¡i ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡d[j]++ ¡ ¡

  9. ImplemenNng ¡Topologic ¡Sort ¡ ¡ O(1) ¡ r ¡= ¡new ¡empty ¡list ¡ O(|V| ¡+ ¡|E|) ¡ ¡ d ¡= ¡map ¡from ¡verNces ¡to ¡their ¡indegrees ¡// ¡null ¡for ¡nodes ¡in ¡r. ¡ repeat ¡|V| ¡Nmes ¡ O(|V|) ¡ O(|V|) ¡ ¡ ¡ ¡ ¡ if ¡d[v] ¡== ¡0 ¡for ¡some ¡v ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then ¡ ¡ r.add-­‑last(v) ¡ O(1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡d[v] ¡= ¡null ¡ O(1) ¡ O(|E|) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for ¡each ¡direct ¡successor ¡v' ¡of ¡v ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡decrease ¡d[v'] ¡by ¡1 ¡ O(1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡raise ¡error: ¡cycle ¡found ¡ Total ¡: ¡O(|V|^2 ¡+ ¡|E|) ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡

  10. ImplemenNng ¡Topologic ¡Sort ¡ ¡ r ¡= ¡new ¡empty ¡list ¡ ¡ d ¡= ¡map ¡from ¡verNces ¡to ¡their ¡indegrees ¡ q ¡= ¡queue ¡with ¡all ¡nodes ¡of ¡indegree ¡0 ¡ while ¡q ¡is ¡non-­‑empty ¡ ¡ ¡ ¡do ¡ ¡ ¡ ¡ ¡ ¡v ¡= ¡q.dequeue() ¡ ¡ ¡ ¡ ¡ ¡r.add-­‑last(v) ¡ ¡ ¡ ¡ ¡ ¡ for ¡each ¡direct ¡successor ¡v' ¡of ¡v ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡decrease ¡d[v'] ¡by ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡d[v'] ¡= ¡0 ¡ then ¡ ¡ q.enqueue(v') ¡ if ¡r.length() ¡< ¡|V| ¡ then ¡ ¡ raise ¡error: ¡cycle ¡found ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡ ¡

  11. QuesNon ¡ ¡ What ¡is ¡the ¡Nme ¡complexity ¡of ¡this ¡soluNon ¡if ¡adjacency ¡lists ¡are ¡ ¡ used ¡to ¡represent ¡edges ¡? ¡ ¡ 1. O(|V|) ¡ 2. O(|E|) ¡ govote.at ¡ ¡ 3. O(|V|+|E|) ¡ 4. O(|V|^2) ¡ Code ¡771118 ¡

  12. ImplemenNng ¡Topologic ¡Sort ¡ ¡ O(1) ¡ r ¡= ¡new ¡empty ¡list ¡ O(|V|+|E|) ¡ ¡ d ¡= ¡map ¡from ¡verNces ¡to ¡their ¡indegrees ¡ O(|V|) ¡ q ¡= ¡queue ¡with ¡all ¡nodes ¡of ¡indegree ¡0 ¡ while ¡q ¡is ¡non-­‑empty ¡ ¡ O(|V|) ¡ ¡ ¡do ¡ ¡ ¡ ¡ ¡ ¡v ¡= ¡q.dequeue() ¡ O(1) ¡ ¡ ¡ ¡ ¡ ¡r.add-­‑last(v) ¡ O(1) ¡ ¡ ¡ ¡ ¡ ¡ for ¡each ¡direct ¡successor ¡v' ¡of ¡v ¡ ¡ O(E) ¡in ¡total ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡do ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡decrease ¡d[v'] ¡by ¡1 ¡ O(1) ¡ O(1) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ¡d[v'] ¡= ¡0 ¡ then ¡ ¡ q.enqueue(v') ¡ if ¡r.length() ¡< ¡|V| ¡ then ¡ ¡ raise ¡error: ¡cycle ¡found ¡ O(1) ¡ return ¡r ¡// ¡Nodes, ¡topologically ¡sorted. ¡ O(1) ¡ ¡

  13. Example ¡of ¡Topologic ¡Sort ¡ ¡ ¡ ¡ Example ¡ discussed ¡on ¡the ¡ board ¡

  14. Depth-­‑first ¡Search ¡– ¡Again! ¡ ¡ ¡ ¡ • Depth-­‑first ¡search ¡ is ¡an ¡alternaNve ¡search ¡ ¡ order ¡that's ¡easier ¡to ¡implement ¡ ¡ • To ¡do ¡a ¡DFS ¡starNng ¡from ¡a ¡node: ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡visit ¡the ¡node ¡ ¡ ¡ ¡ ¡ ¡ ¡-­‑ ¡recursively ¡DFS ¡all ¡adjacent ¡nodes ¡(skipping ¡ any ¡already-­‑visited ¡nodes) ¡ ¡ • Much ¡simpler ¡ J ¡ ¡ ¡

  15. ApplicaNons ¡of ¡DFS ¡ ¡ ¡ ¡ -­‑ ¡ ¡Checking ¡if ¡a ¡graph ¡has ¡a ¡cycle ¡ ¡ ¡ -­‑ Checking ¡if ¡the ¡graph ¡is ¡connected ¡ ¡ -­‑ AlternaNve ¡algorithm ¡for ¡topological ¡sort ¡ -­‑ … ¡

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