Documente Academic
Documente Profesional
Documente Cultură
1
5
2
4
What is a graph ?
G=(V,E)
V = a set of vertices
E = a set of edges
edge = unordered pair of vertices
1
2
3
5
What is a graph ?
G=(V,E)
V = {1,2,3,4,5}
E = {{1,5}, {3,5}, {2,3}, {2,4},
1
5
2
4
What is a graph ?
G=(V,E)
V = {1,2,3,4,5}
E = {{1,5}, {2,3}, {2,4}, {3,4}}
1
5
2
4
Connectedness
1
2
4
1
5
connected
not connected
Representing a graph
adjacency matrix
|V| * |V| symmetric matrix A
with Ai,j = 1 if {i,j} E
Ai,j = 0 otherwise
Representing a graph
adjacency matrix
1
5
2
4
space = (V2)
Representing a graph
adjacency matrix
0
0
1
0
1
0
0
1
1
0
1
1
0
1
0
0
1
1
0
0
1
0
0
0
0
is {u,v} an edge ?
list neighbors of v
2
4
space = (V2)
(?)
(?)
Representing a graph
adjacency matrix
0
0
1
0
1
0
0
1
1
0
1
1
0
1
0
0
1
1
0
0
1
0
0
0
0
is {u,v} an edge ?
list neighbors of v
2
4
space = (V2)
(1)
(n)
Representing a graph
adjacency lists
for each vertex v V
linked list of neighbors of v
Representing a graph
adjacency lists
1
5
1:
2:
3:
4:
5:
3,5
3,4
1,2,4
2,3
1
2
4
space = (E)
Representing a graph
adjacency lists
1:
2:
3:
4:
5:
3,5
3,4
1,2,4
2,3
1
2
4
space = (E)
is {u,v} an edge ?
list neighbors of v
(?)
(?)
Representing a graph
adjacency lists
1:
2:
3:
4:
5:
3,5
3,4
1,2,4
2,3
1
1
5
2
4
space = (E)
is {u,v} an edge ?
list neighbors of v
(min(dv,d
(dv)
Representing a graph
adjacency lists
1:
2:
3:
4:
5:
3,5
3,4
1,2,4
2,3
1
space = (E)
is {u,v} in E ?
neigbors of v ?
(min{du,d
(dv)
adjacency matrix
0 0 1 0 1
space = (V2)
0
1
0
1
0
1
1
0
1
0
1
0
1
1
0
0
0
0
0
0
is {u,v} in E ?
neigbors of v ?
(1)
(n)
INPUT:
OUTPUT:
number of components of G
BFS (G,v)
G undirected graph, V={1,...,n}
seen[v] = false for all v V
Q=queue (FIFO)
seen[v] true
enqueue(Q,v)
while Q not empty do
w dequeue(Q)
for each neighbor u of w
if not seen[u] then
seen[u] true
enqueue(Q,u)
DFS
G undirected graph, V={1,...,n}
visited[v] = false for all v V
explore(G,v)
visited[v] true
for each neighbor u of v
if not visited(u) then explore(G,
DFS
G undirected graph, V={1,...,n}
visited[v] = false for all v V
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
DFS
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
vertex Iv := [pre[v],post[v]]
interval property
for u,v V either
* Iv and Iu are disjoint, or
* one is contained in the other
DFS
A
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
B
C
DFS
A
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
tree edges
B
C
(u,v)
adjacency matrix
|V| * |V| matrix A
with Ai,j = 1 if (i,j) E
Ai,j = 0 otherwise
INPUT:
OUTPUT:
DFS on digraphs
G = digraph, V={1,...,n}
visited[v] = false for all v V
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each out-neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
DFS on digraphs
A
B
C
DFS on digraphs
A
root
B
C
descendant, ancestor
child, parent
DFS on digraphs
A
tree edge
B
C
DFS on digraphs
A
tree edge
B
C
DFS on digraphs
back edge A
tree edge
B
C
DFS on digraphs
back edge A
tree edge
B
C
D
cross edge
DFS on digraphs
back edge A
tree edge
B
forward edge
C
D
cross edge
tree edge
B
forward edge
C
D
cross edge
Lemma:
digraph is a DAG
if and only if
DFS has a back edge.
Lemma:
in a DAG every edge goes
to a vertex with lower post
explore(G,v)
visited[v] true
pre[v] clock; clock++
for each neighbor u of v
if not visited(u) then explore(G,
post[v] clock; clock++
(strong) connectedness
(strong) connectedness
How to check if a digraph is
strongly connected?
(strong) connectedness
How to check if a digraph is
strongly connected?
for every uV do
DFS(G,u)
check if every vV was visited
(strong) connectedness
How to check if a digraph is
strongly connected?
pick some uV
DFS(G,u)
check if every vV was visited
DFS(reverse(G),u)
check if every vV was visited