Date post: | 09-Sep-2018 |
Category: |
Documents |
Upload: | trinhtuong |
View: | 221 times |
Download: | 0 times |
1
Chapter 3 Graphs
Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
3
Undirected Graphs
Undirected graph. G = (V, E)
V = nodes (non-empty)
E = edges between pairs of nodes.
Captures pairwise relationship between objects.
Graph size parameters: n = |V|, m = |E|.
V = { 1, 2, 3, 4, 5, 6, 7, 8 }
E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6,7-8}
n = 8
m = 11
4
Undirected Graphs
Undirected graph. G = (V, E)
u and v are adjacent (neighbors) in G iff there is an edge between u and
v in G
The degree d(u) of a vertex u is the number of neighbors of u
1 and 3 are adjacent
2 and 8 are not adjacent
d(3)=5
d(4)=2
5
Undirected Graphs
Important Property: For every graph G, the sum of degrees of G equals
twice the number of edges.
m=11
Sum of degrees =22
6
Undirected Graphs
Loops
Edge whose two endpoints are the same
Parallel edges
Two Edges with the same endpoints
Simple Graph
A simple graph is a graph with neither loops nor parallel edges
Most of the time we’l’l be considering simple graphs
m <= n(n-1)/2 for simple graphs
– Bound is tight for complete graphs
7
Some Graph Applications
transportation
Graph
street intersections
Nodes Edges
highways
communication computers fiber optic cables
World Wide Web web pages hyperlinks
social people relationships
food web species predator-prey
software systems functions function calls
scheduling tasks precedence constraints
circuits gates wires
8
World Wide Web
Web graph.
Node: web page.
Edge: hyperlink from one page to another.
cnn.com
cnnsi.com novell.com netscape.com timewarner.com
hbo.com
sorpranos.com
9
9-11 Terrorist Network
Social network graph.
Node: people.
Edge: relationship between two
people.
Reference: Valdis Krebs, http://www.firstmonday.org/issues/issue7_4/krebs
10
Ecological Food Web
Food web graph.
Node = species.
Edge = from prey to predator.
Reference: http://www.twingroves.district96.k12.il.us/Wetlands/Salamander/SalGraphics/salfoodweb.giff
11
Graph Representation: Adjacency Matrix
Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge.
Two representations of each edge.
Space proportional to n2.
Checking if (u, v) is an edge takes (1) time.
Identifying all edges takes (n2) time.
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
12
Graph Representation: Adjacency Matrix
Line Graph: n vertices and n-1 edges.
• Adjacency matrix is full of 0’s
• 750M vertices
• Assumption: each person has 130 friends in average
550 Petabytes to store approximately 50 Billion edges;
1 2 3 4 5 6 7 8
1 0 1 1 0 0 0 0 0
2 1 0 1 1 1 0 0 0
3 1 1 0 0 1 0 1 1
4 0 1 0 0 1 0 0 0
5 0 1 1 1 0 1 0 0
6 0 0 0 0 1 0 0 0
7 0 0 1 0 0 0 0 1
8 0 0 1 0 0 0 1 0
13
Graph Representation: Adjacency List
Adjacency list. Node indexed array of lists.
Two representations of each edge.
Space proportional to m + n.
Checking if (u, v) is an edge takes O(deg(u)) time.
Identifying all edges takes (m + n) time.
1 2 3
2
3
4 2 5
5
6
7 3 8
8
1 3 4 5
1 2 5 8 7
2 3 4 6
5
degree = number of neighbors of u
3 7
14
Graph Representation: Adjacency List
Line Graph: n vertices and n-1 edges.
• One vector of size n and one list of size n-1
• 750M vertices
• Assumption: each person has 130 friends in average
100 Gigabytes to store approximately 50 Billion edges;
14
1 2 3
2
3
4 2 5
5
6
7 3 8
8
1 3 4 5
1 2 5 8 7
2 3 4 6
5
3 7
15
Paths and Connectivity
Def. A path in an undirected graph G = (V, E) is a sequence P of nodes
v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is
joined by an edge in E.
Def. A path is simple if all nodes are distinct.
Def. An undirected graph is connected if for every pair of nodes u and
v, there is a path between u and v.
16
Cycles
Def. A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k >3, and the
first k-1 nodes are all distinct.
cycle C = 1-2-4-5-3-1
17
Distance
Def. The distance between vertexes s and t in a graph G is the number
of edges of the shortest path connecting s to t in G.
Distance(1,4) =2 Distance(6,3)= 2 Distance(7,8) =1
18
Trees
Def. An undirected graph is a tree if it is connected and does not
contain a cycle.
Theorem. Let G be an undirected graph on n nodes. Any two of the
following statements imply the third.
G is connected.
G does not contain a cycle.
G has n-1 edges.
19
Rooted Trees
Rooted tree. Given a tree T, choose a root node r and orient each edge
away from r.
Importance. Models hierarchical structure.
a tree the same tree, rooted at 1
v
parent of v
child of v
root r
22
Connectivity
s-t connectivity problem. Given two node s and t, is there a path
between s and t?
s-t shortest path problem. Given two node s and t, what is the length
of the shortest path between s and t?
Applications.
Maze traversal.
Kevin Bacon number/Erdos number.
Fewest number of hops in a communication network.
23
Breadth First Search
BFS intuition. Explore outward from s in all possible directions, adding
nodes one "layer" at a time.
BFS algorithm.
L0 = { s }.
L1 = all neighbors of L0.
L2 = all nodes that do not belong to L0 or L1, and that have an edge
to a node in L1.
Li+1 = all nodes that do not belong to an earlier layer, and that have
an edge to a node in Li.
Theorem. For each i, Li consists of all nodes at distance exactly i
from s. There is a path from s to t iff t appears in some layer.
s L1 L2 L n-1
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s)
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Busca em Largura
Notação
• Adj[u] : vertexes adjacent to u in some order
• Dequeue(S): Remove the first element from S • Enqueue(S,v) : Add v to S
1
2
3 4
5
7
6
S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
Exemplo
1
2
3 4
5
7
6
1 S
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
Exemplo
S
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
4 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
4 5 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
4 5 2 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
5 2 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
Exemplo
1
2
3 4
5
7
6
5 2 S
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
5 2 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
2 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
Exemplo
S
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
6 S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
3
Exemplo
S
3
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
Exemplo
S
3
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
7
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
1
2
3 4
5
7
6
S
Exemplo
BFS(G)
1 for every vertex s of G not explored yet
2 do Enqueue(S,s);
3 mark vertex s as visited
4 while S is not empty do 5 u ← Dequeue(S);
6 For each v in Adj[u] then
7 if v is unexplored then 8 mark edge (v,u) as tree edge
9 mark vertex v as visited
10 Enqueue(S,v)
Adjacency List 1->4,5,2 2-> 4,6,1 3->7 4 -> 5,2,1 5->1,4 6->2 7>-3
Breadth First Search
Definition A BFS tree of G = (V, E), is the tree induced by a BFS
search on G.
• The root of the tree is the startpoint of the BFS
• A node u is a parent of v if v is first visited when the BFS
traverses the neighbors of u
Observation For the same graph there can be different BFS trees.
The BFS tree topology depends on the startpoint of the BFS and the
rule employed to break ties (how the data structure is traversed)
43
Breadth First Search
Property. Let T be a BFS tree of G = (V, E), and let (x, y) be an edge of
G. Then the level of x and y differ by at most 1.
L0
L1
L2
L3
44
Breadth First Search: Analysis
Análise O(n2):
(i) Cada vértice entra na fila S no máximo uma vez Loop While é
executado no máximo n vezes.
(ii) Cada vértice tem no máximo n-1 vizinhos For é executado no
máximo n vezes
(i) e (ii) implicam em O(n2)
Análise O(m + n)
(i) custo dentre do While O(n)
(ii) Custo dentro do For para vértice u é degree(u). Somando para
todos os vertices temos uV degree(u) = 2m
(i) e (ii) implicam em O(n+m )
each edge (u, v) is counted exactly twice in sum: once in deg(u) and once in deg(v)
46
Connected Component
Definition: Connected set. S is a connected set if and only if
v is reachable from u and u is reachable from v for every u,v in S
Definition: Connected Component. S is a connected component if and
only if
S is a connected set
for every u in V-S, S {u} is not connected
47
Connected Component
Connected components.
Connected component containing node 1 = { 1, 2, 3, 4, 5, 6, 7, 8 }.
48
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
49
Flood Fill
Flood fill. Given lime green pixel in an image, change color of entire
blob of neighboring lime pixels to blue.
Node: pixel.
Edge: two neighboring lime pixels.
Blob: connected component of lime pixels.
recolor lime green blob to blue
Busca em Profundidade
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
Busca em Profundidade : Exemplo
1
2
3 4
5
7
6
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
60
Depth First Search: Analysis
Uma DFS um grafo G tem complexidade de pior caso O(m + n) já que
cada vértice e aresta são visitados exatamente uma vez
Depth First Search
Definition A DFS tree of G = (V, E), is the tree induced by a DFS
search on G.
• The root of the tree is the startpoint of the DFS
• A node u is a parent of v if v is first visited when the DFS
traverses the neighbors of u
Observation For a given graph there can be different DFS trees. The
DFS tree topology depends on the startpoint of the DFS and the rule
employed to break ties (how the data structure is traversed)
Propriedades da Busca de Profundidade
Teorema: Seja T a árvore produzida por uma DFS em um grafo não direciondo G
e seja vw uma aresta de G. Se v é visitado antes de w então v é ancestral de w em T.
Arestas em preto: árvore DFS Arestas em preto e laranja: arestas do grafo
Propriedades da Busca de Profundidade
Nota: Se T é uma árvore produzida por uma DFS em G, existe um caminho entre v
e w em G e v é visitado antes de w então não necessariamente v é ancestral de w em
T.
Propriedades da Busca de Profundidade
Nota: Se T é uma árvore produzida por uma DFS em G, existe um caminho entre v
e w em G e v é visitado antes de w então não necessariamente v é ancestral de w em
T.
v w
Encontrando ciclos usando DFS
DFS(G)
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
vpai(w)
6 Senao
7 Se w<>pai(v)
8 Return Existe Ciclo
9 Fim Se
10 Fim Para
Encontrando ciclos usando DFS
Ida. Se w já foi visitado, é vizinho de v e não é pai de v
Note que v não pode ser pai de w porque w já foi visitado.
Temos dois casos:
a) w é um descendente de v. Isto não pode acontecer porque a busca
já teria sido interrompida por detectar um ciclo ao tenta visitar v a
partir de w.
b) w é um ancestral de v. Então o caminho de w até v na árvore mais a
aresta vw formam um ciclo
Volta. Existe um ciclo C no grafo. Então ...
Seja v o último vértice de C visitado pela DFS. Então os dois vizinhos de
v em C são ancestrais de v na árvore DFS e pelo menos um deles não é
pai de v. Portanto ao visitar tal vizinho detectamos um ciclo.
66
67
DFS numbering
DFS(G)
0. time 1
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
1.5 pre(v) time; time++
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
6 Fim Para
7 pos(v) time; time++
1,8
2,7
3,4
5,6
68
Articulation Points
• A node of the graph is an articulation point in an undirected graph if its removal disconnect the graph
• How to find the articulation points of G?
• How to find the articulation points of G quickly?
Articulation Point
69
Articulation Points
• Brute Force Algorithm
For every vertex v
If G-v is not connected Display ‘v is an articulation point’ Time complexity O(n(n+m))
70
Articulation Points
• Clever algorithm (approach) Consider a node v in the DFS tree for G Case 1) v is the root • v has more than one child v is an articulation
point • v has only one child v is not an articulation
point Case 2) v is a leaf v is not an articulation point Case 3) v is an internal node
If one of the children of v, say u, is such that DFS(u) only finds edges (x,y) with pre(y)>=pre(v) then v is an articulation point.
71
Articulation Points
• Clever algorithm (justification) Consider a node v in the DFS tree T for G Case 1) v is the root • v has more than one child v is an
articulation point It is not possible to have na edge between nodes in trees rooted different chidren of v
• v has only one child v is not an articulation
point T-{v} is a connected graph
72
Articulation Points
• Clever algorithm (justification) Case 2) v is a leaf v is not an articulation point because T-{v} is a connected graph
Case 3) v is an internal node If one of the children of v, say u, is such that DFS(u) only finds edges (x,y) with pre(y)>=pre(v) then v is an articulation point. If x lies in subtree rooted at u and (x,y) is an edge then y is a descendant of v. Thus, removing v disconnects T(u) from the parent of v
73
Articulation Points
r é ponto de articulacão. Propriedade da DFS garante que não pode haver arestas entre árvores vermelhas
rRrr
. . ,
75
Articulation Points
Nós de alguma subárvore de v não tem arestas para ancestrais de v v é ponto de articulacãó
vRrr
rRrr
76
Articulation Points
Nós de todas subárvore de v tem arestas para ancestrais de v v não é ponto de articulacão
vRrr
rRrr
77
Menor: vetor global com uma entrada para cada vértice
DFS-Visit(G, v)
Marque v como visitado
pre(v) time; time++
Menor(v)pre(v) % calcula o menor pre encontrado na DFS(v)
Para todo w em Adj(v)
Se w não visitado então Insira w na lista dos filhos de v.
DFS-Visit(G, w)
Menor(v)Min(Menor(v),Menor(w))
Fim Para
Se ( v é raiz e tem mais de um filho) ou (v não é raíz e
menor(w)>=pre(v) para algum filho w de v) entao
“ v é ponto de articulaçao”
Articulation Points
Complexidade O(m+n)
79
Conclusion
• Simple linear O(m+n) algorithm to compute the articulation points of G
• A problem for which the DFS is the suitable choice to traverse the graph
81
Bipartite Graphs
Def. An undirected graph G = (V, E) is bipartite if the nodes can be
colored red or blue such that every edge has one red and one blue end.
Applications.
Stable marriage: men = red, women = blue.
Scheduling: machines = red, jobs = blue.
a bipartite graph
82
Testing Bipartiteness
Testing bipartiteness. Given a graph G, is it bipartite?
Many graph problems become:
– easier if the underlying graph is bipartite (matching)
– tractable if the underlying graph is bipartite (independent set)
Before attempting to design an algorithm, we need to understand
structure of bipartite graphs.
v1
v2 v3
v6 v5 v4
v7
v2
v4
v5
v7
v1
v3
v6
a bipartite graph G another drawing of G
83
An Obstruction to Bipartiteness
Lemma. If a graph G is bipartite, it cannot contain an odd length cycle.
Pf. Not possible to 2-color the odd cycle, let alone G.
bipartite (2-colorable)
not bipartite (not 2-colorable)
84
Bipartite Graphs
Lemma. Let G be a connected graph, and let L0, …, Lk be the layers
produced by BFS starting at node s. Exactly one of the following holds.
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
(ii) An edge of G joins two nodes of the same layer, and G contains an
odd-length cycle (and hence is not bipartite).
Case (i)
L1 L2 L3
Case (ii)
L1 L2 L3
85
Bipartite Graphs
Lemma. Let G be a connected graph, and let L0, …, Lk be the layers
produced by BFS starting at node s. Exactly one of the following holds.
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
(ii) An edge of G joins two nodes of the same layer, and G contains an
odd-length cycle (and hence is not bipartite).
Pf. (i)
Suppose no edge joins two nodes in the same layer.
By previous lemma, this implies all edges join nodes on consecutive
levels.
Bipartition: red = nodes on odd levels, blue = nodes on even levels.
Case (i)
L1 L2 L3
86
Bipartite Graphs
Lemma. Let G be a connected graph, and let L0, …, Lk be the layers
produced by BFS starting at node s. Exactly one of the following holds.
(i) No edge of G joins two nodes of the same layer, and G is bipartite.
(ii) An edge of G joins two nodes of the same layer, and G contains an
odd-length cycle (and hence is not bipartite).
Pf. (ii)
Suppose (x, y) is an edge with x, y in same level Lj.
Let z = lca(x, y) = lowest common ancestor.
Let Li be level containing z.
Consider cycle that takes edge from x to y,
then path from y to z, then path from z to x.
Its length is 1 + (j-i) + (j-i), which is odd. ▪
z = lca(x, y)
(x, y) path from y to z
path from z to x
87
Obstruction to Bipartiteness
Corollary. A graph G is bipartite iff it contains no odd length cycle.
5-cycle C
bipartite (2-colorable)
not bipartite (not 2-colorable)
88
Exercícios de Implementação
1. Modifique o código da BFS para que esta preencha um vetor d com n
entradas aonde d(u) é a distância da origem s até o vértice u.
2. Modifique o algoritmo de busca em profundidade para que ele atribua
números inteiros aos vértices do grafo de modo que
(i) Vértices de uma mesma componente recebam o mesmo número
(ii) Vértices de componentes diferentes recebam números diferentes
3. Modifique o código da BFS para que ela identifique se um grafo é
bipartido ou não.
90
Directed Graphs
Directed graph. G = (V, E)
Edge (u, v) goes from node u to node v.
Ex. Web graph - hyperlink points from one web page to another.
Directedness of graph is crucial.
Modern web search engines exploit hyperlink structure to rank web
pages by importance.
The in-degree d-(u) of a vertex u is the number of edges that arrive at u
The out-degree d+(u) of a vertex u is the number of edges that leave u
Important property
sum of indegrees = sum of outdegre = m
91
Directed Graphs
d-(u)=2
d+(u)=1 u
Representation via Adjacency List
1 2 3
2
3
1 3
1 2
92 92
1
2 3
1 2 3
2
3
1 3
1
2 3
Undirected Graph
Directed Graph
93
Graph Search
Directed reachability. Given a node s, find all nodes reachable from s.
Directed s-t shortest path problem. Given two node s and t, what is
the length of the shortest path between s and t?
Graph search. BFS extends naturally to directed graphs.
Web crawler. Start from web page s. Find all web pages linked from s,
either directly or indirectly.
94
DFS numbering
Useful information pre(v): “time” when a node is visited in a DFS pos(v): “time” when DFS(v) finishes
1/16
d
b
f
e
a
c
g
h
2/15 3/14
4/13
7/12 8/11
9/10 5/6
95
DFS numbering
DFS(G)
0. time 1
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
1.5 pre(v) time; time++
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
6 Fim Para
7 pos(v) time; time++
96
DFS numbering
Importanto property • If u and v are nodes in G and u is visited before v
in a DFS then one of the conditions hols: 1. pre(u) < pre(v) < pos(v) < pos(u). In this case, u
is an ancestor of v in the DFS tree
2. pre(u) < pos(u) < pre(v) < pos(v). In this case, u is not an ancestor of v nor v is an ancestor of u in the DFS tree
Note: it is not possible to have pre(u) < pre(v) < pos(u) < pos(v).
How to find a directed cycle
1
2 3
1 2 3
2
3
3
• When 1 tries to visit 3, 3 has already been visited and 3 is not a parent of 1.
• This does not imply on the existence of a cycle
98
DFS numbering – Finding a Directed Cycle
DFS(G)
0. time 1
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
1.5 pre(v) time; time++
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
6 Senao
7 Se pos(w) indefinido
8 Existe ciclo
9 Fim Para
10 pos(v) time; time++
99
DFS numbering – Finding a Directed Cycle
• Se w já foi visitado e o pos dele é indefinido, enão DFS(w) ainda não terminou de
executar. Logo, w é ancestral de v na árvore DFS
DFS(G)
0. time 1
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
1.5 pre(v) time; time++
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
6 Senao
7 Se post(w) indefinido
8 Existe ciclo
9 Fim Para
10 Post(v) time; time++
101
Directed Acyclic Graphs
Def. A DAG is a directed graph that contains no directed cycles.
Ex. Precedence constraints: edge (vi, vj) means vi must precede vj.
a DAG
v2 v3
v6 v5 v4
v7 v1
Not a DAG
v2 v3
v6 v5 v4
v7 v1
102
Precedence Constraints
Precedence constraints. Edge (vi, vj) means task vi must occur before vj.
Applications.
Course prerequisite graph: course vi must be taken before vj.
Compilation: module vi must be compiled before vj. Pipeline of
computing jobs: output of job vi needed to determine input of job vj.
What is a feasible order to compile the jobs?
103
Directed Acyclic Graphs
Def. A topological order of a directed graph G = (V, E) is an ordering
of its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i < j.
G a topological ordering for G
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7
104
Directed Acyclic Graphs
v2 v3
v6 v5
No topological order
v2 v3
v6 v5
v4
Topological orders: v6->v2->v3->v5->v4 v6->v2->v5->v3->v4
Def. A topological order of a directed graph G = (V, E) is an ordering of
its nodes as v1, v2, …, vn so that for every edge (vi, vj) we have i < j.
106
Directed Acyclic Graphs
Lemma. If G has a topological order, then G is a DAG.
Pf. (by contradiction)
Suppose that G has a topological order v1, …, vn and that G also has a
directed cycle C. Let's see what happens.
Let vi be the lowest-indexed node in C, and let vj be the node just
before vi; thus (vj, vi) is an edge.
By our choice of i, we have i < j.
On the other hand, since (vj, vi) is an edge and v1, …, vn is a
topological order, we must have j < i, a contradiction. ▪
v1 vi vj vn
the supposed topological order: v1, …, vn
the directed cycle C
107
Directed Acyclic Graphs
Lemma. If G has a topological order, then G is a DAG.
Q. Does every DAG have a topological ordering?
Q. If so, how do we compute one?
108
Directed Acyclic Graphs
Lemma. If G is a DAG, then G has a node with no incoming edges.
Pf. (by contradiction)
Suppose that G is a DAG and every node has at least one incoming
edge. Let's see what happens.
Pick any node v, and begin following edges backward from v. Since v
has at least one incoming edge (u, v) we can walk backward to u.
Then, since u has at least one incoming edge (x, u), we can walk
backward to x.
Repeat until we visit a node, say w, twice.
Let C denote the sequence of nodes encountered between
successive visits to w. C is a cycle. ▪
w x u v
109
Directed Acyclic Graphs
Lemma. If G is a DAG, then G has a topological ordering.
Pf. (by induction on n)
Base case: true if n = 1.
Given DAG on n > 1 nodes, find a node v with no incoming edges.
G - { v } is a DAG, since deleting v cannot create cycles.
By inductive hypothesis, G - { v } has a topological ordering.
Place v first in topological ordering; then append nodes of G - { v }
in topological order. This is valid since v has no incoming edges. ▪
DAG
v
117
Topological Ordering Algorithm: Example
Topological order: v1, v2, v3, v4, v5, v6, v7.
v2 v3
v6 v5 v4
v7 v1
v1 v2 v3 v4 v5 v6 v7
118
Topological Sorting Algorithm: Running Time
Algorithm 1
Scan the graph to fill a vector count that stores, for each node
v, the number of remaining edges that are incident in v
i0
While i< n
v node with minimum value in count
i++
If v has value larger than 0
Return G is not a DAG
End If
Add v to the topological order
Remove v from count
Update the vector count for the nodes adjacent to v
End
119
Topological Sorting Algorithm: Running Time
Analysis : count stored as a vector
O(n+m) to compute the count
The loop executes at most n times
O(n) to find the node v with minimum degree
O(1) to remove v
O(d(u)) to update the neighbors of v
O( n2 + m)
Analysis : count stored as a heap
O(n+m) to compute the vector count
The loop executes at most n times
O(1) to find the node v with minimum degree
O(log n) to remove v
O( d+(u) log n) to update the neighbors of v
O( n log n + m log n)
120
Topological Sorting Algorithm: Running Time
Theorem. Algorithm finds a topological order in O(m + n) time.
Pf.
Maintain the following information:
– count[w] = remaining number of incoming edges
– S = set of remaining nodes with no incoming edges
Initialization: O(m + n) via single scan through graph.
Update: to delete v
– remove v from S
– decrement count[w] for all edges from v to w, and add w to S if
count[w] hits 0
– this is O(1) per edge ▪
Strong Connectivity
Def. Node u and v are mutually reachable if there is a path from u to v
and also a path from v to u.
Def. A graph is strongly connected if every pair of nodes is mutually
reachable.
How to decide wether a given graph is strongly connected?
Strong Connectivity
Algorithm 1
SC true
For all u,v in V
If DFS(u) does not reach v
SC False
End If
End
Return SC
Analysis O( n2 (m+n))
Strong Connectivity
Obs. When we execute a DFS (BFS) starting at u we find all nodes
reachable from u
Algorithm 2
SC true
For all u in V
If DFS(u) does not reach |V| nodes
SC False
End If
End
Return SC
Analysis O( n (m+n) )
124
Strong Connectivity
Def. Node u and v are mutually reachable if there is a path from u to v
and also a path from v to u.
Def. A graph is strongly connected if every pair of nodes is mutually
reachable.
Lemma. Let s be any node. G is strongly connected iff every node is
reachable from s, and s is reachable from every node.
Pf. Follows from definition.
Pf. Path from u to v: concatenate u-s path with s-v path.
Path from v to u: concatenate v-s path with s-u path. ▪
s
v
u
ok if paths overlap
125
Reverse Graph
Def. The reverse graph of a graph G=(V,E) is a graph GR=(V,E’) where
E’ ={ (v,u) | (u,v) belongs to E}
Observation: The reverse graph of a graph G can be constructed in
O(m+n) time
127
Strong Connectivity: Algorithm
Theorem. Can determine if G is strongly connected in O(m + n) time.
Pf.
Pick any node s.
Run BFS from s in G.
Run BFS from s in Grev.
Return true iff all nodes reached in both BFS executions.
Correctness follows immediately from previous lemma. ▪
reverse orientation of every edge in G
strongly connected not strongly connected
129
Strongly connected components
Definition: A subgraph induced by a set of vertices C is a strongly connected component C of a directed graph G = (V,E) if the following conditions hold:
(a) For any two vertices u and v in C , there is a path from u to v and from v to u and for every (b) For every C’, with C C’, the condition (a) does not hold. • Equivalence classes of the binary path(u,v) relation,
denoted by u ~ v.
130
Strongly connected components
Applications: networking, communications. Problem: compute the strongly connected components of G in linear time Θ(|V|+|E|).
133
Strongly connected components graph
Definition: the SCC graph G~ = (V~,E~) of the graph G = (V,E) is as follows:
V~ = {C1, …, Ck}. Each SCC is a vertex.
E~ = {(Ci,Cj)| i≠j and there is (x,y)E, where xCi and yCj}.
G~ is a directed acyclic graph (no directed cycles)!
Definition: the reverse graph G R = (V,ER) of the graph G = (V,E) is G with
its edge directions reversed: E R = {(u,v)| (v,u)E}.
Observation 1. The strongly connected components are the same in G and
G R
136
SCC algorithm: Approach
Source node: node with in-degree 0 Sink node: node with out-degree 0 Recall that every DAG has both a source and a sink (see DAG slides)
137
SCC algorithm: Approach
H G While H is not empty v node that lies in a sink node of H~ (*) C SCC retuned by DFS(H,v) H H - C End If we manage to execute (*) we are done
141
DFS numbering
Useful information pre(v): “time” when a node is visited in a DFS post(v): “time” when DFS(v) finishes
1/16
d
b
f
e
a
c
g
h
2/15 3/14
4/13
7/12 8/11
9/10 5/6
142
DFS numbering
DFS(G)
0. time 1
1 Para todo v em G
2 Se v não visitado então 3 DFS-Visit(G, v)
DFS-Visit(G, v)
1 Marque v como visitado
1.5 pre(v) time; time++
2 Para todo w em Adj(v)
3 Se w não visitado então 4 Insira aresta (v, w) na árvore
5 DFS-Visit(G, w)
6 Fim Para
7 Post(v) time; time++
143
DFS numbering
Property If C an D are strongly connected components and there is an edge from a node in C to a node in D then, for every DFS, the highest pos() in C is bigger than the highest pos() in D
Proof. Let c be the first node visited in C and d be the
first node visited in D during a DFS Case 1) c is visited before d. DFS(c) visit all nodes in D (they are reachable
from c due to the existing edge) Thus, post(c) > post(x) for every x in D
144
DFS numbering
Property If C an D are strongly connected components and there is an edge from a node in C to a node in D then, for every DFS, the highest pos() in C is bigger than the highest pos() in D
Proof. Let c be the first node visited in C and d be the
first node visited in D during a DFS Case 2) d is visited before c. DFS(d) visit all nodes in D because all of them are
reachable from d DFS(d) does not visit nodes from C since they are
not reachable from d. Thus , post(x) <post(y) for every pair of nodes x,y,
with x in D and y in C
148
SCC algorithm
Idea: compute the SCC graph G~ = (V~,E~) with two DFS, one for G and one for its reverse GR, visiting the vertices in reverse order.
SCC(G) 1. DFS(G) to compute pos[v], ∀vV 2. Compute GR 3. DFS(G R) in the order of decreasing pos[v] 4. Output the vertices of each tree in the DFS forest
as a separate SCC.
151
Example: computing SCC
d
b
f
e
a
c
g
h 8
1/6
d
b
f
e
a
c
g
h
3/4
10/15
11/12 9/16
7/8 13/14
2/5
16
152
Example: computing SCC
d
b
f
e
a
c
g
h 8
1/6
d
b
f
e
a
c
g
h
3/4
10/15
11/12 9/16
7/8 13/14
2/5
16
C1
C4
C2
C3 1
2
3 4
153
Conclusion
• Simple linear O(m+n) algorithm to compute the SCC of G
• Problem for which the DFS is the suitable choice to traverse the graph
154
Modelagem de problemas com Grafos
Problema
• Seja um grafo G=(V,E) com n vértices representando a planta de um
edifício. Inicialmente temos dois robos localizados em dois vértices
de V, a e b, que devem alcançar os vértices c e d de V.
• No passo i+1 um dos dois robos deve caminhar para um vértice
adjacente ao vértice que ele se encontra no momento i. Exiba um
algoritmo polinomial para resolver o seguinte problema:
• Entrada: Grafo G=(V,E) , quatro vértices: a,b,c e d e um inteiro r.
• Saída: SIM se é possível os robos partirem dos vértices a e b e
chegarem em c e d, respectivamente, sem que em nenhum momento
eles estejam a distância menor do que r. NÃO, caso contrário.
155
Modelando com Grafos
Solução
Seja H=(V ’,E ’ ) um grafo representando as configurações possíveis
(posições dos robos) do problema. Cada nó de H corresponde a um par
ordenado de vértices de V cuja distância é menor ou igual a r. Logo
existem no máximo |V|2 vértices em V ’.
Um par de nós u e v de H tem uma aresta se e somente em um passo é
possível alcançar a configuração v a partir da configuração u. Mais
formalmente, se uv é uma aresta de E’, com u=(u1,u2) e v=(v1,v2), então
uma das alternativas é válida
(i) u1=v1 e (u2,v2) pertence a E
(ii) u2=v2 e (u1,v1) pertence a E
O problema, portanto, consiste em decidir se existe um camìnho entre o
nó x=(a,b) e o nó y=(c,d) em H.
156
Modelando com Grafos
Solução
Para construir o grafo H basta realizar n BFS’s no grafo G, cada uma
delas partindo de um vértice diferente. Ao realizar uma BFS a partir de
um nó s obtemos o conjunto de todos os vértices que estão a distância
maior ou igual a r de s. A obtenção do conjunto V’ tem custo O(n(m+n))
e a do conjunto de arestas E’ tem custo O(n3) .
Decidir se existe um camìnho entre o nó x=(a,b) e o nó y=(c,d) em H tem
complexidade O(|V’|+|E’|). Como |V’| tem O(n2) vértices e |E’| tem
O(n3) arestas, o algoritmo executa em O(n3) . Note que |E’| é O(n3)
porque cada vértice de H tem no máximo 2(n-1) vizinhos
• Análise mais cuidadosa mostra que | E ‘ | tem O(mn) arestas
157
Modelando com Grafos
Exercício. Mostrar que o conjuto E ‘ tem O(mn) arestas e analisar o
impacto disso para o algoritmo
159
Shortest Path Problem
Shortest path network.
Directed graph G = (V, E).
Source s, destination t.
Length ce = length of edge e. (non-negative numbers)
Shortest path problem: find shortest directed path from s to t.
Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 50.
s
3
t
2
6
7
4
5
23
18
2
9
14
15 5
30
20
44
16
11
6
19
6
cost of path = sum of edge costs in path
160
Dijkstra's Algorithm
Approach
Find the node closest to the s, then the second closest, then the
third closest, and so on …
Key observation:
– the shortest path from s to the k-th closest node can be
decomposed as the shortest path from s to the i-th closest node
(for some i<k) and an edge from the i-th closest node to the k-th
closest node.
s
v
u
d(u)
S
ce
161
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
24
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
32
163
Dijkstra's Shortest Path Algorithm
s
3
t
2
6
7
4
5
18
2
9
14
15 5
30
20
44
16
11
6
19
6
15
9
14
0
34
32
164
Dijkstra's Algorithm
Dijkstra's algorithm.
Maintain a set of explored nodes S for which we have determined
the shortest path distance d(u) from s to u.
Initialize S = { s }, d(s) = 0.
Repeatedly choose unexplored node v which minimizes
add v to S, and set d(v) = (v).
Complexity (Naïve Implementation)
n loops, one for each node
(n+m) to find the the node with minimum
Thus, complexity is O(n(n+m)) time
,)(min)(:),(
eSuvue
cudv
shortest path to some u in explored part, followed by a single edge (u, v)
166
Dijkstra's Algorithm: Implementation
For each unexplored node, explicitly maintain
Next node to explore = node with minimum (v).
When exploring v, for each incident edge e = (v, w), update
Efficient implementation. Maintain a priority queue of unexplored
nodes, prioritized by (v).’
† Individual ops are amortized bounds
PQ Operation
Insert
ExtractMin
ChangeKey
Binary heap
log n
log n
log n
Array
n
n
1
IsEmpty 1 1
Priority Queue
Total m log n n2
Dijkstra
n
n
m
n
.)(min)( :),(
eSuvue
cudv
.})( ),( {min )( ecvww
168
Dijkstra's Algorithm: Proof of Correctness
Invariant. For each node u S, d(u) is the length of the shortest s-u path.
Pf. (by induction on |S|)
Base case: |S| = 1 is trivial.
Inductive hypothesis: Assume true for |S| = k 1.
Let v be next node added to S, and let u-v be the chosen edge.
The shortest s-u path plus (u, v) is an s-v path of length (v).
Consider any s-v path P. We'll see that it's no shorter than (v).
Let x-y be the first edge in P that leaves S,
and let P' be the subpath to x.
P is already too long as soon as it leaves S.
c (P) c (P') + c (x,y) d(x) + c(x, y) (y) (v)
nonnegative weights
inductive hypothesis
defn of (y) Dijkstra chose v instead of y
S
s
y
v
x
P
u
P'