You are on page 1of 18

Outline and Reading Breadth-First Search

L0 L1
B A C E F D

Breadth-first search (6.3.3)


Algorithm Example Properties Analysis Applications Comparison of applications Comparison of edge labels
Breadth-First Search 2

L2

DFS vs. BFS (6.3.3)


5/7/2002 11:06 AM

Breadth-First Search

5/7/2002 11:06 AM

Breadth-First Search
Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G

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)
5/7/2002 11:06 AM

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
3

Algorithm BFS(G, s) L0 new empty sequence L0.insertLast(s) setLabel(s, VISITED) i0 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
4

5/7/2002 11:06 AM

Breadth-First Search

Breadth-First Search

Example
A A

L0 L1
B

Example (cont.)
A C E F D

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

L0 L1
B

A C E F D

L0 L1
B

A C E F D

L2

L0 L1
B

A C E F
5

L0
D

A C E F
Breadth-First Search

L0
D

A C E F
6

L1

L1

L1

L2

L2

5/7/2002 11:06 AM

Breadth-First Search

5/7/2002 11:06 AM

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

Properties
L0 L1
B A C E F D

Notation Property 1 Property 2


Gs: connected component of s
B

A C E F D

L2

L2

BFS(G, s) visits all the vertices and edges of Gs The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs For each vertex v in Li

L0 L1
B

L0 L1
B

A C E F
Breadth-First Search 7

Property 3
D

A C E F
8

L2

The path of Ts from s to v has i edges Every path from s to v in Gs has at least i edges
Breadth-First Search

L2

5/7/2002 11:06 AM

5/7/2002 11:06 AM

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

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

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

Recall that

v deg(v) = 2m
Breadth-First Search 9

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
Breadth-First Search 10

5/7/2002 11:06 AM

5/7/2002 11:06 AM

DFS vs. BFS


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

DFS vs. BFS (cont.)


DFS

BFS

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

L0 L1
B

A C E F D B

A C E F D

L0 L1
B

L2

L2

DFS
5/7/2002 11:06 AM Breadth-First Search

BFS
11 5/7/2002 11:06 AM

DFS
Breadth-First Search

BFS
12

Outline and Reading Biconnectivity


SEA ORD SNA MIA PVD FCO

Definitions (6.3.2)

Separation vertices and edges Biconnected graph Biconnected components Equivalence classes Linked edges and link components Auxiliary graph Proxy graph
Biconnectivity 2

Algorithms (6.3.2)

5/7/2002 11:09 AM

Biconnectivity

5/7/2002 11:09 AM

Separation Edges and Vertices


Definitions

Biconnected Graph
Equivalent definitions of a biconnected graph G

Let G be a connected graph A separation edge of G is an edge whose removal disconnects G A separation vertex of G is a vertex whose removal disconnects G Separation edges and vertices represent single points of failure in a network and are critical to the operation of the network DFW, LGA and LAX are separation vertices (DFW,LAX) is a separation edge ORD SFO LGA HNL LAX DFW
Biconnectivity

Applications

Example

Graph G has no separation edges and no separation vertices For any two vertices u and v of G, there are two disjoint simple paths between u and v (i.e., two simple paths between u and v that share no other vertices or edges) For any two vertices u and v of G, there is a simple cycle containing u and v
ORD LGA PVD

Example
PVD HNL MIA
3 5/7/2002 11:09 AM

SFO

LAX

DFW
Biconnectivity

MIA
4

5/7/2002 11:09 AM

Biconnected Components
Biconnected component of a graph G

Equivalence Classes
Given a set S, a relation R on S is a set of ordered pairs of elements of S, i.e., R is a subset of SS An equivalence relation R on S satisfies the following properties
Reflexive: (x,x) R Symmetric: (x,y) R (y,x) R Transitive: (x,y) R (y,z) R (x,z) R

A maximal biconnected subgraph of G, or A subgraph consisting of a separation edge of G and its end vertices An edge belongs to exactly one biconnected component A nonseparation vertex belongs to exactly one biconnected component A separation vertex belongs to two or more biconnected components

Interaction of biconnected components


Example of a graph with four biconnected components


ORD LGA HNL LAX DFW
Biconnectivity

An equivalence relation R on S induces a partition of the elements of S into equivalence classes Example (connectivity relation among the vertices of a graph):

SFO

PVD RDU MIA


5

Let V be the set of vertices of a graph G Define the relation C = {(v,w) VV such that G has a path from v to w} Relation C is an equivalence relation The equivalence classes of relation C are the vertices in each connected component of graph G
Biconnectivity 6

