SlideShare a Scribd company logo
1 of 78
Download to read offline
Algorithms                                R OBERT S EDGEWICK | K EVIN W AYNE




                                          4.1 U NDIRECTED G RAPHS
                                          ‣ introduction
                                          ‣ graph API
                                          ‣ depth-first search
 Algorithms
                                          ‣ breadth-first search
            F O U R T H   E D I T I O N




 R OBERT S EDGEWICK | K EVIN W AYNE
                                          ‣ connected components
http://algs4.cs.princeton.edu             ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Undirected graphs

Graph. Set of vertices connected pairwise by edges.


Why study graph algorithms?
・Thousands of practical applications.
・Hundreds of graph algorithms known.
・Interesting and broadly useful abstraction.
・Challenging branch of computer science and discrete math.




                                                             3
Protein-protein interaction network




    Reference: Jeong et al, Nature Review | Genetics
                                                       4
The Internet as mapped by the Opte Project




http://en.wikipedia.org/wiki/Internet


                                             5
Map of science clickstreams




                http://www.plosone.org/article/info:doi/10.1371/journal.pone.0004803
                                                                                       6
10 million Facebook friends




                     "Visualizing Friendships" by Paul Butler



                                                                7
One week of Enron emails




                           8
The evolution of FCC lobbying coalitions




         “The Evolution of FCC Lobbying Coalitions” by Pierre de Vries in JoSS Visualization Symposium 2010   9
weight status controlled for homophily.25 The                  smoking status of egos and alters at times t and
               key variable of interest was an alter’s obesity at             t + 1 to the foregoing models. We also analyzed
               time t + 1. A significant coefficient for this vari-           the role of geographic distance between egos
Framingham heart study
               able would suggest either that an alter’s weight               and alters by adding such a variable.
               affected an ego’s weight or that an ego and an                     We calculated 95% confidence intervals by sim-
               alter experienced contemporaneous events affect-               ulating the first difference in the alter’s contem-




                 Figure 1. Largest Connected Subcomponent of the Social Network in the Framingham Heart Study in the Year 2000.
                 Each circle (node) represents one person in the data set. There are 2200 persons in this subcomponent of the social
                 network. Circles with red borders denote women, and circles with blue borders denote men. The size of each circle
                 is proportional to the person’s body-mass index. The interior color of the circles indicates the person’s obesity status:
                 yellow denotes an obese person (body-mass index, ≥30) and green denotes a nonobese person. The colors of the
                 ties between the nodes indicate the relationship between them: purple denotes a friendship or marital tie and orange
                 denotes a familial tie.


 “The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007                 10
                                                   n engl j med 357;4   www.nejm.org     july 26, 2007                                       373
Graph applications


          graph                     vertex                          edge

     communication          telephone, computer               fiber optic cable

          circuit          gate, register, processor                wire

       mechanical                    joint                   rod, beam, spring

         financial             stock, currency                  transactions

      transportation      street intersection, airport     highway, airway route

         internet              class C network                  connection

          game                  board position                  legal move

    social relationship         person, actor              friendship, movie cast

      neural network                neuron                        synapse

     protein network                protein              protein-protein interaction

    chemical compound              molecule                        bond

                                                                                       11
Graph terminology

Path. Sequence of vertices connected by edges.
Cycle. Path whose first and last vertices are the same.


Two vertices are connected if there is a path between them.


                                                   vertex
                                           edge
                      cycle of
                     length 5


                                                             path of
                                                            length 4

                      vertex of
                      degree 3




                                                   connected
                                                  components



                                  Anatomy of a graph
                                                                       12
Some graph-processing problems

Path. Is there a path between s and t ?
Shortest path. What is the shortest path between s and t ?


Cycle. Is there a cycle in the graph?
Euler tour. Is there a cycle that uses each edge exactly once?
Hamilton tour. Is there a cycle that uses each vertex exactly once.


Connectivity. Is there a way to connect all of the vertices?
MST. What is the best way to connect all of the vertices?
Biconnectivity. Is there a vertex whose removal disconnects the graph?


Planarity. Can you draw the graph in the plane with no crossing edges
Graph isomorphism. Do two adjacency lists represent the same graph?


Challenge. Which of these problems are easy? difficult? intractable?


                                                                         13
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Graph representation

Graph drawing. Provides intuition about the structure of the graph.




                           two drawings of the same graph
                                                Two drawings of the same graph




Caveat. Intuition can be misleading.
                                                                                 16
      Two drawings of the same graph
Graph representation

Vertex representation.
・This lecture: use integers between 0 and V – 1.
・Applications: convert between names and integers with symbol table.

          A                                                 0



              B          C        G                             1       2       6

                                             symbol table

                  D           E                                     3       4


          F                                                 5




                      self-loop       parallel
                                       edges
Anomalies.

                             Anomalies
                                                                                    17
Graph API

       public class Graph

                    Graph(int V)             create an empty graph with V vertices

                    Graph(In in)               create a graph from input stream

              void addEdge(int v, int w)               add an edge v-w

  Iterable<Integer> adj(int v)                       vertices adjacent to v

               int V()                                number of vertices

               int E()                                 number of edges

            String toString()                        string representation



        In in = new In(args[0]);                      read graph from
        Graph G = new Graph(in);                        input stream


        for (int v = 0; v < G.V(); v++)                print out each
           for (int w : G.adj(v))                       edge (twice)
              StdOut.println(v + "-" + w);
                                                                                     18
Graph API: sample client

Graph input format.

             tinyG.txt                              % java Test tinyG.txt
                                                      mediumG.txt
         V                                        V 0-6250
              13
                    E                                           E
              13                                    0-21273
              0 5                                   0-1244 246
              4 3                                   0-5239 240
              0 1                                      238 245
                                                    1-0
              9 12                                     235 238
              6 4                                   2-0233 240
              5 4                                   3-5232 248
              0 2                                   3-4231 248
              11 12                                 … 229 249
              9 10                                     228 241
                                                    12-11
              0 6                                      226 231
              7 8
                                                    12-9
                                                       ...
              9 11                                      (1261 additional lines)
              5 3
                            Input format for Graph constructor (two examples)

        In in = new In(args[0]);                                read graph from
        Graph G = new Graph(in);                                  input stream


        for (int v = 0; v < G.V(); v++)                           print out each
           for (int w : G.adj(v))                                  edge (twice)
              StdOut.println(v + "-" + w);
                                                                                   19
4.1    Undirected Graphs   523

Typical graph-processing code
               task                    implementation

                                public static int degree(Graph G, int v)
                                {
                                   int degree = 0;
      compute the degree of v
                                   for (int w : G.adj(v)) degree++;
                                   return degree;
                                }



                                public static int maxDegree(Graph G)
                                {
                                   int max = 0;
                                   for (int v = 0; v < G.V(); v++)
     compute maximum degree           if (degree(G, v) > max)
                                         max = degree(G, v);
                                   return max;
                                }



                                public static double averageDegree(Graph G)
      compute average degree    { return 2.0 * G.E() / G.V(); }



                                public static int numberOfSelfLoops(Graph G)
                                {
                                   int count = 0;
                                   for (int v = 0; v < G.V(); v++)
         count self-loops             for (int w : G.adj(v))
                                         if (v == w) count++;
                                   return count/2;   // each edge counted twice
                                }
                                                                                                 20
Set-of-edges graph representation

Maintain a list of the edges (linked list or array).




       0                                           0    1
                                                   0    2
                                                   0    5
           1       2        6
                                                   0    6
                                                   3    4
               3       4                           3    5
                                                   4    5
       5                                           4    6
                                                   7    8
                       9        10                 9   10
                                                   9   11
       7       8                                   9   12
                       11       12                11   12




                                                            21
Adjacency-matrix graph representation

