Graphs Outline and Reading Graphs (12.1) Definition Applications - - PDF document

graphs outline and reading
SMART_READER_LITE
LIVE PREVIEW

Graphs Outline and Reading Graphs (12.1) Definition Applications - - PDF document

1 ORD DFW 802 1233 1843 3 4 7 1 LAX 337 SFO Graphs Graphs Outline and Reading Graphs (12.1) Definition Applications Terminology Properties ADT Data structures for graphs (12.2) Edge list structure


slide-1
SLIDE 1

Graphs 1

Graphs

ORD DFW SFO LAX

802 1 7 4 3 1843 1233 337

slide-2
SLIDE 2

Graphs 2

Outline and Reading

Graphs (§12.1)

Definition Applications Terminology Properties ADT

Data structures for graphs (§12.2)

Edge list structure Adjacency list structure Adjacency matrix structure

slide-3
SLIDE 3

Graphs 3

Graph

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

ORD PVD MIA DFW SFO LAX LGA HNL

8 4 9 802 1 3 8 7 1 7 4 3 1843 1099 1120 1233 337 2555 142

slide-4
SLIDE 4

Graphs 4

Edge Types

Directed edge

  • rdered 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 PVD 849 miles

slide-5
SLIDE 5

Graphs 5

John David Paul

brown.edu cox.net

cs.brown.edu

att.net qwest.net

math.brown.edu cslab1b cslab1a

Applications

Electronic circuits

Printed circuit board Integrated circuit

Transportation networks

Highway network Flight network

Computer networks

Local area network Internet Web

Databases

Entity-relationship diagram

slide-6
SLIDE 6

Graphs 6

Terminology

End 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 V

Adjacent vertices

  • U and V are adjacent

Degree of a vertex

  • X has degree 5

Parallel edges

  • h and i are parallel edges

Self-loop

  • j is a self-loop

X U V W Z Y a c b e d f g h i j

slide-7
SLIDE 7

Graphs 7

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

X U V W Z Y a c b e d f g h P2

slide-8
SLIDE 8

Graphs 8

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 X U V W Z Y a c b e d f g h C2

slide-9
SLIDE 9

Graphs 9

Properties

Notation

n

number of vertices

m

number of edges

deg(v)

degree of vertex v

Property 1

Σv deg(v) = 2m

Proof: each edge is counted twice

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)

What is the bound for a directed graph? Example

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

slide-10
SLIDE 10

Graphs 10

Main Methods of the Graph ADT

Vertices and edges

  • are positions
  • store elements

Accessor methods

  • aVertex()
  • incidentEdges(v)
  • endVertices(e)
  • isDirected(e)
  • rigin(e)
  • destination(e)
  • pposite(v, e)
  • areAdjacent(v, w)

Update methods

  • insertVertex(o)
  • insertEdge(v, w, o)
  • insertDirectedEdge(v, w, o)
  • removeVertex(v)
  • removeEdge(e)

Generic methods

  • numVertices()
  • numEdges()
  • vertices()
  • edges()
slide-11
SLIDE 11

Graphs 11

Edge List Structure

Vertex object

  • element
  • reference to position in

vertex sequence

Edge object

  • element
  • rigin vertex object
  • destination vertex object
  • reference to position in

edge sequence

Vertex sequence

  • sequence of vertex
  • bjects

Edge sequence

  • sequence of edge objects

v u w a c b a z d u v w z b c d

slide-12
SLIDE 12

Graphs 12

Adjacency List Structure

Edge list structure Incidence sequence for each vertex

  • sequence of

references to edge

  • bjects of incident

edges

Augmented edge

  • bjects
  • references to

associated positions in incidence sequences of end vertices u v w a b a u v w b

slide-13
SLIDE 13

Graphs 13

Adjacency Matrix Structure

Edge list structure Augmented vertex

  • bjects
  • Integer key (index)

associated with vertex

2D-array adjacency array

  • Reference to edge
  • bject for adjacent

vertices

  • Null for non

nonadjacent vertices

The “old fashioned” version just has 0 for no edge and 1 for edge

u v w a b 2 1 2 1

∅ ∅ ∅ ∅ ∅

a u v w 1 2 b

slide-14
SLIDE 14

Graphs 14

Asymptotic Performance

n2 n + m n + m

Space

n2 deg(v) m

removeVertex(v)

1 1 1

insertEdge(v, w, o)

n2 1 1

insertVertex(o)

1 1 1

removeEdge(e)

1 min(deg(v), deg(w)) m

