CS210 · Annotated SlidesGraphs — Directed Graphs
Graphs · Annotated slides

Directed Graphs

Digraph applications, the Digraph API and adjacency lists, reachability with DFS, BFS in digraphs (web crawler), topological sort, and directed cycle detection.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: A l g o r i t h m s
Exam weight Low
    Slide 2: Slide 2
    Exam weight Low
      Slide 3: Directed graphs
      Exam weight Low
      1. Digraph: edges have a direction (v → w).
      Slide 4: Road network
      Exam weight Low
        Slide 5: Political blogosphere graph
        Exam weight Low
          Slide 6: Overnight interbank loan graph
          Exam weight Low

            Uber taxi graph

            Vertex = taxi pickup; edge = taxi ride.

            http://blog.uber.com/2012/01/09/uberdata-san-franciscomics/

            7
            Exam weight Low
              Slide 8: Implication graph
              Exam weight Low
                Slide 9: Combinational circuit
                Exam weight Low
                  Slide 10: WordNet graph
                  Exam weight Low
                    Slide 11: Digraph applications
                    Exam weight Low
                      Slide 12: Some digraph problems
                      Exam weight Low
                        Slide 13: Slide 13
                        Exam weight Low
                          Slide 14: Digraph API
                          Exam weight Low
                            Slide 15: Digraph API
                            Exam weight Low
                              Slide 16: Digraph representation: adjacency lists
                              Exam weight Low
                              1. Adjacency lists, but each edge appears once (in v’s list only).
                              Slide 17: Digraph representations
                              Exam weight Low
                                Slide 18: Adjacency-lists graph representation (review): Java implementation
                                Exam weight Low
                                  Slide 19: Adjacency-lists digraph representation: Java implementation
                                  Exam weight Low
                                    Slide 20: Slide 20
                                    Exam weight Low
                                      Slide 21: Reachability
                                      Exam weight Low
                                      1. Reachability: which vertices can be reached from s → DFS.
                                      Slide 22: Depth-Þrst search in digraphs
                                      Exam weight Low
                                        Slide 23: Iiii
                                        Exam weight Low
                                          Slide 24: 8
                                          Exam weight Low
                                            Slide 25: Depth-Þrst search demo
                                            Exam weight Low
                                              Slide 26: Depth-Þrst search (in undirected graphs)
                                              Exam weight Low
                                                Slide 27: Depth-Þrst search (in directed graphs)
                                                Exam weight Low
                                                  Slide 28: Reachability application: program control-ßow analysis
                                                  Exam weight Low
                                                    Slide 29: Reachability application: mark-sweep garbage collector
                                                    Exam weight Low
                                                      Slide 30: Reachability application: mark-sweep garbage collector
                                                      Exam weight Low

                                                        Depth-Þrst search in digraphs summary

                                                        DFS enables direct solution of simple digraph problems.

                                                        • ・ Reachability. ・ consin we Path finding. ・ Topological sort. ・ Directed cycle detection. Basis for solving difficult digraph problems. ・ 2-satisfiability. ・ Directed Euler path. ・ Strongly-connected components. SIAM J. COMPUT. Vol. 1, 2, No. June 1972 DEPTH-FIRST SEARCH AND LINEAR GRAPH ALGORITHMS* ROBERT TARJAN" Abstract. The value of depth-first search "bacltracking" technique for solving problems is or as a An improved version of an algorithm for finding the strongly connected illustrated by two examples. components a directed of graph and ar algorithm for finding the biconnected components an un-of direct graph are presented. The space and time requirements of both algorithms are bounded by k 1V d-+ k2E k for some constants kl, k2, and k where number of and E number Vis the vertices is the a, of edges of the graph being examined. Key words. Algorithm, backtracking, biconnectivity, connectivity, depth-first, graph, search, spanning strong-connectivity. tree,
                                                        31
                                                        Exam weight Low
                                                          Slide 32: Breadth-Þrst search in digraphs
                                                          Exam weight Low
                                                          1. BFS in digraphs finds directed shortest paths.
                                                          Slide 33: Directed breadth-Þrst search demo bfs 11.3.1919
                                                          Exam weight Low
                                                            Slide 34: Directed breadth-Þrst search demo
                                                            Exam weight Low
                                                              Slide 35: Multiple-source shortest paths
                                                              Exam weight Low
                                                                Slide 36: Slide 36
                                                                Exam weight Low
                                                                  Slide 37: direted
                                                                  Exam weight Low
                                                                    Slide 38: Breadth-Þrst search in digraphs application: web crawler
                                                                    Exam weight Low
                                                                      Slide 39: Bare-bones web crawler: Java implementation
                                                                      Exam weight Low
                                                                        Slide 40: Web crawler output
                                                                        Exam weight Low
                                                                          Slide 41: Slide 41
                                                                          Exam weight Low
                                                                            Slide 42: Precedence scheduling
                                                                            Exam weight Low
                                                                              Slide 43: Topological sort
                                                                              Exam weight High
                                                                              1. Topological sort: order vertices so every edge points forward. Possible only for a DAG. Reverse DFS postorder gives it.
                                                                              🎯 How this can come up · 1 question
                                                                              MCQ

                                                                              A topological order exists if and only if the digraph:

                                                                              Slide 44: Topological sort demo
                                                                              Exam weight Low
                                                                                Slide 45: Topological sort demo
                                                                                Exam weight Low
                                                                                  Slide 46: Depth-Þrst search order
                                                                                  Exam weight Low
                                                                                    Slide 47: Topological sort in a DAG: intuition
                                                                                    Exam weight Low
                                                                                      Slide 48: Topological sort in a DAG: correctness proof
                                                                                      Exam weight Low
                                                                                        Slide 49: Directed cycle detection
                                                                                        Exam weight Low
                                                                                        1. Directed cycle detection with DFS (a back edge to a vertex on the stack).
                                                                                        Slide 50: Directed cycle detection application: precedence scheduling
                                                                                        Exam weight Low
                                                                                          Slide 51: Directed cycle detection application: cyclic inheritance
                                                                                          Exam weight Low
                                                                                            Slide 52: Directed cycle detection application: spreadsheet recalculation
                                                                                            Exam weight Low

                                                                                              Depth-Þrst search orders

                                                                                              Observation. DFS visits each vertex exactly once. The order in which it does so can be important.

                                                                                              Orderings.

                                                                                              ・

                                                                                              Preorder: order in which dfs

                                                                                              is called.

                                                                                              ・

                                                                                              Postorder: order in which dfs

                                                                                              returns.

                                                                                              ・

                                                                                              Reverse postorder: reverse order in which dfs

                                                                                              returns.

                                                                                              private void dfs(Graph G, int v) { marked[v] = true; preorder.enqueue(v); for (int w: G.adj(v)) if (!marked[w]) dfs(G, w); postorder.enqueue(v); reversePostorder.push(v); }

                                                                                              53
                                                                                              Exam weight Low
                                                                                                Slide 54: Slide 54
                                                                                                Exam weight Low
                                                                                                  Slide 55: Strongly-connected components
                                                                                                  Exam weight Low
                                                                                                    Slide 56: Connected components vs. strongly-connected components
                                                                                                    Exam weight Low
                                                                                                      Slide 57: Strong component application: ecological food webs
                                                                                                      Exam weight Low
                                                                                                        Slide 58: Strong component application: software modules
                                                                                                        Exam weight Low

                                                                                                          X

                                                                                                          Strong components algorithms: brief history 1960s: Core OR problem.

                                                                                                          ・

                                                                                                          Widely studied; some practical algorithms.

                                                                                                          ・

                                                                                                          Complexity not understood.

                                                                                                          1972: linear-time DFS algorithm (Tarjan).

                                                                                                          ・

                                                                                                          Classic algorithm.

                                                                                                          ・

                                                                                                          Level of difficulty: Algs4++.

                                                                                                          ・

                                                                                                          Demonstrated broad applicability and importance of DFS.

                                                                                                          1980s: easy two-pass linear-time algorithm (Kosaraju-Sharir).

                                                                                                          ・

                                                                                                          Forgot notes for lecture; developed algorithm in order to teach it!

                                                                                                          ・

                                                                                                          Later found in Russian scientific literature (1972).

                                                                                                          1990s: more easy linear-time algorithms.

                                                                                                          ・

                                                                                                          Gabow: fixed old OR algorithm.

                                                                                                          ・

                                                                                                          Cheriyan-Mehlhorn: needed one-pass algorithm for LEDA.

                                                                                                          59
                                                                                                          Exam weight Low
                                                                                                            Slide 60: Kosaraju-Sharir algorithm: intuition
                                                                                                            Exam weight Low
                                                                                                              Slide 61: Kosaraju-Sharir algorithm demo
                                                                                                              Exam weight Low
                                                                                                                Slide 62: Kosaraju-Sharir algorithm demo
                                                                                                                Exam weight Low
                                                                                                                  Slide 63: Kosaraju-Sharir algorithm demo
                                                                                                                  Exam weight Low
                                                                                                                    Slide 64: Kosaraju-Sharir algorithm
                                                                                                                    Exam weight Low
                                                                                                                      Slide 65: Kosaraju-Sharir algorithm
                                                                                                                      Exam weight Low
                                                                                                                        Slide 66: Kosaraju-Sharir algorithm
                                                                                                                        Exam weight Low
                                                                                                                          Slide 67: Connected components in an undirected graph (with DFS)
                                                                                                                          Exam weight Low
                                                                                                                            Slide 68: Strong components in a digraph (with two DFSs)
                                                                                                                            Exam weight Low
                                                                                                                              Slide 69: Digraph-processing summary: algorithms of the day
                                                                                                                              Exam weight Low