Maintain a two-dimensional V-by-V boolean array;
for each edge v–w in graph: adj[v][w] = adj[w][v] = true.

                               two entries
                               for each edge
      0
                                               0   1   2   3   4   5   6   7   8   9   10   11   12

                                          0    0   1   1   0   0   1   1   0   0   0   0    0    0
          1       2        6              1    1   0   0   0   0   0   0   0   0   0   0    0    0
                                          2    1   0   0   0   0   0   0   0   0   0   0    0    0
                                          3    0   0   0   0   1   1   0   0   0   0   0    0    0
              3       4                   4    0   0   0   1   0   1   1   0   0   0   0    0    0
                                          5    1   0   0   1   1   0   0   0   0   0   0    0    0
      5                                   6    1   0   0   0   1   0   0   0   0   0   0    0    0
                                          7    0   0   0   0   0   0   0   0   1   0   0    0    0
                                          8    0   0   0   0   0   0   0   1   0   0   0    0    0
                      9        10         9    0   0   0   0   0   0   0   0   0   0   1    1    1
                                         10    0   0   0   0   0   0   0   0   0   1   0    0    0
      7       8
                                         11    0   0   0   0   0   0   0   0   0   1   0    0    1
                      11       12        12    0   0   0   0   0   0   0   0   0   1   0    1    0




                                                                                                      22
Adjacency-list graph representation

Maintain vertex-indexed array of lists.

                                                                 6      2         1       5

                                                                 0
                                                                                  Bag objects

                                                                 0
      0
                                          adj[]
                                           0                     5      4
                                           1
          1       2        6               2                     5      6         3
                                           3
                                           4                     3      4         0
              3       4                    5
                                           6                     0      4
                                           7
      5                                    8                     8
                                                                            representations
                                           9                                of the same edge
                                          10                     7
                      9        10         11
                                          12                    11     10        12
      7       8
                                                                 9
                      11       12
                                                                 9      12

                                                                11      9


                                          Adjacency-lists representation (undirected graph)     23
Adjacency-list graph representation: Java implementation

   public class Graph
   {
      private final int V;                      adjacency lists
      private Bag<Integer>[] adj;               ( using Bag data type )


       public Graph(int V)
       {
          this.V = V;
                                                create empty graph
          adj = (Bag<Integer>[]) new Bag[V];    with V vertices
          for (int v = 0; v < V; v++)
             adj[v] = new Bag<Integer>();
       }

       public void addEdge(int v, int w)
       {                                        add edge v-w
          adj[v].add(w);                        (parallel edges and
          adj[w].add(v);                        self-loops allowed)

       }

       public Iterable<Integer> adj(int v)      iterator for vertices adjacent to v

       { return adj[v]; }
   }
                                                                                      24
Graph representations

In practice. Use adjacency-lists representation.
・Algorithms based on iterating over vertices adjacent to v.
・Real-world graphs tend to be sparse.
                                                huge number of vertices,
                                                small average vertex degree




           sparse (E = 200)             dense (E = 1000)




                              Two graphs (V = 50)


                                                                              25
Graph representations

In practice. Use adjacency-lists representation.
・Algorithms based on iterating over vertices adjacent to v.
・Real-world graphs tend to be sparse.
                                            huge number of vertices,
                                            small average vertex degree




                                             edge between        iterate over vertices
    representation    space     add edge
                                               v and w?              adjacent to v?


     list of edges      E          1                E                        E


   adjacency matrix    V2          1*               1                        V


    adjacency lists   E+V          1           degree(v)                  degree(v)


                                                                * disallows parallel edges



                                                                                             26
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Maze exploration

Maze graph.
・Vertex = intersection.
・Edge = passage.




           intersection   passage




Goal. Explore every intersection in the maze.
                                                29
Trémaux maze exploration

Algorithm.
・Unroll a ball of string behind you.
・Mark each visited intersection and each visited passage.
・Retrace steps when no unvisited options.




                            Tremaux exploration


                                                            30
Trémaux maze exploration

Algorithm.
・Unroll a ball of string behind you.
・Mark each visited intersection and each visited passage.
・Retrace steps when no unvisited options.
First use? Theseus entered Labyrinth to kill the monstrous Minotaur;
Ariadne instructed Theseus to use a ball of string to find his way back out.




                                        Claude Shannon (with Theseus mouse)


                                                                               31
Maze exploration




                   32
Maze exploration




                   33
Depth-first search

Goal. Systematically search through a graph.
Idea. Mimic maze exploration.



                  DFS (to visit a vertex v)

                  Mark v as visited.
                  Recursively visit all unmarked
                        vertices w adjacent to v.




Typical applications.
・Find all vertices connected to a given source vertex.
・Find a path between two vertices.

Design challenge. How to implement?
Design pattern for graph processing

Design pattern. Decouple graph data type from graph processing.
・Create a Graph object.
・Pass the Graph to a graph-processing routine.
・Query the graph-processing routine for information.

       public class Paths

                    Paths(Graph G, int s)        find paths in G from source s

           boolean hasPathTo(int v)               is there a path from s to v?

  Iterable<Integer> pathTo(int v)           path from s to v; null if no such path




               Paths paths = new Paths(G, s);
               for (int v = 0; v < G.V(); v++)
                  if (paths.hasPathTo(v))
                                                             print all vertices
                     StdOut.println(v);                       connected to s


                                                                                     35
Depth-first search demo

To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
                                                              tinyG.txt
    0                                 7         8         V
                                                               13
                                                                     E
                                                               13
                                                               0 5
                                                               4 3
                                                               0 1
           1            2       6     9         10             9 12
                                                               6 4
                                                               5 4
                                                               0 2
                                                               11 12
                3           4        11         12
                                                               9 10
                                                               0 6
                                                               7 8
                                                               9 11
    5
                                                               5 3




 graph G

                                                                          36
Depth-first search demo

To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
    0                                 7         8         v    marked[]   edgeTo[v]

                                                          0        T         –
                                                          1        T         0
                                                          2        T         0
          1             2        6    9         10        3        T         5
                                                          4        T         6
                                                          5        T         4
                                                          6        T         0
                3            4       11         12        7        F         –
                                                          8        F         –
                                                          9        F         –
    5                                                     10       F         –
                                                          11       F         –
                                                          12       F         –

 vertices reachable from 0

                                                                                      37
Depth-first search

Goal. Find all vertices connected to s (and a corresponding path).
Idea. Mimic maze exploration.


Algorithm.
・Use recursion (ball of string).
・Mark each visited vertex (and keep track of edge taken to visit it).
・Return (retrace steps) when no unvisited options.
Data structures.
・ boolean[] marked to mark visited vertices.
・ int[] edgeTo to keep tree of paths.
   (edgeTo[w] == v) means that edge v-w taken to visit w for first time
Depth-first search

    public class DepthFirstPaths
    {                                             marked[v] = true
       private boolean[] marked;                  if v connected to s
       private int[] edgeTo;                      edgeTo[v] = previous
       private int s;                             vertex on path from s to v


        public DepthFirstSearch(Graph G, int s)
        {
           ...                                    initialize data structures
           dfs(G, s);                             find vertices connected to s
        }

        private void dfs(Graph G, int v)
                                                  recursive DFS does the work
        {
           marked[v] = true;
           for (int w : G.adj(v))
              if (!marked[w])
              {
                 dfs(G, w);
                 edgeTo[w] = v;
              }
        }
    }
                                                                                 39
Depth-first search properties

Proposition. DFS marks all vertices connected to s in time proportional to
the sum of their degrees.


Pf. [correctness]                                        source                set of marked
                                                                                   vertices
・If w marked, then w connected to s (why?)                        s

・If w connected to s, then w marked.
   (if w unmarked, then consider last edge
   on a path from s to w that goes from a                             v
                                                                              no such edge
                                                   set of                      can exist
   marked vertex to an unmarked one).            unmarked
                                                  vertices            x

Pf. [running time]
Each vertex connected to s is visited once.
                                                                          w




                                                                                               40
Depth-first search properties

Proposition. After DFS, can find vertices connected to s in constant time
and can find a path to s (if one exists) in time proportional to its length.


Pf. edgeTo[] is parent-link representation of a tree rooted at s.




  public boolean hasPathTo(int v)
  { return marked[v]; }

                                                                           edgeTo[]
  public Iterable<Integer> pathTo(int v)
                                                                             0
  {
                                                                             1 2
     if (!hasPathTo(v)) return null;                                         2 0
     Stack<Integer> path = new Stack<Integer>();                             3 2
     for (int x = v; x != s; x = edgeTo[x])                                  4 3
        path.push(x);                                                        5 3
     path.push(s);                                     x   path
     return path;                                      5   5
  }                                                    3   3 5
                                                       2   2 3 5
                                                       0   0 2 3 5

                                                           Trace of pathTo() computation
                                                                                     41