areAdjacent (v, w)

n deg(v) m

incidentEdges(v) Adjacency Matrix Adjacency List Edge List

n vertices, m edges

no parallel edges no self-loops Bounds are “big-Oh”

slide-15
SLIDE 15

Graphs 15

Depth-First Search

D B A C E

slide-16
SLIDE 16

Graphs 16

Outline and Reading

Definitions (§12.1)

Subgraph Connectivity Spanning trees and forests

Depth-first search (§12.3.1)

Algorithm Example Properties Analysis

Applications of DFS

Path finding Cycle finding

slide-17
SLIDE 17

Graphs 17

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

A spanning subgraph of G is a subgraph that contains all the vertices

  • f G

Subgraph Spanning subgraph

slide-18
SLIDE 18

Graphs 18

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

Connected graph Non connected graph with two connected components

slide-19
SLIDE 19

Graphs 19

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

A forest is an undirected graph without cycles The connected components of a forest are trees

Tree Forest

slide-20
SLIDE 20

Graphs 20

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

  • f communication

networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

slide-21
SLIDE 21

Graphs 21

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

slide-22
SLIDE 22

Graphs 22

DFS Algorithm

The algorithm uses a mechanism for setting and getting “labels” of vertices and edges

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) 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)

slide-23
SLIDE 23

Graphs 23

Example

D B A C E D B A C E D B A C E

discovery edge back edge

A

visited vertex

A

unexplored vertex unexplored edge

slide-24
SLIDE 24

Graphs 24

Example (cont.)

D B A C E D B A C E D B A C E D B A C E

slide-25
SLIDE 25

Graphs 25

DFS and Maze Traversal

The DFS algorithm is similar to a classic strategy for exploring a maze

We mark each

intersection, corner and dead end (vertex) visited

We mark each corridor

(edge ) traversed

We keep track of the

path back to the entrance (start vertex) by means of a rope (recursion stack)

slide-26
SLIDE 26

Graphs 26

Properties of DFS

Property 1

DFS(G, v) visits all the

vertices and edges in the connected component of v

Property 2

The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v

D B A C E

slide-27
SLIDE 27

Graphs 27

Analysis of DFS

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

  • nce as UNEXPLORED
  • nce as VISITED

Each edge is labeled twice

  • nce as UNEXPLORED
  • nce as DISCOVERY or BACK

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

slide-28
SLIDE 28

Graphs 28

Path Finding

We can specialize the DFS algorithm to find a path between two given vertices u and z We call DFS(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex and the current vertex As soon as destination vertex z is encountered, we return the path as the contents of the stack

Algorithm pathDFS(G, v, z) setLabel(v, VISITED) S.push(v) if v = z return S.elements() for all e ∈ G.incidentEdges(v) if getLabel(e) = UNEXPLORED w ← opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) S.push(e) pathDFS(G, w, z) S.pop(e) else setLabel(e, BACK) S.pop(v)

slide-29
SLIDE 29

Graphs 29

Cycle Finding

We can specialize the DFS algorithm to find a simple cycle 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 stack from the top to vertex w

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

  • ← S.pop()

T.push(o) until o = w return T.elements() S.pop(v)

slide-30
SLIDE 30

Graphs 30

Breadth-First Search

C B A E D

L0 L1

F

L2

slide-31
SLIDE 31

Graphs 31

Outline and Reading

Breadth-first search (§12.3.2)

Algorithm Example Properties Analysis Applications

DFS vs. BFS

Comparison of applications Comparison of edge labels

slide-32
SLIDE 32

Graphs 32

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

slide-33
SLIDE 33

Graphs 33

BFS Algorithm

The algorithm uses a mechanism for setting and getting “labels” of vertices and edges

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 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)

slide-34
SLIDE 34

Graphs 34

Example

C B A E D

discovery edge cross edge

A

visited vertex

A

unexplored vertex unexplored edge

L0 L1

F C B A E D

L0 L1

F C B A E D

L0 L1

F

slide-35
SLIDE 35

Graphs 35

Example (cont.)

C B A E D

L0 L1

F C B A E D

L0 L1

F

L2

C B A E D

L0 L1

F

L2

C B A E D

L0 L1

F

L2

slide-36
SLIDE 36

Graphs 36

Example (cont.)

C B A E D

L0 L1

F

L2

C B A E D

L0 L1

F

L2

C B A E D

L0 L1

F

L2

slide-37
SLIDE 37

Graphs 37

Properties

Notation

Gs: connected component of s

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

  • f Gs

Property 3

For each vertex v in Li

  • The path of Ts from s to v has i

edges

  • Every path from s to v in Gs has at

least i edges C B A E D

L0 L1

F

L2

C B A E D F

slide-38
SLIDE 38

Graphs 38

Analysis

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

  • nce as UNEXPLORED
  • nce as VISITED

Each edge is labeled twice

  • nce as UNEXPLORED
  • nce as DISCOVERY or CROSS

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

Recall that Σv deg(v) = 2m

slide-39
SLIDE 39

Graphs 39

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

slide-40
SLIDE 40

Graphs 40

DFS vs. BFS

C B A E D

L0 L1

F

L2

C B A E D F

DFS BFS

Biconnected components

Shortest paths

√ √

Spanning forest, connected components, paths, cycles

BFS DFS Applications

slide-41
SLIDE 41

Graphs 41

DFS vs. BFS (cont.)

Back edge (v,w)

w is an ancestor of v in

the tree of discovery edges

Cross edge (v,w)

w is in the same level as

v or in the next level in

the tree of discovery edges

C B A E D

L0 L1

F

L2

C B A E D F

DFS BFS

slide-42
SLIDE 42

Graphs 42

Directed Graphs

JFK BOS MIA ORD LAX DFW SFO

slide-43
SLIDE 43

Graphs 43

Outline and Reading (§12.4)

Reachability (§12.4.1)

Directed DFS Strong connectivity

Transitive closure (§12.4.2)

The Floyd-Warshall Algorithm

Directed Acyclic Graphs (DAG’s) (§12.4.3)

Topological Sorting

slide-44
SLIDE 44

Graphs 44

Digraphs

A digraph is a graph whose edges are all directed

Short for “directed graph”

Applications

  • ne-way streets

flights task scheduling

A C E B D

slide-45
SLIDE 45

Graphs 45

Digraph Properties

A graph G= (V,E) such that

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.

A C E B D

slide-46
SLIDE 46

Graphs 46

Digraph Application

Scheduling: edge (a,b) means task a must be completed before b can be started

The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23 ics22 ics21 ics161 ics151 ics171

slide-47
SLIDE 47

Graphs 47

Directed DFS

We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges

  • nly along their

direction A directed DFS starting a a vertex s determines the vertices reachable from s

A C E B D

slide-48
SLIDE 48

Graphs 48

Reachability

DFS tree rooted at v: vertices reachable from v via directed paths

A C E B D F

A C E D A C E B D F

slide-49
SLIDE 49

Graphs 49

Strong Connectivity

Each vertex can reach all other vertices

a d c b e f g

slide-50
SLIDE 50

Graphs 50

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

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

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

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

Running time: O(n+ m).

Strong Connectivity Algorithm

G: G’:

a d c b e f g a d c b e f g

slide-51
SLIDE 51

Graphs 51

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

Strongly Connected Components

{ a , c , g } { f , d , e , b }

a d c b e f g

slide-52
SLIDE 52

Graphs 52

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 B A D C E B A D C E

G G*

slide-53
SLIDE 53

Graphs 53

Computing the Transitive Closure

We can perform DFS starting at each vertex

O(n(n+ m))

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.

Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm

slide-54
SLIDE 54

Graphs 54

Floyd-Warshall Transitive Closure

Idea # 1: Number the vertices 1, 2, …, n. Idea # 2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices:

k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

slide-55
SLIDE 55

Graphs 55

Floyd-Warshall’s Algorithm

Floyd-Warshall’s algorithm numbers the vertices of G as

v1 , …, vn and computes a

series of digraphs G0, …, Gn

G0=G Gk has a directed edge (vi, vj)

if G has a directed path from

vi to vj with intermediate

vertices in the set {v1 , …, vk}

We have that Gn = G* In phase k, digraph Gk is computed from Gk − 1 Running time: O(n3), assuming areAdjacent is O(1) (e.g., adjacency matrix)

Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i ← 1 for all v ∈ G.vertices() denote v as vi i ← i + 1 G0 ← G for k ← 1 to n do Gk ← Gk − 1 for i ← 1 to n (i ≠ k) do for j ← 1 to n (j ≠ i, k) do if Gk − 1.areAdjacent(vi, vk) ∧ Gk − 1.areAdjacent(vk, vj) if ¬Gk.areAdjacent(vi, vj) Gk.insertDirectedEdge(vi, vj , k) return Gn

slide-56
SLIDE 56

Graphs 56

Floyd-Warshall Example

JFK BOS MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

