Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. Topological Sorting (Sequencing vertices)
  2. Only applicable to directed acyclic graphs (DAG)
  3. Vertices are tasks
  4. edges specify precedence
  5. x -> y -> y cannot be done until x is finished
  6. i.e. precedence graph
  7. Find a sequence in which tasks may be performed while obeying all precedence constraints
  8.  
  9. Assign topological numbers to vertices if topnum(x) = i - > i is in position of x in topological sorted sequence i is 0... n-1
  10. Precedence: if x -> y, then topnum(x) < topnum(y)
  11. _________
  12. | |
  13. A - > B - > D - > G - > I
  14. | 2 ^3 ^6 ^7
  15. | \ / | / / Possible Sequence A C B D E H G I
  16. | > C - > E - > H
  17. | 1 4 ^5
  18. |___________________|
  19.  
  20. BFS top sort
  21. compute indegree of each vertex O(n+e)
  22. BFS based sort
  23. 1. for each vertex with indegree O, number that verex with next highest number (0, 1, 2 ... ) enqueue O(n)
  24. 2. while queue is not empty do O(n+e)
  25. V <- dequeue()
  26. for each number w of V do
  27. indegree[w]--;
  28. if (indegree[w] == 0
  29. assign next number to w
  30. number++; O(n)
  31. enqueue(v);
  32. endif
  33. endfor
  34. endwhile
  35.  
  36. O(n) + O(n+e) + O(n) + O(n+e)
  37.  
  38. Dijkstra's Shortest(single source) Paths Algorithm (Any weighted undirected/directed)
  39. Q. Shortest path from A to F
  40. Trial & Error Approach
  41.  
  42. A -5-> B -6-> D -6-> F
  43. | \ ^ ^
  44. 10 3 2 2
  45. | \ / |
  46. C<---2----E----2---->G
  47.  
  48. Distance Array D -> keeps current shortest distance of vertex from A (in implementation, would be largest number storable i.e. Integer.Max.Value
  49. [0 5 10 11 8 inf inf]
  50. A B C D E F G
  51.  
  52. Previous Array -> contents previous array are vertex numbers in implementation
  53. [A A A E B G E]
  54.  
  55. Step|Done|D[B]|D[C]|D[D}|D[E]|D[F]|D[G]|
  56. ----------------------------------------
  57. 0 | A |5,A |10,A|inf |inf |inf |inf |
  58. ----------------------------------------
  59. 1 | B | |10,A|11,B|8,B | |inf |
  60. -----------------------------------
  61. 2 | E | |10,A|10,E| | |10,E| <- For next pick, any vertex u ok -> tie broken arbitrarily
  62. ----------------------------------------
  63. 3 | C | | |10,E| | |10,E|
  64. ----------------------------------------
  65. 4 | D | | | | |16,D|10,E|
  66. ----------------------------------------
  67. 5 | G | | | | |12,G| |
  68. ----------------------------------------
  69.  
  70. Fringe -> all vertices that are not done whose distance is not infinity
  71. Done -> distance will never be lower at any later point
  72.  
  73. Fringe : B(5), C(10)
  74.  
  75. Loop:
  76. Take Minimum distance out of fringe, this vertex is done
  77. For each neighbor, compute new distance and update with new distance if less than old.
  78.  
  79. Use Stack to follow breadcrumb trail of previous vertices
  80.  
  81. Single Source
  82. The first time a question is asked from a starting point X, run the algo through all vertices (dont stop when X is done)
  83. stopping only when fringe is empty. This will compute shortest paths to all vertices
  84. Store all the questions with same source X, just look up path in stored file/db
  85.  
  86. Induced Shortest Path Tree(SPT)
  87. When Algo is run all the way, store this:
  88.  
  89. A
  90. 5 / \ 10
  91. 5 B C 10
  92. 3 |
  93. 8 C
  94. 2 | \ 2
  95. 10 D G 10
  96. | 2
  97. F 12
  98.  
  99. Worst Case Running Time Analysis
  100. Algorithmic Components
  101. - Add Vertex to Fringe
  102. - Pick (delete) minimum distance vertex from fringe
  103. - Check now distance if neighbor against old
  104. - Update distance of neighbor, if new distance is less than old
  105.  
  106. Graphs stored in adjacency linked lists
  107.  
  108. Fringe Version 1: Unsorted linked list (when adding to fringe, add to front of LL)
  109. Worse Case
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement