graph implementation
play

Graph implementation Cinda Heeren / Andy Roth / Will Evans / March - PowerPoint PPT Presentation

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


  1. Graph implementation Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 1 Geoffrey Tien

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Graph implementation Adjacency matrix Adjacency list Cinda Heeren / Andy Roth / Will Evans / March 25, 2020 9 Geoffrey Tien

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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