Graph implementation Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 1 Geoffrey Tien
Graph vocabulary Quiz yourself! π» List the edges incident to vertex π 1. π What is the degree of vertex π ? 2. π π List the vertices adjacent to vertex π 3. 8 9 π π Give a path from 0 to 7 4. Give a path from π to β 5. 6 7 π π Vertices in the largest complete subgraph in π» 6. How many connected components are in π» ? 7. π 4 5 8. How many edges in a spanning forest? π π How many simple paths connect 0 and 9 ? 9. 2 3 π 10. Can you draw π» with no edge crossings (as a π π planar graph)? 0 1 π β π π Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 2 Geoffrey Tien
Weighted graphs β’ In a weighted graph each edge is assigned a weight β Edges are labeled with their 3 1 weights β’ Each edgeβs weight represents 4 1 2 the cost to travel along that edge β The cost could be distance, time, 2 5 3 2 money or some other measure β The cost depends on the underlying problem 3 Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 3 Geoffrey Tien
Graph density β’ A sparse graph has π π edges β’ A dense graph has Ξ π 2 edges β Anything in between is either on the sparse side or on the dense side, depending critically on context! β’ Analysis of graph operations typically must be expressed in terms of both π and πΉ Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 4 Geoffrey Tien
Connectivity β’ Undirected graphs are connected if there is a path between any two vertices β’ Directed graphs are strongly connected if there is a directed path from any vertex to any other β’ Digraphs are weakly connected if there is a path between any two vertices, ignoring direction β’ A complete graph has an edge between every pair of vertices Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 5 Geoffrey Tien
Isomorphism and subgraphs β’ We often care only about the structure of a graph, not the names of its vertices. Then, we can ask: β "Are two graphs isomorphic ?" i.e. do the graphs have identical structure / can you "line up" their vertices so that their edges match? β "Is one graph a subgraph of another?" i.e. is one graph isomorphic to a part of the other graph (a subset of vertices and a subset of edges connecting those vertices)? β’ π» β² = π β² , πΉβ² πβ² β π , πΉβ² β πΉ , if π£, π€ β πΉ β² then π£, π€ β π β² Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 6 Geoffrey Tien
Degree β’ The degree of a vertex π€ β π is denoted deg π€ and represents the number of edges incident on π€ β’ Handshaking theorem: ( ) ο₯ = β If π» = π, πΉ is an undirected graph, then deg v 2 E ο v V πΉ = 7 Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 7 Geoffrey Tien
Degree for directed graphs β’ The in-degree of a vertex π€ β π is denoted deg β π€ and is the number of edges entering π€ β’ The out-degree of a vertex π€ β π is denoted deg + π€ and is the number of edges leaving π€ β’ We let deg π€ = deg + π€ + deg β π€ β’ Then: ( ) ( ) 1 ( ) ο₯ ο₯ ο₯ β + = = = deg v deg v deg v E 2 ο ο ο v V v V v V Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 8 Geoffrey Tien
Graph implementation Adjacency matrix Adjacency list Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 9 Geoffrey Tien
Adjacency matrix Data structure for set πΉ β’ A π Γ π array in which an element π£, π€ is true if and only if there is an edge from π£ to π€ β Note that π is typically a set of integers used as array indices Tom Tom Shelly hamster popcorn Tom Shelly hamster Shelly popcorn hamster popcorn Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 10 Geoffrey Tien
Adjacency matrix examples With weights and/or directed edges 5 A B C A B C 1 1 3 1 2 2 5 D E D E 2 4 3 F G F G 8 A B C D E F G A B C D E F G A A B B C C D D E E F F G G Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 11 Geoffrey Tien
Adjacency matrix performance β’ What would be the complexity of these operations? β insertVertex(Vertex v) β removeVertex(Vertex v) β areAdjacent(Vertex v, Vertex u) β incidentEdges(Vertex v) β insertEdge(Vertex v, Vertex u) π£ π€ π₯ π¨ β removeEdge(Vertex v, Vertex u) π£ 0 1 1 0 π£ π€ 1 0 1 0 π₯ 1 1 0 1 π€ π₯ π¨ π¨ 0 0 1 0 β’ What is the required space usage of this representation? Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 12 Geoffrey Tien
Adjacency list β’ A π -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices β¦ Tom Tom β¦ Shelly Shelly β¦ hamster hamster β¦ popcorn popcorn Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 13 Geoffrey Tien
Adjacency list example 5 A B C A B C 1 1 3 1 2 2 5 D E D E 2 4 3 F G F G 8 A A B B C C D D E E F F G G Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 14 Geoffrey Tien
Adjacency list performance β’ What would be the complexity of these operations? β insertVertex(Vertex v) β removeVertex(Vertex v) β areAdjacent(Vertex v, Vertex u) β incidentEdges(Vertex v) β insertEdge(Vertex v, Vertex u) β removeEdge(Vertex v, Vertex u) π£ π€ π₯ π£ π€ π£ π₯ π₯ π£ π€ π₯ π€ π₯ π¨ π¨ π₯ β’ What is the required space usage of this representation? Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 15 Geoffrey Tien
Graph performance Implementation affects performance! π vertices π edges No self-edges Adjacency list Adjacency matrix Space incidentEdges( π€ ) areAdjacent( π€ , π₯ ) insertVertex( π¦ ) removeVertex( π€ ) insertEdge( π€ , π₯ ) removeEdge( π€ , π₯ ) Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 16 Geoffrey Tien
Recommend
More recommend