algorithms and data structures for data science graphs

39
Department of Computer Science Algorithms and Data Structures for Data Science CS 277 Brad Solomon November 1, 2021 Graphs

Upload: others

Post on 22-Apr-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms and Data Structures for Data Science Graphs

Department of Computer Science

Algorithms and Data Structures for Data Science

CS 277 Brad Solomon

November 1, 2021

Graphs

Page 2: Algorithms and Data Structures for Data Science Graphs

Exam 2

Average (before partial credit):

Page 3: Algorithms and Data Structures for Data Science Graphs

Final Project Mid-Project Check-ins

Mid-project check-ins start next week (November 8th-12th)!

Proposal resubmissions due November 5th

To sign up for a mid-project meeting time:

https://bit.ly/3GzmC3P

Page 4: Algorithms and Data Structures for Data Science Graphs

Learning Objectives

Define graph vocabulary

Discuss graph implementation / storage strategies

Define key graph functions and discuss implementation details

Page 5: Algorithms and Data Structures for Data Science Graphs

In Review: Data Structures

Array - Sorted Array - Unsorted Array - Stacks - Queues

Linked - Linked List - Trees - Binary Tree - Huffman Encoding - BST - AVL Tree

Page 6: Algorithms and Data Structures for Data Science Graphs

The Internet 2003 The OPTE Project (2003) Map of the entire internet; nodes are routers; edges are connections. Example from Carl Evans

Page 7: Algorithms and Data Structures for Data Science Graphs

“Rule of 7” Unknown Source Presented by Cinda Heeren, 2016

This graph can be used to quickly calculate whether a given number is divisible by 7.

1. Start at the circle node at the top. 2. For each digit d in the given number, follow d blue (solid) edges in succession. As you move from one digit to the next, follow 1 red (dashed) edge. 3. If you end up back at the circle node, your number is divisible by 7.

3703

Page 8: Algorithms and Data Structures for Data Science Graphs

Conflict-Free Final Exam Scheduling Graph Unknown Source Presented by Cinda Heeren, 2016

Page 9: Algorithms and Data Structures for Data Science Graphs

“Rush Hour” Solution Figure from Christopher Wolfram Idea from Cinda Heeren

Page 10: Algorithms and Data Structures for Data Science Graphs

“Stanford Bunny” Greg Turk and Mark Levoy (1994)

Page 11: Algorithms and Data Structures for Data Science Graphs

Graphs

To study all of these structures: 1. A common vocabulary 2. Graph implementations 3. Graph traversals 4. Graph algorithms

Page 12: Algorithms and Data Structures for Data Science Graphs

Graph Vocabulary

5

36

4

2

1

4

G = (V, E)

Cardinality:

Vertex:

Edges:

Page 13: Algorithms and Data Structures for Data Science Graphs

Graph Vocabulary

5

36

4

2

1

4

G = (V, E)

Path:

Degree:

Adjacent:

Cycle:

Page 14: Algorithms and Data Structures for Data Science Graphs

Connected Graphs

Page 15: Algorithms and Data Structures for Data Science Graphs

Graph Efficiency

Graph efficiency has two factors:

m, the number of edges.

n, the number of vertices

XU

V

W

Z

Y

∑v∈V

deg(v) =

Minimum Edges:

Maximum Edges:

Page 16: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Edge List

v

u

w z

u

v

w

z

Vertex Storage:

Edge Storage:

|V|= n,|E|= m

Page 17: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Edge List

v

u

w z

u

v

w

z

u v

v w

u w

w z

insertVertex(v):

removeVertex(v):

|V|= n,|E|= m

Page 18: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Edge List

v

u

w z

u

v

w

z

u v

v w

u w

w z

insertEdge(u, v):

removeEdge(u, v):

|V|= n,|E|= m

Page 19: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Edge List

v

u

w z

u

v

w

z

u v

v w

u w

w z

getEdges(u):

areAdjacent(u, v):

|V|= n,|E|= m

Page 20: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Edge List

v

u

w z

u

v

w

z

u v

v w

u w

w z

Pros:

Cons:

|V|= n,|E|= m

Page 21: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

Vertex Storage:

Edge Storage:

u v w z

u

v

w

z

Page 22: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

insertVertex(v):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 23: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

removeVertex(v):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 24: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

insertEdge(u, v):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 25: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

removeEdge(u, v):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 26: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

getEdges(u):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 27: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

areAdjacent(u, v):

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Page 28: Algorithms and Data Structures for Data Science Graphs

Graph Implementation: Adjacency Matrix

v

u

w z

u

v

w

z

Pros:

u v w z

u - 1 1 0

v - 1 0

w - 1

z -

Cons:

Page 29: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

u

v

w

z

d=2

d=2

d=3

d=1

Vertex Storage:

Edge Storage:

Page 30: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Vertex Storage:

Edge Storage:

Page 31: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

insertVertex(v):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 32: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

removeVertex(v):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 33: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

insertEdge(u, v):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 34: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

removeEdge(u, v):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 35: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

getEdges(u):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 36: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

areAdjacent(u, v):

u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 37: Algorithms and Data Structures for Data Science Graphs

Adjacency List

v

u

w z

Pros:

Cons:u

v

w

z

v w

u w

v u z

z

d=2

d=2

d=3

d=1

Page 38: Algorithms and Data Structures for Data Science Graphs

Expressed as O(f)Edge List Adjacency Matrix Adjacency List

Space n+m n2 n+m

insertVertex(v) 1* n* 1*

removeVertex(v) m** n deg(v)

insertEdge(u, v) 1 1 1*

removeEdge(u, v) m 1min( deg(u),

deg(v) )

getEdges(v) m n deg(v)

areAdjacent(u, v) m 1min( deg(u),

deg(v) )

Page 39: Algorithms and Data Structures for Data Science Graphs

Traversal: Objective: Visit every vertex and every edge in the graph.

Purpose: Search for interesting sub-structures in the graph.

We’ve seen traversal before ….but it’s different:

• Ordered • Obvious Start •

• • •