slide-57
SLIDE 57

Graphs 57

Floyd-Warshall, Iteration 1

JFK BOS MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

slide-58
SLIDE 58

Graphs 58

Floyd-Warshall, Iteration 2

JFK BOS MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

slide-59
SLIDE 59

Graphs 59

Floyd-Warshall, Iteration 3

JFK BOS MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

slide-60
SLIDE 60

Graphs 60

Floyd-Warshall, Iteration 4

JFK BOS MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

slide-61
SLIDE 61

Graphs 61

Floyd-Warshall, Iteration 5

JFK MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

BOS

slide-62
SLIDE 62

Graphs 62

Floyd-Warshall, Iteration 6

JFK MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

BOS

slide-63
SLIDE 63

Graphs 63

Floyd-Warshall, Conclusion

JFK MIA ORD LAX DFW SFO

v2 v1 v3 v4 v5 v6

v7

BOS

slide-64
SLIDE 64

Graphs 64

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

  • f the vertices such that for every

edge (vi , vj), we have i < j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological

  • rdering if and only if it is a DAG

B A D C E DAG G B A D C E Topological

  • rdering of G

v1 v2 v3 v4 v5

slide-65
SLIDE 65

Graphs 65

write c.s. program play

Topological Sorting

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

wake up eat nap study computer sci. more c.s. work out sleep dream about graphs

A typical day

1 2 3 4 5 6 7 8 9 10 11 Go out w/ friends

slide-66
SLIDE 66

Graphs 66

Running time: O(n + m). Why?

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 edges Label v ← n n ← n - 1 Remove v from H

slide-67
SLIDE 67

Graphs 67

Topological Sorting Algorithm using DFS

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) = 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 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)

slide-68
SLIDE 68

Graphs 68

Topological Sorting Example

slide-69
SLIDE 69

Graphs 69

Topological Sorting Example

9

slide-70
SLIDE 70

Graphs 70

Topological Sorting Example

8 9

slide-71
SLIDE 71

Graphs 71

Topological Sorting Example

7 8 9

slide-72
SLIDE 72

Graphs 72

Topological Sorting Example

7 8 6 9

slide-73
SLIDE 73

Graphs 73

Topological Sorting Example

7 8 5 6 9

slide-74
SLIDE 74

Graphs 74

Topological Sorting Example

7 4 8 5 6 9

slide-75
SLIDE 75

Graphs 75

Topological Sorting Example

7 4 8 5 6 3 9

slide-76
SLIDE 76

Graphs 76

Topological Sorting Example

2 7 4 8 5 6 3 9

slide-77
SLIDE 77

Graphs 77

Topological Sorting Example

2 7 4 8 5 6 1 3 9

slide-78
SLIDE 78

Graphs 78

Shortest Paths

C B A E D F

3 2 8 5 8 4 8 7 1 2 5 2 3 9

slide-79
SLIDE 79

Graphs 79

Outline and Reading

Weighted graphs (§12.1)

Shortest path problem Shortest path properties

Dijkstra’s algorithm (§12.6.1)

Algorithm Edge relaxation

The Bellman-Ford algorithm Shortest paths in DAGs All-pairs shortest paths

slide-80
SLIDE 80

Graphs 80

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

ORD PVD MIA DFW SFO LAX LGA HNL

8 4 9 802 1 3 8 7 1 7 4 3 1843 1099 1120 1233 337 2555 142 1205

slide-81
SLIDE 81

Graphs 81

Shortest Path Problem

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

ORD PVD MIA DFW SFO LAX LGA HNL

8 4 9 802 1 3 8 7 1 7 4 3 1843 1099 1120 1233 337 2555 142 1205

slide-82
SLIDE 82

Graphs 82

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

ORD PVD MIA DFW SFO LAX LGA HNL

8 4 9 802 1 3 8 7 1 7 4 3 1843 1099 1120 1233 337 2555 142 1205

slide-83
SLIDE 83

Graphs 83

Dijkstra’s Algorithm

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

  • f all the vertices from a

given start vertex s Assumptions:

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

  • 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 (edge

relaxation)

slide-84
SLIDE 84

Graphs 84

Edge Relaxation

Consider 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) = 50 10 z s u

d(z) = 60

d(u) = 50 10 z s u e e

slide-85
SLIDE 85

Graphs 85

Example

C B A E D F

4 2 8 ∞ ∞ 4 8 7 1 2 5 2 3 9

C B A E D F

3 2 8 5 11 4 8 7 1 2 5 2 3 9

