Graphs

43 7
802

SFO
337

1843

ORD

LAX

1 1233

DFW

© 2004 Goodrich, Tamassia

Graphs

1

Graphs (§ 12.1)
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 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 849 PVD

Example:

802

SFO
337

1843

ORD

14

2

HNL

2555

3 74 1
1233

LAX

8 13

7

LGA

10

DFW

1120

99

MIA
2

© 2004 Goodrich, Tamassia

Graphs

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 unordered pair of vertices (u,v) e.g., a flight route all the edges are directed e.g., route network all the edges are undirected e.g., flight network
Graphs

flight ORD AA 1206 PVD 849 miles

Undirected edge
 

ORD

PVD

Directed graph
 

Undirected graph
 

© 2004 Goodrich, Tamassia

3

Applications
cslab1a cslab1b math.brown.edu

Electronic circuits
 

Printed circuit board Integrated circuit

Transportation networks
 

cs.brown.edu

brown.edu qwest.net att.net

Highway network Flight network Local area network Internet Web

Computer networks
  

cox.net John Paul David

Databases
Entity-relationship © 2004 Goodrich, Tamassia

Graphs

4

Terminology
End vertices (or endpoints) of an edge

U and V are the endpoints of a a, d, and b are incident on V U and V are adjacent X has degree 5 h and i are parallel edges j is a self-loop
Graphs

a U c

V d

b X e

Edges incident on a vertex

h Z i g

j

Adjacent vertices

Degree of a vertex

W f

Parallel edges

Y

Self-loop

© 2004 Goodrich, Tamassia

5

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 path such that all its vertices and edges are distinct 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
Graphs

a U c

V d P2 W f

b

P1 X g Y

Simple path

e

h

Z

Examples

© 2004 Goodrich, Tamassia

6

Terminology (cont.)
Cycle

circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints cycle such that all its vertices and edges are distinct

a U c

V d C2 W f

b X e h C1 g Y Z

Simple cycle

Examples
C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a simple cycle  C =(U,c,W,e,X,g,Y,f,W,d,V,a, 2 ↵) is a cycle that is not simple Graphs © 2004 Goodrich, Tamassia

7

Properties
Property 1

Σ v deg(v) = 2m
Proof: each edge is counted twice

Notation
n m deg(v) number of vertices number of edges degree of vertex vExample
  

Property 2
In an undirected graph with no self-loops and no multiple edges m ≤ n (n − 1)/2 Proof: each vertex has degree at most (n − 1)

n= 4 m= 6 deg(v) = 3

What is the bound for © 2004 Goodrich, Tamassia graph? a directed

Graphs

8

Main Methods of the Graph ADT
Vertices and edges
 

Update methods
 

are positions store elements 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
Graphs

Accessor 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 incidentEdges(v): edges incident to v vertices(): all vertices in the graph edges(): all edges in the graph
9

Iterator methods
 

© 2004 Goodrich, Tamassia

Structure (§ 12.2.1)
Vertex object
 

u a v b c w d z

element reference to position in vertex sequence element origin vertex object destination vertex object reference to position in edge sequence sequence of vertex objects

Edge object
  

u

v

w

z

Vertex sequence

a

b

c

d

Edge sequence
sequence of edge © 2004 Goodrich,objects Tamassia

Graphs

10

Adjacency List Structure (§ 12.2.2)
Edge list structure Incidence sequence for each vertex

a u

v

b w

sequence of references to edge objects of incident edges

u

v

w

Augmented edge objects

references to associated positions in incidence sequences of end vertices
Graphs

a

b

© 2004 Goodrich, Tamassia

11

Adjacency Matrix Structure (§ 12.2.3)
Edge list structure Augmented vertex objects

a u

v

b w

Integer key (index) associated with vertex 0 u 1 0 0 1 a 2 ∅ ∅ ∅ ∅ b v 1 2 ∅ 2 w

2D-array adjacency array

Reference to edge object for adjacent vertices Null for non nonadjacent vertices

The “old fashioned” version just has 0 for no edge and 1 for © 2004 Goodrich, Tamassia edge

