3 Notes
3 Notes
CAI Leizhen
CSE-CUHK-HK-CHN
Keywords: Graph algorithms, lower bound for sorting, BFS, DFS and Euler tour.
1. Essentially, computers deal with relations amongst discrete objects, and graphs
are very useful models for representing binary relations. Graph problems arise
naturally and very frequently in numerous applications, for instance, the exam
scheduling problem can be modeled as a vertex colouring problem on so called
conflict graphs.
In general, any computation can be regarded as a path finding problem in a graph.
Suppose that x1 , . . . , xn are objects involved in the computation. A given set of
values of x1 , . . . , xn forms a configuration, and a computation moves from con-
figurations to configurations. Regard each configuration as a vertex, and regard
a move by the computation from configuration x to configuration y as edge xy.
Then a computation is to find a path from the initial configuration (the input) to
a final configuration (a solution as the output).
• Each internal node is a comparison a i < aj between two elements, and its two
children are subsequent two-element comparisons that depend on the answer
to ai < aj (Yes or No).
• Each leaf is a permutation of a1 , . . . , an .
1
CSCI3160-18F Design and Analysis of Algorithms 2
4. Exploring a graph: During a search, some vertices are visited and some are not,
and an edge uv that connects a visited vertex u with an unvisited vertex v is a
frontier edge. The search continues by choosing a frontier edge and visiting its
unvisited end. The objective of a search algorithm is to start at a vertex s and
visits all vertices reachable from s. The frontier edges selected in the process form
a rooted tree T containing all vertices reachable from s. In the following general
scheme, S contains the set of visited vertices.
S ← {s}, T ← ∅;
while there is a frontier edge uv do
S ← S ∪ {v};
T ← T ∪ uv:
end while
5. Breadth-first search (BFS) and depth-first search (DFS) are two most im-
portant ways to explore (search) vertices and edges in a graph in a systematic
way.
BFS chooses a frontier edge uv where u was visited least recently, and DFS chooses
a frontier edge uv where u was visited most recently.
The frontier edges chosen in BFS and DFS, respectively, form BFS and DFS trees.
We can use queue and stack to implement BFS and DFS, respectively, in time
O(m + n).
6. BFS: Use a queue Q to store potential frontier edges. Initially, Q contains all edges
going out from the starting vertex s, no vertex other than s is visited, and the BFS
tree T is empty.
Note that when an edge is removed from Q, it may not be a frontier edge anymore.
Also an edge is in Q at most once and examined at most twice, which implies that
the running time for BFS is O(m + n).
7. DFS: Similar to BFS, but use a stack S to store potential frontier edges.
procedure dfs(u);
visited(u) ← true;
for each frontier edge uv do dfs(v).
8. Shortest paths: For unweighted graph, BFS can find shortest paths from the start-
ing vertex s to all vertices reachable from s. Define (u, v)-distance to be the number
of edges in a shortest (u, v)-path.
Let N i (s) denote the i-th neighborhood of s, i.e., N 1 (s) = N (s) the neighborhood
of s and N i+1 (s) consists of neighbors of vertices in N i (s) that are not in N j (s)
for all j ≤ i.
Since a BFS from s visits vertices in the order of N 1 (s), N 2 (s), N 3 (s), . . ., we have
the following result.
Theorem. Every (s, v)-path in a BFS tree is a shortest (s, v)-path in the graph.
10. Euler tour (a.k.a Eulerian circuit, Eulerian cycle): a close walk that visits each
edge of a graph exactly once.
Euler Theorem: A connected graph admits an Euler tour iff every vertex has an
even degree.
CSCI3160-18F Design and Analysis of Algorithms 4
Note that for a digraph, the condition becomes: the indegree of each vertex equals
its outdegree.
A graph that admits an Euler tour is called an Eulerian graph. Two methods
for constructing an Euler tour in an Eulerian graph: merging closed trails, and
avoiding bridges. Both algorithms work in a manner similar to DFS: walk along
unvisited edges, and go as farther as possible. However we need to be careful in
choosing the next unvisited edge.
Algorithm 1. Merging closed trails: Find a closed trail T 0 and merge it with the
current closed trail T .
Initially, T is a closed trail consisting of a single vertex v.
repeat the following until there are no unvisited edges:
Step 1. Start at vertex v, walk along unvisited edges to obtain a closed trail T 0 .
Step 2. Merge T and T 0 to form a larger closed trail T .
Step 3. Find a vertex v in T that has unvisited edges going out from v. Why does
such a vertex v always exist?