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