scientific programming part b
play

Scientific Programming: Part B Graphs Luca Bianco - Academic Year - PowerPoint PPT Presentation

Scientific Programming: Part B Graphs Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Graphs: examples http://www.kegg.jp/ [From: Compeau et al, How to apply de Bruijn graphs to genome


  1. BFS: application. Shortest distance/Shortest path printing the shortest path... root or nodes not reached == -1 All parents: {'a': 'a', 'c': 'a', 'f': 'a', 'e': 'a', 'b': 'c', 'd': 'c', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1} Path from 'a' to 'j': a --> f --> g --> j Path from 'a' to 'k': Not available

  2. BFS: application. Shortest distance/Shortest path printing the shortest path... root or nodes not reached == -1 All parents: {'a': 'j', 'c': 'a', 'f': 'b', 'e': 'a', 'b': 'b', 'd': 'j', 'g': 'f', 'j': 'g', 'h': 'e', 'k': -1, 'l': -1} Path from 'b' to 'c': b --> f --> g --> j --> a --> c

  3. Exercise What if the shortest path between (a,j) is j→ a??? Shortest path from 'a' to 'j': j --> a

  4. Traversals: Depth First Search (DFS)

  5. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends )

  6. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1)

  7. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2))

  8. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(3)))

  9. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(3, DFS(4))))

  10. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(3))) DFS(4): nothing to do. Done.

  11. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(3, DFS(6))))

  12. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(3)))) DFS(6): nothing to do. Done.

  13. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2))) DFS(3): nothing to do. Done.

  14. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2, DFS(5))))

  15. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1, DFS(2)) DFS(5): nothing to do. Done.

  16. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack:DFS(1) DFS(2): nothing to do. Done.

  17. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack: DONE! DFS(1): nothing to do. Done.

  18. Traversals: Depth First Search (DFS) Idea: Visit the first node ( mark it as visited )… … then recursively all its children nodes ( follow one path until it ends ) Execution stack: DFS(7) Done.

  19. Recursive Depth First Search (DFS) DFS from a: visiting: a visiting: c visiting: b visiting: f visiting: g visiting: j visiting: d visiting: e visiting: h

  20. Recursive Depth First Search (DFS) DFS from b: visiting: b visiting: f visiting: g visiting: j visiting: a visiting: c visiting: d visiting: e visiting: h

  21. Recursive Depth First Search (DFS) With recursive calls, “unclosed” calls are memorized in the stack and with big graphs this can cause a stack overflow error.

  22. Iterative Depth First Search (DFS) DFS from a: DFS from b: visiting a visiting b visiting e visiting f visiting h visiting g visiting j visiting j visiting d visiting d visiting b visiting a visiting f visiting e visiting g visiting h visiting c visiting c

  23. Connected graphs and components

  24. Connected components

  25. Reachability

  26. Application of DFS

  27. Connected components ● ids is a list containing the component identifiers (it is also used as ‘visited’ structure) ● ids[u] is the identifier of the connected component to which u belongs

  28. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  29. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  30. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  31. Connected components ids is != 0 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  32. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  33. Connected components ids is != 0 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  34. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  35. Connected components ids is != 0 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  36. Connected components ids is != 0 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  37. Connected components call on d completed 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  38. Connected components call on c,b,a completed in the order The algorithm tries to restart from b,c,d but some steps later… nodes are component 1 is visited… done, component 2 starts... 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  39. Connected components 3 connected components: {'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 2, 'g': 2, 'f': 2, 'h': 2, 'i': 2, 'j': 3, 'k': 3}

  40. Definitions Ignored, trivial cycle

  41. Definitions Idea: perform a DFS visit, if it finds a node already visited then there is a cycle

  42. Cycle detection: undirected graph

Recommend


More recommend