C B A E D F

3 2 8 5 8 4 8 7 1 2 5 2 3 9

C B A E D F

3 2 7 5 8 4 8 7 1 2 5 2 3 9

slide-86
SLIDE 86

Graphs 86

Example (cont.)

C B A E D F

3 2 7 5 8 4 8 7 1 2 5 2 3 9

C B A E D F

3 2 7 5 8 4 8 7 1 2 5 2 3 9

slide-87
SLIDE 87

Graphs 87

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:

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

slide-88
SLIDE 88

Graphs 88

Analysis

Graph operations

  • Method incidentEdges is called once for each vertex

Label operations

  • We set/get the distance and locator labels of vertex z O(deg(z)) times
  • Setting/getting a label takes O(1) time

Priority queue operations

  • 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

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

The running time can also be expressed as O(m log n) since the graph is connected

slide-89
SLIDE 89

Graphs 89

Extension

We can extend Dijkstra’s algorithm to return a tree of shortest paths from the start vertex to all

  • ther vertices

We 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)

slide-90
SLIDE 90

Graphs 90

Why Dijkstra’s Algorithm Works

Dijkstra’s algorithm is based on the greedy

  • method. It adds vertices by increasing distance.

C B A E D F

3 2 7 5 8 4 8 7 1 2 5 2 3 9

Suppose it didn’t find all 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 vertex.

slide-91
SLIDE 91

Graphs 91

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.

C B A E D F

4 5 7 5 9 4 8 7 1 2 5 6

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

slide-92
SLIDE 92

Graphs 92

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 = s setDistance(v, 0) else setDistance(v, ∞) for i ← 1 to n-1 do for 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)

slide-93
SLIDE 93

Graphs 93

  • 2

Bellman-Ford Example

∞ ∞ ∞ ∞ ∞ 4 8 7 1

  • 2

5

  • 2

3 9 ∞ ∞ ∞ ∞ 4 8 7 1

  • 2

5 3 9

Nodes are labeled with their d(v) values

  • 2
  • 2

8 4 ∞ 4 8 7 1

  • 2

5 3 9 ∞ 8

  • 2

4

  • 1

5 6 1 9

  • 2

5 1

  • 1

9 4 8 7 1

  • 2

5

  • 2

3 9 4

slide-94
SLIDE 94

Graphs 94

DAG-based Algorithm

Works even with negative-weight edges Uses topological order Uses simple 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)

slide-95
SLIDE 95

Graphs 95

  • 2

DAG Example

∞ ∞ ∞ ∞ ∞ 4 8 7 1

  • 5

5

  • 2

3 9 ∞ ∞ ∞ ∞ 4 8 7 1

  • 5

5 3 9

Nodes are labeled with their d(v) values

  • 2
  • 2

8 4 ∞ 4 8 7 1

  • 5

5 3 9 ∞

  • 2

4

  • 1

1 7

  • 2

5 1

  • 1

7 4 8 7 1

  • 5

5

  • 2

3 9 4

1 1 2 4 3 6 5 1 2 4 3 6 5

8

1 2 4 3 6 5 1 2 4 3 6 5

5

(two steps)

slide-96
SLIDE 96

Graphs 96

All-Pairs Shortest Paths

Find the distance between every pair of vertices in a weighted directed graph G. We can make n calls to Dijkstra’s algorithm (if no negative edges), which takes O(nmlog n) time. Likewise, n calls to Bellman-Ford would take O(n2m) time. We can achieve O(n3) time using dynamic programming (similar to the Floyd-Warshall algorithm).

Algorithm AllPair(G) {assumes vertices 1,…,n} for all vertex pairs (i,j) if i = j D0[i,i] ← 0 else if (i,j) is an edge in G D0[i,j] ← weight of edge (i,j) else D0[i,j] ← + ∞ for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do Dk[i,j] ← min{Dk-1[i,j], Dk-1[i,k]+Dk-1[k,j]} return Dn

k j i

Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (compute weight of this edge)

slide-97
SLIDE 97

Graphs 97

Minimum Spanning Trees

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

slide-98
SLIDE 98

Graphs 98

Outline and Reading

Minimum Spanning Trees (§12.7)

Definitions A crucial fact

The Prim-Jarnik Algorithm (§12.7.2) Kruskal's Algorithm (§12.7.1) Baruvka's Algorithm

slide-99
SLIDE 99

Graphs 99

Minimum Spanning Tree

Spanning 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

10 1 9 8 6 3 2 5 7 4

slide-100
SLIDE 100