Depth-first search application: preparing for a date




       http://xkcd.com/761/




                                                      42
Depth-first search application: flood fill

Challenge. Flood fill (Photoshop magic wand).
Assumptions. Picture has millions to billions of pixels.




Solution. Build a grid graph.
・Vertex: pixel.
・Edge: between two adjacent gray pixels.
・Blob: all pixels connected to given pixel.
                                                           43
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Breadth-first search demo

Repeat until queue is empty:
・Remove vertex v from queue.
・Add to queue all unmarked vertices adjacent to v and mark them.

                                                        tinyCG.txt            standard d
           0                         2
                                                        V
                                                                6         E
                                                                8
                                                                0   5
                 1                                              2   4
                                                                2   3
                                                                1   2         drawing w
                                                                0   1
                     3                                          3   4
                                                                3   5
           5                         4                          0   2




                                                        adjacency lists
                                                                                2

 graph G
                                                                                0
                                                            adj[]
                                                                                    46
                                                            0
                                                                                0
Breadth-first search demo

Repeat until queue is empty:
・Remove vertex v from queue.
・Add to queue all unmarked vertices adjacent to v and mark them.

        0                            2
                                                           v   edgeTo[] distTo[]

                                                           0       –       0
                                                           1       0       1
                 1                                         2       0       1
                                                           3       2       2
                                                           4       2       2
                     3                                     5       0       1
        5                            4




 done

                                                                               47
Breadth-first search

Depth-first search. Put unvisited vertices on a stack.
Breadth-first search. Put unvisited vertices on a queue.


Shortest path. Find path from s to t that uses fewest number of edges.



   BFS (from source vertex s)

   Put s onto a FIFO queue, and mark s as visited.
   Repeat until the queue is empty:
    - remove the least recently added vertex v
    - add each of v's unvisited neighbors to the queue,
     and mark them as visited.




                                                                   Breadth-first
                                                                  maze exploration
Intuition. BFS examines vertices in increasing distance from s.
                                                                                     48
Breadth-first search properties

Proposition. BFS computes shortest paths (fewest number of edges)
from s to all other vertices in a graph in time proportional to E + V.


Pf. [correctness] Queue always consists of zero or more vertices of
distance k from s, followed by zero or more vertices of distance k + 1.


Pf. [running time] Each vertex connected to s is visited once.




                                                     1
                                                                    4
   0                   2


         1                              0            2

                                                                    3
              3
   5                   4
                                                     5


             graph                   dist = 0      dist = 1       dist = 2
                                                                             49
Breadth-first search

             public class BreadthFirstPaths
             {
                private boolean[] marked;
                private int[] edgeTo;
                …

                 private void bfs(Graph G, int s)
                 {
                   Queue<Integer> q = new Queue<Integer>();
                    q.enqueue(s);
                    marked[s] = true;
                    while (!q.isEmpty())
                    {
                       int v = q.dequeue();
                       for (int w : G.adj(v))
                       {
                          if (!marked[w])
                          {
                             q.enqueue(w);
                             marked[w] = true;
                             edgeTo[w] = v;
                          }
                       }
                    }
                 }
             }

                                                              50
Breadth-first search application: routing

Fewest number of hops in a communication network.




                            ARPANET, July 1977



                                                    51
Breadth-first search application: Kevin Bacon numbers

Kevin Bacon numbers.




                                                 Endless Games board game




                                                 SixDegrees iPhone App
 http://oracleofbacon.org

                                                                            52
Kevin Bacon graph

・Include one vertex for each performer and one for each movie.
・Connect a movie to all performers that appear in that movie.
・Compute shortest path from s = Kevin Bacon.
                                                                          Patrick                Dial M           Grace
                         Caligola                                                                                 Kelly
                                                                           Allen               for Murder



                                      Glenn            The Stepford
                                      Close                Wives                                                                High
                                                                                                       To Catch                 Noon
                                                                                                        a Thief
                 John
               Gielgud
                                    Portrait                                                                                  Lloyd
                                    of a Lady                                   The Eagle                                    Bridges
                                                             Nicole             Has Landed
                                                             Kidman


                    Murder on the
                    Orient Express
                                                  Cold                           Donald
                                                                               Sutherland           Kathleen               Joe Versus
                                                Mountain
                                                                                                    Quinlan               the Volcano



                                         An American            John              Animal
           Hamlet                          Haunting            Belushi             House                                         performer
                                                                                                  Apollo 13                        vertex
                           Vernon
                          Dobtcheff
                                                       The
                                                     Woodsman                       Kevin
                                                                                    Bacon                              Tom
                                                                                                                      Hanks
                                                                                                      Bill
           movie                                                                                     Paxton
                                                            Wild
           vertex                                          Things                 The River
                                       Jude                                          Wild
                                                                                                                      The Da
                                                                                                        Paul
                                                                                                                    Vinci Code
                                                                                                      Herbert
                                                                       Meryl
               Enigma                                                 Streep                                                      Serretta
                                      Kate                                                                                         Wilson
                                    Winslet                                                              Yves
                                                                                     Titanic
                                                                                                        Aubert        Shane
           Eternal Sunshine                                                                                            Zaza
            of the Spotless
                 Mind




                                                                                                                                             53
                                        V and E
            movies.txt
                                      not explicitly
                                        specified
Breadth-first search application: Erdös numbers




               hand-drawing of part of the Erdös graph by Ron Graham
                                                                       54
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Connectivity queries

Def. Vertices v and w are connected if there is a path between them.


Goal. Preprocess graph to answer queries of the form is v connected to w?
in constant time.


      public class CC

                    CC(Graph G)                find connected components in G

           boolean connected(int v, int w)        are v and w connected?

               int count()                    number of connected components

               int id(int v)                     component identifier for v




Union-Find? Not quite.
Depth-first search. Yes. [next few slides]
                                                                               57
Connected components

The relation "is connected to" is an equivalence relation:
・Reflexive: v is connected to v.
・Symmetric: if v is connected to w, then w is connected to v.
・Transitive: if v connected to w and w connected to x, then v connected to x.
Def. A connected component is a maximal set of connected vertices.
                                                                    v    id[]
                                                                    0      0
                                               7    8
               0                                                    1      0
                                                                    2      0
                                                                    3      0
                   1         2       6                              4      0
                                               9    10              5      0
                                                                    6      0
                       3         4                                  7      1
                                              11    12              8      1
               5                                                    9      2
                                                                   10      2
                           3 connected components                  11      2
                                                                   12      2


Remark. Given connected components, can answer queries in constant time.
                                                                                58
Connected components

Def. A connected component is a maximal set of connected vertices.




                          63 connected components
                                                                     59
Connected components

Goal. Partition vertices into connected components.


    Connected components


    Initialize all vertices v as unmarked.


    For each unmarked vertex v, run DFS to identify all           tinyG.txt
    vertices discovered as part of the same component.        V
                                                                   13
                                                                          E
                                                                   13
                                                                   0 5
                       tinyG.txt                              mediumG.txt
                                                                   4 3
                   V                                      V    250 0 1
                         13
                               E                                         E
                         13                                    1273 12
                                                                   9
                         0 5                                   244 6246
                                                                      4
                         4 3                                   239 5240
                                                                      4
                         0 1                                   238 0245
                                                                      2
                         9 12                                  235 11 12
                                                                    238
                                                                   9 10
                         6 4                                   233 240
                                                                   0 6
                         5 4                                   232 248
                                                                   7 8
                         0 2                                   231 9248
                                                                      11
                         11 12                                 229 5249
                                                                      3
                         9 10                                 228 241                        I
                         0 6                                  226 231
                         7 8                                  ...                       60

                         9 11                                 (1261 additional lines)
Connected components demo

To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
    0                                 7         8         v    marked[]   id[]

                                                          0       F       –
                                                          1       F       –
                                                          2       F       –
           1            2       6     9         10        3       F       –
                                                          4       F       –
                                                          5       F       –
                                                          6       F       –
                3           4        11         12        7       F       –
                                                          8       F       –
                                                          9       F       –
    5                                                     10      F       –
                                                          11      F       –
                                                          12      F       –

 graph G

                                                                                 61