5/7/2002 11:09 AM

5/7/2002 11:09 AM

Link Relation
Edges e and f of connected graph G are linked if

Link Components
a b c e d f g j i The link components of a connected graph G are the equivalence classes of edges with respect to the link relation A biconnected component of G is the subgraph of G induced by an equivalence class of linked edges A separation edge is a single-element equivalence class of linked edges A separation vertex has incident edges in at least two distinct equivalence classes of linked edge
ORD LGA PVD RDU MIA
8

e = f, or G has a simple cycle containing e and f

Theorem: The link relation on the edges of a graph is an equivalence relation


Proof Sketch: The reflexive and symmetric properties follow from the definition For the transitive property, consider two simple cycles sharing an edge
5/7/2002 11:09 AM

Equivalence classes of linked edges: {a} {b, c, d, e, f} {g, i, j} i j

b c

e d f

SFO HNL

LAX

DFW
Biconnectivity

Biconnectivity

5/7/2002 11:09 AM

Auxiliary Graph
Auxiliary graph B for a connected graph G

g b c a d e

h i j f i

Auxiliary Graph (cont.)


In the worst case, the number of edges of the auxiliary graph is proportional to nm

Associated with a DFS traversal of G The vertices of B are the edges of G For each back edge e of G, B has edges (e,f1), (e,f2) , , (e,fk), where f1, f2, , fk are the discovery edges of G that form a simple cycle with e Its connected components correspond to the the link components of G

DFS on graph G
g b c a d e h f j i

Auxiliary graph B
9

DFS on graph G
5/7/2002 11:09 AM Biconnectivity

Auxiliary graph B
10

5/7/2002 11:09 AM

Biconnectivity

Proxy Graph
Algorithm proxyGraph(G) Input connected graph G Output proxy graph F for G F empty graph DFS(G, s) { s is any vertex of G} for all discovery edges e of G F.insertVertex(e) setLabel(e, UNLINKED) for all vertices v of G in DFS visit order for all back edges e = (u,v) F.insertVertex(e) while u s f discovery edge with dest. u F.insertEdge(e,f,) if f getLabel(f) = UNLINKED setLabel(f, LINKED) u origin of edge f else u s { ends the while loop } return F
5/7/2002 11:09 AM Biconnectivity

Proxy Graph (cont.)


g b c a d e j f

h i i

Proxy graph F for a connected graph G

g b c a d e

h i j f i

DFS on graph G
g b c a d e h f j i

Spanning forest of the auxiliary graph B Has m vertices and O(m) edges Can be constructed in O(n + m) time Its connected components (trees) correspond to the the link components of G

DFS on graph G
g b c a d e h f j i

Given a graph G with n vertices and m edges, we can compute the following in O(n + m) time

Proxy graph F
11

The biconnected components of G The separation vertices of G The separation edges of G


Biconnectivity

Proxy graph F
12

5/7/2002 11:09 AM

Outline and Reading


Definitions (6.1)

Depth-First Search
A B C D E

Subgraph Connectivity Spanning trees and forests Algorithm Example Properties Analysis Path finding Cycle finding
Depth-First Search 2

Depth-first search (6.3.1)


Applications of DFS (6.5)


Depth-First Search

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

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
3

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

Connected graph

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


Spanning subgraph
Depth-First Search

Non connected graph with two connected components


4

Depth-First Search

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

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
5 Depth-First Search

Tree

Graph

A forest is an undirected graph without cycles The connected components of a forest are trees
Depth-First Search

Forest

Spanning tree
6

Depth-First Search
Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G

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

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


7

Depth-First Search

Depth-First Search

Example
A A

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

unexplored vertex visited vertex unexplored edge discovery edge back edge
A B C
Depth-First Search

A D E B C
9

A D E B C
Depth-First Search

A D E B C
10

DFS and Maze Traversal


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

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

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)
Depth-First Search 11

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

Depth-First Search

12

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

Path Finding
We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern 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
13

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
Depth-First Search

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

Depth-First Search

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 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 o S.pop() T.push(o) until o = w return T.elements() S.pop(v)
15

Depth-First Search

Outline and Reading (6.4) Directed Graphs


ORD JFK SFO

Reachability (6.4.1)

BOS

Directed DFS Strong connectivity

Transitive closure (6.4.2)


DFW

LAX

MIA

The Floyd-Warshall Algorithm

Directed Acyclic Graphs (DAGs) (6.4.4)

