1 graphs ord dfw sfo lax 802 1743 1843 1233 337 many slides taken from goodrich, tamassia 2004

23
1 Graphs ORD DFW SFO LAX 8 0 2 1743 1843 1233 3 3 7 Many slides taken from Goodrich, Tamassia 2004

Post on 20-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

1

Graphs

ORD

DFW

SFO

LAX

802

1743

1843

1233

337

Many slides taken from Goodrich, Tamassia 2004

Page 2: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

2© 2004 Goodrich, Tamassia

GraphsA graph is a pair (V, E), where

V is a set of nodes, called vertices (node=vertex) E is a collection of pairs of vertices, called edges Vertices and edges are positions and store elements

Example: A vertex represents an airport and stores the three-letter airport

code An edge represents a flight route between two airports and stores

the mileage of the route

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Page 3: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

3© 2004 Goodrich, Tamassia

Edge TypesDirected edge

ordered pair of vertices (u,v) first vertex u is the origin second vertex v is the

destination e.g., a flight

Undirected edge unordered pair of vertices (u,v) e.g., a flight route

Directed graph all the edges are directed e.g., route network

Undirected graph all the edges are undirected e.g., flight network

ORD PVDflight

AA 1206

ORD PVD849

miles

Page 4: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

4© 2004 Goodrich, Tamassia

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.netqwest.net

math.brown.edu

cslab1bcslab1a

ApplicationsElectronic circuits

Printed circuit board Integrated circuit

Transportation networks Highway network Flight network

Computer networks Local area network Internet Web

Databases Entity-relationship

diagram

Page 5: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

5© 2004 Goodrich, Tamassia

TerminologyEnd vertices (or endpoints) of an edge

U and V are the endpoints of a

Edges incident on a vertex a, d, and b are incident on

VAdjacent vertices

U and V are adjacentDegree of a vertex

X has degree 5 Parallel edges

h and i are parallel edgesSelf-loop

j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 6: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

6© 2004 Goodrich, Tamassia

P1

Terminology (cont.)Path

sequence of alternating vertices and edges

begins with a vertex ends with a vertex each edge is preceded and

followed by its endpoints

Simple path path such that all its vertices

and edges are distinct

Examples P1=(V,b,X,h,Z) is a simple

path P2=(U,c,W,e,X,g,Y,f,W,d,V) is

a path that is not simple

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 7: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

7© 2004 Goodrich, Tamassia

Terminology (cont.)Cycle

circular sequence of alternating vertices and edges

each edge is preceded and followed by its endpoints

Simple cycle cycle such that all its vertices

and edges are distinct

Examples C1=(V,b,X,g,Y,f,W,c,U,a,) is a

simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,)

is a cycle that is not simple

C1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hC2

Page 8: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

8© 2004 Goodrich, Tamassia

PropertiesNotation

n number of vertices m number of edgesdeg(v) degree of vertex v

Property 1

v deg(v) 2m

Proof: each edge is counted twice

Property 2In an undirected graph

with no self-loops and no multiple edges

m n (n 1)2Proof: each vertex has

degree at most (n 1)

What is the bound for a directed graph?

Example n 4 m 6 deg(v) 3

Page 9: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

9© 2004 Goodrich, Tamassia

Main Methods of the Graph ADT

Vertices and edges are positions store elements

Accessor methods endVertices(e): an array

of the two endvertices of e

opposite(v, e): the vertex opposite of v on e

areAdjacent(v, w): true iff v and w are adjacent

replace(v, x): replace element at vertex v with x

replace(e, x): replace element at edge e with x

Update methods insertVertex(o): insert a

vertex storing element o insertEdge(v, w, o):

insert an edge (v,w) storing element o

removeVertex(v): remove vertex v (and its incident edges)

removeEdge(e): remove edge e

Iterator methods incidentEdges(v): edges

incident to v vertices(): all vertices in

the graph edges(): all edges in the

graph

Page 10: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

10© 2004 Goodrich, Tamassia

Implementation of Graphs

Vertex adjacency listsAdjacency matrix

Page 11: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

11© 2004 Goodrich, Tamassia

Example problem

Store the airport flight graphGiven an airport, output all airports that have direct flightsGiven an airport, output all airports that have flights with at most 1 connectionGiven two airports, find minimum number of hops needed to connect them

