graph implementation

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