Topological Sorting
Directed Graphs 2

Directed Graphs

Digraphs
A digraph is a graph whose edges are all directed Applications

Digraph Properties
C E D C B A

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.

one-way streets flights task scheduling

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 inedges and out-edges in time proportional to their size.
Directed Graphs 4

Directed Graphs

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

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

ics51

ics53

ics52 ics161

ics131

ics141

ics121

ics171

discovery edges back edges forward edges cross edges

ics151
Directed Graphs

The good life

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


5 Directed Graphs

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

Strong Connectivity
Each vertex can reach all other vertices
a c d
D C A
Directed Graphs

E C A

D F B
A E

e
F

f
7 Directed Graphs

b
8

Strong Connectivity Algorithm


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

Strongly Connected Components


G:
a c d g

If theres a w not visited, print no.

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


e b

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 c g

If theres a w not visited, print no. Else, print yes.

{a,c,g}

G: Running time: O(n+m).


f Directed Graphs d

d f

e b

e b

{f,d,e,b}
10

Directed Graphs

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
Directed Graphs

Computing the Transitive Closure


D B C A D B C A G*
11

E G

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


Directed Graphs 12

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:
i Uses only vertices numbered 1,,k-1 k Uses only vertices numbered 1,,k (add this edge if its not already in)

Floyd-Warshalls Algorithm
Algorithm FloydWarshall(G) Input digraph G Output transitive closure G* of G i1 for all v G.vertices() G0=G denote v as vi Gk has a directed edge (vi, vj) ii+1 if G has a directed path from G0 G vi to vj with intermediate for k 1 to n do vertices in the set {v1 , , vk} Gk Gk 1 We have that Gn = G* for i 1 to n (i k) do for j 1 to n (j i, k) do In phase k, digraph Gk is computed from Gk 1 if Gk 1.areAdjacent(vi, vk) Gk 1.areAdjacent(vk, vj) Running time: O(n3), if Gk.areAdjacent(vi, vj) assuming areAdjacent is O(1) Gk.insertDirectedEdge(vi, vj , k) (e.g., adjacency matrix) return Gn

Floyd-Warshalls algorithm numbers the vertices of G as v1 , , vn and computes a series of digraphs G0, , Gn

j Uses only vertices numbered 1,,k-1


13

Directed Graphs

Directed Graphs

14

Floyd-Warshall Example
BOS ORD

v7

Floyd-Warshall, Iteration 1
BOS ORD

v7

v4
JFK

v4
JFK

v2
SFO

v6

v2
SFO

v6

LAX

DFW

v1

v3
MIA

LAX

DFW

v1

v3
MIA

v5
Directed Graphs 15 Directed Graphs

v5
16

Floyd-Warshall, Iteration 2
BOS ORD

v7

Floyd-Warshall, Iteration 3
BOS ORD

v7

v4
JFK

v4
JFK

v2
SFO

v6

v2
SFO

v6

LAX

DFW

v1

v3
MIA

LAX

DFW

v1

v3
MIA

v5
Directed Graphs 17 Directed Graphs

v5
18

Floyd-Warshall, Iteration 4
BOS ORD

v7

Floyd-Warshall, Iteration 5
BOS ORD

v7

v4
JFK

v4
JFK

v2
SFO

v6

v2
SFO

v6

LAX

DFW

v1

v3
MIA

LAX

DFW

v1

v3
MIA

v5
Directed Graphs 19 Directed Graphs

v5
20

Floyd-Warshall, Iteration 6
BOS ORD

v7

Floyd-Warshall, Conclusion
BOS ORD

v7

v4
JFK

v4
JFK

v2
SFO

v6

v2
SFO

v6

LAX

DFW

v1

v3
MIA

LAX

DFW

v1

v3
MIA

v5
Directed Graphs 21 Directed Graphs

v5
22

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 a task sequence that satisfies the v2 precedence constraints Theorem A digraph admits a topological v1 ordering if and only if it is a DAG
Directed Graphs

Topological Sorting
Number vertices, so that (u,v) in E implies u < v
wake up 1

D B C A D B C A v4

A typical student day


eat 4 3

2 study computer sci.

DAG G E v5
7 play

nap 8 write c.s. program

5 more c.s.

v3 Topological ordering of G
23

9 make cookies for professors sleep

6 work out

10 11 dream about graphs


Directed Graphs 24

Algorithm for Topological Sorting


Note: This algorithm is different than the one in Goodrich-Tamassia
Method TopologicalSort(G) HG // 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 nn-1 Remove v from H

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. 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 nn-1
26

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