Connected components demo

To visit a vertex v :
・Mark vertex v as visited.
・Recursively visit all unmarked vertices adjacent to v.
    0                                 7         8         v    marked[]   id[]

                                                          0       T       0
                                                          1       T       0
                                                          2       T       0
          1             2       6     9         10        3       T       0
                                                          4       T       0
                                                          5       T       0
                                                          6       T       0
                3           4        11         12        7       T       1
                                                          8       T       1
                                                          9       T       2
    5                                                     10      T       2
                                                          11      T       2
                                                          12      T       2

 done

                                                                                 62
Finding connected components with DFS

    public class CC
    {
       private boolean[] marked;
       private int[] id;                     id[v] = id of component containing v
       private int count;                    number of components


        public CC(Graph G)
        {
           marked = new boolean[G.V()];
           id = new int[G.V()];
           for (int v = 0; v < G.V(); v++)
           {
              if (!marked[v])
              {                              run DFS from one vertex in
                 dfs(G, v);                  each component
                 count++;
              }
           }
        }

        public int count()                   see next slide
        public int id(int v)
        private void dfs(Graph G, int v)

    }
                                                                                    63
Finding connected components with DFS (continued)



   public int count()
                                             number of components
   { return count; }

   public int id(int v)                      id of component containing v
   { return id[v]; }

   private void dfs(Graph G, int v)
   {
      marked[v] = true;                      all vertices discovered in
      id[v] = count;                         same call of dfs have same id
      for (int w : G.adj(v))
         if (!marked[w])
            dfs(G, w);
   }




                                                                             64
Connected components application: study spread of STDs




                               Relationship graph at "Jefferson High"


    Peter Bearman, James Moody, and Katherine Stovel. Chains of affection: The structure of
    adolescent romantic and sexual networks. American Journal of Sociology, 110(1): 44–99, 2004.
                                                                                                   65
Connected components application: particle detection

Particle detection. Given grayscale image of particles, identify "blobs."
・Vertex: pixel.
・Edge: between two adjacent pixels with grayscale value ≥ 70.
・Blob: connected component of 20-30 pixels.                       black = 0
                                                                  white = 255




Particle tracking. Track moving particles over time.
                                                                                66
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
4.1 U NDIRECTED G RAPHS
                                      ‣ introduction
                                      ‣ graph API
                                      ‣ depth-first search
 Algorithms
                                      ‣ breadth-first search

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ connected components
http://algs4.cs.princeton.edu         ‣ challenges
Graph-processing challenge 1

Problem. Is a graph bipartite?


                                                             0                       0-1
                                                                                     0-2
                                                                                     0-5
                                                                 1       2       6
                                                                                     0-6
                                                                                     1-3
How difficult?                                                       3       4       2-3
 ・Any programmer could do it.                                5
                                                                                     2-4
                                                                                     4-5
✓・Typical diligent algorithms student could do it.                                   4-6

 ・Hire an expert.
 ・Intractable.                   simple DFS-based solution
                                      (see textbook)         0                       0-1
 ・No one knows.                                                                      0-2
                                                                                     0-5
 ・Impossible.                                                    1       2       6
                                                                                     0-6
                                                                                     1-3
                                                                     3       4       2-3
                                                                                     2-4
                                                             5                       4-5
                                                                                     4-6
                                                             { 0, 3, 4 }

                                                                                           69
Bipartiteness application: is dating graph bipartite?




                                              Image created by Mark Newman.




                                                                              70
Graph-processing challenge 2

Problem. Find a cycle.


                                                          0                       0-1
                                                                                  0-2
                                                                                  0-5
                                                              1       2       6
                                                                                  0-6
                                                                                  1-3
How difficult?                                                    3       4       2-3
 ・Any programmer could do it.                             5
                                                                                  2-4
                                                                                  4-5
✓・Typical diligent algorithms student could do it.                                4-6

 ・Hire an expert.                                         0-5-4-6-0

 ・Intractable.                simple DFS-based solution
                                   (see textbook)
 ・No one knows.
 ・Impossible.



                                                                                        71
Bridges of Königsberg

The Seven Bridges of Königsberg. [Leonhard Euler 1736]

          “ … in Königsberg in Prussia, there is an island A, called the
          Kneiphof; the river which surrounds it is divided into two branches ...
          and these branches are crossed by seven bridges. Concerning these
          bridges, it was asked whether anyone could arrange a route in such a
          way that he could cross each bridge once and only once. ”




Euler tour. Is there a (general) cycle that uses each edge exactly once?
Answer. Yes iff connected and all vertices have even degree.


                                                                                    72
Graph-processing challenge 3

Problem. Find a (general) cycle that uses every edge exactly once.


                                                                  0                       0-1
                                                                                          0-2
                                                                                          0-5
                                                                      1       2       6
                                                                                          0-6
                                                                                          1-2
How difficult?                                                            3       4       2-3
 ・Any programmer could do it.                                     5
                                                                                          2-4
                                                                                          3-4
✓・Typical diligent algorithms student could do it.                                        4-5

 ・Hire an expert.                                             0-1-2-3-4-2-0-6-4-5-0
                                                                                          4-6

 ・Intractable.                       Eulerian tour
                         (classic graph-processing problem)
 ・No one knows.
 ・Impossible.



                                                                                                73
Graph-processing challenge 4

Problem. Find a cycle that visits every vertex exactly once.


                                                          0                        0-1
                                                                                   0-2
                                                                                   0-5
                                                               1       2       6
                                                                                   0-6
                                                                                   1-2
How difficult?                                                     3       4       2-6
 ・Any programmer could do it.                             5
                                                                                   3-4
                                                                                   3-5
 ・Typical diligent algorithms student could do it.                                 4-5

 ・Hire an expert.                                         0-5-3-4-6-2-1-0
                                                                                   4-6

✓・Intractable.
                              Hamiltonian cycle
 ・No one knows.       (classical NP-complete problem)

 ・Impossible.



                                                                                         74
Graph-processing challenge 5

Problem. Are two graphs identical except for vertex names?


                                                           0                       0-1
                                                                                   0-2
                                                                                   0-5
                                                               1       2       6
                                                                                   0-6
                                                                                   3-4
How difficult?                                                     3       4       3-5
 ・Any programmer could do it.                              5
                                                                                   4-5
                                                                                   4-6
 ・Typical diligent algorithms student could do it.
 ・Hire an expert.
 ・Intractable.                                                 3                   0-4
✓ No one knows.
 ・                                                                         2
                                                                                   0-5
                                                                                   0-6
 ・Impossible.          graph isomorphism is
                                                                   4
                                                                           1
                                                                                   1-4
                                                                                   1-5
                                                          6
                    longstanding open problem
                                                                               5   2-4
                                                                                   3-4
                                                               0                   5-6

                                                0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1

                                                                                         75
Graph-processing challenge 6

 Problem. Lay out a graph in the plane without crossing edges?


                                                                        1                   0-1
                                                                                            0-2
                                                                                    2
                                                                                            0-5
                                                                            0               0-6
                                                                                    6
                                                                                            3-4
 How difficult?                                                     3
                                                                                        4   3-5
  ・Any programmer could do it.                                                              4-5
                                                                                            4-6
  ・Typical diligent algorithms student could do it.                     5

✓ ・Hire an expert.
  ・Intractable.         linear-time DFS-based planarity algorithm
                                                                    0

  ・No one knows.              discovered by Tarjan in 1970s

  ・Impossible.           (too complicated for most practitioners)       1       2       6


                                                                            3       4

                                                                    5




                                                                                                  76
4.1 U NDIRECTED G RAPHS
                                      ‣ graph API
                                      ‣ depth-first search
                                      ‣ breadth-first search
 Algorithms
                                      ‣ connected components

 R OBERT S EDGEWICK | K EVIN W AYNE
                                      ‣ challenges
http://algs4.cs.princeton.edu
Algorithms                                R OBERT S EDGEWICK | K EVIN W AYNE




                                          4.1 U NDIRECTED G RAPHS
                                          ‣ introduction
                                          ‣ graph API
                                          ‣ depth-first search
 Algorithms
                                          ‣ breadth-first search
            F O U R T H   E D I T I O N




 R OBERT S EDGEWICK | K EVIN W AYNE
                                          ‣ connected components
