Escolar Documentos
Profissional Documentos
Cultura Documentos
10
BFS and DFS
Graph
G (V, E)
Graph
Representations
Adjacency Matrix:
M[i,j] = 1 (vi, vj) E(G)
M[i,j] = 0 (vi, vj) E(G)
BFS
Breadth-First
Search
Adjacency List:
Each vertex v V(G) has a list of all vertices u V(G)
where (v,u) E(G)
Question 1
Given an undirected graph G = (V,E), suggest an algorithm to direct the edges in E(G) for creating an
acyclic directed graph G' = (V,E').
Solution:
Question 2
Given a graph G=(V,E), the graph Gk is defined as follows:
Gk=(V,Ek), where (i,j) Ek There is a path of length k (not necessarily simple) from i to j in G.
1. Assume G is represented in an adjacency matrix, how can you find the graph G k
2.
Solution:
Notation:
Let n = |V|
Let Mnxn be the the adjacency matrix representing G.
Let Mk= MMM k times
claim:
Mk[i,j]= the number of paths of length k from i to j in G.
Proof by induction on k
base case:
M = M1, true by definition:
M1[i,j] = 1 (vi, vj) E(G) there is a path of length k from i to j in G.
M1[i,j] = 0 (vi, vj) E(G) there is no path from i to j in G.
Assumption:
Mk-1[i,j]= the number of paths of length k-1 from i to j in G.
Induction step:
M k M k 1 M
n
Answer:
1. Let M be the adjacency matrix representing G.
a. compute Mk
b. Let A be the adjacency matrix of Gk
c. A[i,j] = 1 Mk[i,j] > 0
A[i,j] = 0 Mk[i,j] = 0
2. Time Complexity:
Multiplying two nn matrices takes O(n3).
Therefore, Mk takes O((k-1)n3) = O(kn3).
This time can be improved to O(n3logk) by calculating Mk as follows:
Mk = Mk-1M, if k is odd
Mk = Mk/2Mk/2, if k is even.
Question 3
Update the BFS algorithm so that each node v in the graph will contain not only d[v], the length of
shortest path from s to v, but also and the number of different paths in that length.
Solution:
Each node v in the graph will contain M(v), the number of different paths to v that their length is d(v).
When going over the neighbors v of u :
Initialize M(v) = 0 for all the vertices except s
Initialize M(s) = 1 (exactly one path from s to s in length 0).
If v is a white neighbor then M(v) = M(u)
else :
If d(v) = d(u) + 1 then M(v) = M(v) + M(u)
Another way is visiting the nodes by the order of their distance from S, after a complete BFS.
for a node v in distance i from s:
M [v ]
M [u]
( u ,v )E
d [ u ] i 1
Question 4
Given an undirected graph G = (V,E).
Let's define 2 set of vertices V1 and V2 such as:
V1 V2 V
distance(u, v) = the length of the shortest path from u to v in G.
distance(V1, V2) = the shortest path between u V1 and v V2.
if V1V2 , then distance(V1, V2) = 0.
Solution:
Question 5
A road from city A to city B is represented as a couple (A,B).
Assume the roads are bi-directional.
Given a set of m roads and a set of n cities, suggest a way to preprocess the input in O(n+m) time in
order to support the following operation in O(1) time:
reachable(i,j) Returns TRUE if it is possible to drive from city i to city j
and FALSE otherwise.
Solution:
Preprocessing:
Represent the input as an undirected graph G=(V,E), where the nodes
represent the cities and the edges represent the roads.
The graph represented as an array A of size n, each entry A[i] is the
head of a linked list representing the neighbors of node i.O(n+m)
Execute DFS and for each node i update a variable c[i], the
connected-component to whom it belongs ....O(n+m)
Reachable(i,j):
return TRUE if both i and j belong to the same connected-component.O(1)
Question 6
Suggest algorithm in O(|V|) to determine whether a given undirected graph contains a cycle.
Solution:
An undirected graph is acyclic a DFS yields no back edges (u,v) from u to its ancestor v in the
depth tree .
Execute DFS on the graph
If there is a back edge, an edge from node v to an ancestor in the DFS forest, return TRUE
Otherwise, return FALSE