Top Banner
Graphs Lê Sỹ Vinh Computer science department Email: [email protected]
46
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture8_9Graphs2

Graphs

Lê Sỹ Vinh

Computer science department

Email: [email protected]

Page 2: Lecture8_9Graphs2

2

Graphs• A graph is a pair (V, E), where

– V is a set of nodes, called vertices– 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

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

Page 3: Lecture8_9Graphs2

3

Edge Types• Directed 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 PVD flight AA 1206

ORD PVD849miles

Page 4: Lecture8_9Graphs2

4

Breadth-First Search

• Breadth-first search (BFS) is a general technique for traversing a graph

• A BFS traversal of a graph G – Visits all the vertices and

edges of G– Determines whether G is

connected– Computes the connected

components of G– Computes a spanning forest

of G

• BFS on a graph with n vertices and m edges takes O(n + m ) time

• BFS can be further extended to solve other graph problems– Find and report a path with

the minimum number of edges between two given vertices

– Find a simple cycle, if there is one

Page 5: Lecture8_9Graphs2

5

Depth-First Search

• Depth-first search (DFS) is a general technique for traversing a graph

• A DFS traversal of a graph G – Visits all the vertices and

edges of G– Determines whether G is

connected– Computes the connected

components of G– Computes a spanning forest

of G

• DFS on a graph with n vertices and m edges takes O(n + m ) time

• DFS can be further extended to solve other graph problems– Find and report a path

between two given vertices– Find a cycle in the graph

• Depth-first search is to graphs what Euler tour is to binary trees

Page 6: Lecture8_9Graphs2

6

write c.s. program

play

Topological Sorting

• Number vertices, so that (u,v) in E implies u < vwake up

eat

nap

study computer sci.

more c.s.

work out

sleep

dream about graphs

A typical student day1

2 3

4 5

6

7

8

9

1011

make cookies for professors

Page 7: Lecture8_9Graphs2

7

• Running time: O(n + m). How…?

Algorithm for Topological Sorting

Method TopologicalSort(G) H G // Temporary copy of G n G.numVertices() while H is not empty do

Let v be a vertex with no outgoing edgesLabel v nn n - 1Remove v from H

Page 8: Lecture8_9Graphs2

8

Topological Sorting Algorithm using DFS

• Simulate the algorithm by using depth-first search

• O(n+m) time.

Algorithm topologicalDFS(G, v)Input graph G and a start

vertex v of G Output labeling of the vertices

of G in the connected

component of v setLabel(v, VISITED)

for all e G.incidentEdges(v)if getLabel(e) =

UNEXPLOREDw opposite(v,e)if getLabel(w) =

UNEXPLORED

setLabel(e, DISCOVERY)

topologicalDFS(G, w)Label v with topological number n n n - 1

Algorithm topologicalDFS(G)Input dag GOutput topological ordering

of G n G.numVertices()

for all u G.vertices() setLabel(u,

UNEXPLORED)for all e G.edges()

setLabel(e, UNEXPLORED)for all v G.vertices()

if getLabel(v) = UNEXPLORED

topologicalDFS(G, v)

Page 9: Lecture8_9Graphs2

9

Topological Sorting Example

Page 10: Lecture8_9Graphs2

10

Topological Sorting Example

9

Page 11: Lecture8_9Graphs2

11

Topological Sorting Example

8

9

Page 12: Lecture8_9Graphs2

12

Topological Sorting Example

7

8

9

Page 13: Lecture8_9Graphs2

13

Topological Sorting Example

7

8

6

9

Page 14: Lecture8_9Graphs2

14

Topological Sorting Example

7

8

56

9

Page 15: Lecture8_9Graphs2

15

Topological Sorting Example

7

4

8

56

9

Page 16: Lecture8_9Graphs2

16

Topological Sorting Example

7

4

8

56

3

9

Page 17: Lecture8_9Graphs2

17

Topological Sorting Example

2

7

4

8

56

3

9

Page 18: Lecture8_9Graphs2

18

Topological Sorting Example

2

7

4

8

56

1

3

9

Page 19: Lecture8_9Graphs2

Shortest Paths

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Page 20: Lecture8_9Graphs2

20

Weighted Graphs

• In a weighted graph, each edge has an associated numerical value, called the weight of the edge

• Edge 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

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

Page 21: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 21

Shortest Paths• Given 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

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

1205

Dec 2008

Page 22: Lecture8_9Graphs2

Dijkstra Algorithm

Given G=(V, E), where weight (u,v) > 0 is the weight of edge (u, v) E. Find ∈the shortest path from s to e.

General idea

- d[v]: the shortest distance from s to v.

- pre[v] = u: the previous vertex of v on the shortest path from s to v.

Relaxation (s, u, v):

If d [u] + weight (u, v) < d [v]

d [v] = d [u] + weight (u, v)

pre[v] = u

s → … → u → v

d(z) = 75

d(u) = 5010

zsu

d(z) = 60

d(u) = 5010