Page 12: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

12

Shortest Paths

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Page 13: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

13© 2004 Goodrich, Tamassia

Weighted GraphsIn a weighted graph, each edge has an associated numerical value, called the weight of the edgeEdge weights may represent, distances, costs, etc.Example:

In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

12

05

Page 14: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

14© 2004 Goodrich, Tamassia

Shortest PathsGiven a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v.

Length of a path is the sum of the weights of its edges.

Example: Shortest path between Providence and Honolulu

Applications Internet packet routing Flight reservations Driving directions

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

12

05

Page 15: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

15© 2004 Goodrich, Tamassia

Shortest Path PropertiesProperty 1:

A subpath of a shortest path is itself a shortest pathProperty 2:

There is a tree of shortest paths from a start vertex to all the other vertices

Example:Tree of shortest paths from Providence

ORD PVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

12

05

Page 16: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

16© 2004 Goodrich, Tamassia

Dijkstra’s AlgorithmThe distance of a vertex v from a vertex s is the length of a shortest path between s and vDijkstra’s algorithm computes the distances of all the vertices from a given start vertex sAssumptions:

the graph is connected the edges are

undirected the edge weights are

nonnegative

We grow a “cloud” of vertices, beginning with s and eventually covering all the verticesWe store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent verticesAt each step

We add to the cloud the vertex u outside the cloud with the smallest distance label, d(u)

We update the labels of the vertices adjacent to u

Page 17: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

17© 2004 Goodrich, Tamassia

Edge RelaxationConsider an edge e (u,z) such that

u is the vertex most recently added to the cloud

z is not in the cloud

The relaxation of edge e updates distance d(z) as follows:d(z) min{d(z),d(u) weight(e)}

d(z) 75

d(u) 5010

zsu

d(z) 60

d(u) 5010

zsu

e

e

Page 18: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

18© 2004 Goodrich, Tamassia

Example

CB

A

E

D

F

0

428

48

7 1

2 5

2

3 9

CB

A

E

D

F

0

328

5 11

48

7 1

2 5

2

3 9

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

Page 19: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

19© 2004 Goodrich, Tamassia

Example (cont.)

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

Page 20: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

20© 2004 Goodrich, Tamassia

Dijkstra’s Algorithm

A priority queue stores the vertices outside the cloud: Key: distance, Element: vertex

nnn

nnlogn (priority heap queue)nnlogn (priority heap queue)Sumn(deg(n))=2m

2m2m2m2mlogn (priority heap queue)

Algorithm DijkstraDistances(G, s)Q new heap-based priority queuefor all v G.vertices()

if v ssetDistance(v, 0)

else setDistance(v, )

Q.insert(getDistance(v), v)while Q.isEmpty()

u Q.removeMin() for all e= (z,u)

G.incidentEdges(u)/* relax edge e */r getDistance(u) weight(e)if r getDistance(z)

setDistance(z,r) Recompute position of z in Q

O(mlogn)

Page 21: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

21© 2004 Goodrich, Tamassia

Shortest Paths TreeUsing the template method pattern, we can extend Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all other verticesWe store with each vertex a third label:

parent edge in the shortest path tree

In the edge relaxation step, we update the parent label

Algorithm DijkstraShortestPathsTree(G, s)

for all v G.vertices()…

setParent(v, )…

for all e G.incidentEdges(u){ relax edge e }z G.opposite(u,e)r getDistance(u) weight(e)if r getDistance(z)

setDistance(z,r)setParent(z,e) Q.replaceKey(getLocator(z),r)

Page 22: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

22© 2004 Goodrich, Tamassia

Why It Doesn’t Work for Negative-Weight Edges

If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.

CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8

Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.

C’s true distance is 1, but it is already in the

cloud with d(C)=5!

Page 23: 1 Graphs ORD DFW SFO LAX 802 1743 1843 1233 337 Many slides taken from Goodrich, Tamassia 2004

23© 2004 Goodrich, Tamassia

Other Graph issues

Traversal depth-first Breadth-first

Very hard (NP-complete) problems on graphs Coloring

Given a graph, can we assign one of three colors (say red, blue, or green) to each vertex, such that no adjacent vertices have the same color?

Traveling salesman (Hamiltonian cycle) path that travels through every vertex once, and

winds up where it started