Graphs

12

Asymptotic Performance
n vertices, m edges no parallel edges no self-loops Bounds are “bigOh”

E dge List n+m m m 1 1 m 1
Graphs

Adjacency List n+m deg(v) min(deg(v), deg(w)) 1 1 deg(v) 1

Adjacenc y Matrix n2 n 1 n2 1 n2 1
13

Space

incidentEdges(v) areAdjacent (v, w) insertVertex(o) insertEdge(v, w, o) removeVertex(v ) removeEdge(e)
© 2004 Goodrich, Tamassia

Depth-First Search
A B C D E

© 2004 Goodrich, Tamassia

Graphs

14

Subgraphs
A subgraph S of a graph G is a graph such that

The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G

Subgraph

A spanning subgraph of G is a subgraph that contains all the vertices of G
© 2004 Goodrich, Tamassia Graphs

Spanning subgraph
15

Connectivity
A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G
© 2004 Goodrich, Tamassia Graphs

Connected graph

Non connected graph with two connected components
16

Trees and Forests
A (free) tree is an undirected graph T such that
T is connected  T has no cycles This definition of tree is different from the one of a rooted tree

Tree

A forest is an undirected graph without cycles The connected components of a forest are trees
© 2004 Goodrich, Tamassia Graphs

Forest
17

Spanning Trees and Forests
A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest
© 2004 Goodrich, Tamassia Graphs

Graph

Spanning tree
18

Depth-First Search (§ 12.3.1)
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

© 2004 Goodrich, Tamassia

Graphs

Depth-first search is to graphs what Euler 19

DFS Algorithm
The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges 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 DFS(G, v)
© 2004 Goodrich, Tamassia

Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK)
20

Graphs

Example
A A

unexplored vertex visited vertex unexplored edge discovery edge back edge
A

A B C D E

A D E B C
Graphs 21

B C
© 2004 Goodrich, Tamassia

D

E

Example (cont.)
A B C D E B C A D E

A B C
© 2004 Goodrich, Tamassia Graphs

A D E B C
22

D

E

Properties of DFS
Property 1
DFS(G, v) visits all the vertices and edges in the connected component of v
A

Property 2
The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v
© 2004 Goodrich, Tamassia

B

D

E

C

Graphs

23

Analysis of DFS
Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice
 

once as UNEXPLORED once as VISITED once as UNEXPLORED once as DISCOVERY or BACK

Each edge is labeled twice
 

Method incidentEdges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure

Recall that

Σ v deg(v) = 2m

© 2004 Goodrich, Tamassia

Graphs

24

Path Finding
We can specialize the Algorithm pathDFS(G, v, z) DFS algorithm to find a setLabel(v, VISITED) path between two S.push(v) given vertices u and z if v = z using the template return S.elements() method pattern for all e ∈ G.incidentEdges(v) We call DFS(G, u) with u if getLabel(e) = UNEXPLORED as the start vertex w ← opposite(v,e) We use a stack S to if getLabel(w) = UNEXPLORED keep track of the path setLabel(e, DISCOVERY) between the start S.push(e) vertex and the current pathDFS(G, w, z) vertex S.pop(e) As soon as destination else vertex z is setLabel(e, BACK) encountered, we S.pop(v) return the path as the Graphs 25 © 2004 Goodrich, Tamassia contents of the stack

Cycle Finding
We can specialize the DFS algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the © 2004 Goodrich, Tamassia stack from the top to
Algorithm cycleDFS(G, v, z) setLabel(v, VISITED) S.push(v) for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) S.push(e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) pathDFS(G, w, z) S.pop(e) else T ← new empty stack repeat o ← S.pop() T.push(o) until o = w return T.elements() S.pop(v)
Graphs 26

Breadth-First Search
L0 L1
A C E F D

B

L2

© 2004 Goodrich, Tamassia

Graphs

27

Breadth-First Search (§ 12.3.3)
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

© 2004 Goodrich, Tamassia

Graphs

Find and report a path with the minimum number of edges between two given vertices Find a simple cycle,28 if

