Graphs Lect2

Embed Size (px)

Citation preview

  • 8/14/2019 Graphs Lect2

    1/29

    Graphs

    Nitin Upadhyay

    February 25, 2006

  • 8/14/2019 Graphs Lect2

    2/29

    Discussion

    What is a Graph?

    Applications of Graphs

    Categorization

    Terminology

  • 8/14/2019 Graphs Lect2

    3/29

    Discussion

    Hamiltonian Cycle

    Special Graph Structures

    Graph Representations

  • 8/14/2019 Graphs Lect2

    4/29

    Hamiltonian Cycle

    Hamiltonian Cycle: A Cycle that contains all

    the vertices of the graph

    1,4,5,3,2,1 is a Hamiltonian

    Cycle

    1

    2

    3

    4

    5

  • 8/14/2019 Graphs Lect2

    5/29

    Special Graph Structures

    Special cases of undirected graph structures:

    Complete graphs Kn

    Cycles Cn Wheels Wn

    n-Cubes Qn

    Bipartite graphs Complete bipartite graphs Km,n

  • 8/14/2019 Graphs Lect2

    6/29

    Revisiting Paths and

    cycles Apath is a sequence of nodesv1, v2, , vN such that (vi,vi+1)E for 0

  • 8/14/2019 Graphs Lect2

    7/29

    Complete Graphs

    For any nN, a complete graph on n vertices,

    Kn, is a simple graph with n nodes in which

    every node is adjacent to every other node:

    u,vV: uv{u,v}E.

    K1 K2 K3 K4 K5 K6Note that Kn has edges.2

    )1(1

    1

    =

    =

    nni

    n

    i

  • 8/14/2019 Graphs Lect2

    8/29

    Cycles

    For any n3, a cycle on n vertices, Cn, is a

    simple graph where V={v1,v2, ,vn} and

    E={{v1,v2},{v2,v3},,{vn1,vn},{vn,v1}}.

    C3 C4C5 C6 C7 C8

    How many edges are there in Cn?

  • 8/14/2019 Graphs Lect2

    9/29

    Wheels

    For any n3, a wheelWn, is a simple graph

    obtained by taking the cycle Cn and adding

    one extra vertex vhub and n extra edges

    {{vhub,v1}, {vhub,v2},,{vhub,vn}}.

    W3 W4 W5 W6 W7W8

    How many edges are there in Wn?

  • 8/14/2019 Graphs Lect2

    10/29

    n-cubes (hypercubes)

    For any nN, the hypercube Qn is a simple

    graph consisting of two copies ofQn-1

    connected together at corresponding nodes.

    Q0 has 1 node.

    Q0

    Q1 Q2 Q3 Q4

    Number of vertices: 2n. Number of edges:Exercise to try!

  • 8/14/2019 Graphs Lect2

    11/29

    Bipartite Graphs

    Let G=(V,E) be a graph, G is said to be

    bipartite graph:

    If its vertex set V can be partitioned into two

    nonempty disjoint subsets V1 and V2, calleda bipartition.

    And edge connecting one of its end in V1 and

    other with V2.

  • 8/14/2019 Graphs Lect2

    12/29

    Complete Bipartite Graphs

    A complete bipartite graph is a bipartite graph

    with partition V1 and V2 in which each vertex

    of V1 is joined by an edge to each vertex of

    V2. A complete bipartite graph with | V1 | = m and

    | V2 | = n is denoted as Km,n.

  • 8/14/2019 Graphs Lect2

    13/29

    Subgraphs

    A subgraph of a graph G=(V, E) is a graph

    H=(W, F) where WVand FE.

    G H

  • 8/14/2019 Graphs Lect2

    14/29

    Graph Representations

    Adjacency-matrix representation

    Incidence Matrix representation

    Edge-set representation

    Adjacency-set representation

    Adjacency List

  • 8/14/2019 Graphs Lect2

    15/29

    Adjacency-matrixrepresentationAdjacency Matrix Representation

    AB

    G

    E

    F

    D

    C One simple way of

    representing a graph

    is the adjacency

    matrix A 2-D array has a

    mark at [i][j] if there

    is an edge from node i

    to nodej The adjacency matrix

    is symmetric about

    the main diagonal

    ABC

    DEFG

    A B C D E F G

  • 8/14/2019 Graphs Lect2

    16/29

    Adjacency-matrixrepresentation Adjacency Matrix (A)

    The Adjacency MatrixA=(ai,j) of a graph G=(V,E)

    with n nodes is an nXn matrix

    Each element of A is either0 or 1, depending onthe adjacency of the nodes.

    1 if (i,j) E

    0 otherwiseaij =

  • 8/14/2019 Graphs Lect2

    17/29

    Adjacency-matrixrepresentation Find the adjacency matrices of the following

    graphs1

    2

    3

    4

    5

    1

    2

    3

    01100

    10001

    10011

    00101

    01110

    011

    100

    010

  • 8/14/2019 Graphs Lect2

    18/29

    Adjacency-matrixrepresentationRepresentation

    AB

    G

    E

    F

    D

    C

    ABC

    DEFG

    A B C D E F G

    An adjacency matrixcan equally well be

    used for digraphs

    (directed graphs)

    A 2-D array has a

    mark at [i][j] if there

    is an edge from node i

    to nodej

  • 8/14/2019 Graphs Lect2

    19/29

    Incidence Matrices

    Let G = (V, E) be an undirected graph and v1,

    v2, , vn be the vertices and e1, e2, , em be

    the edges ofG. Then the incidence matrix

    with respect to this ordering ofVand Eis the

    n x m matrix M = [mij], where

    1 when edge ej is incident with vi,

    mij =0 otherwise

  • 8/14/2019 Graphs Lect2

    20/29

    Incidence Matrix Example

    Represent the graph shown below with an

    incidence matrix.

    e9

    e1

    e2

    e5

    e3

    e7

    e4

    e6

    e8

    v1 v2 v3

    v4 v6v5

    xxxxxxxxx

    xxxxxxxxx

    xxxxxxxxx

    011000100

    000111101

    000000011v1

    v2

    v3

    v4

    v5

    v6

    e1 e2 e3 e4 e5 e6 e7 e8 e9

    For your exercise

  • 8/14/2019 Graphs Lect2

    21/29

    Edge-set representation

    An edge-set representation uses a setof nodes and a setofedges The sets might be represented by, say, linked lists The set links are stored in the nodes and edges themselves

    The only other information in a node is its element (that is, its

    value)it does not hold information about its edges The only other information in an edge is its source and

    destination (and attribute, if any) If the graph is undirected, we keep links to both nodes, but dont

    distinguish between source and destination

    This representation makes it easy to find nodes from an edge,but you must search to find an edge from a node This is seldom a good representation

  • 8/14/2019 Graphs Lect2

    22/29

    Edge-set representation

    edge nodeSet = {A, B, C, D, E, F, G}

    edgeSet = { p: (A, E),

    q: (B, C), r: (E, B),

    s: (D, D), t: (D, A),

    u: (E, G), v: (F, E),

    w: (G, D) }

    AB

    G

    E

    FD

    Cpq

    r

    s

    t

    uv

    w

    Here we have a set of nodes,

    and each node contains only its

    element (not shown)

    Each edge contains references to its source

    and its destination (and its attribute, if any)

  • 8/14/2019 Graphs Lect2

    23/29

    Adjacency-setrepresentation An adjacency-set representation uses a set

    of nodes Each node contains a reference to the set ofits

    edges For a directed graph, a node might only know

    about (have references to) its out-edges

    Thus, there is not one single edge set, but

    rather a separate edge set for each node Each edge would contain its attribute (if any) andits destination (and possibly its source)

  • 8/14/2019 Graphs Lect2

    24/29

    Adjacency-setrepresentation Adjacency

    AB

    G

    E

    FD

    Cpq

    r

    s

    t

    uv

    w

    A { p }B { q }

    C { }

    D { s, t }

    E { r, u }

    F { v }

    G { w }

    p: (A, E)

    q: (B, C)

    r: (E, B)

    s: (D, D)

    t: (D, A)

    u: (E, G)

    v: (F, E)

    w: (G, D)

    Here we have a set of

    nodes, and each node

    refers to a set of edges Each edge contains references to its

    source and its destination (and itsattribute, if any)

  • 8/14/2019 Graphs Lect2

    25/29

    Adjacency-setrepresentation If the edges have no associated attribute, there is no

    need for a separate Edge class Instead, each node can refer to a set of its neighbors

    In this representation, the edges would be implicit in the

    connections between nodes, not a separate data structure

    For an undirected graph, the node would have

    references to all the nodes adjacent to it

    For a directed graph, the node might have: references to all the nodes adjacent to it, or

    references to only those adjacent nodes connected by an

    out-edge from this node

  • 8/14/2019 Graphs Lect2

    26/29

    Adjacency-setrepresentation Adjacency

    AB

    G

    E

    F

    D

    C

    A { E }

    B { C }

    C { }

    D { D, A }

    E { B, G }

    F { E }

    G { D }

    Here we have a set ofnodes, and each node

    refers to a set of other(pointed to) nodes

    The edges are implicit

  • 8/14/2019 Graphs Lect2

    27/29

    Adjacency List

    An Adjacency list is an array of lists, each list

    showing the vertices a given vertex is

    adjacent to

    1

    2

    3

    4

    5

  • 8/14/2019 Graphs Lect2

    28/29

    Adjacency List

    Adjacency Listof a Weighted Graph The weight is included in the list

    1

    2

    39

    4 5

  • 8/14/2019 Graphs Lect2

    29/29

    Questions

    Questions ?