07a-DepthAndBreathSearch

download 07a-DepthAndBreathSearch

of 35

Transcript of 07a-DepthAndBreathSearch

  • 8/13/2019 07a-DepthAndBreathSearch

    1/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page 1

    Searching

    through the

    Graphs

  • 8/13/2019 07a-DepthAndBreathSearch

    2/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page 2

    One limitation of the trees is that the trees are hierarchal in

    nature. They only represent relations of hierarchal types such as

    relations between parents and child

    A generalization of tree, called GRAP, is a data structure in

    which these limitations are lifted

    A graph is a collection of !ertices "nodes# and connectionbetween them called edges

    A simple graph G= (V,E)consists of a nonempty set of Vof

    !ertices and possibly empty setEof edges where each edge is aconnection between two !ertices

    The number of !ertices and edges of a graph are represented as |

    V|and |E|,respecti!ely.

  • 8/13/2019 07a-DepthAndBreathSearch

    3/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page 3

    Examples of Simple Graphs

    Graph 1 Graph 2

    Graph 3 Graph 4

  • 8/13/2019 07a-DepthAndBreathSearch

    4/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page 4

    iagraphs (irecte! Graphs)

    A directed graph "diagraph# G(V,E)consists of a non$empty set of V!ertices and a set of edges "arcs# where each

    edge is a pair of !ertices from V

    The difference between diagraph and simple graphs is that

    edge "vi,vj#% "vj,vi#in a simple graph but this is not the

    case in a diagraph

    ere is an e&ample of a diagraph

  • 8/13/2019 07a-DepthAndBreathSearch

    5/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page $

    %ulti&graphs

    'n a simple graph, two !ertices can only be (oined by one edge

    owe!er, in a multi$graphmore than one edge canconnect the same two!ertices together.

    A multi$graph that has anedge going from !erte& vito the same !erte& vi"loop edge# is called

    pseudo$graph

  • 8/13/2019 07a-DepthAndBreathSearch

    6/35

    Dr. Ahmad R. Hadaegh

    A.R. Hadaegh National University Page '

    ath A path from v1to vnis a se)uence of edges edge(v1,v2,

    edge(v2, v!, " (vn#1, vn

    *+cle 'f v1$ vnand no edge is repeated then it is called a cycle

    1 2

    3

    $

    4

    '

    1 2

    3

    $

    4

    '

    -n the follo.ing graph, the

    e!ges that connects v1to v2an!v2to v!an! v!to v%ma/es a

    path from v1to v%0

    he path going from v1

    to v2an! from v2to v!an! from v!to v1again

    ma/es a c+cle0

  • 8/13/2019 07a-DepthAndBreathSearch

    7/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page

    eighte! Graph A graph is called weighted graph if each edge has an

    assigned number

    *epending on the conte&t in which such a graphs areused, the number assigned to an edge is called itsweight, cost, distance, etc.

    *omplete Graph

    A graph with n!ertices is called+complete and is denoted as &niffor each pair of distinct !erticese&actly one edge connecting them.

    That is each !erte& can be connected to any other !erte& The number of edges in such a graph is-

    |V|

    |E| = = |V| 5 (|V|&1) 6 27 .hich is 8(|V|2)

    2

    /4

  • 8/13/2019 07a-DepthAndBreathSearch

    8/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 9

    Su:&graph A sub$graph 'of graph G=(V, E)is a graph G;=(V;, E;)

    such that Vis a subset of VandEis a subset ofE. A

    ub$graph induced by !ertices Vis a graph "V, Esuch

    that if an edge eis inE, theneis inE

    Two !ertices viand vjare called ad(acent if the edge (vi,vjis inE. uch an edge is called in)identwith the !ertices vi

    and vj

    egree The degree of a !erte& v, deg(vis the number of edges

    incident with v. 'f deg(v$*, then vis called isolated

    !erte&.

  • 8/13/2019 07a-DepthAndBreathSearch

    9/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page >

    Graph ?epresentation

    Graph can be represented"implemented# in se!eral ways.

    A simple representation is gi!en byad(acency list that specifies all!ertices ad(acent to each !erte& of agraph

    This is called+AR re-resentation

    :

    c

    !

    e

    f

    f

    a c !

    ! e

    f

    a f

    ea :

    : !

    !a c

    gc

    a

    f

    !

    :

    e

    g

  • 8/13/2019 07a-DepthAndBreathSearch

    10/35

  • 8/13/2019 07a-DepthAndBreathSearch

    11/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 11

    *omparison

    /hich representation is better0

    Thin1 about searching for an edge

    Thin1 about printing all edges

    Thin1 about inserting an edge

    ow about inserting a !erte&

    ow about deleting a !erte&

    ow about deleting an edge

  • 8/13/2019 07a-DepthAndBreathSearch

    12/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 12

    Graph raersal

    Two systematic method for tra!ersal of graph are depth$first

    tra!ersal and breadth$first tra!ersal

    Of course since graph may contain cycle, they ha!e different

    structure compare to trees

    Therefore, tra!ersal algorithms we used for trees cannot be applied

    for graphs e&actly the same way and they should be modified to fit

    the graph re)uirements

    2urther, not all graphs are connected. A graph may contain

    isolated trees, isolated graphs or isolated !ertices.

    'n a connected graph, we can find a path between any two !ertices

    in the graph but this is not the case for unconnected graphs

  • 8/13/2019 07a-DepthAndBreathSearch

    13/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 13

    epth&Airst raersal Bsing ?ecursion

    A !erte& vis !isited once then each ad(acent !erte& to vis!isited and so on

    'f !erte& vhas no ad(acent !erte& or all of its ad(acent !ertices

    ha!e already been !isited, we bac1trac1 to the predecessor of v

    The tra!ersal is finished if this !isiting and bac1trac1ing

    process leads to the first !erte& where the traversal started

    2inally, if there is still some un!isited !erte& in the graph, thetra!ersal continues for one of the un!isited !ertices

  • 8/13/2019 07a-DepthAndBreathSearch

    14/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 14

    AS()

    num() = iCC 66 ma/ing ertex isite!

    for all ertices u a!acent to

    if num(u) is @ 66 it means it is not isite! +etattach e!ge(u) to e!ges

    AS(u)D

    !epthAirstSearch()

    for all ertices

    num() = @D 66 initialiing all ertices to unisite!

    e!ges = nullD

    i = 1D

    .hile there is a ertex such that num() is @

    AS()D

    output e!gesD

  • 8/13/2019 07a-DepthAndBreathSearch

    15/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1$

    3ote that this algorithm guarantees generating a tree "or a forest"combination of se!eral smaller trees# which includes or spawnso!er all !ertices of the original graph

    The tree that meets these condition is called aspanning tree

    Also note that an edge is added to edges only if the condition in+i n/m(/ is * is true4 that is if !erte& /reachable from !erte& v

    has not been processed

    Therefore, certain edges in the original graph do not appear in theresulting tree

    The edges included in the tree are calledforward edges"or treeedges# and the ones not included are called back edges

    'n the ne&t e&ample, we show bac1 edges with dash lines andforward edges with straight line

  • 8/13/2019 07a-DepthAndBreathSearch

    16/35

  • 8/13/2019 07a-DepthAndBreathSearch

    17/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1

    c(2)

    a(1)

    i

    f

    :

    g

    e

    h

    !

    The ad(acent !ertices to aare ), , gandi

    Any of the ad(acent !ertices to a

    can be chosen randomly as long asit is not !isited already

    5ets pic1 ). 6ar1 )as !isited andadd edge a)to the edges

    The ad(acent !ertices to )are a, andi

    7erte& acannot be chosen because

    it is already !isited. 8ut !erte&ori can be chosen

    5ets pic1. 6ar1as !isited andadd edge ) to the edges

    c(2)

    a(1)

    i

    f(3)

    :

    g

    e

    h

    !

  • 8/13/2019 07a-DepthAndBreathSearch

    18/35

  • 8/13/2019 07a-DepthAndBreathSearch

    19/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 1>

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e

    h

    !

    The ad(acent !ertices togare aand0

    7erte& acannot be chosen

    because it is already !isited. othe only choice is !erte& 0

    6ar1 0as !isited and add edgeg0to the edges

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e()

    h

    !

    The ad(acent !ertices to 0isg,butgis already !isited

    /e bac1trac1 togand !erticesad(acent tog are all !isited.

    /e bac1trac1 to abut all ad(acent!ertices to aare also !isited

    ince ais where we started, wecannot bac1trac1 anymore, so we

    pic1 another un!isited !erte& if thereis still one

    5ets pic1 eand mar1 eas !isited.

  • 8/13/2019 07a-DepthAndBreathSearch

    20/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2@

    The only ad(acent !erte& to eis h

    6ar1 has !isited and addedge eh to the edges

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e()

    h(9)

    !

    The ad(acent !ertices to hare eandd

    7erte& ecannot be chosen

    because it is already !isited. othe only choice is !erte& d

    6ar1 das !isited and add edge hdto the edges

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e()

    h(9)

    !(>)

  • 8/13/2019 07a-DepthAndBreathSearch

    21/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 21

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e()

    h(9)

    !(>)

    The ad(acent !ertices to dis h,buth is already!isited

    /e bac1trac1 to handad(acent !ertices to h areall !isited.

    /e bac1trac1 to eandad(acent !ertices to eare

    also !isited

    ince eis where westarted, we cannot

    bac1trac1 anymore, so wepic1 another un!isited

    !erte& if there is still one

    8ut there is no more!erte& left to pic1.Therefore, we are done

    c(2)

    a(1)

    i(4)

    f(3)

    :(')

    g($)

    e()

    h(9)

    !(>)

    he final result is

  • 8/13/2019 07a-DepthAndBreathSearch

    22/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 22

  • 8/13/2019 07a-DepthAndBreathSearch

    23/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 23

    c(2)

    a(1)

    i

    f

    :

    g

    e

    h

    !

    The ad(acent !ertices to aare ),and

    Any of the ad(acent !ertices to acan be chosen randomly as longas it is not !isited already

    5ets pic1 ). 6ar1 )as !isitedand add edge a )to the edges

    c(2)

    a(1)

    i(3)

    f

    :

    g

    e

    h

    ! The only ad(acent !ertices to )is i

    6ar1 ias !isited and add edge) ito the edges

  • 8/13/2019 07a-DepthAndBreathSearch

    24/35

  • 8/13/2019 07a-DepthAndBreathSearch

    25/35Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2$

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e

    h

    ! The only ad(acent !erte& to 0

    isg

    /e mar1gas !isited and add0gto the edges

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e()

    h

    !

    The only ad(acent !erte& togis awhich is already !isited

    /e bac1trac1 to 0and the ad(acent!erte& to 0is also !isited

    ince we started with 0, we cannotbac1trac1 anymore. o we pic1another un!isited !erte& if theree&ist one

    5ets pic1 eand mar1 it as !isited

  • 8/13/2019 07a-DepthAndBreathSearch

    26/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2'

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e()

    h(9)

    ! The only ad(acent !erte& to e

    is h

    /e mar1 has !isited and addehto the edges

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e()

    h(9)

    !(>) The only ad(acent !erte& to h

    is d

    /e mar1 das !isited and addhdto the edges

  • 8/13/2019 07a-DepthAndBreathSearch

    27/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2

    There is no ad(acent!erte& to d

    /e bac1trac1 to hand the

    ad(acent !erte& to h isalready!isited.

    /e bac1trac1 to eand thead(acent !erte& to eis also!isited

    ince eis where westarted, we cannot

    bac1trac1 anymore, so wepic1 another un!isited

    !erte& if there is still one

    8ut there is no more!erte& left to pic1.Therefore, we are done

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e()

    h(9)

    !(>)

    c(2)

    a(1)

    i(3)

    f(4)

    :($)

    g(')

    e()

    h(9)

    !(>)

    he final result is

  • 8/13/2019 07a-DepthAndBreathSearch

    28/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 29

    *omplexit+ of !epth&first search

    The comple&ity of depth2irstearch"# is 8(|V| C |E|)because-

    'nitializing n/m(vfor each !erte& re)uires |V|steps

    D+(vis called deg(vtimes for each v4 that is, once for

    each edge of v"to spawn into more calls or to finish the chainof recursi!e calls#, hence, the total number of calls is 2|E|

    because each edge is ad(acent to two !ertices

    earching for !ertices as re)uired by the statement

    hile there is ertex vsuch that n/m(vis @

    9an be assumed to re)uire |V|steps in worse case.

  • 8/13/2019 07a-DepthAndBreathSearch

    29/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 2>

    Frea!th&Airst Search

    This type of tra!ersal uses )ueue instead of recursion or stac1

    :reathAirstSearch()

    for all ertices u

    num(u) =@D

    e!ges = nullD

    i = 1D.hile there is a ertex such that num() = @

    num() = iCCD

    enueue()D 66 pushing into ueue

    .hile ueue is not empt+

    = !eueue()D 66 popping from the ueue

    for all ertices u a!acent to

    if num(u) is @

    num(u) = iCCD

    enueue(u)D

    a!! e!ge(u) to e!gesD

    8utput e!gesD

  • 8/13/2019 07a-DepthAndBreathSearch

    30/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 3@

    Original Graph

    c

    a(1)

    i

    f

    :

    g

    e

    h

    ! /e pic1 up a !erte&

    randomly and mar1 it as

    !isited

    5ets pic1 !erte& aand

    mar1 it as !isited and

    place it in the )ueue

    Example of Frea!th&Airst Search

    a

    c

    a

    i

    f

    :

    g

    e

    h

    !

  • 8/13/2019 07a-DepthAndBreathSearch

    31/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 31

    c(2)

    a(1)

    i(3)

    f(4)

    :

    g($)

    e

    h

    !

    /e pop a from the )ueue.

    The ad(acent !ertices to aareg, , iand )that are not

    !isited

    /e mar1 them all one by oneand place them in the )ueue"order is not important#

    The related edges are added

    c

    i

    f

    g

    c(2)

    a(1)

    i(3)

    f(4)

    :

    g($)

    e

    h

    !

    /e pop )from the )ueue.

    All ad(acent !ertices to )are

    !isited already.

    o no !erte& goes into the)ueue

    i

    f

    g

  • 8/13/2019 07a-DepthAndBreathSearch

    32/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 32

    c(2)

    a(1)

    i(3)

    f(4)

    :

    g($)

    e

    h

    ! /e pop ifrom the )ueue.

    All ad(acent !ertices to iare !isited already.

    f

    g

    c(2)

    a(1)

    i(3)

    f(4)

    :

    g($)

    e

    h

    ! /e pop from the )ueue.

    All ad(acent !ertices toare !isited already.

    g

  • 8/13/2019 07a-DepthAndBreathSearch

    33/35

  • 8/13/2019 07a-DepthAndBreathSearch

    34/35

    Dr. Ahmad R. HadaeghA.R. Hadaegh National University Page 34

    c(2)

    a(1)

    i(3)

    f(4)

    :(')

    g($)

    e(9)

    h()

    !(>) /e pop hfrom the )ueue

    Ad(acent !ertices to hareeand d

    /e mar1 them and placethem in the )ueue

    /e also add the relatededges

    !

    e

    c(2)

    a(1)

    i(3)

    f(4)

    :(')

    g($)

    e(9)

    h()

    !(>) /e pop e

    Ad(acent !ertices to eareall !isited already

    !

  • 8/13/2019 07a-DepthAndBreathSearch

    35/35

    c(2)

    a(1)

    i(3)

    f(4)

    :(')

    g($)

    e(9)

    h()

    !(>) /e pop d

    Ad(acent !ertices to dareall !isited already

    3othing more is left topop.

    3o more un!isited !erte&e&ist

    Therefore, we are done

    he final result is

    c(2)

    a(1)

    i(3)

    f(4)

    :(')

    g($)

    e(9)

    h()

    !(>)