Guest User

Untitled

a guest
Nov 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. # Algorithm Analysis
  2.  
  3. ## Gale-Shapley Algorithm(Stable Marriage Problem)
  4.  
  5. Correctness:
  6. - Termination: algo terminates after at most n2 iterations.
  7. - Perfection: Everyone get married
  8. - Stability: The marriages are stable
  9.  
  10. Algorithm:
  11. ```
  12. initialize each person to be free
  13. while (some man m is free and hasn't proposed to every woman) do
  14. w = highest ranked woman in m's list to whom m has not yet proposed
  15. if (w is free) then
  16. (m, w) become engaged
  17. else if (w prefers m to her fiancÈ m') then
  18. (m, w) become engaged
  19. m' become free
  20. return the set S of engaged pairs
  21. ```
  22. Even with the same procedure, we might implement the algorthm with different time complexity due to many reasons(e.g. different data structures)
  23.  
  24. # Graph
  25.  
  26. ~~~~~~~~~~~~~~~~~~~~
  27. The core of graph is to discuss the relationship of nodes(objects), which represent the edges
  28. ~~~~~~~~~~~~~~~~~~~~
  29.  
  30. ## How to solve problems? → Car Theorem
  31. - C → Criticality: Identify the core of problems, and remove extraneous details
  32. - A → Abstraction: First think at high level(design the algorithm), then go down to low-level(implementation)
  33. - R → Restriction: List the limitations, and think how to extend the algorithm to simplify the limitations
  34.  
  35. ## Graph concepts
  36. - Directed edge: ()
  37. - Undirected edge: {}
  38. - Neighbor nodes: Adjancency
  39.  
  40. An undirected graph G with n vertices is a tree if it satisfied two of the following statements:
  41. - Graph G does not contains cycle
  42. - Graph G is connected(all vertices are reachable)
  43. - Graph G has n-1 edges
  44.  
  45. A rooted tree encodes the notion of hierarchy (parent, child, sibling, uncle, nephew, root)
  46.  
  47. ### BFS vs DFS
  48.  
  49. ~~~~~~~~~~~~~~~~~~~~
  50. Find all reachable nodes and paths of certain node
  51. ~~~~~~~~~~~~~~~~~~~~
  52.  
  53. BFS:
  54. - Able to find the shortest path of two vertices by using 'Layer'. Other visited & labeled nodes will be skipped.
  55. - The distance between two node will be the difference of their layer in BFS tree. (note: the difference of layer each edge got in BFS tree will no greater than 1, which means that the edge in BFS tree will not cross 2 layers or above)
  56. Application using BFS: The color fill-in feature of painter(If two pixels hold the same color, then we build a edge between them and give them the new color)
  57.  
  58. Non-tree edges in BFS: They are ancestor-decendent relationship
  59. Non-tree edges in DFS: They are sibling or uncle relationship
  60.  
  61. Traverse time of a graph -> O(m+n) m=number of edges & n= number of nodes
  62.  
  63. How to store graph?
  64. Adjacency matrix -> Inefficient when encounter sparse graph
  65. Time: neighbor nodes lookup adjacency : O(n)
  66. Space: O(n2)
  67.  
  68. Adjacency list (Best suited for BFS since it can lower the complexity)
  69. Time: O(deg(u)))
  70. Space: O(n+2m) = O(n+m) (undirected)
  71.  
  72. #### Implementation of BFS & DFS
  73.  
  74. Queue(FIFO) a double-opened structure with front(pop) & rear(push) pointer
  75. Note that the push pop in Javascript array is not queue but stack since it is LIFO
  76.  
  77. Stack(LIFO) a single-opened structure with top pointer
  78.  
  79. Implement Stack & Queue with linked list:
  80. - variable size
  81. - insert/delete in O(1)
  82.  
  83. For the implementation of BFS, both queue and stack are fine because the order does not matter.
  84. Adjacency list is preferable for storing Graph in BFS, since the step of neighbor searching in BFS is important.
  85. And the complexity of neighbor searching in adjacency list O(n+m) is lower than adjacency matrix, which is O(n2)
  86.  
  87. The implementation of DFS uses stack
  88. - Recursive ver: We don't need to maintain stack since we are using a system stack
  89. - Iteration ver: we need to maintain a stack data structure by ourselves
Add Comment
Please, Sign In to add comment