BFS Algorithm
The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G 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 BFS(G, v)
© 2004 Goodrich, Tamassia

Graphs

Algorithm BFS(G, s) L0 ← new empty sequence L0.insertLast(s) setLabel(s, VISITED) i←0 while ¬Li.isEmpty() Li +1 ← new empty sequence for all v ∈ Li.elements() for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.insertLast(w) else setLabel(e, CROSS) i ← i +1
29

Example
A A

L0 L1

unexplored vertex visited vertex unexplored edge discovery edge cross edge
L0
A C E F
Graphs

A C E F D

B

L0 L1

A C E F
30

L1

B

D

B

D

© 2004 Goodrich, Tamassia

Example (cont.)
L0 L1
A C E F D

L0 L1

A C E F D

B

B

L2

L0 L1

A C E F
Graphs

L0 L1

A C E F
31

B

D

B

D

L2

L2

© 2004 Goodrich, Tamassia

Example (cont.)
L0 L1
A C E F D

L0 L1

A C E F D

B

B

L2

L2

L0 L1

A C E F
Graphs 32

B

D

L2

© 2004 Goodrich, Tamassia

Properties
Notation
Gs: connected component of s
A B E C F D

Property 1
BFS(G, s) visits all the vertices and edges of Gs

Property 2
The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs L0

A C E F
33

Property 3
For each vertex v in Li

L1

B

D

© 2004 Goodrich, Tamassia

The path of Ts from s to v has i edges Every path from s to v in Gs has at least i edges
Graphs

L2

Analysis
Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice
 

once as UNEXPLORED once as VISITED once as UNEXPLORED once as DISCOVERY or CROSS

Each edge is labeled twice
 

Each vertex is inserted once into a sequence Li Method incidentEdges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure
© 2004 Goodrich, Tamassia 

Recall that

Σ

deg(v) = 2m

Graphs

34

Applications
Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time
  

Compute the connected components of G Compute a spanning forest of G Find a simple cycle in G, or report that G is a forest Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists
Graphs 35

© 2004 Goodrich, Tamassia

DFS vs. BFS
Applications
Spanning forest, connected components, paths, cycles Shortest paths Biconnected components
A B E C F D

DFS BFS
√ √ √ √

L0 L1

A C E F D

B

L2

DFS
© 2004 Goodrich, Tamassia Graphs

BFS
36

DFS vs. BFS (cont.)
Back edge (v,w)

Cross edge (v,w)

w is an ancestor of v in the tree of discovery edges

w is in the same level as v or in the next level in the tree of discovery edges
A C E F D

A B E C F D

L0 L1

B

L2

DFS
© 2004 Goodrich, Tamassia Graphs

BFS
37

Directed Graphs
ORD JFK SFO

BOS

LAX

DFW

MIA

© 2004 Goodrich, Tamassia

Graphs

38

Digraphs (§ 12.4)
A digraph is a graph whose edges are all directed

E D C B A

Short for “directed graph”

Applications
  

one-way streets flights task scheduling
Graphs

© 2004 Goodrich, Tamassia

39

E

Digraph Properties
C

D

A graph G=(V,E) such that

B A

Each edge goes in one direction:

 Edge (a,b) goes from a to b, but not b to a.

If G is simple, m < n*(n-1). If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size.
© 2004 Goodrich, Tamassia Graphs 40

Digraph Application
Scheduling: edge (a,b) means task a must be completed before b can be started
ics21 ics22 ics23

ics51

ics53

ics52 ics161

ics131

ics141

ics121

ics171

ics151
© 2004 Goodrich, Tamassia Graphs

The good life

41

Directed DFS
We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges
   

E D C B A

discovery edges back edges forward edges cross edges

A directed DFS starting a a vertex s determines the vertices reachable from s
© 2004 Goodrich, Tamassia Graphs

42

Reachability
DFS tree rooted at v: vertices reachable from v via directed paths
E D C

E C A

D F B
A A E

D C B
43

F

© 2004 Goodrich, Tamassia

Graphs

