CSC317_22ndClass

At the same time: Breadth-first search tree: If node v is discovered after u then
edge uv is added to the tree. We say that u is a predecessor (parent) of v. A vertex
is discovered at most once.
Run time:
n = number of vertices
m = number of edges
• Each vertex is enqueued and dequeued at most once (why? They are never
white again). Costs? Per vertex 1, n times O(n).
• Scans adjacency list only when dequeued. Therefore, each adjacency list only at
most once. Sum of all adjacency lists depends on number of edges O(m).
• Total O(n + m) (i.e. linear in number of vertices and edges)
CSC317
1
Why are we doing this?
BFS allows us to find shortest paths!
Shortest path distance from s to v:
d(s,v) … minimum number of edges from any path from vertex s to v (∞ if no paths exist)
Theorem:
Let G = (V,E) be a (un)directed graph. Let sÎ V be an abitrary vertex. Then, for any edge
(u, v) Î E, d (s, v) £ d (s, u)+1 .
How are we going to proof this?
If u is reachable from s, then so is v. In this case the shortest path from s to v can
not be longer than the shortest path from s to u followed by the edge (u,v). So, the
inequality holds. (Forgot something?) If u is not reachable from s then d(s,u)=∞ and
the inequality holds.
CSC317
2
Main Theorem:
Let G = (V,E) be a (un)directed graph and suppose that BFS is run on G from a given
source vertex sÎ V. Upon termination, for each vertex v Î V the value v.d
computed by BFS satisfies v.d ³ d (s, v).
v.d ... Is the shortest path, shortes number of edges from source s to v.
Proof: We use induction on the number of ENQUEUE operations. Our inductive
hypothesis is v.d ³ d (s, v) for all v Î V.
The basis of the induction is the situation immediately after enqueuing s in line 9
of BFS.
Does the inductive hypothesis in the beginning hold?
s.d = 0 = d (s, s) and v.d = ¥ ³ d (s, v) for all v Î V -{s} .
For the inductive step, consider a white vertex v that is discovered
during the search for vertex u, implying that u.d ³ d (s, v). From the
above theorem (and assignment in line 15) we get
v.d = u.d +1
³ d (s, u) +1
³ d (s, v)
CSC317
3
v.d = u.d +1
³ d (s, u) +1
³ d (s, v)
What does that mean?
Vertex v is then enqueued, and it is never enqueued again. I.e.
clause of lines 14–17 is executed only for white vertices. Thus, the
value of v.d never changes again!
CSC317
4
Graph algorithms part 2
Depth First Search (DFS)
Search deeply first... (What’s the difference to BFS?) Explores edges out of
most recently discovered vertex so long as there are still unexplored edges
leaving it... and then backtracks (like searching in a maze). Run time as in BFS
will be linear in number of vertices and edges O(n+m)
Applications:
- topological sort in directed acyclic graphs (sequences of events where one
comes before another; later);
- strongly connected components (later).
https://www.cs.usfca.edu/~galles/visualization/DFS.html
CSC317
5
Simple example of DFS ordering showing main idea and distinguishing from BFS:
2
a
3
c
6
e
b
d
f
5
4
7
1
s
The predecessor subgraph formed by DFS is composed of several trees because
the search may repeat from multiple sources.
CSC317
6
- One way to implement: Stack instead of queue. Last in first out (LIFO). But often
implemented recursively.
- Marking nodes in algorithm: Colors are the same:
: Vertices are initially white
Gray: First time vertex encountered, make gray
Black: Only when all its adjacent neighbors discovered, turn to black.
We also mark a node with a number “time stamp” both when it is first
discovered (turns gray) and when we have gone through all its neighbors
(black).
The discovery time is u.d and the finish time is u.f (u.f > u.d).
CSC317
7
Main algorithm with recursion: We’ll have a function DFS that initializes, and then
calls DFS-Visit, which is a recursive function and does the depth first search.
// start time
// we discovered u
we are finished going through all of u’s neighbors
// finish time
CSC317
8
CSC317
9
Run time DFS: O(n + m) with n number vertices and m number edges. Procedure
DFS-Visit is called exactly once for each vertex (only if white and then changed).
During the DFS-Visit all adjacent nodes of each vertex are used, so overall
includes all edges.
Some properties of DFS:
1.) Every vertex v that gets discovered between start of u and end of u, is a child
of u in the DFS tree. The v will also finish before u does (see path from u to v to y
to x)
This can be seen in recursion: DFS-Visit(u) called first; and then calls DFSvisit(
v), which finishes first (last in first out, as in a stack)
2.) There is never an edge from a black to a white node (because if a node is
black, we already visited all its neighbors).
3.) White path theorem: vertex v is a descendant of vertex u if and only if when
the search discovers u, there is a path from u to v consisting entirely of white
vertices.
CSC317
10
4.) Disjoint trees, contained trees, and
parenthesis theorem:
Consider vertices u and v in a graph G(V,E). Then
one of the following conditions hold:
• intervals [u.d, u.f] and [v.d, v.f] are entirely
disjoint, and neither u nor v is a descendant of
the other in the depth-first forest,
• interval [u.d, u.f] is contained entirely within
[v.d, v.f], and u is a descendant of v (which
means u was discovered after v), or
• interval [v.d, v.f] is contained entirely within
[u.d, u.f], and v is a descendant of u (which
means v was discovered after u) (vice versa)
CSC317
11
Application DFS: topological sort:
A directed edge from u to v, means that v happens after u. Topological order: All
directed edges only go forward (needs to be acyclic graph DAG). Useful for sorting
problems with tasks where one needs to be done before another. Again linear time
in vertices and edges O(n+m), since doing DFS.
Definitions: A topological sort of a dag G = (V,E) is a linear ordering of all its
vertices such that if G contains an edge (u,v) then u appears before v in the
ordering. (If the graph contains a cycle, then no linear ordering is possible.)
CSC317
12
CSC317
13
How can we do that?
We can perform topological sort in time Θ (V+E) since depth-first search
takes Θ (V+E) time and it takes O(1) time to insert each of the |V| vertices onto
the front of the linked list.
CSC317
14
Application DFS: strongly connected components:
CSC317
15