cs/engrd 2110 object-oriented programming and data structures fall 2014 doug james lecture 17:...

35
CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Upload: thomas-lyons

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

CS/ENGRD 2110Object-Oriented Programming

and Data StructuresFall 2014

Doug James

Lecture 17: Graphs

Page 2: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Readings

• Chapter 28 Graphs• Chapter 29 Graph Implementations

2

Page 3: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

These are not Graphs

3...not the kind we mean, anyway

Page 4: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

These are Graphs

4

K5 K3,3

=

Page 5: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Applications of Graphs• Communication networks; social networks• Routing and shortest path problems• Commodity distribution (network flow)• Traffic control• Resource allocation• Numerical linear algebra (sparse matrices)• Geometric modeling (meshes, topology, …)• Image processing (e.g., graph cuts)• Computer animation (e.g., motion graphs)• Systems biology• …

5

Page 6: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Applications of Graphs• Communication networks; social networks• Routing and shortest path problems• Commodity distribution (network flow)• Traffic control• Resource allocation• Numerical linear algebra (sparse matrices)• Image processing (e.g., graph cuts)• Systems biology• Geometric modeling (meshes, topology, …)• …

6High School Dating (Bearman, Moody, and Stovel, 2004) (Image by Mark Newman)

Page 7: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Applications of Graphs• Communication networks; social networks• Routing and shortest path problems• Commodity distribution (network flow)• Traffic control• Resource allocation• Numerical linear algebra (sparse matrices)• Image processing (e.g., graph cuts)• Systems biology• Geometric modeling (meshes, topology, …)• …

7

http://www.willowgarage.com/blog/2009/09/04/robot-comics-path-planning

Page 8: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Applications of Graphs• Communication networks; social networks• Routing and shortest path problems• Commodity distribution (network flow)• Traffic control• Resource allocation• Numerical linear algebra (sparse matrices)• Image processing (e.g., graph cuts)• Systems biology• Geometric modeling (meshes, topology, …)• …

8

http://semiengineering.com/wp-content/uploads/2014/03/Fig13_Sparse_Matrix_reordering.png

Page 9: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

9

http:

//w

ww

.turb

osqu

id.c

om/3

d-m

odel

s/3d

-201

5-bm

w-i8

-mod

el/7

8091

8

Page 10: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

10

http:

//w

ww

.turb

osqu

id.c

om/3

d-m

odel

s/3d

-201

5-bm

w-i8

-mod

el/7

8091

8

Page 11: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

11

http:

//w

ww

.turb

osqu

id.c

om/3

d-m

odel

s/3d

-201

5-bm

w-i8

-mod

el/7

8091

8

Page 12: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

12

http:

//w

ww

.turb

osqu

id.c

om/3

d-m

odel

s/3d

-201

5-bm

w-i8

-mod

el/7

8091

8

Page 13: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Graph Definitions• A directed graph (or digraph) is a pair (V, E)

where– V is a set– E is a set of ordered pairs (u,v) where u,v V

• Usually require u ≠ v (i.e., no self-loops)

• An element of V is called a vertex or node• An element of E is called an edge or arc

• |V| = size of V, often denoted n• |E| = size of E, often denoted m

13

Page 14: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Example Directed Graph (Digraph)

14

V = { a,b,c,d,e,f }E = { (a,b), (a,c), (a,e), (b,c), (b,d), (b,e), (c,d),

(c,f), (d,e), (d,f), (e,f) }

|V| = 6, |E| = 11

b

a

cd

e

f

Page 15: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Example Undirected Graph

15

An undirected graph is just like a directed graph, except the edges are unordered pairs (sets) {u,v}

Example:b

a

c

e

d

f

V = { a,b,c,d,e,f }E = { {a,b}, {a,c}, {a,e}, {b,c}, {b,d}, {b,e}, {c,d}, {c,f},

{d,e}, {d,f }, {e,f } }

Page 16: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Some Graph Terminology• Vertices u and v are called the source and sink of the

directed edge (u,v), respectively• Vertices u and v are called the endpoints of (u,v)• Two vertices are adjacent if they are connected by an

edge• The outdegree of a vertex u in a directed graph is the

number of edges for which u is the source• The indegree of a vertex v in a directed graph is the

number of edges for which v is the sink• The degree of a vertex u in an undirected graph is the

number of edges of which u is an endpoint

16

b

a

c

e

d

f

b

a

cd

e

f

Page 17: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

More Graph Terminology

• A path is a sequence v0,v1,v2,...,vp of vertices such that (vi,vi+1) E, 0 ∈ ≤ i ≤ p – 1