Strong Connectivity
Each vertex can reach all other vertices
a c d e f
© 2004 Goodrich, Tamassia Graphs

g

b
44

Connectivity Algorithm
Pick a vertex v in G. Perform a DFS from v in G.

G:

a c d g

If there’s a w not visited, print “no”.
f

Let G’ be G with edges reversed. Perform a DFS from v in G’.

e b

If there’s a w not visited, print “no”. Else, print “yes”.

a

G’:
d

c

g

e b

f

© 2004 Goodrich, Tamassia

Running time: O(n+m). Graphs

45

Strongly Connected Components
Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). a g {a,c,g} c d f
© 2004 Goodrich, Tamassia Graphs

e b

{f,d,e,b}
46

Transitive Closure
Given a digraph G, the transitive closure of G is the digraph G* such that  G* has the same vertices as G  if G has a directed path from u to v (u ≠ v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph
© 2004 Goodrich, Tamassia Graphs

D B C A D B C A

E G

E

G*
47

Computing the Transitive Closure
We can perform DFS starting at each vertex

If there's a way to get from A to B and from B to C, then there's a way to get from A to C.

O(n(n+m))

Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm
© 2004 Goodrich, Tamassia Graphs 48

Floyd-Warshall Transitive Closure

Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, Uses only vertices numbered 1,…,k as intermediate vertices:
i Uses only vertices numbered 1,…,k-1 k
© 2004 Goodrich, Tamassia Graphs

(add this edge if it’s not already in)

j Uses only vertices numbered 1,…,k-1
49

Floyd-Warshall’s Algorithm
Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i←1 for all v ∈ G.vertices() denote v as vi  G =G 0 i←i+1  G has a directed edge (v , G0 ← G k i vj) if G has a directed path for k ← 1 to n do from vi to vj with Gk ← Gk − 1 intermediate vertices in for i ← 1 to n (i ≠ k) do the set {v1 , …, vk} for j ← 1 to n (j ≠ i, k) do if Gk − 1.areAdjacent(vi, vk) ∧ We have that Gn = G* Gk − 1.areAdjacent(vk, vj) In phase k, digraph Gk is if ¬ Gk.areAdjacent(vi, vj) computed from Gk − 1 Gk.insertDirectedEdge(vi, vj , k) 3 Running time: O(n ), return Gn Graphs 50

Floyd-Warshall’s algorithm numbers the vertices of G as v1 , …, vn and computes a series of digraphs G0, …, Gn

© 2004 Goodrich, Tamassia assuming areAdjacent

is

Floyd-Warshall Example
BOS ORD

v7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 51

Floyd-Warshall, Iteration 1 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 52

Floyd-Warshall, Iteration 2 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 53

Floyd-Warshall, Iteration 3 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 54

Floyd-Warshall, Iteration 4 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 55

Floyd-Warshall, Iteration 5 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 56

Floyd-Warshall, Iteration 6 v
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 57

Floyd-Warshall, Conclusionv
BOS ORD

7

v4
JFK

v2
SFO

v6

LAX

DFW

v1

v3
MIA

v5
© 2004 Goodrich, Tamassia Graphs 58

DAGs and Topological Ordering
A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v1 , …, vn of the vertices such that for every edge (vi , vj), we have i < j Example: in a task scheduling digraph, a topological ordering v2 a task sequence that satisfies the precedence constraints Theorem v1 A digraph admits a topological ordering if and only if it is a DAG Graphs

D B C A D B C A v4

E

DAG G E v5

v3 Topological ordering of G 59

© 2004 Goodrich, Tamassia

Topological Sorting
Number vertices, so that (u,v) in E implies u < v wake up 1 A typical student day
2 study computer sci. 4 7 play nap 8 write c.s. program eat 3 5 more c.s.

9 make cookies for professors sleep

6 work out

10 11 dream about graphs
Graphs 60

© 2004 Goodrich, Tamassia

Algorithm for Topological Sorting
Note: This algorithm is different than the one in Goodrich-Tamassia
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 edges Label v ← n n← n-1 Remove v from H

Running time: O(n + m). How…?
© 2004 Goodrich, Tamassia Graphs 61

Topological Sorting Algorithm using DFS
Simulate the algorithm by using depth-first search Algorithm topologicalDFS(G) Input dag G Output 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) O(n+m) time.
© 2004 Goodrich, Tamassia

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) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) topologicalDFS(G, w) else {e is a forward or cross edge} Label v with topological number n n←n-1
62