Directed Graphs 25

Directed Graphs

Topological Sorting Example

Topological Sorting Example

9
Directed Graphs 27 Directed Graphs 28

Topological Sorting Example

Topological Sorting Example

7 8 9
Directed Graphs 29

8 9
Directed Graphs 30

Topological Sorting Example

Topological Sorting Example

6 7 8 9
Directed Graphs 31

6 7 8 9
Directed Graphs

32

Topological Sorting Example

Topological Sorting Example


3

4 6 7 8 9
Directed Graphs 33

4 5 6 7 8 9
Directed Graphs 34

Topological Sorting Example


2 3 4 6 7 8 9
Directed Graphs 35

Topological Sorting Example


2 3 4 1

6 7 8 9
Directed Graphs

36

Outline and Reading Graphs


802

Graphs (6.1)

SFO
337

1843
43 17

ORD

Definition Applications Terminology Properties ADT Edge list structure Adjacency list structure Adjacency matrix structure
Graphs 2

Data structures for graphs (6.2)


LAX

1233

DFW

5/3/2002 7:41 AM

Graphs

5/3/2002 7:41 AM

Graph
A graph is a pair (V, E), where

Edge Types
Directed edge

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

Example:

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

ORD

flight AA 1206 849 miles

PVD

Undirected edge

ORD

PVD

802

SFO
337

1843
43 17

ORD

849

2 14

PVD

Directed graph

HNL

2555

LAX

1233

DFW

7 138 1120

LGA
10 99

Undirected graph

MIA
3

5/3/2002 7:41 AM

Graphs

5/3/2002 7:41 AM

Applications
cslab1a cslab1b math.brown.edu

Terminology
End vertices (or endpoints) of an edge

Electronic circuits

Printed circuit board Integrated circuit Highway network Flight network

Transportation networks

cs.brown.edu

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


brown.edu qwest.net att.net

h Z i g

Adjacent vertices

Computer networks

Local area network Internet Web


Paul

Degree of a vertex

W f

cox.net John David

Parallel edges

Databases

Self-loop

Entity-relationship diagram
Graphs 5

5/3/2002 7:41 AM

5/3/2002 7:41 AM

Terminology (cont.)
Path

Terminology (cont.)
Cycle

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

a U c

V d P2 W f

P1 X h Z

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

a U c

V d C2 W f

b X e h C1 g Y Z

Simple cycle

Simple path

e g Y

Examples

Examples

5/3/2002 7:41 AM

Graphs

5/3/2002 7:41 AM

Graphs

Properties
Property 1 Notation
n m deg(v) number of vertices number of edges degree of vertex v

Main Methods of the Graph ADT


Vertices and edges

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

Update methods

are positions store elements aVertex() incidentEdges(v) endVertices(e) isDirected(e) origin(e) destination(e) opposite(v, e) areAdjacent(v, w)

Property 2
In an undirected graph with no selfloops and no multiple edges m n (n 1)/2 Proof: each vertex has degree at most (n 1)
5/3/2002 7:41 AM Graphs

Accessor methods

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

insertVertex(o) insertEdge(v, w, o) insertDirectedEdge(v, w, o) removeVertex(v) removeEdge(e) numVertices() numEdges() vertices() edges()

Generic methods

5/3/2002 7:41 AM

Graphs

10

Edge List Structure


Vertex object

Adjacency List Structure


u c b 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 sequence of edge objects
Graphs

a v

Edge list structure Incidence sequence for each vertex

a u

b w

Edge object

sequence of references to edge objects of incident edges

Augmented edge objects

Vertex sequence

Edge sequence

references to associated positions in incidence sequences of end vertices

5/3/2002 7:41 AM

11

5/3/2002 7:41 AM

Graphs

12

Adjacency Matrix Structure


Edge list structure Augmented vertex objects

Performance
n vertices m edges no parallel edges no self-loops

a u

b w

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

Edge 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

Adjacency Matrix n2 n 1 n2 1 n2 1
14

Space incidentEdges(v) areAdjacent (v, w) insertVertex(o) insertEdge(v, w, o) removeVertex(v) removeEdge(e)


5/3/2002 7:41 AM

2D-array adjacency array

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

5/3/2002 7:41 AM

Graphs

13

Outline and Reading Campus Tour


Overview of the assignment Review

Adjacency matrix structure (6.2.3) Kruskals MST algorithm (7.3.1)

Partition ADT and implementation (4.2.2) The decorator pattern (6.5.1) The traveling salesperson problem