http://algs4.cs.princeton.edu             ‣ challenges

More Related Content

What's hot

Software Engineering- Requirement Elicitation and Specification
Software Engineering- Requirement Elicitation and SpecificationSoftware Engineering- Requirement Elicitation and Specification
Software Engineering- Requirement Elicitation and SpecificationNishu Rastogi
 
Software Engineering unit 2
Software Engineering unit 2Software Engineering unit 2
Software Engineering unit 2Abhimanyu Mishra
 
Data science applications and usecases
Data science applications and usecasesData science applications and usecases
Data science applications and usecasesSreenatha Reddy K R
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchNisha Soms
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Goal based and utility based agents
Goal based and utility based agentsGoal based and utility based agents
Goal based and utility based agentsMegha Sharma
 
Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering arvind pandey
 
Elimination of left recursion
Elimination of left recursionElimination of left recursion
Elimination of left recursionMahmudul Hasan
 
REQUIREMENT ENGINEERING
REQUIREMENT ENGINEERINGREQUIREMENT ENGINEERING
REQUIREMENT ENGINEERINGSaqib Raza
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualizationDr. Hamdan Al-Sabri
 

What's hot (20)

Software Engineering- Requirement Elicitation and Specification
Software Engineering- Requirement Elicitation and SpecificationSoftware Engineering- Requirement Elicitation and Specification
Software Engineering- Requirement Elicitation and Specification
 
Software Engineering unit 2
Software Engineering unit 2Software Engineering unit 2
Software Engineering unit 2
 
Lecture 1 ddbms
Lecture 1 ddbmsLecture 1 ddbms
Lecture 1 ddbms
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Dbms unit-3
Dbms unit-3Dbms unit-3
Dbms unit-3
 
Data science applications and usecases
Data science applications and usecasesData science applications and usecases
Data science applications and usecases
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
 
Propositional Logic and Pridicate logic
Propositional Logic and Pridicate logicPropositional Logic and Pridicate logic
Propositional Logic and Pridicate logic
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Goal based and utility based agents
Goal based and utility based agentsGoal based and utility based agents
Goal based and utility based agents
 
Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering
 
R studio
R studio R studio
R studio
 
Elimination of left recursion
Elimination of left recursionElimination of left recursion
Elimination of left recursion
 
REQUIREMENT ENGINEERING
REQUIREMENT ENGINEERINGREQUIREMENT ENGINEERING
REQUIREMENT ENGINEERING
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualization
 

Similar to Undirected graphs

Topology ppt
Topology pptTopology ppt
Topology pptboocse11
 
Interpretation of the biological knowledge using networks approach
Interpretation of the biological knowledge using networks approachInterpretation of the biological knowledge using networks approach
Interpretation of the biological knowledge using networks approachElena Sügis
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug designReihaneh Safavi
 
Knowledge Graphs and Milestone
Knowledge Graphs and MilestoneKnowledge Graphs and Milestone
Knowledge Graphs and MilestoneBarry Norton
 
Higher-order spectral graph clustering with motifs
Higher-order spectral graph clustering with motifsHigher-order spectral graph clustering with motifs
Higher-order spectral graph clustering with motifsAustin Benson
 
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptxthanhdowork
 
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...BigMine
 
Disintegration of the small world property with increasing diversity of chemi...
Disintegration of the small world property with increasing diversity of chemi...Disintegration of the small world property with increasing diversity of chemi...
Disintegration of the small world property with increasing diversity of chemi...N. Sukumar
 
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Xiaohan Zeng
 
Socialnetworkanalysis (Tin180 Com)
Socialnetworkanalysis (Tin180 Com)Socialnetworkanalysis (Tin180 Com)
Socialnetworkanalysis (Tin180 Com)Tin180 VietNam
 
Geometric correlations in multiplexes and how they make them more robust
Geometric correlations in multiplexes and how they make them more robustGeometric correlations in multiplexes and how they make them more robust
Geometric correlations in multiplexes and how they make them more robustKolja Kleineberg
 
Using spectral radius ratio for node degree
Using spectral radius ratio for node degreeUsing spectral radius ratio for node degree
Using spectral radius ratio for node degreeIJCNCJournal
 
A multithreaded method for network alignment
A multithreaded method for network alignmentA multithreaded method for network alignment
A multithreaded method for network alignmentDavid Gleich
 
Scale-Free Networks to Search in Unstructured Peer-To-Peer Networks
Scale-Free Networks to Search in Unstructured Peer-To-Peer NetworksScale-Free Networks to Search in Unstructured Peer-To-Peer Networks
Scale-Free Networks to Search in Unstructured Peer-To-Peer NetworksIOSR Journals
 

Similar to Undirected graphs (20)

TopologyPPT.ppt
TopologyPPT.pptTopologyPPT.ppt
TopologyPPT.ppt
 
Topology ppt
Topology pptTopology ppt
Topology ppt
 
Topology ppt
Topology pptTopology ppt
Topology ppt
 
Topology ppt
Topology pptTopology ppt
Topology ppt
 
Interpretation of the biological knowledge using networks approach
Interpretation of the biological knowledge using networks approachInterpretation of the biological knowledge using networks approach
Interpretation of the biological knowledge using networks approach
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug design
 
Knowledge Graphs and Milestone
Knowledge Graphs and MilestoneKnowledge Graphs and Milestone
Knowledge Graphs and Milestone
 
Higher-order spectral graph clustering with motifs
Higher-order spectral graph clustering with motifsHigher-order spectral graph clustering with motifs
Higher-order spectral graph clustering with motifs
 
A tutorial in Connectome Analysis (1) - Marcus Kaiser
A tutorial in Connectome Analysis (1) - Marcus KaiserA tutorial in Connectome Analysis (1) - Marcus Kaiser
A tutorial in Connectome Analysis (1) - Marcus Kaiser
 
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx
240401_JW_labseminar[LINE: Large-scale Information Network Embeddin].pptx
 
Basics of Genome Assembly
Basics of Genome Assembly Basics of Genome Assembly
Basics of Genome Assembly
 
Book chapter 2
Book chapter 2Book chapter 2
Book chapter 2
 
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...
Inside the Atoms: Mining a Network of Networks and Beyond by HangHang Tong at...
 
Disintegration of the small world property with increasing diversity of chemi...
Disintegration of the small world property with increasing diversity of chemi...Disintegration of the small world property with increasing diversity of chemi...
Disintegration of the small world property with increasing diversity of chemi...
 
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
Social Network Analysis: What It Is, Why We Should Care, and What We Can Lear...
 
Socialnetworkanalysis (Tin180 Com)
Socialnetworkanalysis (Tin180 Com)Socialnetworkanalysis (Tin180 Com)
Socialnetworkanalysis (Tin180 Com)
 
Geometric correlations in multiplexes and how they make them more robust
Geometric correlations in multiplexes and how they make them more robustGeometric correlations in multiplexes and how they make them more robust
Geometric correlations in multiplexes and how they make them more robust
 
Using spectral radius ratio for node degree
Using spectral radius ratio for node degreeUsing spectral radius ratio for node degree
Using spectral radius ratio for node degree
 
A multithreaded method for network alignment
A multithreaded method for network alignmentA multithreaded method for network alignment
A multithreaded method for network alignment
 
Scale-Free Networks to Search in Unstructured Peer-To-Peer Networks
Scale-Free Networks to Search in Unstructured Peer-To-Peer NetworksScale-Free Networks to Search in Unstructured Peer-To-Peer Networks
Scale-Free Networks to Search in Unstructured Peer-To-Peer Networks
 

Recently uploaded

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 

