Transcript Slide 1

Graphs
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
An edge e = (u1,u2) is a pair of vertices
• Examples:
– Bus routes
– Transitions in a finite state machine
– Course prerequisites
2
What is a Graph?
• If the elements of E(G) are ordered pairs
then G is called a directed graph or digraph;
else G is called an undirected graph
• In an undirected graph, the pairs (u1,u2) and
(u2,u1) represent the same edge
3
What is a Graph?
2
2
3
1
3
1
4
5
Undirected Graph
4
5
Directed Graph
4
Applications
• Electronic circuits
– Printed circuit board
– Integrated circuit
• Transportation networks
– Highway network
– Flight network
• Computer networks
– Local area network
– Internet
– Web
JFK
LAX
HNL
STL
DFW
5
Terminology
Adjacent and Incident
• If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
• If (v0, v1) is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge (v0, v1) is incident on v0 and v1
v0
v1
v0
v1
6
Terminology (cont.)
Degree of a Vertex
• The degree of a vertex is the number of edges incident to
that vertex
• For directed graph,
- the in-degree of a vertex v is the number of edges that have v as the head
- the out-degree of a vertex v is the number of edges that have v as the tail
if di is the degree of a vertex i in a graph G with n vertices and e edges, the
number of edges is
e(
n 1
d ) / 2
i
0
Why? Since adjacent vertices each
count the adjoining edge, it will be
counted twice
7
Examples
Undirected Graph
3
Directed Graph
0
in:1, out: 1
1
in: 1, out: 2
2
in: 1, out: 0
0
3
1
2
3
G1
3
3
G2
8
Terminology (cont.)
• Path
– sequence of vertices v1,v2,. . .vk
such that consecutive vertices
vi and vi+1 are adjacent.
• Simple path
– no repeated vertices
• Examples
– P1=(V, X, Z) is a simple path
a
U
c
V
b
d
P2
P1
X
e
W
h
Z
g
f
Y
– P2=(U, W, X, Y, W, V) is a path that
is not simple
9
Terminology (cont.)
• Cycle
– circular sequence of vertices,
where that the last vertex is the
same as the first vertex .
• Simple cycle
– simple path, except that the
last vertex is the same as the
first vertex
• Examples
– C1=(V, X, Y, W, U, V) is a simple
cycle
– C2=(U, W, X, Y, W, V, U) is a cycle
that is not simple
a
U
c
V
b
d
C2
X
e
C1
g
W
f
h
Z
Y
10
Terminology (cont.)
• Subgraph
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 S
• Spanning subgraph
A spanning subgraph of G is a
subgraph that contains all the
vertices of G
Spanning subgraph
11
Subgraphs Examples
0
1
0
0
1
0
2
1
2
2
3
G1
0
0
1
2
G2
(i)
2
3
3
(i)
1
(ii)
(iii)
(a) Some of the subgraph of G1
(iv)
0
0
0
1
1
1
2
2
(ii)
(iii)
(b) Some of the subgraph of G2
(iv)12
Terminology (cont.)
• Connected graph
– there is a path between every
pair of vertices
• Connected component
Connected graph
– it is the maximal connected
subgraph of G
Non connected graph with two
connected components
13
Terminology (cont.)
• Tree
– it is a connected graph
without cycles
tree
// This definition is different from
the rooted tree (in chapter 9)
tree
• Forest
– collection of trees (i.e. it is an
undirected graph without
cycles)
forest
tree
tree
14
Terminology (cont.)
•
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
Graph
Spanning tree
15
Terminology (cont.)
• A complete graph is a graph in which
all pairs of vertices are adjacent
• How many total edges in a complete
graph?
– Each of the n vertices is incident to n-1
edges, however, we would have counted
each edge twice! Therefore, intuitively,
m = n(n -1)/2 edges.
• Therefore, if a graph is not complete,
m < n(n -1)/2
n=5
m = (5*4)/2
16
Graph Representation
Two standard ways
– Adjacency-matrix representation
Space required O(n2).
– Adjacency-list representation
Space required O(|E|)
Depending on problems, both representations are useful.
17
Adjacency-matrix
representation


Assume that the nodes are numbered 1, 2, …, n.
The adjacency-matrix consists of a |V ||V | matrix A=(aij) such
that
if (i,j)  E
aij= 1
else aij= 0.
1
2
3
5
1
2
3
4
5
1
0
1
0
0
1
2
1
0
1
1
1
3
0
1
0
1
0
4
0
1
1
0
1
5
1
1
0
1
0
4
18
Adjacency-matrix
representation
•
•
Space required O(n2)
The adjacency matrix
for an undirected graph
is symmetric. Hence,
we may store only lower
or upper triangle
1
2
3
4
5
1
0
1
0
0
1
2
1
0
1
1
1
3
0
1
0
1
0
4
0
1
1
0
1
5
1
1
0
1
0
19
Examples for Adjacency Matrix
0
0
1
2
1
3
2
0
1

