0% found this document useful (0 votes)
23 views

3 Notes

The document outlines a lecture on graph algorithms. It discusses lower bounds for sorting, breadth-first search and depth-first search algorithms for exploring graphs, and algorithms for finding shortest paths and constructing Euler tours in graphs.

Uploaded by

Cyrus Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

3 Notes

The document outlines a lecture on graph algorithms. It discusses lower bounds for sorting, breadth-first search and depth-first search algorithms for exploring graphs, and algorithms for finding shortest paths and constructing Euler tours in graphs.

Uploaded by

Cyrus Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lecture Outline (CSCI3160-18F, 3rd week)

CAI Leizhen
CSE-CUHK-HK-CHN

September 20, 2018

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).

2. Graphs: An undirected graph (usually just called graph) G = (V, E) consists of a


set V of vertices and a set E of edges, where each edge uv is an unordered pair
{u, v} of distinct vertices.
A directed graph (digraph in short) G = (V, E) consists of a set V of vertices and
a set E of ordered pairs (u, v) of vertices as edges, which are also called arcs.
The number of vertices in G is the order of G, and the number of edges is the size
of G. Conventionally, we use n for |V |, m for |E|, and the input size of a graph is
m + n.

3. Lower bound for sorting: Ω(n log n).


We can use a binary tree T to describe a comparison-based sorting algorithm to
sort a1 , . . . , an :

• 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

A root-to-leaf path in T indicates comparisons in the algorithm for the permutation


corresponding to the leaf, and the height of T gives the the number of comparisons
in the algorithm for the worst-case.
Since there are n! possible permutations, the height of T is at least
n
log 2 n! ≥ log 2 ( )n/2 = Ω(n log n)
2
as ln n! = n ln n − n + O(ln n). Therefore any comparison-based sorting algorithm
for sorting n elements takes Ω(n log n).

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.

repeat the following until Q is empty


remove edge uv from the head of Q;
if uv is a frontier edge then
visited(v) ← true,
add edge uv to the BFS tree T , and
put all frontier edges going out from v to the tail of Q.
CSCI3160-18F Design and Analysis of Algorithms 3

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.

repeat the following until S is empty


remove edge uv from the top of S;
if uv is a frontier edge then
visited(v) ← true,
add edge uv to the DFS tree T , and
put all frontier edges going out from v to the top of S.

We also have the following recursive version for DFS:

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.

9. Walk and trail: A (u, v)-walk is an alternating sequence v 0 , e1 , v1 , e2 , . . . , vk−1 , ek , vk


of vertices and edges such that v0 = u, vk = v and each edge ei = vi−1 vi . The length
of the walk is the number of edges in the walk, and a closed walk is a (u, v)-walk
with u = v.
A (u, v)-trail is a (u, v)-walk without repetition of edges, and a closed trail is a
(u, v)-trail with u = v.
A (u, v)-path is a (u, v)-walk without repetition of edges or vertices, and a cycle is
a close walk without repetition of edges or vertices.

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?

A bridge in a connected graph is an edge whose removal disconnects the graph.


Let G∗ be the graph consisting of unvisited edges.
Algorithm 2. Avoiding bridges: In choosing an unvisited edge going out from a
vertex, we always choose an edge that is not a bridge of G ∗ , unless we have no
other choice.
Correctness: We claim that the following invariant always holds: G ∗ is connected
and contains the last vertex of the current trail T . The claim implies that we can
always extend the trail until we obtain an Euler tour.
The claim clearly holds initially, and we show that the claim remains true every
time we extend the trail T by an edge.
Let e = uv be the unvisited edge chosen by the algorithm to extend the trail. If e
is not a bridge of G∗ , then G∗ − e remains connected and contains vertex v, which
is the last vertex of T . Otherwise e is the only edge incident with u (why?), and
G∗ − e has exactly two connected components, one containing vertex u only and
the other one containing v, and thus the new G ∗ is connected and contains the last
vertex v of T .

You might also like