ALgorithm Full First Ctvcxvcxv Syllabus

download ALgorithm Full First Ctvcxvcxv Syllabus

of 106

Transcript of ALgorithm Full First Ctvcxvcxv Syllabus

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    1/106

    GRAPHALGORITHMS

    Dr. Tanzima Hashem

    Assistant Professor

    CSE, BUET

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    2/106

    GRAPHS

    A graph G = (V, E)

    V = set of vertices

    E = set of edges = subset of V V

    Thus |E| = O(|V|2)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    3/106

    GRAPHVARIATIONS

    Variations:

    A connected graphhas a path from every vertex to

    every other

    In an undirected graph:Edge (u,v) = edge (v,u)

    No self-loops

    In a directedgraph:

    Edge (u,v) goes from vertex u to vertex v,notated uv

    Self loops are allowed.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    4/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    5/106

    GRAPHS

    We will typically express running times in terms of |E|

    and |V| (often dropping the |s)

    If |E| |V|2the graph is dense

    If |E| |V| the graph is sparse If you know you are dealing with dense or sparse

    graphs, different data structures may make sense

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    6/106

    REPRESENTINGGRAPHS

    Assume V = {1, 2, , n}

    An adjacency matrixrepresents the graph as a n x n

    matrix A:

    A[i,j] = 1 if edge (i,j) E (or weight of edge)= 0 if edge (i,j) E

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    7/106

    GRAPHS: ADJACENCYMATRIX

    Example:

    1

    2 4

    3

    a

    d

    b c

    A 1 2 3 4

    1

    2

    3??

    4

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    8/106

    GRAPHS: ADJACENCYMATRIX

    Example:

    1

    2 4

    3

    a

    d

    b c

    A 1 2 3 4

    1 0 1 1 0

    2 0 0 1 0

    3 0 0 0 0

    4 0 0 1 0

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    9/106

    GRAPHS: ADJACENCYMATRIX

    Space:(V2).

    Not memory efficient for large graphs.

    Time:to list all vertices adjacent to u: (V).

    Time:to determine if (u, v)E: (1).

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    10/106

    GRAPHS: ADJACENCYMATRIX

    The adjacency matrix is a dense representation

    Usually too much storage for large graphs

    But can be very efficient for small graphs

    Most large interesting graphs are sparse E.g., planar graphs, in which no edges cross, have

    |E| = O(|V|) by Eulers formula

    For this reason the adjacency listis often a more

    appropriate respresentation

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    11/106

    GRAPHS: ADJACENCYLIST

    Adjacency list: for each vertex v V, store a list of

    vertices adjacent to v

    Example:

    Adj[1] = {2,3} Adj[2] = {3}

    Adj[3] = {}

    Adj[4] = {3}

    Variation: can also keepa list of edges coming into vertex

    1

    2 4

    3

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    12/106

    GRAPHS: ADJACENCYLIST

    For directed graphs:

    Sum of lengths of all adj. lists is

    out-degree(v) = |E|

    v

    V

    Total storage:(V+E)

    For undirected graphs:

    Sum of lengths of all adj. lists is

    degree(v) = 2|E|vV

    Total storage:(V+E)

    No. of edges leaving v

    No. of edges incident on v. Edge (u,v) is

    incident on vertices uand v.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    13/106

    GRAPHDEFINITIONS

    Path

    Sequence of nodes n1, n2, nk

    Edge exists between each pair of nodes ni ,ni+1

    ExampleA, B, Cis a path

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    14/106

    GRAPHDEFINITIONS

    Path

    Sequence of nodes n1, n2, nk

    Edge exists between each pair of nodes ni ,ni+1

    ExampleA, B, Cis a path

    A, E, Dis not a path

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    15/106

    GRAPHDEFINITIONS

    Cycle

    Path that ends back at starting node

    Example

    A, E, A

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    16/106

    GRAPHDEFINITIONS

    Cycle

    Path that ends back at starting node

    Example

    A, E, AA, B, C, D, E, A

    Simple path

    No cycles in path

    Acyclic graph

    No cycles in graph

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    17/106

    GRAPHSEARCHING

    Given: a graph G = (V, E), directed or undirected

    Goal: methodically explore every vertex and every

    edge

    Ultimately: build a tree on the graph Pick a vertex as the root

    Choose certain edges to produce a tree

    Note: might also build a forestif graph is not

    connected

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    18/106

    BREADTH-FIRSTSEARCH

    Explore a graph, turning it into a tree

    One vertex at a time

    Expand frontier of explored vertices across the

    breadthof the frontier Builds a tree over the graph

    Pick a source vertexto be the root

    Find (discover) its children, then their children,

    etc.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    19/106

    BREADTH-FIRSTSEARCH

    Input:Graph G = (V, E), either directed orundirected, and source vertex s V.

    Output:

    d[v]=

    distance (smallest # of edges, or shortestpath) from s to v, for all v V. d[v] = if vis notreachable from s.

    [v] = u such that (u, v)is last edge on shortestpath s v.

    uis vspredecessor.

    Builds breadth-first tree with root sthat contains allreachable vertices.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    20/106

    BREADTH-FIRSTSEARCH

    Associate vertex colors to guide the algorithm

    White vertices have not been discovered

    All vertices start out white

    Grey vertices are discovered but not fully exploredThey may be adjacent to white vertices

    Black vertices are discovered and fully explored

    They are adjacent only to black and gray

    vertices Explore vertices by scanning adjacency list of grey

    vertices

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    21/106

    BFS(G,s)

    1. foreach vertex u in V[G] {s}

    2 color[u] white

    3 d[u]

    4 [u] nil5 color[s] gray

    6 d[s] 0

    7 [s] nil

    8 Q

    9 enqueue(Q,s)10whileQ

    11 u dequeue(Q)

    12 foreach vin Adj[u]

    13 ifcolor[v] = white

    14 color[v] gray15 d[v] d[u] + 1

    16 [v] u

    17 enqueue(Q,v)

    18 color[u] black

    white: undiscoveredgray: discovered

    black: finished

    Q: a queue of discovered

    vertices

    color[v]: color of vd[v]: distance from s to v

    [u]: predecessor of v

    initialization

    access source s

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    22/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    r s t u

    v w x y

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    23/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    0

    r s t u

    v w x y

    sQ:

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    24/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    0

    1

    r s t u

    v w x y

    wQ: r

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    25/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    0

    1

    2

    2

    r s t u

    v w x y

    rQ: t x

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    26/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    2

    0

    1

    2

    2

    r s t u

    v w x y

    Q: t x v

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    27/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    2

    0

    1

    2

    2

    3

    r s t u

    v w x y

    Q: x v u

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    28/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q: v u y

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    29/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q: u y

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    30/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    31/106

    BREADTH-FIRSTSEARCH: EXAMPLE

    1

    2

    0

    1

    2

    2

    3

    3

    r s t u

    v w x y

    Q:

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    32/106

    ANALYSISOFBFS

    Initialization takes O(|V|).

    Traversal Loop After initialization, each vertex is enqueued and

    dequeued at most once, and each operation takesO(1). So, total time for queuing is O(|V|).

    The adjacency list of each vertex is scanned at mostonce. The total time spent in scanning adjacency lists

    is O(|E|).

    Summing up over all vertices => total running time ofBFS isO(|V| + |E|)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    33/106

    BREADTH-FIRSTTREE

    For a graph G= (V, E) with source s, thepredecessor subgraphof Gis G

    = (V

    , E) where

    V={vV : [v] nil} {s}

    E={([v], v) E : v V - {s}}

    The predecessor subgraph Gis a breadth-first treeif:

    V consists of the vertices reachable from sand

    for all v V, there is a unique simple path from s to v

    in G that is also a shortest path from sto vin G.

    The edges in Eare called tree edges.

    |E| = |V

    | - 1.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    34/106

    DEPTH-FIRSTSEARCH(DFS)

    Explore edges out of the most recently discovered

    vertex v.

    When all edges of vhave been explored, backtrack

    to explore other edges leaving the vertex from which

    vwas discovered (itspredecessor).

    Search as deep as possible first.

    Continue until all vertices reachable from the original

    source are discovered.

    If any undiscovered vertices remain, then one of

    them is chosen as a new source and search is

    repeated from that source.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    35/106

    DEPTH-FIRSTSEARCH

    Input:G = (V,E), directed or undirected. No

    source vertex given!

    Output:

    2 timestampson each vertex. d[v] = discovery t ime (v turns from white to gray)

    f [v] = f in ish ing t ime(vturns from gray to black)

    [v] : predecessor of v = u, such that vwas

    discovered during the scan of us adjacency list. Depth-first forest

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    36/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    37/106

    DFS(G)

    1. foreach vertex u V[G]

    2. docolor[u] white

    3. [u] NIL

    4. time0

    5. foreach vertex u V[G]

    6. doifcolor[u] = white

    7. thenDFS-Visit(u)

    Uses a global timestamp t ime.

    DFS-Visit(u)

    1. color [u] GRAY // White vertex u

    has been discovered

    2. timetime+ 1

    3. d[u] time

    4. foreach v Adj[u]

    5. doifcolor[v] = WHITE

    6. then[v] u

    7. DFS-Visit(v)8. color[u] BLACK // Blacken u;

    it is finished.

    9. f[u] time time + 1

    PSEUDOCODE

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    38/106

    DFS EXAMPLE

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    39/106

    DFS EXAMPLE

    1 | | |

    |||

    | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    40/106

    DFS EXAMPLE

    1 | | |

    |||

    2 | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    41/106

    DFS EXAMPLE

    1 | | |

    ||3 |

    2 | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    42/106

    DFS EXAMPLE

    1 | | |

    ||3 | 4

    2 | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    43/106

    DFS EXAMPLE

    1 | | |

    |5 |3 | 4

    2 | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    44/106

    DFS EXAMPLE

    1 | | |

    |5 | 63 | 4

    2 | |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    45/106

    DFS EXAMPLE

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    46/106

    DFS EXAMPLE

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    47/106

    DFS EXAMPLE

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 9 |

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    48/106

    DFS EXAMPLE

    1 | 8 | |

    |5 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    49/106

    DFS EXAMPLE

    1 | 8 |11 |

    |5 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    50/106

    DFS EXAMPLE

    1 |12 8 |11 |

    |5 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    51/106

    DFS EXAMPLE

    1 |12 8 |11 13|

    |5 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    52/106

    DFS EXAMPLE

    1 |12 8 |11 13|

    14|5 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    53/106

    DFS EXAMPLE

    1 |12 8 |11 13|

    14|155 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    54/106

    DFS EXAMPLE

    1 |12 8 |11 13|16

    14|155 | 63 | 4

    2 | 7 9 |10

    d f

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    55/106

    ANALYSISOFDFS

    Loops on lines 1-3 & 5-7 take (V)time, excluding

    time to execute DFS-Visit.

    DFS-Visit is called once for each white vertex vV

    when its painted gray the first time. Lines 4-7 ofDFS-Visit is executed |Adj[v]| times. The total cost of

    executing DFS-Visit is vV|Adj[v]| = (E)

    Total running time of DFS is(|V| + |E|).

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    56/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    57/106

    TIME-STAMPSTRUCTUREINDFS

    There is also a nice structure to the time stamps,

    which is referred to as Parenthesis Structure.

    Theorem 22.7

    For all u, v, exactly one of the following holds:1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and

    neither u nor v is a descendant of the other.

    2. d[u] < d[v] < f [v] < f [u] and v is a descendant of u.

    3. d[v] < d[u] < f [u] < f [v] and u is a descendant of v.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    58/106

    TIME-STAMPSTRUCTUREINDFS

    So d[u] < d[v] < f [u] < f [v] cannot happen.

    Like parentheses:

    OK: ( ) [ ] ( [ ] ) [ ( ) ]

    Not OK: ( [ ) ] [ ( ] )

    Corol lary

    v is a proper descendant of u if and only if

    d[u] < d[v]< f [v] < f [u].

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    59/106

    DFS: KINDSOFEDGES

    Consider a directed graph G = (V, E). After a DFS of

    graph G we can put each edge into one of four

    classes:

    A tree edgeis an edge in a DFS-tree.

    A back edgeconnects a vertex to an ancestor in a

    DFS-tree. Note that a self-loop is a back edge.

    A forward edgeis a non-tree edge that connects

    a vertex to a descendent in a DFS-tree.

    A cross edgeis any other edge in graph G. It

    connects vertices in two different DFS-tree or two

    vertices in the same DFS-tree neither of which is

    the ancestor of the other.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    60/106

    EXAMPLEOFCLASSIFYINGEDGES

    a

    d

    b e

    fc

    in DFS forest

    not in DFS

    forest

    tree

    tree

    tree

    treeforward

    back

    back

    cross

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    61/106

    61

    CLASSIFYING EDGESOFADIGRAPH

    (u, v) is:

    Tree edgeif v is white

    Back edgeif v is gray

    Forward or cross - if v is black (u, v) is:

    Forward edgeif v is black and d[u] < d[v] (v was

    discovered after u)

    Cross edgeif v is black and d[u] > d[v] (udiscovered after v)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    62/106

    DFS: KINDSOFEDGES

    DFS-Visit(u) with edge classification. G must be adirected graph

    1. color[u] GRAY2. time time + 13. d[u] time4. foreach vertex vadjacent to u

    5. do ifcolor[v] BLACK6. then ifd[u] < d[v]7. thenClassify (u, v) as a forward edge8. elseClassify (u, v) as a cross edge9. ifcolor[v] GRAY10. thenClassify (u, v) as a back edge11. ifcolor[v] WHITE12. then[v] u13. Classify (u, v) as a tree edge14. DFS-Visit(v)15. color[u] BLACK16. time time + 117. f[u] time

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    63/106

    DFS: KINDSOFEDGES

    Suppose G be an undirected graph, then we havefollowing edge classification:

    Tree Edge an edge connects a vertex with its

    parent.

    Back Edge a non-tree edge connects a vertex

    with an ancestor.

    Forward Edge There is no forward edges

    because they become back edges when

    considered in the opposite direction.

    Cross Edge There cannot be any cross edge

    because every edge of G must connect an

    ancestor with a descendant.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    64/106

    64

    SOMEAPPLICATIONSOFBFSANDDFS

    BFS

    To find the shortest path from a vertex s to a vertex

    v in an unweighted graph

    To find the length of such a path

    Find the bipartiteness of a graph.

    DFS

    To find a path from a vertex s to a vertex v. To find the length of such a path.

    To find out if a graph contains cycles

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    65/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    66/106

    APPLICATIONOFBFS: BIPARTITEGRAPH

    non bipartite

    bipartite:

    Question: given a graph G, how to test if the graph is

    bipartite?

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    67/106

    APPLICATIONOFBFS: BIPARTITEGRAPH

    Foreach vertex uin V[G] {s}docolor[u] WHITE

    d[u] partition[u] 0

    color[s] GRAYpartition[s] 1d[s] 0Q [s]whileQueue 'Q' is non-empty

    dou head [Q]foreach vin Adj[u] do

    ifpartition [u] = partition [v] then

    return 0elseifcolor[v] WHITE then

    color[v] gray

    d[v] = d[u] + 1partition[v] 3 partition[u]ENQUEUE (Q, v)

    DEQUEUE (Q)Color[u] BLACK

    Return 1

    A DFS

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    68/106

    APPLICATIONOFDFS:

    DETECTINGCYCLEFORDIRECTEDGRAPH

    DFS_visit(u)

    color(u) GRAY

    d[u] time time + 1

    foreach vadjacent to u do

    ifcolor[v] GRAY then

    return "cycle exists"

    else ifcolor[v] WHITE then

    predecessor[v] u

    DFS_visit(v)

    color[u] BLACK

    f[u] time time + 1

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    69/106

    APPLICATIONOFDFS:

    DETECTINGCYCLEFORUNDIRECTEDGRAPH

    DFS_visit(u)

    color(u) GRAY

    d[u] time time + 1

    foreach vadjacent to u do

    ifcolor[v] GRAY and [u] v then

    return "cycle exists"

    else ifcolor[v] WHITE then

    predecessor[v] u

    DFS_visit(v)

    color[u] BLACK

    f[u] time time + 1

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    70/106

    TOPOLOGICALSORT

    Want to sort a directed acyclic graph (DAG).B

    E

    D

    C

    A

    C EDA B

    Think of original DAG as a partial order.

    Want a total orderthat extends this partial order.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    71/106

    TOPOLOGICALSORT

    Performed on a DAG. Linear ordering of the vertices of Gsuch that if (u, v) E,

    then uappears somewhere before v.

    Topological-Sort (G)

    1. call DFS(G)to compute finishing times f [v] for all v V

    2. as each vertex is finished, insert it onto the front of a linked list

    3. returnthe linked list of vertices

    Time:(V + E).

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    72/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    73/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    74/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/

    2/3

    E

    2/3

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    75/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    76/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

    5/

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    77/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

    5/

    6/

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    78/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

    5/

    6/7

    6/7

    C

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    79/106

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    80/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

    5/8

    6/7

    6/7

    C

    5/8

    B

    9/

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    81/106

    EXAMPLE

    Linked List:

    A B D

    C E

    1/4

    2/3

    E

    2/31/4

    D

    5/8

    6/7

    6/7

    C

    5/8

    B

    9/10

    9/10

    A

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    82/106

    PRECEDENCEEXAMPLE

    Tasks that have to be done to eat breakfast:

    get glass, pour juice, get bowl, pour cereal, pour

    milk, get spoon, eat.

    Certain events must happen in a certain order (ex:

    get bowl before pouring milk)

    For other events, it doesn't matter (ex: get bowl and

    get spoon)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    83/106

    PRECEDENCEEXAMPLE

    get glass

    pour juice

    get bowl

    pour cereal

    pour milkget spoon

    eat breakfast

    Order: glass, juice, bowl, cereal, milk, spoon, eat.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    84/106

    PRECEDENCEEXAMPLE

    Topological Sort

    eat

    juice

    glass

    milk

    cereal

    bowl

    spoon

    consider reverse order of finishing times:

    spoon, bowl, cereal, milk, glass, juice, eat

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

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    85/106

    PRECEDENCEEXAMPLE

    What if we started withjuice?

    eat

    juice

    glass milk

    cereal

    bowl

    spoon

    consider reverse order of finishing times:

    spoon, bowl, cereal, milk, glass, juice, eat

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

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    86/106

    WHYACYCLIC?

    Why must directed graph by acyclic for thetopological sort problem?

    Otherwise, no way to order events linearly without

    violating a precedence constraint.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    87/106

    CORRECTNESSPROOF

    Show if (u, v)E, then f [v] < f [u].

    When we explore (u, v), what are their colors? Note, u is graywe are exploring it Is v gray?

    No, because then v would be an ancestor of u.(u, v)is a back edge.a cycle (dag has no back edges).

    Is v white?Then vbecomes descendant of u.By parenthesis theorem, d[u] < d[v] < f [v] < f [u].

    Is v black?Then v is already finished.Since were exploring (u, v), we have not yet finished

    u.Therefore, f [v] < f [u].

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    88/106

    STRONGLYCONNECTEDCOMPONENTS

    Gis strongly connected if every pair (u, v) of verticesin G is reachable from one another.

    A strongly connected component(SCC) of G is a

    maximal set of vertices C V such that for all u, v

    C, both u v and v uexist.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    89/106

    COMPONENTGRAPH

    GSCC= (VSCC, ESCC).

    VSCChas one vertex for each SCC in G.

    ESCChas an edge if theres an edge between the

    corresponding SCCs in G.

    GSCC for the example considered:

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    90/106

    GSCC ISADAG

    Proof:

    Suppose there is a path v v in G.

    Then there are paths u u vand v v u in G.

    Therefore, u and vare reachable from each other, so theyare not in separate SCCs.

    Lemma 22.13

    Let C and C be distinct SCCs in G, let u, vC, u, vC, and suppose there isa path u uin G. Then there cannot also be a path v v in G.

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    91/106

    TRANSPOSEOFADIRECTEDGRAPH

    GT= transposeof directed G.

    GT= (V, ET), ET= {(u, v): (v, u)E}.

    GTis G with all edges reversed.

    Can create GTin (V + E)time if using adjacency

    lists.

    G and GThave the same SCCs. (u and v are

    reachable from each other in G if and only if

    reachable from each other in GT.)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    92/106

    SCC EXAMPLE

    h f a e

    g c b d

    four SCCs

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    93/106

    HOWCANDFS HELP?

    Suppose we run DFS on the directed graph.

    All vertices in the same SCC are in the same DFS

    tree.

    But there might be several different SCCs in the

    same DFS tree.

    Example: start DFS from vertex h in previous

    graph

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    94/106

    MAINIDEAOFSCC ALGORITHM

    DFS tells us which vertices are reachable from theroots of the individual trees

    Also need information in the "other direction": is the

    root reachable from its descendants?

    Run DFS again on the "transpose" graph (reverse thedirections of the edges)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    95/106

    ALGORITHMTODETERMINESCCS

    SCC(G)

    1. call DFS(G)to compute finishing times f [u] for all u

    2. compute GT

    3. call DFS(GT), but in the main loop, consider vertices in

    order of decreasing f [u] (as computed in first DFS)

    4. output the vertices in each tree of the depth-first forest

    formed in second DFS as a separate SCC

    Time:(V + E).

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    96/106

    EXAMPLEa b c d

    e f g h

    1/

    2/3/ 4 65/7

    8/11/

    12/

    13/ 91014

    15

    16

    f

    4

    h

    6

    g

    7

    d

    9

    c

    10

    a

    14

    e

    15

    b

    16

    DFS on the initial graph G

    DFS on GT:

    start at b: visit a, e

    start at c: visit d

    start at g: visit fstart at h

    Strongly connected components: C1= {a, b, e}, C2= {c, d}, C3= {f, g}, C4= {h}

    a b c d

    e f g h

    COMPONENT GRAPH

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    97/106

    COMPONENTGRAPH

    The component graphGSCC= (VSCC, ESCC):

    VSCC= {v1, v2, , vk}, where vicorresponds to eachstrongly connected component Ci

    There is an edge (vi, vj) ESCCif G contains a directed

    edge (x, y) for some x Ciandy Cj

    The component graph is a DAG

    a b c d

    e f g h

    a b e

    c d

    f g h

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    98/106

    NOTATIONS

    Extend notation for d(starting time) and f(finishing time) to sets of vertices U V:

    d(U) = minuU{ d[u]} (earliest discovery time)

    f(U) = maxuU{ f[u]} (latest finishing time)

    a b c d

    e f g h

    1/

    2/3/ 4 65/7

    8/11/

    12/

    13/ 91014

    15

    16

    C1 C2

    C3 C4

    d(C1)f(C1)

    d(C2)f(C2)

    d(C3)f(C3)

    d(C4)f(C4)

    =11

    =16

    =1

    =10

    =2

    =7

    =5

    =6

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    99/106

    SCCSANDDFS FINISHINGTIMES

    Proof:

    Case 1: d(C)< d(C) Letx be the first vertex discovered in C.

    At time d[x], all vertices in C and Carewhite. Thus, there exist paths of whitevertices fromx to all vertices in C andC.

    By the white-path theorem, all verticesin C and Care descendants ofx indepth-first tree.

    By the parenthesis theorem, f [x] = f (C)> f(C).

    Lemma 22.14

    Let C and Cbe distinct SCCs in G = (V, E). Suppose there is an edge (u, v)E

    such that u C and v C. Then f (C)> f (C).

    C C

    u v

    x

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    100/106

    SCCSANDDFS FINISHINGTIMES

    Proof: Case 2: d(C)> d(C)

    Let y be the first vertex discovered in C. At time d[y], all vertices in Care white

    and there is a white path from y to eachvertex in Call vertices in Cbecomedescendants of y. Again, f [y] = f (C).

    At time d[y], all vertices in C are alsowhite.

    By earlier lemma, since there is an edge(u, v), we cannot have a path from Cto C.

    So no vertex in C is reachable from y. Therefore, at time f [y], all vertices in C

    are still white. Therefore, for all w C, f [w] > f [y], which

    implies that f (C)> f (C).

    Lemma 22.14

    Let C and Cbe distinct SCCs in G = (V, E). Suppose there is an edge (u, v)E

    such that u C and v C. Then f (C)> f (C).

    C C

    u v

    yx

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    101/106

    SCCSANDDFS FINISHINGTIMES

    Proof:

    (u, v)ET(v, u)E.

    Since SCCs of G and GTare the same, f(C)> f (C), by

    Lemma 22.14.

    Corollary 22.15

    Let C and Cbe distinct SCCs in G = (V, E). Suppose there is an edge

    (u, v)ET, where u C and v C. Then f(C)< f(C).

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    102/106

    CORRECTNESSOFSCC

    When we do the second DFS, on G

    T

    , we start with a component Csuch that f(C) is maximum (b, in our case)

    We start from band visit all vertices in C1

    From corollary: f(C) > f(C) in G for all C Cthere are no edges

    from C to any other SCCs in GT

    DFS will visit only vertices in C1

    The depth-first tree rooted at bcontains exactly the vertices of C1

    C1 C2

    C3 C4

    a b c d

    e f g h

    f4

    h6

    g7

    d9

    c10

    a14

    e15

    b16

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    103/106

    CORRECTNESSOFSCC

    The next root chosen in the second DFS is in SCC C2such that f(C)is maximum over all SCCs other than C1

    DFS visits all vertices in C2

    the only edges out of C2go to C1, which weve already visited

    The only tree edges will be to vertices in C2

    Each time we choose a new root it can reach only: vertices in its own component

    vertices in components already visited

    C1 C2

    C3 C4

    a b c d

    e f g h

    f

    4

    h

    6

    g

    7

    d

    9

    c

    10

    a

    14

    e

    15

    b

    16

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    104/106

    RUNNINGTIMEOFSCC ALGORITHM

    Step 1: O(V+E) to run DFS Step 2: O(V+E) to construct transpose graph,

    assuming adjacency list rep.

    Step 3: O(V+E) to run DFS again

    Step 4: O(V) to output result

    Total: O(V+E)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    105/106

    ACKNOWLEDGEMENTS

    These slides contain material developed andcopyright by:

    Roger Crawfis(Ohio State university) (Course#

    CSE680, 2010)

    David Luebke(Virginia University) (Course#CS332, 2009)

    Jennifer Welch (Texas A&M University)

    (Course#CSCE411, 2012)

  • 8/12/2019 ALgorithm Full First Ctvcxvcxv Syllabus

    106/106

    THEEND