Graphs 100

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

  • f smaller weight by

replacing e with f 8 4 2 3 6 7 7 9 8

e

C

f

8 4 2 3 6 7 7 9 8

C

e f

Replacing f with e yields a better spanning tree

slide-101
SLIDE 101

Graphs 101

U V

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 replacing

f with e

7 4 2 8 5 7 3 9 8

e f

7 4 2 8 5 7 3 9 8

e f

Replacing f with e yields another MST

U V

slide-102
SLIDE 102

Graphs 102

Prim-Jarnik’s Algorithm

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 vertex in the cloud At each step:

We add to the cloud the

vertex u outside the cloud with the smallest distance label

We update the labels of the

vertices adjacent to u

slide-103
SLIDE 103

Graphs 103

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:

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

slide-104
SLIDE 104

Graphs 104

Example

B D C A F E 7 4 2 8 5 7 3 9 8 7 2 8

∞ ∞

B D C A F E 7 4 2 8 5 7 3 9 8 7 2 5

7 B D C A F E 7 4 2 8 5 7 3 9 8 7 2 5

7 B D C A F E 7 4 2 8 5 7 3 9 8 7 2 5 4 7

slide-105
SLIDE 105

Graphs 105

Example (contd.)

B D C A F E 7 4 2 8 5 7 3 9 8 3 2 5 4 7 B D C A F E 7 4 2 8 5 7 3 9 8 3 2 5 4 7

slide-106
SLIDE 106

Graphs 106

Analysis

Graph operations

  • Method incidentEdges is called once for each

vertex

Label operations

  • We set/get the distance, parent and locator

labels of vertex z O(deg(z)) times

  • Setting/getting a label takes O(1) time

Priority queue operations

  • 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

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

The running time is O(m log n) since the graph is connected

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)

slide-107
SLIDE 107

Graphs 107

Kruskal’s Algorithm

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

slide-108
SLIDE 108

Graphs 108

Kruskal Example

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

slide-109
SLIDE 109

Graphs 109

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

Example

slide-110
SLIDE 110

Graphs 110

Example

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

slide-111
SLIDE 111

Graphs 111

Example

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

slide-112
SLIDE 112

Graphs 112

Example

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

slide-113
SLIDE 113

Graphs 113

Example

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

slide-114
SLIDE 114

Graphs 114

Example

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

slide-115
SLIDE 115

Graphs 115

Example

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

slide-116
SLIDE 116

Graphs 116

Example

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

slide-117
SLIDE 117

Graphs 117

Example

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

slide-118
SLIDE 118

Graphs 118

Example

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

slide-119
SLIDE 119

Graphs 119

Example

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

slide-120
SLIDE 120

Graphs 120

Example

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

slide-121
SLIDE 121

Graphs 121

Example

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

slide-122
SLIDE 122

Graphs 122

Data Structure for Kruskal Algortihm

The algorithm maintains a forest of trees An edge is accepted if it 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

slide-123
SLIDE 123

Graphs 123

Representation of a Partition

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

  • peration 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 element is processed at most log n times

slide-124
SLIDE 124

Graphs 124

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 Add (u,v) to T P.union(u,v) return T

Running time: O((n+ m)log n)

slide-125
SLIDE 125

Graphs 125

Baruvka’s Algorithm

Like Kruskal’s Algorithm, Baruvka’s algorithm grows many “clouds” at once. Each iteration of the while-loop halves the number of connected compontents in T.

  • The running time is O(m log n).

Algorithm BaruvkaMST(G) T V {just the vertices of G} while T has fewer than n-1 edges do for each connected component C in T do Let edge e be the smallest-weight edge from C to another component in T. if e is not already in T then Add edge e to T return T

slide-126
SLIDE 126

Graphs 126

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

Baruvka Example

slide-127
SLIDE 127

Graphs 127

Example

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

slide-128
SLIDE 128

Graphs 128

Example

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

slide-129
SLIDE 129

Graphs 129

Traveling Salesperson Problem

A tour of a graph is a spanning cycle (e.g., a cycle that goes through all the vertices) A traveling salesperson tour of a weighted graph is a tour that is simple (i.e., no repeated vertices or edges) and has has minimum weight No polynomial-time algorithms are known for computing traveling salesperson tours The traveling salesperson problem (TSP) is a major open problem in computer science

  • Find a polynomial-time algorithm

computing a traveling salesperson tour or prove that none exists B D C A F E 7 4 2 8 5 3 2 6 1

Example of traveling salesperson tour (with weight 17)