Definition Approximation algorithm (13.4.3)

6/8/2002 2:07 PM

Campus Tour

6/8/2002 2:07 PM

Campus Tour

Graph Assignment
Goals

Adjacency Matrix Structure


Edge list structure Augmented vertex objects

a u

b w

Learn and implement the adjacency matrix structure an Kruskals minimum spanning tree algorithm Understand and use the decorator pattern and various JDSL classes and interfaces Implement the adjacency matrix structure for representing a graph Implement Kruskals MST algorithm Computation and visualization of an approximate traveling salesperson tour
Campus Tour 3

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

Your task

2D-array adjacency array

Frontend

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

6/8/2002 2:07 PM

6/8/2002 2:07 PM

Campus Tour

Kruskals Algorithm
The vertices are partitioned into clouds

Example
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 A F 1 B 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F

We start with one cloud per vertex Clouds are merged during the execution of the algorithm makeSet(o): create set {o} and return a locator for object o find(l): return the set of the object with locator l union(A,B): merge sets A and B

Algorithm KruskalMSF(G) Input weighted graph G Output labeling of the edges of a minimum spanning forest of G Q new heap-based priority queue for all v G.vertices() do l makeSet(v) { elementary cloud } setLocator(v,l) for all e G.edges() do Q.insert(weight(e), e) while Q.isEmpty() e Q.removeMin() [u,v] G.endVertices(e) A find(getLocator(u)) B find(getLocator(v)) if A B setMSFedge(e) { merge clouds } union(A, B)
5

Partition ADT:

B 1 A 5 7

8 9 C 11 10 E 3 D

G 4 6 H 2 A
Campus Tour

B F 1 5 7

8 9 C 11 10 E 3 D

G 4 6 H 2 F

6/8/2002 2:07 PM

Campus Tour

6/8/2002 2:07 PM

Example (contd.)
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 A F 1 B 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F

Partition Implementation
Partition implementation

st e

ps

four steps
B 1 A 5 7 10 8 9 C 11 E 3 D H G 4 6 2 F

B 1 A 5 7

8 9 C 11 10 E 3 D

4 6 H 2 F

tw o

A set is represented the sequence of its elements A position stores a reference back to the sequence itself (for operation find) The position of an element in the sequence serves as locator for the element in the set In operation union, we move the elements of the smaller sequence into to the larger sequence makeSet, find: O(1) union: O(min(nA, nB))
Campus Tour

Amortized analysis

Worst-case running times


Consider a series of k Partiton ADT operations that includes n makeSet operations Each time we move an element into a new sequence, the size of its set at least doubles An element is moved at most log2 n times Moving an element takes O(1) time The total time for the series of operations is O(k + n log n)
8

6/8/2002 2:07 PM

Campus Tour

6/8/2002 2:07 PM

Analysis of Kruskals Algorithm


Graph operations

Decorator Pattern
Labels are commonly used in graph algorithms

Methods vertices and edges are called once Method endVertices is called m times We perform m insert operations and m removeMin operations We perform n makeSet operations, 2m find operations and no more than n 1 union operations

Priority queue operations

Auxiliary data Output DFS: unexplored/visited label for vertices and unexplored/ forward/back labels for edges Dijkstra and Prim-Jarnik: distance, locator, and parent labels for vertices Kruskal: locator label for vertices and MSF label for edges
Campus Tour

The decorator pattern extends the methods of the Position ADT to support the handling of attributes (labels)

Examples

Partition operations

Label operations

We set vertex labels n times and get them 2m times

Kruskals algorithm runs in time O((n + m) log n) time provided the graph has no parallel edges and is represented by the adjacency list structure
6/8/2002 2:07 PM Campus Tour 9

has(a): tests whether the position has attribute a get(a): returns the value of attribute a set(a, x): sets to x the value of attribute a destroy(a): removes attribute a and its associated value (for cleanup purposes)

The decorator pattern can be implemented by storing a dictionary of (attribute, value) items at each position
10

6/8/2002 2:07 PM

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

TSP Approximation
4

B 2 A 5 6 C

7 2 8 1

D F E 3

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

Find a polynomial-time algorithm computing a traveling salesperson tour or prove that none exists
Campus Tour

Example of traveling salesperson tour (with weight 17)

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 Compute a minimum spanning tree Form an Eulerian circuit around the MST Transform the circuit into a tour
Campus Tour 12

Approximation algorithm

6/8/2002 2:07 PM

11

6/8/2002 2:07 PM

You might also like