Graphs

Topological Sorting Example

© 2004 Goodrich, Tamassia

Graphs

63

Topological Sorting Example

9
© 2004 Goodrich, Tamassia Graphs 64

Topological Sorting Example

8 9
© 2004 Goodrich, Tamassia Graphs 65

Topological Sorting Example

7 8 9
© 2004 Goodrich, Tamassia Graphs 66

Topological Sorting Example

6 7 8 9
© 2004 Goodrich, Tamassia Graphs 67

Topological Sorting Example

6 7 8 9
© 2004 Goodrich, Tamassia Graphs

5

68

Topological Sorting Example

4 6 7 8 9
© 2004 Goodrich, Tamassia Graphs 69

5

Topological Sorting Example
3 4 6 7 8 9
© 2004 Goodrich, Tamassia Graphs 70

5

Topological Sorting Example
2 3 4 6 7 8 9
© 2004 Goodrich, Tamassia Graphs 71

5

Topological Sorting Example
2 3 4 6 7 8 9
© 2004 Goodrich, Tamassia Graphs 72

1

5

Shortest Paths
8 B 2 8 2 7 5 E 3 C A 0 4 1 8 F D 5 3 2 9

© 2004 Goodrich, Tamassia

Graphs

73

Weighted Graphs (§ 12.5)
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

802

SFO
337

1843

ORD
8 13 7

849

1

42

PVD
1205

HNL

2555

LAX

1 1233

43 7

LGA

10

DFW

1120

99

MIA
74

© 2004 Goodrich, Tamassia

Graphs

Shortest Paths (§ 12.6)
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. Shortest path between Providence and Honolulu Internet packet routing Flight reservations Driving directions 1843

Example:

Applications
  

802

SFO

ORD
8 13 7

849

14

2

PVD
1205

HNL

2555

3 74 1
1233

LGA

LAX

337

10

DFW

1120

99
75

MIA

© 2004 Goodrich, Tamassia

Graphs

Shortest Path Properties
Property 1:
A subpath of a shortest path is itself a shortest path

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

Example:
Tree of shortest paths from Providence

802

SFO
337

1843

ORD
8 13 7

849

14

2

PVD
1205

HNL

2555

3 74 1
1233

LGA

10

LAX

DFW

1120

99

MIA
76

© 2004 Goodrich, Tamassia

Graphs

Dijkstra’s Algorithm (§ 12.6.1)
The distance of a vertex v from a vertex s is the length of a shortest path between s and v Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s Assumptions:
the graph is connected  the edges are undirected © 2004 Goodrich, Tamassia  the edge weights are

We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices We 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 vertices At each step

Graphs

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

Edge Relaxation
Consider an edge e = (u,z) such that

d(u) = 50 s u e

u is the vertex most recently added to the cloud z is not in the cloud

10 d(z) = 75

z

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

d(u) = 50 u e

10 d(z) = 60

z

© 2004 Goodrich, Tamassia

Graphs

78

Example
8 B 2 8 ∞ E 2 7 3 C A 0 4 1 ∞ F D 5 4 B 2 8 8 2 7 5 E 3 C A 0 4 1 8 F D 5 3 2 9 2 9

8 B 2 8 2 7 5 E 3

A C

0

4 1 11 F D 5
Graphs

8 3 B 2 7 2 7 5 E 3

A C

0

4 1 8 F D 5
79

2 9

2 9

3

© 2004 Goodrich, Tamassia

Example (cont.)
8 B 2 7 5 E 2 7 3 C A 0 4 1 8 F D 5 8 B 2 7 5 E 2 7 3 C A 0 4 1 8 F D 5 3 3 2 9

