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

β–Ά
graph implementation
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Graph implementation

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

slide-2
SLIDE 2

Graph vocabulary

1. List the edges incident to vertex 𝑐 2. What is the degree of vertex 𝑒? 3. List the vertices adjacent to vertex 𝑗 4. Give a path from 0 to 7 5. Give a path from 𝑙 to β„Ž 6. Vertices in the largest complete subgraph in 𝐻 7. How many connected components are in 𝐻? 8. How many edges in a spanning forest? 9. How many simple paths connect 0 and 9?

  • 10. Can you draw 𝐻 with no edge crossings (as a

planar graph)?

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 2

Quiz yourself!

𝑐 𝑏 𝑑 𝑓 𝑒 𝑕 𝑔 π‘œ 𝑛 𝑙 π‘˜ 𝑝 π‘š 𝑗 π‘Ÿ π‘ž 8 9 6 7 4 5 2 3 1 β„Ž

𝐻

slide-3
SLIDE 3

Weighted graphs

  • In a weighted graph each edge

is assigned a weight

– Edges are labeled with their weights

  • Each edge’s weight represents

the cost to travel along that edge

– The cost could be distance, time, money or some other measure – The cost depends on the underlying problem

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 3

1 2 4 3 1 3 5 2 2 3

slide-4
SLIDE 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 𝐹

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 4

slide-5
SLIDE 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

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 5

slide-6
SLIDE 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 𝑣, 𝑀 ∈ π‘Šβ€²

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 6

slide-7
SLIDE 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

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 7

( )

E v

V v

2 deg =

οƒ₯

οƒŽ

𝐹 = 7

slide-8
SLIDE 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:

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 8

( ) ( ) ( )

E v v v

V v V v V v

= = =

οƒ₯ οƒ₯ οƒ₯

οƒŽ οƒŽ + οƒŽ βˆ’

deg 2 1 deg deg

slide-9
SLIDE 9

Graph implementation

Adjacency matrix Adjacency list

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 9

slide-10
SLIDE 10

Adjacency matrix

  • 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

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 10

Data structure for set 𝐹

Tom Shelly hamster popcorn Tom Tom Shelly Shelly hamster hamster popcorn popcorn

slide-11
SLIDE 11

Adjacency matrix examples

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 11

With weights and/or directed edges

A B C D E F G A B C D E F G

3 1 1 1 2 2 2 3 4 5 5 8

A B C D E F G A B C D E F G A B C D E F G A B C D E F G

slide-12
SLIDE 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)

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 12

𝑀 π‘₯ 𝑨 𝑣 𝑣 𝑀 π‘₯ 𝑨 𝑣 1 1 𝑀 1 1 π‘₯ 1 1 1 𝑨 1

  • What is the required space usage of this representation?
slide-13
SLIDE 13

Adjacency list

  • A π‘Š -ary list (array) in which each entry stores a list (linked

list) of all adjacent vertices

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 13

Tom Shelly hamster popcorn Tom Shelly hamster popcorn … … … …

slide-14
SLIDE 14

Adjacency list example

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 14

A B C D E F G A B C D E F G

3 1 1 1 2 2 2 3 4 5 5 8

A B C D E F G A B C D E F G

slide-15
SLIDE 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)

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 15

𝑀 π‘₯ 𝑨 𝑣

  • What is the required space usage of this representation?

𝑣 𝑀 π‘₯ 𝑨 𝑀 π‘₯ 𝑣 π‘₯ 𝑀 π‘₯ 𝑣 π‘₯

slide-16
SLIDE 16

Graph performance

March 25, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 16

Implementation affects performance!

π‘œ vertices 𝑛 edges No self-edges Adjacency list Adjacency matrix Space incidentEdges(𝑀) areAdjacent(𝑀, π‘₯) insertVertex(𝑦) removeVertex(𝑀) insertEdge(𝑀, π‘₯) removeEdge(𝑀, π‘₯)