1

1
1
0
1
1
1
1
0
1
1
1 
1

0
5
1
2
6
3
G2
G1
symmetric
undirected: n2/2
directed: n2
4
0
0 1 0


1
0
1


0 0 0
7
0
1

1

0
0

0
0

0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
G3
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0

0
0

0
1

0
20
Adjacency Matrix Analysis
Operation
The degree of a vertex v is
Time
n 1
 a[i][ j ]
O(n)
j 0
For a digraph (= directed graph), the row sum is the out degree,
while the column sum is the in degree
n 1
indegree(vi )   A[ j ][i]
j 0
n 1
outdegree(vi )   A[i][ j ]
O(n)
j 0
Find the edges incident to a given vertex v
O(n)
Check that two vertices v and w are adjacent
O(1)
Insert (or remove) a Vertex v
O(n2)
Insert (or remove) and Edge e
O(1)
21
Adjacency-list
representation
• Let G=(V, E) be a graph
– V– set of nodes (vertices)
– E– set of edges.
• For each wV, the adjacency list A[w] contains all nodes
in V that are adjacent to u.
1
2
3
5
4
(a)
1
2
5
2
1
5
3
2
4
4
2
5
3
/
5
4
1
2
/
(b)
/
4
3
/
/
22
Examples for Adjacency List
4
0
0
1
2
1
2
3
1
1
0
2
3
2
0
1
3
0
1
2
3
0
1
2
1
0
3
2
0
3
3
1
2
4
5
5
4
6
6
5
7
7
6
6
3
7
2
3
G1
0
1
2
5
0
0
1
1
0
2
2
G2
G3
23
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Adjacency List Analysis
Operation
Time
Find the edges incident to a given vertex v
O(degree(v))
Check that two vertices v and w are adjacent
O(min
(degree(v),
degree(w)))
Insert a Vertex v
O(1)
Remove a Vertex v
O(degree(v))
Insert an Edge e
O(1)
24
Example Graph in JAVA
public class Graph<E>
{
// 1. The vertex numbers range from 0 to labels.length-1.
// 2. For each vertex number i, labels[i] contains the label for vertex i.
// 3. For any two vertices i and j, edges[i][j] is true if there is a
//
vertex from i to j; otherwise edges[i][j] is false.
private boolean[ ][ ] edges;
private E[ ] labels;
:
:
// Graph methods appear here
}
25
Example Graph in JAVA
public Graph(int n)
{
edges = new boolean[n][n]; // All values initially false
labels = (E[ ]) new Object[n];
// All values initially null
}
public void addEdge(int source, int target)
{
edges[source][target] = true;
}
26
Example Graph in JAVA
public Object getLabel(int vertex)
{
return labels[vertex];
}
public boolean isEdge(int source, int target)
{
return edges[source][target];
}
27
Example Graph in JAVA
public int[ ] neighbors(int vertex)
{
int i;
int count;
int[ ] answer;
// First count how many edges have the vertex as their source
count = 0;
for (i = 0; i < labels.length; i++)
{
if (edges[vertex][i])
count++;
}
:
:
28
Example Graph in JAVA
public int[ ] neighbors(int vertex)
{
:
:
// Allocate the array for the answer
answer = new int[count];
// Fill the array for the answer
count = 0;
for (i = 0; i < labels.length; i++)
{
if (edges[vertex][i])
answer[count++] = i;
}
return answer;
}
29
Example Graph in JAVA
public void removeEdge(int source, int target)
{
edges[source][target] = false;
}
public void setLabel(int vertex, E newLabel)
{
labels[vertex] = newLabel;
}
public int size( )
{
return labels.length;
}
30
Graph Traversals
•
No obvious order of traversal (like trees)
•
No obvious starting point (no root)
•
Traversals may not reach every vertex by
following edges (connectedness)
•
Traversals may return to a vertex (cycles)
References:
Data Structures and Algorithms in JAVA
By M. Goodrich and R. Tamassia
Chapters 9 & 10
31
Graph Traversals
• Traversal Algorithms:
- Depth-first search DFS.
- Breadth-first search BFS.
References:
Data Structures and Algorithms in JAVA
By M. Goodrich and R. Tamassia
Chapters 9 & 10
32
Depth-first search DFS
• Visits a vertex, then
– visit a neighbor of the vertex,
– visit a neighbor of the neighbor,
– Etc.
• Advance as possible from the original
vertex
• Then back up by one vertex
– Considers the next neighbor
33
Example
unexplored vertex
visited vertex
unexplored edge
discovery edge
back edge
A
A
A
B
D
E
A
D
C
E
C
A
B
D
E
B
C
34
Example (cont.)
A
B
A
D
E
B
C
C
A
A
B
D
C
E
B
D
E
D
E
C
35
DFS Algorithm
Algorithm DFS( v)
Input A vertex v of a graph G
Output labeling of the edges of G in the connected component of v as discovery
edges and back edges
label v as visited
for each edge e incident on v do
if edge e is unexplored then
let w be the other endpoint of e
if vertex w is unvisited then
Label e as a discovery edge
recursively call DFS(w)
else
label e as a back edge
36
Example 2
(a)
(b)
(c)
(d)
(e)
(f)
Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia
37
Properties of DFS
Property 1
v
DFS(G, v) visits all the vertices in
the connected component of v
A
B
Property 2
The discovery edges labeled by
DFS(G, v) form a spanning tree of
the connected component of v
D
E
C
38
Properties of DFS
Property 3
Let G be a graph with n vertices and m edges. A DFS traversal of G
can be performed in time O(n+m). Also, there exist O(n+m) time
algorithms based on DFS for the following problems:
– Computing a spanning tree of G, if G is connected
– Computing the connected components of G
– Computing a path from one given vertex to another in G
– Computing a cycle in G, or reporting that G has no cycles
39
Breadth-first search BFS
• Visits a vertex, then
– visit each of the vertex's neighbors before
advancing
40
Example
unexplored vertex
visited vertex
unexplored edge
discovery edge
cross edge
A
A
L0
L1
L0
L1
L0
C
E
B
D
F
C
E
A
B
A
L1
D
F
A
B
C
E
D
F
Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
41
Example
L0
L1
L0
A
B
C
E
L0
L1
F
L0
C
E
B
L2
A
B
L2
D
L1
D
F
L1
A
C
E
F
A
B
L2
D
C
E
D
F
Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
42
Example
L0
L1
L0
L1
A
B
L2
C
E
D
L1
B
L2
F
A
C
E
D
F
A
B
L2
L0
C
E
D
F
Note: BFS subdivides the vertices into levels (L0, L1,….etc.)
43
Breadth-first search BFS
Algorithm BFS(v)
Input A vertex v of a graph G
Output labeling of the edges of G in the connected component of v as discovery
edges and cross edges
initialize container L0 to contain vertex v
i0
while Li is not empty do
create container Li+1 to initially be empty
for each vertex v in Li do
for each edge e incident on v do
if edge e is unexplored then
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else label e as a cross edge
ii+1
44
Example
45
Data Structures and Algorithms in JAVA, M. Goodrich and R. Tamassia
Properties
A
Property 1
BFS(s) visits all the vertices in the
connected component of s
B
Property 2
E
The discovery edges form a spanning
tree Ts of connected component of s
Property 3
L0
For each vertex v in Li
– The path of Ts from s to v has i
edges
– Any other path from s to v in Gs has
at least i edges
C
L1
F
A
B
L2
D
C
E
D
F
46
Properties
Property 4
Let G be a graph with n vertices and m edges. A BFS traversals of
G takes time O(n+m). Also, there exist O(n+m) time algorithms
based on BFS for the following problems:
– Testing whether G is connected
– Computing a spanning tree of G, if G is connected
– Computing the connected components of G
– Computing, for every vertex v of G, the minimum number of edges of
any path between s and v
47
Another Implementation for BFS
Algorithm BFS(v)
Input A vertex v of a graph G
Output processing the vertices of the connected component of v
vertexQueue = a new queue to hold vertices as they are visited
Mark v as visited
vertexQueue.enqueue (v)
while (!vertexQueue.isEmpty ()) do
frontVertex = vertexQueue.dequeue ()
//process frontVertex
while (frontVertex has a neighbor) do
nextNeighbor = next neighbor of frontVertex
if (nextNeighbor is not visited) then
Mark nextNeighbor as visited
vertexQueue.enqueue (nextNeighbor)
48
Applications: 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
49
Applications: Path Finding
• Find path from source vertex s to destination
vertex d
• Use graph search starting at s and terminating
as soon as we reach d
• Need to remember edges traversed
• Use depth first search DFS?
• Use breath first search BFS?
50
DFS vs. BFS
DFS Process
F
B
A
G
D
C
start
E
destination
B DFS on B
A DFS on A A
G
D
B
A
C
B
A
DFS on C D
B
A
Call DFS on D
Return to call on B
Call DFS on G found destination - done!
Path is implicitly stored in DFS recursion
Path is: A, B, D, G
51
DFS vs. BFS
F
B
A
G
destination
D
C
rear
front
E
start
BFS Process
rear
front
rear
front
B
D C
Initial call to BFS on A Dequeue A
Add A to queue
Add B
Dequeue B
Add C, D
A
rear
rear
front
D
Dequeue C
Nothing to add
front
G
Dequeue D
Add G
found destination - done!
Path must be stored separately
52
Digraphs
• A digraph is a graph whose edges are all directed
– Short for “directed graph”
E
• Applications
D
– one-way streets
– flights
– task scheduling
C
B
• 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.
A
53
Digraph Application
• Scheduling: edge (a,b) means task a must be completed
before b can be started
cs21
cs22
cs23
cs51
cs53
cs52
cs181
cs131
cs141
cs151
cs121
cs171
The good life
54
Reachability
• Directed Graph rooted at v : vertices reachable from v via directed
paths
v E
v
E
D
C
A
D
C
A
F
B
v E
D
C
A
F
B
55
Strong Connectivity
 Each vertex can reach all other vertices
a
g
c
d
f
e
b
56
Strong Connectivity Algorithm
 Pick a vertex v in G.
a
G:
d
 Perform a DFS from v in G.
– If there’s a w not visited, print “no”.
 Running time: O(n+m).
b
a
G’:
g
c
d
– If there’s a w not visited, print “no”.
– Else, print “yes”.
e
f
 Let G’ be G with edges reversed.
 Perform a DFS from v in G’.
g
c
f
e
b
57
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
D
E
B
B
C
A
D
E
C
G
A
G*
58
Computing the Transitive Closure
 We can perform DFS starting at each vertex
– Based on the idea that, 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 The Floyd-Warshall Algorithm
59
Floyd-Warshall Transitive Closure
 Floyd-Warshall’s algorithm numbers the vertices of G as
v1 , …, vn and computes a the transitive closure in a series of rounds
– Initialize G0=G
– Begin a computation of the rounds, beginning with round 1
– In generic round k , we construct Gk as:
1. Gk = Gk-1
2. Add a directed edge (vi, vj) to Gk , if Gk-1
contains both the edges (vi, vk) and (vk, vj)
 After n rounds, we have that Gn = G*
 Running time: O(n3), assuming adjacency matrix representation (i.e.
checking whether 2 vertices are adjacent is O(1) )
60
Floyd-Warshall - Example
v7
G0=G
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
61
Floyd-Warshall - Iteration 1
v7
G1
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
62
Floyd-Warshall - Iteration 2
v7
G2
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
63
Floyd-Warshall - Iteration 3
G3
v7
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
64
Floyd-Warshall - Iteration 4
G4
v7
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
65
Floyd-Warshall - Iteration 5
v7
G5
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
66
Floyd-Warshall - Iteration 6
v7
G6
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
67
Floyd-Warshall - Conclusion
v7
G7
BOS
ORD
v
v
4
JFK
v
2
6
SFO
DFW
LAX
v
v
3
1
MIA
v
5
68
Floyd-Warshall - Algorithm
Algorithm FloydWarshall(G)
Input digraph G with n vertices
Output transitive closure G* of G
i := 1
for all v  G
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 both edges (vi, vk) and (vk, vj)  Gk  1 then
add edge (vi, vj) to Gk. (if it is not already present)
return Gn
69
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
D
B
C
v1 , …, vn
A
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 precedence constraints
A digraph might has a topological ordering if and
only if it is a DAG
DAG G
D
v2
v1
Theorem
E
v4
v5
E
B
C
A
v3
Topological
ordering of
G
70
Topological Sorting
• Number vertices, so that (u,v) in G implies u < v
wake up
1
3
2
study computer sci.
eat
4
7
play
nap
8
write c.s. program
9
A typical student day
5
more c.s.
6
work out
make cookies
for professors
10
sleep
11
dream about graphs
71
Algorithm for Topological
Sorting
Algorithm TopologicalSort(G)
H := G
// Temporary copy of G
n := the number of vertices in G
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)
72
Topological Sorting
Example
73
Topological Sorting
Example
9
74
Topological Sorting
Example
9
75
Topological Sorting
Example
8
9
76
Topological Sorting
Example
8
9
77
Topological Sorting
Example
7
8
9
78
Topological Sorting
Example
7
8
9
79
Topological Sorting
Example
6
7
8
9
80
Topological Sorting
Example
6
7
8
9
81
Topological Sorting
Example
6
5
7
8
9
82
Topological Sorting
Example
6
5
7
8
9
83
Topological Sorting
Example
4
6
5
7
8
9
84
Topological Sorting
Example
4
6
5
7
8
9
85
Topological Sorting
Example
3
4
6
5
7
8
9
86
Topological Sorting
Example
3
4
6
5
7
8
9
87
Topological Sorting
Example
2
1
3
4
6
5
7
8
9
88
Topological Sorting
Example
2
1
3
4
6
5
7
8
9
89