• The length of a path is its number of edges – In this example, the length is 5

• A path is simple if it does not repeat any vertices• A cycle is a path v0,v1,v2,...,vp such that v0 = vp

• A cycle is simple if it does not repeat any vertices except the first and last

• A graph is acyclic if it has no cycles• A directed acyclic graph is called a dag

17

v0

v5

b

a

cd

e

f

Page 18: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Is This a Dag?

• Intuition: – If it’s a dag, there must be a vertex with indegree zero

• This idea leads to an algorithm– A digraph is a dag if and only if we can iteratively

delete indegree-0 vertices until the graph disappears

18

b

a

cd

e

f

Page 19: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Topological Sort• We just computed a topological sort of the dag

– This is a numbering of the vertices such that all edges go from lower- to higher-numbered vertices

• Useful in job scheduling with precedence constraints19

1

0

23

4

5

Page 20: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Example of Topological Sort• Starcraft II build order: Roach Rush

Hatchery Spawning Pool Roach Warren Roaches

Gas

Possible Topological Sorts1.Hatch, SPool, RWarren, Gas, Roaches2.Hatch, SPool, Gas, RWarren, Roaches3.Hatch, Gas, SPool, RWarren, Roaches

Timing is everything though ;)

Page 21: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Graph Coloring• A coloring of an undirected graph is an assignment

of a color to each node such that no two adjacent vertices get the same color

• How many colors are needed to color this graph?21

Page 22: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Graph Coloring• A coloring of an undirected graph is an

assignment of a color to each node such that no two adjacent vertices get the same color

• How many colors are needed to color this graph?22

Page 23: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

An Application of Coloring

• Vertices are jobs• Edge (u,v) is present if jobs u and v each

require access to the same shared resource, and thus cannot execute simultaneously

• Colors are time slots to schedule the jobs• Minimum number of colors needed to color

the graph = minimum number of time slots required

23

Page 24: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Planarity

• A graph is planar if it can be embedded in the plane with no edges crossing

• Is this graph planar?

24

Page 25: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Planarity

• A graph is planar if it can be embedded in the plane with no edges crossing

• Is this graph planar?– Yes

25

Page 26: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Planarity

• A graph is planar if it can be embedded in the plane with no edges crossing

• Is this graph planar?– Yes

26

Page 27: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Detecting Planarity

• Kuratowski's Theorem

• A graph is planar if and only if it does not contain a copy of K5 or K3,3 (possibly with other nodes along the edges shown)

27

K3,3K5

Page 28: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Four-Color Theorem:Every planar graph is 4-colorable.

(Appel & Haken, 1976)

28

Page 29: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Another 4-colored planar graph

29

http://www.cs.cmu.edu/~bryant/boolean/maps.html

Page 30: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Bipartite Graphs

• A directed or undirected graph is bipartite if the vertices can be partitioned into two sets such that all edges go between the two sets

• The following are equivalent– G is bipartite– G is 2-colorable– G has no cycles of odd length

30

Page 31: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Traveling Salesperson

• Find a path of minimum distance that visits every city

31

Amsterdam

Rome

Boston

Atlanta

London

Paris

Copenhagen

Munich

Ithaca

New York

Washington

1202

1380

1214

1322

1356

1002

512

216

441

189160

15561323

419

210

224 132

660 505

1078

Page 32: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Representations of Graphs

32

2 3

2 4

3

1

2

3

4

0 1 0 1

0 0 1 0

0 0 0 0

0 1 1 0

1 2 3 4

1

2

3

4

Adjacency List Adjacency Matrix

1 2

34

Page 33: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Adjacency Matrix or Adjacency List?

• Definitions– n = number of vertices– m = number of edges– d(u) = degree of u = number of edges leaving u

• Adjacency Matrix– Uses space O(n2)– Can iterate over all edges in time O(n2)– Can answer “Is there an edge from u to v?” in O(1) time– Better for dense graphs (lots of edges)

• Adjacency List– Uses space O(m+n)– Can iterate over all edges in time O(m+n)– Can answer “Is there an edge from u to v?” in O(d(u)) time– Better for sparse graphs (fewer edges)

33

Page 34: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Graph Algorithms

• Search– depth-first search– breadth-first search

• Shortest paths– Dijkstra's algorithm

• Minimum spanning trees– Prim's algorithm– Kruskal's algorithm

34

Page 35: CS/ENGRD 2110 Object-Oriented Programming and Data Structures Fall 2014 Doug James Lecture 17: Graphs

Readings

• Chapter 28 Graphs• Chapter 29 Graph Implementations

35