2 9

© 2004 Goodrich, Tamassia

Graphs

80

Dijkstra’s Algorithm
A priority queue stores the vertices outside the cloud
 

Key: distance Element: vertex

Locator-based methods

insert(k,e) returns a locator replaceKey(l,k) changes the key of an item

We store two labels with each vertex:
 

© 2004 Goodrich, Tamassia

Distance (d(v) label) locator in priority queue

Algorithm DijkstraDistances(G, s) Q ← new heap-based priority queue for all v ∈ G.vertices() if v = s setDistance(v, 0) else setDistance(v, ∞) l ← Q.insert(getDistance(v), v) setLocator(v,l) while ¬Q.isEmpty() u ← Q.removeMin() 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) Q.replaceKey(getLocator(z),r)
Graphs 81

Analysis of Dijkstra’s Algorithm
Graph operations

Method incidentEdges is called once for each vertex We set/get the distance and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time

Label operations

Priority queue operations

Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure

Recall that

Σ v deg(v) = 2m
82

The running time can also be expressed as O(m log n) Graphs © 2004 Goodrich, Tamassia since the graph is connected

Shortest Paths Tree
Using the template method pattern, we can extend Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all other vertices We store with each vertex a third 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)
Graphs 83

parent edge in the shortest path tree

In the edge relaxation step, we update the parent label
© 2004 Goodrich, Tamassia

Why Dijkstra’s Algorithm Works
Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing didn’t find all Suppose it distance.
shortest distances. Let F be the first wrong vertex the algorithm processed.  When the previous node, D, on the true shortest path was considered, its distance was correct.  But the edge (D,F) was relaxed at that time!  Thus, so long as d(F)>d(D), F’s distance cannot be wrong. That is, there is no wrong Graphs © 2004 Goodrich, Tamassia vertex.
8 B 2 7 5 E 2 7 3 C A 0 4 1 8 F D 5 3

2 9

84

Why It Doesn’t Work for Negative-Weight Edges
Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.

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.

8 B 2 7 5 E 6 7 0

A C

0

4 1 9 F D 5 4

5 -8

C’s true distance is 1, but it is already in the cloud with d(C)=5!
© 2004 Goodrich, Tamassia Graphs 85

Bellman-Ford Algorithm (not in book)
Works even with Algorithm BellmanFord(G, s) negative-weight edges for all v ∈ G.vertices() if v = s Must assume directed setDistance(v, 0) edges (for otherwise we else would have negativesetDistance(v, ∞) weight cycles) for i ← 1 to n-1 do Iteration i finds all for each e ∈ G.edges() shortest paths that use i { relax edge e } edges. u ← G.origin(e) z ← G.opposite(u,e) Running time: O(nm). r ← getDistance(u) + weight(e) Can be extended to if r < getDistance(z) detect a negativesetDistance(z,r) weight cycle if it exists

How?

© 2004 Goodrich, Tamassia

Graphs

86

Bellman-Ford Example
Nodes are labeled with their d(v) values
8 -2 ∞ -2 7 3 ∞ 0 -2 5 8 1 -2 7 6 ∞ 3 -2 9 1 9 ∞ 4 5
Graphs

0 ∞ 9 ∞ 1

4 ∞ 5 8 ∞ -2

8 -2 7 3 ∞

0 ∞ -2 9 ∞ 0 -2 7 3 -2 9 1 4 9 1

4 ∞ 5 4

8

4 -1 5 -2

8

4 -1 5
87

1

© 2004 Goodrich, Tamassia

DAG-based Algorithm (not in book)
Works even with negative-weight edges Uses topological order Doesn’t use any fancy data structures Is much faster than Dijkstra’s algorithm Running time: O(n+m).
Algorithm DagDistances(G, s) for all v ∈ G.vertices() if v = s setDistance(v, 0) else setDistance(v, ∞) Perform a topological sort of the vertices for u ← 1 to n do {in topological order} for each e ∈ G.outEdges(u) { relax edge e } z ← G.opposite(u,e) r ← getDistance(u) + weight(e) if r < getDistance(z) setDistance(z,r)

