Você está na página 1de 6

Exercise No.

10
BFS and DFS
Graph
G (V, E)
Graph
Representations

V = Set of all vertices in G


E = Set of all edges (u,v) in G, where u,v V(G)

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)

Search algorithm for both directed and undirected graphs.


Starting from the source node s, BFS computes the minimal distance from s to
any other node v that can be reached from s.
The algorithm builds a breadth-tree rooted at s with the shortest paths to nodes
that can be reached from s.

color[v] - the color of node v


o white - an unvisited node
o gray - a visited node who has white neighbors
(draws a limit between the visited and unvisited nodes)
o black - a node whose neighbors were all visited
(colored gray or black)
pred[v] - the predecessor of v in the breadth-tree
d[v] - the minimal distance from s to v

Time complexity: O(V + E)


DFS
Depth-First
Search

Search algorithm for both directed and undirected graphs.


Starting from the source node s, DFS checks the edges from the last visited
node, until reaching a node v whose descendants were all visited, than back
tracks to the previous node and continues the search.
The DFS may result in several trees (a forest) since the search can start from
several sources.
color[v] - the color of node v
o white - an unvisited node
o gray - a visited node
o black - a node whose descendants were all searched
pred[v] - the predecessor of v in the one of the depth trees
d[v] - the time when v was discovered
f[v] - the time when v was colored in black
A node v is white before d[v], gray between d[v] and f[v] and black after [fv].

Time complexity: O(V + E)

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:

Number all the vertices in V(G) from 1 to |V|.


For each (vi,vj) E(G)
if i < j E' = E' (vi,vj)
if i > j E' = E' (vj,vi)

Claim: The graph G is acyclic.


Proof: Assume that G is not acyclic, meaning it contains a cycle.
Let vi1 , vi2 ,..., v ik , vi1 be that cycle.
According to the building of E, i1 i 2 i3 ... i k i1 , hence i1 ik i1 , but that is a
contradiction

the assumption is wrong G is acyclic

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.

(represented in an adjacency matrix).


What is the time complexity of computing Gk

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

M [i, j ] M k 1 [i, u ] M 1 [u, j ] = the number of paths of length k from i to j in G.


u 1

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.

Find distance(V1, V2) in O(|V| + |E|).

Solution:

Add 2 new vertices s and t to V(G).

E(G) { (s,u) : u V1 } { (v, t) : v V2 }


Execute BFS from s and find d(t).
distance(V1, V2) = d(t) - 2

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

Time complexity: O(|V|)


Case a: no back edges
If there was no back edges the graph is acyclic |E|<|V|, otherwise there was a cycle
in O(|V|+|E|) = O(|V|)

the DFS runs

Case b: there was a back edge


Let ne be the number of edges visited during the DFS until discovering a back edge and
returning (ne doesnt include the back edge).
Let nv be the number of nodes visited during the DFS until discovering the back edge and
returning.
Lets prove that ne < |V|.
Assume that ne |V|.
Let G be the sub-graph that includes the ne edges and the nv nodes.
G is acyclic since it doesnt include a back edge.
But since ne > |V| > nv , G contains a cycle contradiction.
Therefore the assumption is wrong ne < |V|.
The DFS on G runs in O(nv+ ne) = O(|V|)

Você também pode gostar