lec15-8.au The square of a directed graph G=(V,E) is the graph such that iff for some , both and ; ie. there is a path of exactly two edges.
Give efficient algorithms for both adjacency lists and matricies. Given an adjacency matrix, we can check in constant time whether a given edge exists. To discover whether there is an edge , for each possible intermediate vertex v we can check whether (u,v) and (v,w) exist in O(1).
Since there are at most n intermediate vertices to check, and pairs of vertices to ask about, this takes time.
With adjacency lists, we have a list of all the edges in the graph. For a given edge (u,v), we can run through all the edges from v in O(n) time, and fill the results into an adjacency matrix of , which is initially empty.
It takes O(mn) to construct the edges, and to initialize and read the adjacency matrix, a total of O((n+m)n). Since unless the graph is disconnected, this is usually simplified to O(mn), and is faster than the previous algorithm on sparse graphs.
Why is it called the square of a graph? Because the square of the adjacency matrix is the adjacency matrix of the square! This provides a theoretically faster algorithm.
lec16-5.au
Traversal Orders
The order we explore the vertices depends upon what kind of data structure is used:
The three possible colors of each node reflect if it is unvisited (white), visited but unexplored (grey) or completely explored (black). lec16-6.au Breadth-First Search
BFS(G,s)
for each vertex do
\> color[u] = white
\> , ie. the distance from s
\> p[u] = NIL, ie. the parent in the BFS tree
color[u] = grey
d[s] = 0
p[s] = NIL
while do
\> u = head[Q]
\> for each do
\> \> if color[v] = white then
\> \> \> color[v] = gray
\> \> \> d[v] = d[u] + 1
\> \> \> p[v] = u
\> \> \> enqueue[Q,v]
\> \> dequeue[Q]
\> \> color[u] = black
lec16-8.au Depth-First Search
DFS has a neat recursive implementation which eliminates the need to explicitly use a stack.
Discovery and final times are sometimes a convenience to maintain.
DFS(G)
for each vertex do
\> color[u] = white
\> parent[u] = nil
time = 0
for each vertex do
\> if color[u] = white then DFS-VISIT[u]
Initialize each vertex in the main routine, then do a search from each connected component. BFS must also start from a vertex in each component to completely visit the graph.
DFS-VISIT[u]
color[u] = grey (*u had been white/undiscovered*)
discover[u] = time
time = time+1
for each do
\> if color[v] = white then
\> \> parent[v] = u
\> \> DFS-VISIT(v)
color[u] = black (*now finished with u*)
finish[u] = time
time = time+1
lec16-10.au
BFS Trees
If BFS is performed on a connected, undirected graph, a tree is defined by the edges involved with the discovery of new nodes:
f312in
This tree defines a shortest path from the root to every other node in the tree.
The proof is by induction on the length of the shortest path from the root:
The key idea about DFS
A depth-first search of a graph organizes the edges of the graph in a precise way.
In a DFS of an undirected graph, we assign a direction to each edge, from the vertex which discover it:
f323in
In a DFS of a directed graph, every edge is either a tree edge or a black edge.
In a DFS of a directed graph, no cross edge goes to a higher numbered or rightward vertex. Thus, no edge from 4 to 5 is possible:
f333in lec16-12.au Edge Classification for DFS
What about the other edges in the graph? Where can they go on a search?
Every edge is either:
f346in
On any particular DFS or BFS of a directed or undirected graph, each edge gets classified as one of the above. lec17-3.au DFS Trees
The reason DFS is so important is that it defines a very nice ordering to the edges of the graph.
In a DFS of an undirected graph, every edge is either a tree edge or a back edge.
Why? Suppose we have a forward edge. We would have encountered (4,1) when expanding 4, so this is a back edge.
f351.5in
f363.0in
Paths in search trees
Where is the shortest path in a DFS?
f373in
It could use multiple back and tree edges, where BFS only uses tree edges.
DFS gives a better approximation of the longest path than BFS.
f384in