25.graphsds

10
1 Data Structures for Graphs DATA STRUCTURES FOR GRAPHS Edge list Adjacency lists Adjacency matrix DFW BOS ORD MIA SFO JFK LAX DL 247 DL 335 UA 877 NW 35 AA 523 AA 411 TW 45 UA 120 AA 49 AA 903 AA 1387 E V

Upload: mansoor-cheema

Post on 19-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

DATASTRUCTURES FORGRAPHS

TRANSCRIPT

  • 1Data Structures for Graphs

    DATA STRUCTURES FORGRAPHS

    Edge list

    Adjacency lists

    Adjacency matrix

    DFWBOS ORDMIA SFOJFKLAX

    DL 247 DL 335 UA 877NW 35 AA 523 AA 411 TW 45UA 120AA 49 AA 903AA 1387

    E

    V

  • 2Data Structures for Graphs

    Data Structures for Graphs A Graph! How can we represent it?

    To start with, we store theverticesand theedgesintotwo containers, and each edge object has referencesto the vertices it connects.

    Additional structures can be used to performefficiently the methods of the Graph ADT

    JFK

    BOS

    MIA

    ORD

    LAXDFW

    SFO

    TW 45

    AA 411

    AA 13

    87

    AA

    903

    DL

    247

    AA 523

    NW

    35

    UA

    877

    DL

    335

    AA 49

    UA 12

    0

    JFK

    BOS

    MIA

    ORD

    LAXDFW

    SFO

  • 3Data Structures for Graphs

    Edge List Theedge list structure simply stores the vertices and

    the edges into unsorted sequences.

    Easy to implement.

    Finding the edges incident on a given vertex isinefficient since it requires examining the entireedge sequence

    DFWBOS ORDMIA SFOJFKLAX

    DL 247 DL 335 UA 877NW 35 AA 523 AA 411 TW 45UA 120AA 49 AA 903AA 1387

    E

    V

  • 4Data Structures for Graphs

    Performance of the Edge ListStructure

    Operation Time

    size, isEmpty, replaceElement, swap O(1)

    numVertices, numEdges O(1)

    vertices O(n)

    edges, directedEdges, undirectedEdges O(m)

    elements, positions O(n+m)

    endVertices, opposite, origin, destination,isDirected

    O(1)

    incidentEdges, inIncidentEdges, outInci-dentEdges, adjacentVertices, inAdja-centVertices, outAdjacentVertices,areAdjacent, degree, inDegree, outDegree

    O(m)

    insertVertex, insertEdge, insertDirected-Edge, removeEdge, makeUndirected,reverseDirection, setDirectionFrom, setDi-rectionTo

    O(1)

    removeVertex O(m)

  • 5Data Structures for Graphs

    Adjacency List(traditional)

    adjacency list of a vertex v:sequence of vertices adjacent tov

    represent the graph by the adjacency lists of all thevertices

    Space =(N + deg(v)) = (N + M )

    a b

    c

    d e

    b

    b

    c

    c

    c

    d

    a e

    a d e

    a e

    d

    a

    b

    c

    d

    e

  • 6Data Structures for Graphs

    Adjacency List(modern)

    Theadjacency list structure extends the edge liststructure by addingincidence containers to eachvertex.

    The space requirement is O(n + m).

    in out in out in out in out in out in out in out

    NW 35

    DL 247

    AA 49

    AA 411

    UA 120 AA1387

    AA 523

    UA 877

    DL335

    AA 49

    NW 35 AA1387

    AA 903

    TW 45

    DL 247

    AA 903

    AA523

    AA 411

    UA 120

    DL 335

    UA 877 TW 45

    DFWBOS ORDMIA SFOJFKLAX

    DL 247 DL 335 UA 877NW 35 AA 523 AA 411 TW 45UA 120AA 49 AA 903AA 1387

  • 7Data Structures for Graphs

    Performance of the AdjacencyList Structure

    Operation Time

    size, isEmpty, replaceElement, swap O(1)

    numVertices, numEdges O(1)

    vertices O(n)

    edges, directedEdges, undirectedEdges O(m)

    elements, positions O(n+m)

    endVertices, opposite, origin, destina-tion, isDirected, degree, inDegree, out-Degree

    O(1)

    incidentEdges(v), inIncidentEdges(v),outIncidentEdges(v), adjacentVerti-ces(v), inAdjacentVertices(v), outAdja-centVertices(v)

    O(deg(v))

    areAdjacent(u, v) O(min(deg(u),deg(v)))

    insertVertex, insertEdge, insertDirected-Edge, removeEdge, makeUndirected,reverseDirection,

    O(1)

    removeVertex(v) O(deg(v))

  • 8Data Structures for Graphs

    Adjacency Matrix(traditional)

    matrix M with entries for all pairs of vertices

    M[i,j] = true means that there is an edge (i,j) in thegraph.

    M[i,j] = false means that there is no edge (i,j) in thegraph.

    There is an entry for every possible edge, therefore:Space =(N2)

    F T T T FT F F F TT F F T TT F T F TF T T T F

    a b

    c

    d e

    a b c d eabcde

  • 9Data Structures for Graphs

    Adjacency Matrix(modern)

    The adjacency matrix structures augments the edgelist structure with a matrix where each row andcolumn corresponds to a vertex.

    BOS DFW JFK LAX MIA ORD SFO0 1 2 3 4 5 6

    The space requirement is O(n2 + m)

    0 1 2 3 4 5 6

    0 NW35

    DL247

    1 AA49

    DL335

    2 AA1387

    AA903

    TW45

    3 UA120

    4 AA523

    AA411

    5 UA877

    6

  • 10Data Structures for Graphs

    Performance of the AdjacencyMatrix StructureOperation Time

    size, isEmpty, replaceElement, swap O(1)

    numVertices, numEdges O(1)

    vertices O(n)

    edges, directedEdges, undirectedEdges O(m)

    elements, positions O(n+m)

    endVertices, opposite, origin, destination,isDirected, degree, inDegree, outDegree

    O(1)

    incidentEdges, inIncidentEdges, outInci-dentEdges, adjacentVertices, inAdja-centVertices, outAdjacentVertices,

    O(n)

    areAdjacent O(1)

    insertEdge, insertDirectedEdge, remov-eEdge, makeUndirected, reverseDirection,setDirectionFrom, setDirectionTo

    O(1)

    insertVertex, removeVertex O(n2)