Recently uploaded (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

Undirected graphs

  • 1. Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 2. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 3. Undirected graphs Graph. Set of vertices connected pairwise by edges. Why study graph algorithms? ・Thousands of practical applications. ・Hundreds of graph algorithms known. ・Interesting and broadly useful abstraction. ・Challenging branch of computer science and discrete math. 3
  • 4. Protein-protein interaction network Reference: Jeong et al, Nature Review | Genetics 4
  • 5. The Internet as mapped by the Opte Project http://en.wikipedia.org/wiki/Internet 5
  • 6. Map of science clickstreams http://www.plosone.org/article/info:doi/10.1371/journal.pone.0004803 6
  • 7. 10 million Facebook friends "Visualizing Friendships" by Paul Butler 7
  • 8. One week of Enron emails 8
  • 9. The evolution of FCC lobbying coalitions “The Evolution of FCC Lobbying Coalitions” by Pierre de Vries in JoSS Visualization Symposium 2010 9
  • 10. weight status controlled for homophily.25 The smoking status of egos and alters at times t and key variable of interest was an alter’s obesity at t + 1 to the foregoing models. We also analyzed time t + 1. A significant coefficient for this vari- the role of geographic distance between egos Framingham heart study able would suggest either that an alter’s weight and alters by adding such a variable. affected an ego’s weight or that an ego and an We calculated 95% confidence intervals by sim- alter experienced contemporaneous events affect- ulating the first difference in the alter’s contem- Figure 1. Largest Connected Subcomponent of the Social Network in the Framingham Heart Study in the Year 2000. Each circle (node) represents one person in the data set. There are 2200 persons in this subcomponent of the social network. Circles with red borders denote women, and circles with blue borders denote men. The size of each circle is proportional to the person’s body-mass index. The interior color of the circles indicates the person’s obesity status: yellow denotes an obese person (body-mass index, ≥30) and green denotes a nonobese person. The colors of the ties between the nodes indicate the relationship between them: purple denotes a friendship or marital tie and orange denotes a familial tie. “The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007 10 n engl j med 357;4 www.nejm.org july 26, 2007 373
  • 11. Graph applications graph vertex edge communication telephone, computer fiber optic cable circuit gate, register, processor wire mechanical joint rod, beam, spring financial stock, currency transactions transportation street intersection, airport highway, airway route internet class C network connection game board position legal move social relationship person, actor friendship, movie cast neural network neuron synapse protein network protein protein-protein interaction chemical compound molecule bond 11
  • 12. Graph terminology Path. Sequence of vertices connected by edges. Cycle. Path whose first and last vertices are the same. Two vertices are connected if there is a path between them. vertex edge cycle of length 5 path of length 4 vertex of degree 3 connected components Anatomy of a graph 12
  • 13. Some graph-processing problems Path. Is there a path between s and t ? Shortest path. What is the shortest path between s and t ? Cycle. Is there a cycle in the graph? Euler tour. Is there a cycle that uses each edge exactly once? Hamilton tour. Is there a cycle that uses each vertex exactly once. Connectivity. Is there a way to connect all of the vertices? MST. What is the best way to connect all of the vertices? Biconnectivity. Is there a vertex whose removal disconnects the graph? Planarity. Can you draw the graph in the plane with no crossing edges Graph isomorphism. Do two adjacency lists represent the same graph? Challenge. Which of these problems are easy? difficult? intractable? 13
  • 14. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 15. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 16. Graph representation Graph drawing. Provides intuition about the structure of the graph. two drawings of the same graph Two drawings of the same graph Caveat. Intuition can be misleading. 16 Two drawings of the same graph
  • 17. Graph representation Vertex representation. ・This lecture: use integers between 0 and V – 1. ・Applications: convert between names and integers with symbol table. A 0 B C G 1 2 6 symbol table D E 3 4 F 5 self-loop parallel edges Anomalies. Anomalies 17
  • 18. Graph API public class Graph Graph(int V) create an empty graph with V vertices Graph(In in) create a graph from input stream void addEdge(int v, int w) add an edge v-w Iterable<Integer> adj(int v) vertices adjacent to v int V() number of vertices int E() number of edges String toString() string representation In in = new In(args[0]); read graph from Graph G = new Graph(in); input stream for (int v = 0; v < G.V(); v++) print out each for (int w : G.adj(v)) edge (twice) StdOut.println(v + "-" + w); 18
  • 19. Graph API: sample client Graph input format. tinyG.txt % java Test tinyG.txt mediumG.txt V V 0-6250 13 E E 13 0-21273 0 5 0-1244 246 4 3 0-5239 240 0 1 238 245 1-0 9 12 235 238 6 4 2-0233 240 5 4 3-5232 248 0 2 3-4231 248 11 12 … 229 249 9 10 228 241 12-11 0 6 226 231 7 8 12-9 ... 9 11 (1261 additional lines) 5 3 Input format for Graph constructor (two examples) In in = new In(args[0]); read graph from Graph G = new Graph(in); input stream for (int v = 0; v < G.V(); v++) print out each for (int w : G.adj(v)) edge (twice) StdOut.println(v + "-" + w); 19
  • 20. 4.1 Undirected Graphs 523 Typical graph-processing code task implementation public static int degree(Graph G, int v) { int degree = 0; compute the degree of v for (int w : G.adj(v)) degree++; return degree; } public static int maxDegree(Graph G) { int max = 0; for (int v = 0; v < G.V(); v++) compute maximum degree if (degree(G, v) > max) max = degree(G, v); return max; } public static double averageDegree(Graph G) compute average degree { return 2.0 * G.E() / G.V(); } public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) count self-loops for (int w : G.adj(v)) if (v == w) count++; return count/2; // each edge counted twice } 20
  • 21. Set-of-edges graph representation Maintain a list of the edges (linked list or array). 0 0 1 0 2 0 5 1 2 6 0 6 3 4 3 4 3 5 4 5 5 4 6 7 8 9 10 9 10 9 11 7 8 9 12 11 12 11 12 21
  • 22. Adjacency-matrix graph representation Maintain a two-dimensional V-by-V boolean array; for each edge v–w in graph: adj[v][w] = adj[w][v] = true. two entries for each edge 0 0 1 2 3 4 5 6 7 8 9 10 11 12 0 0 1 1 0 0 1 1 0 0 0 0 0 0 1 2 6 1 1 0 0 0 0 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 1 1 0 0 0 0 0 0 0 3 4 4 0 0 0 1 0 1 1 0 0 0 0 0 0 5 1 0 0 1 1 0 0 0 0 0 0 0 0 5 6 1 0 0 0 1 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 1 0 0 0 0 8 0 0 0 0 0 0 0 1 0 0 0 0 0 9 10 9 0 0 0 0 0 0 0 0 0 0 1 1 1 10 0 0 0 0 0 0 0 0 0 1 0 0 0 7 8 11 0 0 0 0 0 0 0 0 0 1 0 0 1 11 12 12 0 0 0 0 0 0 0 0 0 1 0 1 0 22
  • 23. Adjacency-list graph representation Maintain vertex-indexed array of lists. 6 2 1 5 0 Bag objects 0 0 adj[] 0 5 4 1 1 2 6 2 5 6 3 3 4 3 4 0 3 4 5 6 0 4 7 5 8 8 representations 9 of the same edge 10 7 9 10 11 12 11 10 12 7 8 9 11 12 9 12 11 9 Adjacency-lists representation (undirected graph) 23
  • 24. Adjacency-list graph representation: Java implementation public class Graph { private final int V; adjacency lists private Bag<Integer>[] adj; ( using Bag data type ) public Graph(int V) { this.V = V; create empty graph adj = (Bag<Integer>[]) new Bag[V]; with V vertices for (int v = 0; v < V; v++) adj[v] = new Bag<Integer>(); } public void addEdge(int v, int w) { add edge v-w adj[v].add(w); (parallel edges and adj[w].add(v); self-loops allowed) } public Iterable<Integer> adj(int v) iterator for vertices adjacent to v { return adj[v]; } } 24
  • 25. Graph representations In practice. Use adjacency-lists representation. ・Algorithms based on iterating over vertices adjacent to v. ・Real-world graphs tend to be sparse. huge number of vertices, small average vertex degree sparse (E = 200) dense (E = 1000) Two graphs (V = 50) 25
  • 26. Graph representations In practice. Use adjacency-lists representation. ・Algorithms based on iterating over vertices adjacent to v. ・Real-world graphs tend to be sparse. huge number of vertices, small average vertex degree edge between iterate over vertices representation space add edge v and w? adjacent to v? list of edges E 1 E E adjacency matrix V2 1* 1 V adjacency lists E+V 1 degree(v) degree(v) * disallows parallel edges 26
  • 27. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 28. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 29. Maze exploration Maze graph. ・Vertex = intersection. ・Edge = passage. intersection passage Goal. Explore every intersection in the maze. 29
  • 30. Trémaux maze exploration Algorithm. ・Unroll a ball of string behind you. ・Mark each visited intersection and each visited passage. ・Retrace steps when no unvisited options. Tremaux exploration 30
  • 31. Trémaux maze exploration Algorithm. ・Unroll a ball of string behind you. ・Mark each visited intersection and each visited passage. ・Retrace steps when no unvisited options. First use? Theseus entered Labyrinth to kill the monstrous Minotaur; Ariadne instructed Theseus to use a ball of string to find his way back out. Claude Shannon (with Theseus mouse) 31
  • 34. Depth-first search Goal. Systematically search through a graph. Idea. Mimic maze exploration. DFS (to visit a vertex v) Mark v as visited. Recursively visit all unmarked vertices w adjacent to v. Typical applications. ・Find all vertices connected to a given source vertex. ・Find a path between two vertices. Design challenge. How to implement?
  • 35. Design pattern for graph processing Design pattern. Decouple graph data type from graph processing. ・Create a Graph object. ・Pass the Graph to a graph-processing routine. ・Query the graph-processing routine for information. public class Paths Paths(Graph G, int s) find paths in G from source s boolean hasPathTo(int v) is there a path from s to v? Iterable<Integer> pathTo(int v) path from s to v; null if no such path Paths paths = new Paths(G, s); for (int v = 0; v < G.V(); v++) if (paths.hasPathTo(v)) print all vertices StdOut.println(v); connected to s 35
  • 36. Depth-first search demo To visit a vertex v : ・Mark vertex v as visited. ・Recursively visit all unmarked vertices adjacent to v. tinyG.txt 0 7 8 V 13 E 13 0 5 4 3 0 1 1 2 6 9 10 9 12 6 4 5 4 0 2 11 12 3 4 11 12 9 10 0 6 7 8 9 11 5 5 3 graph G 36
  • 37. Depth-first search demo To visit a vertex v : ・Mark vertex v as visited. ・Recursively visit all unmarked vertices adjacent to v. 0 7 8 v marked[] edgeTo[v] 0 T – 1 T 0 2 T 0 1 2 6 9 10 3 T 5 4 T 6 5 T 4 6 T 0 3 4 11 12 7 F – 8 F – 9 F – 5 10 F – 11 F – 12 F – vertices reachable from 0 37
  • 38. Depth-first search Goal. Find all vertices connected to s (and a corresponding path). Idea. Mimic maze exploration. Algorithm. ・Use recursion (ball of string). ・Mark each visited vertex (and keep track of edge taken to visit it). ・Return (retrace steps) when no unvisited options. Data structures. ・ boolean[] marked to mark visited vertices. ・ int[] edgeTo to keep tree of paths. (edgeTo[w] == v) means that edge v-w taken to visit w for first time
  • 39. Depth-first search public class DepthFirstPaths { marked[v] = true private boolean[] marked; if v connected to s private int[] edgeTo; edgeTo[v] = previous private int s; vertex on path from s to v public DepthFirstSearch(Graph G, int s) { ... initialize data structures dfs(G, s); find vertices connected to s } private void dfs(Graph G, int v) recursive DFS does the work { marked[v] = true; for (int w : G.adj(v)) if (!marked[w]) { dfs(G, w); edgeTo[w] = v; } } } 39
  • 40. Depth-first search properties Proposition. DFS marks all vertices connected to s in time proportional to the sum of their degrees. Pf. [correctness] source set of marked vertices ・If w marked, then w connected to s (why?) s ・If w connected to s, then w marked. (if w unmarked, then consider last edge on a path from s to w that goes from a v no such edge set of can exist marked vertex to an unmarked one). unmarked vertices x Pf. [running time] Each vertex connected to s is visited once. w 40
  • 41. Depth-first search properties Proposition. After DFS, can find vertices connected to s in constant time and can find a path to s (if one exists) in time proportional to its length. Pf. edgeTo[] is parent-link representation of a tree rooted at s. public boolean hasPathTo(int v) { return marked[v]; } edgeTo[] public Iterable<Integer> pathTo(int v) 0 { 1 2 if (!hasPathTo(v)) return null; 2 0 Stack<Integer> path = new Stack<Integer>(); 3 2 for (int x = v; x != s; x = edgeTo[x]) 4 3 path.push(x); 5 3 path.push(s); x path return path; 5 5 } 3 3 5 2 2 3 5 0 0 2 3 5 Trace of pathTo() computation 41
  • 42. Depth-first search application: preparing for a date http://xkcd.com/761/ 42
  • 43. Depth-first search application: flood fill Challenge. Flood fill (Photoshop magic wand). Assumptions. Picture has millions to billions of pixels. Solution. Build a grid graph. ・Vertex: pixel. ・Edge: between two adjacent gray pixels. ・Blob: all pixels connected to given pixel. 43
  • 44. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 45. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 46. Breadth-first search demo Repeat until queue is empty: ・Remove vertex v from queue. ・Add to queue all unmarked vertices adjacent to v and mark them. tinyCG.txt standard d 0 2 V 6 E 8 0 5 1 2 4 2 3 1 2 drawing w 0 1 3 3 4 3 5 5 4 0 2 adjacency lists 2 graph G 0 adj[] 46 0 0
  • 47. Breadth-first search demo Repeat until queue is empty: ・Remove vertex v from queue. ・Add to queue all unmarked vertices adjacent to v and mark them. 0 2 v edgeTo[] distTo[] 0 – 0 1 0 1 1 2 0 1 3 2 2 4 2 2 3 5 0 1 5 4 done 47
  • 48. Breadth-first search Depth-first search. Put unvisited vertices on a stack. Breadth-first search. Put unvisited vertices on a queue. Shortest path. Find path from s to t that uses fewest number of edges. BFS (from source vertex s) Put s onto a FIFO queue, and mark s as visited. Repeat until the queue is empty: - remove the least recently added vertex v - add each of v's unvisited neighbors to the queue, and mark them as visited. Breadth-first maze exploration Intuition. BFS examines vertices in increasing distance from s. 48
  • 49. Breadth-first search properties Proposition. BFS computes shortest paths (fewest number of edges) from s to all other vertices in a graph in time proportional to E + V. Pf. [correctness] Queue always consists of zero or more vertices of distance k from s, followed by zero or more vertices of distance k + 1. Pf. [running time] Each vertex connected to s is visited once. 1 4 0 2 1 0 2 3 3 5 4 5 graph dist = 0 dist = 1 dist = 2 49
  • 50. Breadth-first search public class BreadthFirstPaths { private boolean[] marked; private int[] edgeTo; … private void bfs(Graph G, int s) { Queue<Integer> q = new Queue<Integer>(); q.enqueue(s); marked[s] = true; while (!q.isEmpty()) { int v = q.dequeue(); for (int w : G.adj(v)) { if (!marked[w]) { q.enqueue(w); marked[w] = true; edgeTo[w] = v; } } } } } 50
  • 51. Breadth-first search application: routing Fewest number of hops in a communication network. ARPANET, July 1977 51
  • 52. Breadth-first search application: Kevin Bacon numbers Kevin Bacon numbers. Endless Games board game SixDegrees iPhone App http://oracleofbacon.org 52
  • 53. Kevin Bacon graph ・Include one vertex for each performer and one for each movie. ・Connect a movie to all performers that appear in that movie. ・Compute shortest path from s = Kevin Bacon. Patrick Dial M Grace Caligola Kelly Allen for Murder Glenn The Stepford Close Wives High To Catch Noon a Thief John Gielgud Portrait Lloyd of a Lady The Eagle Bridges Nicole Has Landed Kidman Murder on the Orient Express Cold Donald Sutherland Kathleen Joe Versus Mountain Quinlan the Volcano An American John Animal Hamlet Haunting Belushi House performer Apollo 13 vertex Vernon Dobtcheff The Woodsman Kevin Bacon Tom Hanks Bill movie Paxton Wild vertex Things The River Jude Wild The Da Paul Vinci Code Herbert Meryl Enigma Streep Serretta Kate Wilson Winslet Yves Titanic Aubert Shane Eternal Sunshine Zaza of the Spotless Mind 53 V and E movies.txt not explicitly specified
  • 54. Breadth-first search application: Erdös numbers hand-drawing of part of the Erdös graph by Ron Graham 54
  • 55. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 56. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 57. Connectivity queries Def. Vertices v and w are connected if there is a path between them. Goal. Preprocess graph to answer queries of the form is v connected to w? in constant time. public class CC CC(Graph G) find connected components in G boolean connected(int v, int w) are v and w connected? int count() number of connected components int id(int v) component identifier for v Union-Find? Not quite. Depth-first search. Yes. [next few slides] 57
  • 58. Connected components The relation "is connected to" is an equivalence relation: ・Reflexive: v is connected to v. ・Symmetric: if v is connected to w, then w is connected to v. ・Transitive: if v connected to w and w connected to x, then v connected to x. Def. A connected component is a maximal set of connected vertices. v id[] 0 0 7 8 0 1 0 2 0 3 0 1 2 6 4 0 9 10 5 0 6 0 3 4 7 1 11 12 8 1 5 9 2 10 2 3 connected components 11 2 12 2 Remark. Given connected components, can answer queries in constant time. 58
  • 59. Connected components Def. A connected component is a maximal set of connected vertices. 63 connected components 59
  • 60. Connected components Goal. Partition vertices into connected components. Connected components Initialize all vertices v as unmarked. For each unmarked vertex v, run DFS to identify all tinyG.txt vertices discovered as part of the same component. V 13 E 13 0 5 tinyG.txt mediumG.txt 4 3 V V 250 0 1 13 E E 13 1273 12 9 0 5 244 6246 4 4 3 239 5240 4 0 1 238 0245 2 9 12 235 11 12 238 9 10 6 4 233 240 0 6 5 4 232 248 7 8 0 2 231 9248 11 11 12 229 5249 3 9 10 228 241 I 0 6 226 231 7 8 ... 60 9 11 (1261 additional lines)
  • 61. Connected components demo To visit a vertex v : ・Mark vertex v as visited. ・Recursively visit all unmarked vertices adjacent to v. 0 7 8 v marked[] id[] 0 F – 1 F – 2 F – 1 2 6 9 10 3 F – 4 F – 5 F – 6 F – 3 4 11 12 7 F – 8 F – 9 F – 5 10 F – 11 F – 12 F – graph G 61
  • 62. Connected components demo To visit a vertex v : ・Mark vertex v as visited. ・Recursively visit all unmarked vertices adjacent to v. 0 7 8 v marked[] id[] 0 T 0 1 T 0 2 T 0 1 2 6 9 10 3 T 0 4 T 0 5 T 0 6 T 0 3 4 11 12 7 T 1 8 T 1 9 T 2 5 10 T 2 11 T 2 12 T 2 done 62
  • 63. Finding connected components with DFS public class CC { private boolean[] marked; private int[] id; id[v] = id of component containing v private int count; number of components public CC(Graph G) { marked = new boolean[G.V()]; id = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { run DFS from one vertex in dfs(G, v); each component count++; } } } public int count() see next slide public int id(int v) private void dfs(Graph G, int v) } 63
  • 64. Finding connected components with DFS (continued) public int count() number of components { return count; } public int id(int v) id of component containing v { return id[v]; } private void dfs(Graph G, int v) { marked[v] = true; all vertices discovered in id[v] = count; same call of dfs have same id for (int w : G.adj(v)) if (!marked[w]) dfs(G, w); } 64
  • 65. Connected components application: study spread of STDs Relationship graph at "Jefferson High" Peter Bearman, James Moody, and Katherine Stovel. Chains of affection: The structure of adolescent romantic and sexual networks. American Journal of Sociology, 110(1): 44–99, 2004. 65
  • 66. Connected components application: particle detection Particle detection. Given grayscale image of particles, identify "blobs." ・Vertex: pixel. ・Edge: between two adjacent pixels with grayscale value ≥ 70. ・Blob: connected component of 20-30 pixels. black = 0 white = 255 Particle tracking. Track moving particles over time. 66
  • 67. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 68. 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges
  • 69. Graph-processing challenge 1 Problem. Is a graph bipartite? 0 0-1 0-2 0-5 1 2 6 0-6 1-3 How difficult? 3 4 2-3 ・Any programmer could do it. 5 2-4 4-5 ✓・Typical diligent algorithms student could do it. 4-6 ・Hire an expert. ・Intractable. simple DFS-based solution (see textbook) 0 0-1 ・No one knows. 0-2 0-5 ・Impossible. 1 2 6 0-6 1-3 3 4 2-3 2-4 5 4-5 4-6 { 0, 3, 4 } 69
  • 70. Bipartiteness application: is dating graph bipartite? Image created by Mark Newman. 70
  • 71. Graph-processing challenge 2 Problem. Find a cycle. 0 0-1 0-2 0-5 1 2 6 0-6 1-3 How difficult? 3 4 2-3 ・Any programmer could do it. 5 2-4 4-5 ✓・Typical diligent algorithms student could do it. 4-6 ・Hire an expert. 0-5-4-6-0 ・Intractable. simple DFS-based solution (see textbook) ・No one knows. ・Impossible. 71
  • 72. Bridges of Königsberg The Seven Bridges of Königsberg. [Leonhard Euler 1736] “ … in Königsberg in Prussia, there is an island A, called the Kneiphof; the river which surrounds it is divided into two branches ... and these branches are crossed by seven bridges. Concerning these bridges, it was asked whether anyone could arrange a route in such a way that he could cross each bridge once and only once. ” Euler tour. Is there a (general) cycle that uses each edge exactly once? Answer. Yes iff connected and all vertices have even degree. 72
  • 73. Graph-processing challenge 3 Problem. Find a (general) cycle that uses every edge exactly once. 0 0-1 0-2 0-5 1 2 6 0-6 1-2 How difficult? 3 4 2-3 ・Any programmer could do it. 5 2-4 3-4 ✓・Typical diligent algorithms student could do it. 4-5 ・Hire an expert. 0-1-2-3-4-2-0-6-4-5-0 4-6 ・Intractable. Eulerian tour (classic graph-processing problem) ・No one knows. ・Impossible. 73
  • 74. Graph-processing challenge 4 Problem. Find a cycle that visits every vertex exactly once. 0 0-1 0-2 0-5 1 2 6 0-6 1-2 How difficult? 3 4 2-6 ・Any programmer could do it. 5 3-4 3-5 ・Typical diligent algorithms student could do it. 4-5 ・Hire an expert. 0-5-3-4-6-2-1-0 4-6 ✓・Intractable. Hamiltonian cycle ・No one knows. (classical NP-complete problem) ・Impossible. 74
  • 75. Graph-processing challenge 5 Problem. Are two graphs identical except for vertex names? 0 0-1 0-2 0-5 1 2 6 0-6 3-4 How difficult? 3 4 3-5 ・Any programmer could do it. 5 4-5 4-6 ・Typical diligent algorithms student could do it. ・Hire an expert. ・Intractable. 3 0-4 ✓ No one knows. ・ 2 0-5 0-6 ・Impossible. graph isomorphism is 4 1 1-4 1-5 6 longstanding open problem 5 2-4 3-4 0 5-6 0↔4, 1↔3, 2↔2, 3↔6, 4↔5, 5↔0, 6↔1 75
  • 76. Graph-processing challenge 6 Problem. Lay out a graph in the plane without crossing edges? 1 0-1 0-2 2 0-5 0 0-6 6 3-4 How difficult? 3 4 3-5 ・Any programmer could do it. 4-5 4-6 ・Typical diligent algorithms student could do it. 5 ✓ ・Hire an expert. ・Intractable. linear-time DFS-based planarity algorithm 0 ・No one knows. discovered by Tarjan in 1970s ・Impossible. (too complicated for most practitioners) 1 2 6 3 4 5 76
  • 77. 4.1 U NDIRECTED G RAPHS ‣ graph API ‣ depth-first search ‣ breadth-first search Algorithms ‣ connected components R OBERT S EDGEWICK | K EVIN W AYNE ‣ challenges http://algs4.cs.princeton.edu
  • 78. Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 4.1 U NDIRECTED G RAPHS ‣ introduction ‣ graph API ‣ depth-first search Algorithms ‣ breadth-first search F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE ‣ connected components http://algs4.cs.princeton.edu ‣ challenges