Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Problem Solving with Algorithms and Data Struct...

Problem Solving with Algorithms and Data Structure - Graphs

Study slides for http://interactivepython.org/runestone/static/pythonds/index.html

Focus on Graphs and Graph Algorithms

Avatar for Bruce Tsai

Bruce Tsai

March 31, 2015
Tweet

More Decks by Bruce Tsai

Other Decks in Programming

Transcript

  1. Graph A set of objects where some pairs of objects

    are connected by links road map airline flights internet connection skill tree Tree is special kind of graph Vertex (node) Edge (arc) 3
  2. (Undirected) Graph G = (V, E) V = {v_1, v_2,

    v_3, v_4, v_5, v_6} E = {(v_1, v_2), (v_1, v_5), (v_2, v_3), (v_2, v_5), (v_3, v_4), (v_4, v_5), (v_4, v_6)} (v_1, v_5) == (v_5, v_1) 5
  3. Directed Graph D = (V, A) V = {v_0, v_1,

    v_2, v_3, v_4, v_5} A = {(v_0, v_1), (v_0, v_5), (v_1, v_2), (v_2, v_3), (v_3, v_5), (v_3, v_4), (v_4, v_0), (v_5, v_2), (v_5, v_4)} 6
  4. Path and Cycle Path Sequence of vertices that are connected

    by edges [v_1, v_5, v_4, v_6] Cycle A path that starts and ends at same vertex [v_2, v_3, v_4, v_5, v_2] 8
  5. Discussion Question A B C D E F 7 5

    1 2 7 3 2 8 1 2 4 5 6 1 8 10
  6. Word Ladder Problem Make change occur gradually by changing one

    letter at a time FOOL -> POOL -> POLL -> POLE -> PALE -> SALE -> SAGE 13
  7. Discussion Question - BFS [1] [1, 2, 3, 6] [2,

    3, 6] [3, 6, 4] [6, 4, 5] [4, 5] [5] 1 2 3 4 5 6 20
  8. Choose Better Next Vertex Visit hard-to-reach corners early Use the

    middle square to hop across board only when necessary 28
  9. General Depth First Search (DFS) Knight’s Tour is special case

    of depth first search create the depth first tree General depth first search is to search as deeply as possible 30
  10. P versus NP problem P: questions for which some algorithms

    can provide an answer in polynomial time NP: questions for which an answer can be verified in polynomial time Subset sum problem {-7, -3, -2, 5, 8} -> {-3, -2, 5} 34
  11. NP-complete Reduction: algorithm for transforming one problem into another problem

    NP-complete: a problem p in NP is NP- Complete if every other problem in NP can be transformed into p in polynomial time hardest problems in NP 36
  12. NP-hard Decision problem: any arbitrary yes-or-no question on an infinite

    set of inputs NP-hard: a decision problem H in NP-hard when for any problem L in NP, there is polynomial-time reduction from L to H at least as hard as the hardest problems in NP 37
  13. Topological Sorting Linear ordering of all vertices of a directed

    graph (u, v) is an edge of directed graph and u is before v in ordering 38
  14. Implementation 1. Call dfs(g) 2. Store vertices based on decreasing

    order of finish time 3. Return the ordered list 41
  15. Lemma A directed graph G is acyclic if and only

    if a depth-first search of G yields no back edges. Back edge: edge (u, v) connecting vertex u to an ancestor v in depth-first tree (v ↝ u → v) =>: back edge exists then cycle exists <=: cycle exists then back edge exists 42
  16. Proof of Implementation For any pair of distinct vertices u,

    v ∈ V, if there is an edge in G from u to v, then f[v] < f[u]. Consider edge (u, v) explored by DFS, v cannot be gray since then that edge would be back edge. Therefore v must be white or black. If v is white, v is descendant of u, and so f[v] < f[u] If v is black, it has already been finished, so that f[v] < f[u] 43
  17. Strongly Connected Components G = (V, E) and C ⊂

    V such that ∀ (v_i, v_j) ∈ C, path v_i to v_j and path v_j to v_i both exist C is strongly connected components (SCC) 44
  18. Implementation 1. Call dfs(g) 2. Compute transpose of g as

    g_t 3. Call dfs(g_t) but explore each vertex in decreasing order of finish time 4. Each tree of step 3 is a SCC original transpose 46
  19. Lemma Let C and C’ be distinct SCCs in directed

    graph G = (V, E), let u, v ∈ C, let u’, v’ ∈ C’, and suppose that there is a path u ↝ u’ in G. Then there cannot also be a path v’ ↝ v in G. if v’ ↝ v exists then both u ↝ u’ ↝ v’ and v’ ↝ v ↝ u exist C and C’ are not distinct SCCs 49
  20. Lemma Let C and C’ be distinct SCCs in directed

    graph G = (V, E). Suppose that there is an edge (u, v) ∈ E, where u ∈ C and v ∈ C’. Then f(C) > f(C’) from x ∈ C to w ∈ C’ from y ∈ C’ cannot reach any vertex in C 50
  21. Corollary Let C and C’ be distinct SCCs in directed

    graph G = (V, E). Suppose that there is an edge (u, v) ∈ ET, where u ∈ C and v ∈ C’. Then f(C) < f(C’). (v, u) ∈ E then f(C’) < f(C) 51
  22. Shortest Path Problem Find the path with the smallest total

    weight along which to route any given message 53
  23. Dijkstra’s Algorithm Iterative algorithm providing the shortest path from one

    particular starting node to all other nodes in graph Path distance, priority queue 54
  24. Minimum Spanning Tree T is acyclic subset of E that

    connects all vertices in V The sum of weight of edges in T is minized 61
  25. Kruskal’s Algorithm Sort edges in E into ascending order by

    weight For (u, v) ∈ E, if set of u not equal to set of v A ← A ∪ {(u, v)} 65