© 2004 Goodrich, Tamassia

Graphs

88

DAG Example
1

Nodes are labeled with their d(v) values
1

8 -2
3

0 7
2

4 1 9 ∞ 5
5 4

8 ∞
3

0 -2 7 3
2

4 -2 9 ∞ 5
5

∞ -5

∞ -5

8

1

4

4

3 ∞
6 1

6 1

8
3

0 -2 7 1 ∞ 3
6 2

4 1 9 7 ∞
4

8 -2 4 5 -1
3

0 7 0 1 3
6 2

4 1 9 7 4
5 89 4

8 -5

5

-2

5 -5

-2

-1 5

5 Graphs

© 2004 Goodrich, Tamassia

(two steps)

Minimum Spanning Trees
2704 867 849 ORD 1846 SFO 337 LAX 1464 1235 DFW 1121 2342 802 1391 740 621 187 JFK 184 BWI 1090 946 BOS PVD 144 1258

MIA

© 2004 Goodrich, Tamassia

Graphs

90

Minimum Spanning Trees (§ 12.7)
Spanning subgraph

Subgraph of a graph G containing all the vertices of G Spanning subgraph that is itself a (free) tree Spanning tree of a weighted graph with minimum total edge weight Communications networks Transportation networks
Graphs

ORD
1

10

PIT
6 7 3 5

Spanning tree

DEN
9 4 8

Minimum spanning tree (MST)

STL

DCA
2

Applications
 

DFW

ATL

© 2004 Goodrich, Tamassia

91

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 C let 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

f

8 4 9 3 e 7

C
2 6 8 7

Replacing f with e yields a better spanning tree f 2 6 8 4 9 3 8 7 e 7
92

C

© 2004 Goodrich, Tamassia

Graphs

Partition Property
Partition 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 Graphs © 2004 Goodrich, Tamassia replacing f with e

U

f 5 8

7 9 8 e 7

V 4

2

3

Replacing f with e yields another MST U f 5 8 7
93

7 9 8 e

V 4

2

3

Kruskal’s Algorithm (§ 12.7.1)
A 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 key T∅ 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 T Merge Cloud(v) and Cloud(u) return T

© 2004 Goodrich, Tamassia

Graphs

94

Data Structure for Kruskal Algortihm (§ 10.6.2)
The algorithm maintains a forest of trees An edge is accepted it if connects distinct trees We need a data structure that maintains a partition, i.e., a collection of disjoint sets, with the operations: -find(u): return the set storing u -union(u,v): replace the sets storing u and v with their union

© 2004 Goodrich, Tamassia

Graphs

95

Representation of a Partition
Each set is stored in a sequence Each element has a reference back to the set

operation find(u) takes O(1) time, and returns the set of which u is a member. in operation union(u,v), we move the elements of the smaller set to the sequence of the larger set and update their references the time for operation union(u,v) is min(nu,nv), where nu and nv are the sizes of the sets storing u and v

Whenever an element is processed, it goes into a set of size at least double, hence each Graphs element is © 2004 Goodrich, Tamassia processed at most log n times

96

Partition-Based Implementation
A partition-based version of Kruskal’s Algorithm performs cloud merges as unions and tests as finds. Algorithm Kruskal(G):
Input: A weighted graph G. Output: An MST T for G. Let P be a partition of the vertices of G, where each vertex forms a separate set. Let Q be a priority queue storing the edges of G, sorted by their weights Let T be an initially-empty tree while Q is not empty do (u,v) ← Q.removeMinElement() if P.find(u) != P.find(v) then Running time: Add (u,v) to T O((n+m)log n) P.union(u,v) return T
© 2004 Goodrich, Tamassia Graphs 97

Kruskal Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

98

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

99

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

100

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

101

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

102

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

103

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

104

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

105

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

106

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

107

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

108

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

109

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA

© 2004 Goodrich, Tamassia

Graphs

110

Example
1846 SFO 337 LAX 1464 1235

2704

867 849 ORD 740 621 187 JFK 184 1391 BWI

