Data Structures And Algorithm Unit 5

download Data Structures And Algorithm Unit 5

of 57

Transcript of Data Structures And Algorithm Unit 5

  • 7/30/2019 Data Structures And Algorithm Unit 5

    1/57

    Graphs

  • 7/30/2019 Data Structures And Algorithm Unit 5

    2/57

    What is a Graph?

    A graph G = (V,E) is composed of:

    V: set ofvertices

    E: set ofedges connecting the vertices in V

    An edge e = (u,v) is a pair ofvertices

    Example:

    a b

    c

    d e

    V= {a,b,c,d,e}

    E= {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),

    (d,e)}

  • 7/30/2019 Data Structures And Algorithm Unit 5

    3/57

    Applications

    electronic circuits

    networks (roads, flights,communications)

    CS16

    LAX

    JFK

    LAX

    DFW

    STLHNL

    FTL

  • 7/30/2019 Data Structures And Algorithm Unit 5

    4/57

    Terminology

    Adjacent and Incident If (v0, v1) is an edge in an undirected graph,

    v0 and v1 are adjacent (w.r.t.vertices)

    The edge (v0, v1) is incident on vertices v0

    and v1 (w.r.t.edges)

    If is an edge in a directed graph

    v0 is adjacent to v1, and v1 is adjacent from v0

    The edge is incident on v0 and v1

  • 7/30/2019 Data Structures And Algorithm Unit 5

    5/57

    The degree of a vertex is the number of edges incident to thatvertex

    For directed graph,

    the in-degree of a vertex vis the number of edges that have vasthe head

    the out-degree of a vertex vis the number of edgesthat have vas the tail

    ifdiis the degree of a vertex iin a graph G with n vertices and eedges, the number of edges is

    Degree of a Vertex

    e di

    n

    ( ) /0

    1

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    6/57

    0

    1 2

    3 4 5 6

    G1 G2

    3 2

    3 3

    1 1 1 1

    directed graph

    in-degree

    out-degree

    0

    1

    2

    G3

    in:1, out: 1

    in: 1, out: 2

    in: 1, out: 0

    0

    1 2

    3

    33

    3

    Examples

  • 7/30/2019 Data Structures And Algorithm Unit 5

    7/57

    7

    Path

    path: sequence of

    vertices v1,v2,. . .vk

    such that

    consecutive verticesvi and vi+1 are

    adjacent.

    3

    3 3

    3

    2

    a b

    c

    d e

    a b

    c

    d e

    a b e d c b e d c

  • 7/30/2019 Data Structures And Algorithm Unit 5

    8/57

    More Terminology

    simple path: no repeated vertices

    cycle: simple path, except that the last vertex is the

    same as the first vertex

    a b

    c

    d e

    b e c

  • 7/30/2019 Data Structures And Algorithm Unit 5

    9/57

    Even More Terminology

    subgraph: subset of vertices and edges forming a graph

    connected component: maximal connected subgraph. E.g.,the graph below has 3 connected components.

    connected not connected

    connected graph: any two vertices are connected by some path

  • 7/30/2019 Data Structures And Algorithm Unit 5

    10/57

    0 0

    1 2 3

    1 2 0

    1 2

    3(i) (ii) (iii) (iv)(a) Some of the subgraph of G1

    0 0

    1

    0

    1

    2

    0

    1

    2

    (i) (ii) (iii) (iv)

    (b) Some of the subgraph of G3

    0

    1 2

    3

    G1

    0

    1

    2

    G3

    Subgraphs Examples

  • 7/30/2019 Data Structures And Algorithm Unit 5

    11/57

    Connectivity

    Let n= #vertices, and m = #edges

    A complete graph: one in which all pairs of vertices are adjacent

    How many total edges in a complete graph?

    Each of the n vertices is incident to n-1 edges, however, we

    would have counted each edge twice! Therefore, intuitively, m= n(n -1)/2.

    Therefore, if a graph is not complete, m < n(n -1)/2

    n5m (5

  • 7/30/2019 Data Structures And Algorithm Unit 5

    12/57

    More Connectivity

    n = #vertices

    m = #edges

    For a tree m = n - 1

    n 5

    m 4

    n 5m 3

    Ifm < n - 1, G is

    not connected

  • 7/30/2019 Data Structures And Algorithm Unit 5

    13/57

    Directed vs. Undirected Graph

    An undirected graph is one in which the

    pair of vertices in a edge is unordered, (v0,

    v1) = (v1,v0)

    A directed graph is one in which each

    edge is a directed pair of vertices, != tail head

  • 7/30/2019 Data Structures And Algorithm Unit 5

    14/57

    Graph Representations

    Adjacency Matrix

    Adjacency Lists

  • 7/30/2019 Data Structures And Algorithm Unit 5

    15/57

    Adjacency Matrix

    Let G=(V,E) be a graph with n vertices.

    The adjacency matrix of G is a two-dimensional

    n by n array, say adj_mat

    If the edge (vi, vj) is in E(G), adj_mat[i][j]=1

    If there is no such edge in E(G), adj_mat[i][j]=0

    The adjacency matrix for an undirected graph is

    symmetric; the adjacency matrix for a digraphneed not be symmetric

  • 7/30/2019 Data Structures And Algorithm Unit 5

    16/57

    Examples for Adjacency Matrix

    0

    1

    1

    1

    1

    0

    1

    1

    1

    1

    0

    1

    1

    1

    1

    0

    0

    1

    0

    1

    0

    0

    0

    1

    0

    0

    1

    1

    0

    0

    0

    0

    0

    1

    0

    0

    1

    0

    0

    0

    0

    1

    0

    0

    1

    0

    0

    0

    0

    0

    1

    1

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    1

    0

    0

    0

    0

    0

    0

    1

    0

    G1G2

    G4

    0

    1 23

    0

    1

    2

    1

    0

    2

    3

    4

    5

    6

    7

    symmetric

    undirected: n2/2

    directed: n2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    17/57

    Merits of Adjacency Matrix

    From the adjacency matrix, to determine

    the connection of vertices is easy

    The degree of a vertex is For a digraph (= directed graph), the row

    sum is the out_degree, while the column

    sum is the in_degree

    adj mat i jj

    n

    _ [ ][ ]

    01

    ind vi A j ij

    n

    ( ) [ , ]

    0

    1 outd vi A i jj

    n

    ( ) [ , ]

    0

    1

  • 7/30/2019 Data Structures And Algorithm Unit 5

    18/57

    Adjacency Lists (data structure

    #define MAX_VERTICES 50

    typedef struct node *node_pointer;

    typedef struct node {

    int vertex;

    struct node *link;

    };node_pointer graph[MAX_VERTICES];

    Each row in adjacency matrix is represented as an adjacency list.

    0 4

  • 7/30/2019 Data Structures And Algorithm Unit 5

    19/57

    0

    1

    2

    3

    01

    2

    0

    1

    23

    4

    5

    67

    1 2 3

    0 2 3

    0 1 30 1 2

    G1

    10 2

    G3

    1 2

    0 3

    0 31 2

    5

    4 6

    5 7

    6

    G4

    0

    1 2

    3

    0

    1

    2

    1

    0

    2

    3

    4

    5

    6

    7

    An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

  • 7/30/2019 Data Structures And Algorithm Unit 5

    20/57

    Graph Traversal

    Problem: Search for a certain node or

    traverse all nodes in the graph, find a path

    from one node to another

    Depth First Search

    Once a possible path is found, continue the

    search until the end of the path

    Breadth First Search

  • 7/30/2019 Data Structures And Algorithm Unit 5

    21/57

    Depth First Search

    Generalized version of pre-order traversal

    Starting @ a vertex v, we process v and

    then recursively traverse all vertices

    adjacent to v

    Be careful about cycles !!

    Mark a discovered vertex as visited and

    perform DFS on vertices that are not already

    visited

    Backtracking !! what DS comes to your

    mind ?

    Exploring a Labyrinth

  • 7/30/2019 Data Structures And Algorithm Unit 5

    22/57

    Exploring a Labyrinth

    Without Getting Lost

    A depth-first search (DFS) in an undirected graph G is

    like wandering in a labyrinth with a string and a can of

    red paint without getting lost.

    We start at vertex s, tying the end of our string to the

    point and painting svisited. Next we label sas ourcurrent vertex called u.

    Now we travel along an arbitrary edge (u, v).

    If edge (u, v) leads us to an already visited vertex vwe

    return to u.

    If vertex vis unvisited, we unroll our string and move to

    v, paint vvisited, set vas our current vertex, and

    repeat the previous steps.

  • 7/30/2019 Data Structures And Algorithm Unit 5

    23/57

    Depth-First Search

    Algorithm DFS(v); Input: A vertex v in a graph

    Output: A labeling of the edges as discovery edges andbackedges

    foreach edge e incident on vdo

    ifedge e is unexplored then let wbe the other endpointofe

    ifvertex w is unexplored then label e as a discoveryedge

    recursively call DFS(w)else label e as a backedge

  • 7/30/2019 Data Structures And Algorithm Unit 5

    24/57

    Recursive call

    Void DFS(Vertex v){

    Visited[v] = true;

    For each W adjacent to V

    if(!Visited[W]) DFS (W)

    }

    What should be the input and how will you usethis ?

  • 7/30/2019 Data Structures And Algorithm Unit 5

    25/57

    Breadth First Search

    Start several paths at a time, and

    advance in each one step at a time

    Many simultaneous explorations starting

    a common point and spreading out

    independently What traversal comes to

    your mind ??

    There is no backtracking

  • 7/30/2019 Data Structures And Algorithm Unit 5

    26/57

    26

    Breadth-First Search

    The starting vertex s has level 0,

    In the first round, the string is unrolled the length of

    one edge, and all of the edges that are only one

    edge away from the anchor are visited. These edges are placed into level 1

    In the second round, all the new edges that can be

    reached by unrolling the string 2 edges are visitedand placed in level 2.

    This continues until every vertex has been

    assigned a level.

    BFS A G hi l

  • 7/30/2019 Data Structures And Algorithm Unit 5

    27/57

    27

    BFS - A Graphical

    Representation

    M N O P

    I J K L

    E F G H

    A B C D

    0

    M N O P

    I J K L

    E F G H

    A B C D

    0 1

    M N O P

    I J K L

    E F G H

    A C DB

    0 1 2

    M N O P

    I J K L

    E F G H

    A B C D

    0 1 2 3

    d)c)

    b)a)

  • 7/30/2019 Data Structures And Algorithm Unit 5

    28/57

    More BFS

    M N O P

    I J K L

    E F G H

    A B C D

    4

    0 1 2 3

    M N O P

    I J K L

    E F G H

    A B C D

    4

    5

    0 1 2 3

  • 7/30/2019 Data Structures And Algorithm Unit 5

    29/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    30/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    31/57

    DFS vs. BFS

    EF

    G

    B

    CD

    A start

    destination

    A DFS on A ADFS on BB

    A

    DFS on CB

    C

    A

    B Return to call on B

    D Call DFS on D

    A

    B

    D

    Call DFS on GG found destination - done!

    Path is implicitly stored in DFS recursion

    Path is: A, B, D, G

    DFS Process

  • 7/30/2019 Data Structures And Algorithm Unit 5

    32/57

    DFS vs. BFS

    EF

    G

    B

    CD

    Astart

    destination

    BFS Process

    A

    Initial call to BFS on A

    Add A to queue

    B

    Dequeue A

    Add B

    frontrear frontrear

    C

    Dequeue B

    Add C, D

    frontrear

    D D

    Dequeue C

    Nothing to add

    frontrear

    G

    Dequeue D

    Add G

    frontrear

    found destination - done!

    Path must be stored separately

  • 7/30/2019 Data Structures And Algorithm Unit 5

    33/57

    Dijk t Al ith

  • 7/30/2019 Data Structures And Algorithm Unit 5

    34/57

    Dijkstras Algorithm For a Graph G

    Vertices V = {v1, vn}

    And edge weights wij, for edgeconnecting vi to vj.

    Let the source be v1.

    Initialize a Set S = . Keep track of the vertices for

    which weve alreadycomputed their shortest pathfrom the source.

    Initialize an array D ofestimates of shortest

    distances. Initialize D[source]=0,

    everything else D[i] = .

    This says our estimate fromthe source to the source is 0,everything else is initially.

    While S!=V (or while

    we havent finished allvertices):

    1) Find the vertex with

    the minimum dist (not

    already in S).

    2) Add this vertex, vi to S

    3) Recompute all

    estimates based on vi

    Specifically compute

    D[i]+wij. If this < D[j] then

    set D[j] = D[i]+wij

    Dijk t Al ith

  • 7/30/2019 Data Structures And Algorithm Unit 5

    35/57

    Dijkstras Algorithm

    Dijkstras

    algorithm relieson:

    Knowing that all

    shortest pathscontain subpaths

    that are also

    shortest paths.

    c

    ba d10 2

    48

    The shortest path

    from a to b is 10, so the shortest path

    from a to d has to go

    through b

    so it will also contain

    the shortest path from

    a to b which is 10,

    plus the additional 2 =

    12.

  • 7/30/2019 Data Structures And Algorithm Unit 5

    36/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    Walk-Through

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Initialize array

    K dv pv

    A F

    B F

    C F

    D F

    E F

    F F

    G F

    H F

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    37/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Start with G

    K dv pv

    A

    B

    CD

    E

    F

    G T 0

    H

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    38/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Update unselected nodes

    K dv pv

    A

    B

    CD 2 G

    E

    F

    G T 0

    H 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    39/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A

    B

    CD T 2 G

    E

    F

    G T 0

    H 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    40/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Update unselected nodes

    K dv pv

    A

    B

    CD T 2 G

    E 27 D

    F 20 D

    G T 0

    H 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    41/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A

    B

    C

    D T 2 G

    E 27 D

    F 20 D

    G T 0

    H T 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    42/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    43/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    44/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    45/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A T 7 H

    B T 12 H

    C

    D T 2 G

    E 27 D

    F 17 A

    G T 0

    H T 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    46/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    47/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A T 7 H

    B T 12 H

    C T 16 B

    D T 2 G

    E 22 B

    F 17 A

    G T 0

    H T 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    48/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    49/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    34

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A T 7 H

    B T 12 H

    C T 16 B

    D T 2 G

    E 22 B

    F T 17 A

    G T 0

    H T 3 G

    2

  • 7/30/2019 Data Structures And Algorithm Unit 5

    50/57

  • 7/30/2019 Data Structures And Algorithm Unit 5

    51/57

    25

    A

    H

    B

    F

    E

    D

    C

    G

    9

    7

    2

    10

    18

    2

    4

    3

    7

    5

    8

    9

    4

    3

    10

    Select minimum distance

    K dv pv

    A T 7 H

    B T 12 H

    C T 16 B

    D T 2 G

    E T 19 F

    F T 17 A

    G T 0

    H T 3 G

    Done

    Minimum Spanning Tree

  • 7/30/2019 Data Structures And Algorithm Unit 5

    52/57

    Minimum Spanning Tree

    Let G = (V, E) be a simple, connected, undirected graph that is not

    edge-weighted.

    A spanning tree of G is a sub graph with all the vertices .

    A MST for a for a weight graph is a spanning tree with minimum weight

    Thus a minimum spanning tree for G is a graph, T = (V, E) with thefollowing properties:

    V = V

    T is connected

    T is acyclic.

    MST Property For a weighed connected Graph G, and T, a spanning

    tree for G. Suppose that for every edge uv of G that is not in T, if uv is

    added to T it creates a cycle such that uv is the max weighed edge on

    the cycle. Then T is said to have the MST property

  • 7/30/2019 Data Structures And Algorithm Unit 5

    53/57

    MST - Example

  • 7/30/2019 Data Structures And Algorithm Unit 5

    54/57

    p For an edge-weighted , connected, undirected graph, G, the total

    cost of G is the sum of the weights on all its edges.

    A minimum-cost spanning tree for G is a minimum spanning tree of

    G that has the least total cost. Example: The graph

    Has 16 spanning trees. Some are:

    The graph has two minimum-cost spanning trees, each with a cost of 6:

  • 7/30/2019 Data Structures And Algorithm Unit 5

    55/57

    Kruskal's Algorithm

  • 7/30/2019 Data Structures And Algorithm Unit 5

    56/57

    Kruskal s Algorithm.

    Kruskals algorithm also finds the minimum cost

    spanning tree of a graph by adding edges one-by-one.

    enqueue edges of G in a queue in increasing order of cost.T = ;while(queue is not empty){

    dequeue an edge e;

    if(e does not create a cycle with edges in T)

    add e to T;}

    return T;

  • 7/30/2019 Data Structures And Algorithm Unit 5

    57/57