Graph A graph G is a set V(G) of vertices (nodes) and a set E(G) of edges which are pairs of...

Preview:

Citation preview

GraphA graph G is a set V(G) of vertices (nodes) and a set E(G) of edges which are pairs of vertices.

a b c d

e

i

f g h

j k l

V = { a, b, c, d, e, f, g, h, i, j, k, l }E = { (a, b), (a, e), (b, e), (b, f), (c, j), (c, g), (c, h), (d, h), (e, j), (g, k), (g, l), (g, h), (i, j) }

A Directed Graph In a directed graph (digraph), edges are ordered pairs.

TW 45

UA

120

AA 49

AA 411

AA 523

AA

1387

DL

335

UA

877

AA 903

DL 247

NW 35

From Goodrich and Tamassia (1998)

SFO

ORDBOS

JFK

LAX DFW

MIA

Applications of Graphs

Graphs describe relationships

The Internet

Streets / Highways (Roadmaps)

Molecules

Social Networks

Geometric Surfaces (CAD)

Circuits

Parts in an Assembly

… JohnYoko Ringo

George

Paul

Linda

Flow Charts

More General GraphsA multipgraph allows multiple edges between two vertices.

a

b d f

c

A pseudograph is a multigraph that allows self-loops (edges from a vertex to itself).

1

54

2 3

6

Edges & Degrees

u

w

v

e

e1

2

endpoint

incident on u and v

degree(u) = 2

deg(w) = 1

b

a

d

e

c

destination

source

in-degree(b) = 3out-degree(b) = 4

u and v are adjacent

Handshaking Theorem

Let G = (V, E) be an undirected simple graph.

∑ deg(v) = 2 | E |

v V

If G is directed:

∑∑v Vv V

indeg(v) = outdeg(v) = | E |

| E | ≤ | V | · (| V | – 1) / 2

K has ( ) = 6 edges442

| E | ≤ | V | · (| V | – 1)

Path

A path of length k is a sequence v , v , …, v of vertices such that (v , v ) for i = 0, 1, …, k – 1 is an edge of G.

0 1 k

i i+1

g

a

e

j

n

b

f

k

dc

o

h

l

p

m

q

Non-simple path:

a, b, e, f, g, b, g, l

Simple path:a, e, k, p, l, qm, h, d, c, g

(no repeatedvertices)

b, c, d not a path

Cycle

a

e

j

n

b

f

k

dc

o

g h

l

p

m

q

A cycle is a path that starts and ends at the same vertex.

A simple cycle has no repeated vertices.

k, j, n, k, p, o,k is not simple.

Subgraph

a

e

j

n

b

f

k

dc

o

g h

l

p

m

q

A subgraph H of Gis a graph; its edges and vertices are subsets of those of G.

V(H) = {b, d, e, f, g, h, l, p, q} E(H) = {(b, e), (b, g), (e, f), (d, h), (l, p), (l, q)}

ConnectivityG is connected if there is a path between every pair of vertices.

a

b

d

fe

c

If G is not connected, the maximal connected subgraphs are the connected components of G.

e f ga

cb

dC

CC

1

32

Strong & Weak Connectivity

A directed graph is strongly connected if every two vertices are reachable from each other.

a

f

e

cd

b

It is weakly connected if the underlying undirected graph is connected.

f

e

a

d

c

b

Property of Connectivity

Let G = (V, E) be an undirected graph.

If G is connected, then | E | ≥ | V | – 1.

If G is a tree, then | E | = | V | – 1.

If G is a forest, then | E | ≤ | V | – 1.

Adjacency Lists

2

1 3

5 4

2 3 5

1 3

1 2 4 5

3 5

1 3 4

If G is directed, the total length of all the adjacency lists is | E |.

1

2

3

4

5

Adj

If G is undirected, the total length is 2 | E |.

Space requirement: (|V| + |E|). Preferable representation.

Adjacency Matrix

2

1 3

5 4

ijA = (a )

a = ij

1 if (i, j) E(G)

0 otherwise

1 0 1 1 0 12 1 0 1 0 03 1 1 0 1 14 0 0 1 0 15 1 0 1 1 0

1 2 3 4 5

Space: (|V| ). 2

Preferred when the graph is small or dense.

Operation Time

Operation Adjacency List Adjacency Matrix

Scan incident edges (deg(v)) (|V|)

Test adjacency of u and v

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

Scan outgoing edges (outdeg(v)) (|V|)

Scan incoming edges (indeg(v)) (|V|)

Lecture notes modified over Dr. Fernandez-Baca’s

Space (|V|+|E|) (|V| )2

Recommended