BOS PVD 144 1258

802

1090 DFW 1121 2342 946

MIA
111

© 2004 Goodrich, Tamassia

Graphs

Prim-Jarnik’s Algorithm (§ 12.7.2)
Similar to Dijkstra’s algorithm (for a connected graph) We pick an arbitrary vertex s and we grow the MST as a cloud of vertices, starting from s We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a At each step: vertex into the cloud the  We add the cloud
vertex u outside the cloud with the smallest distance label  We update the labels of the vertices adjacent to u

© 2004 Goodrich, Tamassia

Graphs

112

Prim-Jarnik’s Algorithm (cont.)
A priority queue stores the vertices outside the cloud
 

Key: distance Element: vertex

Locator-based methods

insert(k,e) returns a locator replaceKey(l,k) changes the key of an item

We store three labels with each vertex:
  

© 2004 Goodrich, Tamassia

Distance Parent edge in MST Locator in priority queue

Algorithm PrimJarnikMST(G) Q ← new heap-based priority queue s ← a vertex of G for all v ∈ G.vertices() if v = s setDistance(v, 0) else setDistance(v, ∞) setParent(v, ∅) l ← Q.insert(getDistance(v), v) setLocator(v,l) while ¬Q.isEmpty() u ← Q.removeMin() for all e ∈ G.incidentEdges(u) z ← G.opposite(u,e) r ← weight(e) if r < getDistance(z) setDistance(z,r) setParent(z,e) Q.replaceKey(getLocator(z),r)
Graphs 113

Example
2 2 A 7 B 5 8 0 7 7 B 5 8 0 A 7 5 C 9 8 E 7
Graphs

D 9 8

∞ 4 F 2 ∞ 2 A B 5 8 0 5 C

7 9 8

D

7 4 F ∞

8 C

E

3 7 7 4 F 3 0 A 2 ∞ 2

E 7 D 9 8 E 7

3 7

2 2

D

7 B 5 8 5 C

7 4 F 3 7 4

© 2004 Goodrich, Tamassia

114

Example (contd.)
2 2 A 7 B 5 8 0 7 5 C 9 8 E 3 2 2 A B 5 8 0
Graphs

D

7 4 F 3 7 5 C 7 9 8 E 3 D 7 4 F 3 4 4

© 2004 Goodrich, Tamassia

115

Analysis
Graph operations

Method incidentEdges is called once for each vertex We set/get the distance, parent and locator labels of vertex z O(deg(z)) times Setting/getting a label takes O(1) time Each vertex is inserted once into and removed once from the priority queue, where each insertion or removal takes O(log n) time The key of a vertex w in the priority queue is modified at most deg(w) times, where each key change takes O(log n) time

Label operations

Priority queue operations

Prim-Jarnik’s algorithm runs in O((n + m) log n) time provided the graph is represented by the adjacency list structure

Recall that

Σ v deg(v) = 2m
Graphs 116

The running time is O(m log n) since the graph is connected
© 2004 Goodrich, Tamassia

Traveling Salesperson Problem
A tour of a graph is a spanning cycle (e.g., a cycle that goes through all the vertices) D 7 A traveling salesperson tour of a B 4 weighted graph is a tour that is 2 5 F simple (i.e., no repeated vertices 2 C 8 or edges) and has has minimum 3 6 weight E A 1 No polynomial-time algorithms are known for computing Example of traveling traveling salesperson tours The traveling salesperson salesperson tour problem (TSP) is a major open (with weight 17) problem in computer science
Find a polynomial-time algorithm computing a traveling salesperson tour or prove that Graphs none exists © 2004 Goodrich, Tamassia

117

TSP Approximation
We can approximate a TSP tour with a tour of at most twice the weight for the case of Euclidean graphs

Vertices are points in the plane Every pair of vertices is connected by an edge The weight of an edge is the length of the segment joining the points

Approximation algorithm
Compute a minimum spanning tree  Form an Eulerian circuit around the MST  Transform the circuit into a Graphs © 2004 Goodrich,tour Tamassia

118