zsu

e

e

Page 23: Lecture8_9Graphs2

Dijkstra algorithm

Known = {the set of vertices which the shortest paths are known}

Unknown = {the set of vertices which the shortest paths are unknown}

Repeat

Find u ∈ unknown with the smallest d[u] if u = e then

done

else {

known = known {u}

unknown = unknown – {u}

for each edge (u, v):

relaxation (s, u, v)

}

Page 24: Lecture8_9Graphs2

Dijkstra Algorithm {Known = {s};Unknown = {E} – {s}for v ∈ unknown {

d[v] = weight [s, v];pre[v] = s;

}u = s; // at the first stepwhile (u != e ){

Select u such that d[u] = min {d[x] | x unknown}∈Known = Known {u}Unknown = Unknown – {u}for v ∈ Unknown

if d[u] + weight [u, v] < d[v] {//relax (s, u, v)d[v] = d[u] + weight[u,v];pre[v] = u;

}}path = {e}while (e != s) {

e = pre[e]path = {e} path

}}

Page 25: Lecture8_9Graphs2

25

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 26: Lecture8_9Graphs2

26

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 27: Lecture8_9Graphs2

27

Why It Doesn’t Work for Negative-Weight Edges

CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8

C’s true distance is 1, but it is already in the cloud with d(C)=5!

Page 28: Lecture8_9Graphs2

28

Bellman-Ford Algorithm• Works even with negative-

weight edges• Must assume directed edges

(for otherwise we would have negative-weight cycles)

• Iteration i finds all shortest paths that use i edges.

• Running time: O(nm).• Can be extended to detect a

negative-weight cycle if it exists – How?

Algorithm BellmanFord(G, s)for all v G.vertices()

if v = ssetDistance(v, 0)

else setDistance(v, )

for i 1 to n-1 dofor each e G.edges()

{ relax edge e }u G.origin(e)z G.opposite(u,e)r getDistance(u) +

weight(e)if r < getDistance(z)

setDistance(z,r)

Page 29: Lecture8_9Graphs2

29

-2

Bellman-Ford Example

0

48

7 1

-2 5

-2

3 9

0

48

7 1

-2 53 9

Nodes are labeled with their d(v) values

-2

-28

0

4

48

7 1

-2 53 9

8 -2 4

-15

61

9

-25

0

1

-1

9

48

7 1

-2 5

-2

3 94

Page 30: Lecture8_9Graphs2

Minimum Spanning Trees

Page 31: Lecture8_9Graphs2

31

Minimum Spanning TreesSpanning subgraph

– Subgraph of a graph G containing all the vertices of G

Spanning tree– Spanning subgraph that is itself a

(free) tree

Minimum spanning tree (MST)– Spanning tree of a weighted

graph with minimum total edge weight

• Applications– Communications networks– Transportation networks

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 32: Lecture8_9Graphs2

32

Cycle Property

Cycle Property:– Let T be a minimum

spanning tree of a weighted graph G

– Let e be an edge of G that is not in T and let C be the cycle formed by e with T

– For every edge f of C, weight(f) weight(e)

Proof:– By contradiction– If weight(f) > weight(e) we

can get a spanning tree of smaller weight by replacing e with f

84

2 36

7

7

9

8e

C

f

84

2 36

7

7

9

8

C

e

f

Replacing f with e yieldsa better spanning tree

Page 33: Lecture8_9Graphs2

33

U V

Partition PropertyPartition Property:

– Consider a partition of the vertices of G into subsets U and V

– Let e be an edge of minimum weight across the partition

– There is a minimum spanning tree of G containing edge e

Proof:– Let T be an MST of G– If T does not contain e, consider the cycle C

formed by e with T and let f be an edge of C across the partition

– By the cycle property,weight(f) weight(e)

– Thus, weight(f) = weight(e)– We obtain another MST by replacing f with e

74

2 85

7

3

9

8 e

f

74

2 85

7

3

9

8 e

f

Replacing f with e yieldsanother MST

U V

Page 34: Lecture8_9Graphs2

34

Kruskal’s AlgorithmA priority queue stores the edges outside the cloud Key: weight Element: edge

At the end of the algorithm We are left with one cloud

that encompasses the MST A tree T which is our MST

Algorithm KruskalMST(G)for each vertex V in G do

define a Cloud(v) of {v}

let Q be a priority queue.Insert all edges into Q using their weights as the keyT while T has fewer than n-1 edges

do edge e = T.removeMin()

Let u, v be the endpoints of e

if Cloud(v) Cloud(u) then

Add edge e to TMerge Cloud(v)

and Cloud(u)return T

Page 35: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 35

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Dec 2008

Page 36: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 36

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 37: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 37

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 38: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 38

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 39: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 39

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 40: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 40

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 41: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 41

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 42: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 42

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 43: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 43

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 44: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 44

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 45: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 45

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008

Page 46: Lecture8_9Graphs2

Phạm Bảo Sơn - DSA 46

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Dec 2008