Beruflich Dokumente
Kultur Dokumente
12
DFS, BFS, Topological-Sort
Graph G = (V, E)
Graph
Representations
BFS
Adjacency Matrix:
M[i,j] = 1 - (vi, vj) in E(G)
M[i,j] = 0 - (vi, vj) not in E(G)
Adjacency List:
Each vertex v in V(G) has a list of all vertices u in V(G) where
(v,u) in E(G)
Breadth-First Search
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 minimal paths to nodes that
can be reached from s.
The algorithm is suitable for directed or undirected graphs.
Depth-First Search
Starting from the source node s, DFS checks the edges from the last
visited node, until reaching a node v whose all descendants were all
visited, than back tracks to the previous node and continues the
search. The DFS may result in several trees since the search can start
from several sources.
A node v is white before d[v], gray between d[v] and f[v] and black
after f[v].
Time complexity: O(|V| + |E|)
Topological-Sort
Edges
DFS can be used to classify the edges of the input graph G=(V,E).
Tree-edges
(u,v) - if v was initially discovered using edge from u to v
Back-edges
(u,v) - if v is an ancestor of u in the depth-tree
Forward-edges
(u,v) - not a tree-edge, where u is v's ancestor
Cross-edges
(u,v) - All the other edges, where u and v are vertices in
different depth-tree, u is not v's ancestor or v is not u's ancestor
Question 1
When an adjacency-matrix representation is used, most graph algorithms require time
O(V2).
Show that determining whether a directed graph, represented in an adjacency-matrix
Time Complexity: O(V), because at most 2|V| cells in the matrix are examined.
Question 2
Suggest algorithm in O(|V|) independent of E that determines whether a given undirected
graph contains a cycle.
Solution:
An undirected graph is acyclic if and only if a DFS yields no back edges, edges (u,v)
where v has already been discovered and is u's ancestor .
Execute DFS on the graph .
If there is a back edge - the graph has a cycle.
If the graph does not contain a cycle then |E| < |V| and DFS cost O(|V|)
If the graph contains a cycle, then a back edge is discovered after 2|V| steps at most.
Question 3
Given an undirected complete graph G = (V,E), Suggest an algorithm to direct the edges
in E(G) for creating an asyclic directed graph G' = (V,E').
Solution:
Question 4
2 lists are given:
A - Courses list
B - Prerequisites
B contains couples (a, b) where a,b in A
indicating that course b can't be taken before course a.
Design a schedule for each of the following students.
1. A lazy student who wants to take only one course in a semester.
The student must study all the courses in A.
2. A student who wants to take all the courses in A, in the minimal number of
semesters.
The student is ready to take any number of courses in a semester.
Example:
1. Print the courses in a way that course i won't be printed before course j, if j is
prerequisite of i.
In other words topologically sort the courses.
Complexity: O(|V| + |E|)
2. A variation of the above topological sort algorithm with a slight change:
In each semester k, execute the algorithm on all the nodes with degree 0.
(instead of dealing with one source in each stage, all the sources will be dealt and
printed.)
Complexity: O(|V| + |E|)
Question 5
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 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 and M(s) = 1
Question 6
Given an undirected graph G = (V,E).
Let's define 2 set of vertices V1 and V2 such as:
V1 V2 V