Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III

2 Classifying Edges DFS can be used to classify the edges of G:
An edge, (u, v) is a: Tree edge: if v was first discovered by exploring the edge (u, v). (u, v) is an edge in the tree formed by depth-first-search. Back edge: if it is a non-tree edge in which v is an ancestor of u. Forward edge: if it is a non-tree edge in which v is a descendent of u. Cross edge: if it is a non-tree edge that connects two vertices that do not have an ancestor/descendant relationship, or that are in two different trees.

3 Classifying edges during DFS
We can classify the edges of a graph during the DFS algorithm the following way: If v is white when first examined from u, then (u, v) is a tree edge. If v is gray when first examined from u, then (u, v) is a back edge. If v is black when first examined from u, then (u, v) is either a forward edge or a cross edge.

4 Ambiguities for undirected graphs
There is some ambiguity between back and forward edges for an undirected graph. The same edge can be classified as back or forward depending on which vertex is listed first (u, v) or (v, u). We classify these edges according to the first type of edge from the above list that is applicable (e.g. back edge). This is equivalent to classifying it according to whether (u, v) or (v, u) is encountered first in the DFS algorithm. It can be shown that undirected graphs never have forward or cross edges.

5 Example 1: previous graph
t u v s w x v w x y t Graph DFS tree u Tree edges: (r, v), (r, s), (s, w), (w, x), (x, t), (t, u), (u, y) (t, w) is a back edge (y, x) is a back edge y

6 Example 2 z s w v We will work this out in class.

7 Pseudocode for DFS DFS(G)
for each vertex u in G.V //Initialize vertices u.color = white u.pred = NIL for each vertex u in G.V if u.color == white //If any vertices not discovered DFS-Visit(G, u) //Start a new tree with them DFS-Visit(G, u) u.color = gray for each v in G.Adj[u] if v.color == white v.pred = u DFS-Visit(G, v) u.color = black

8 Time Stamps We can gain information about the relationships between vertices using a global time stamp. A time stamp records when a vertex is first discovered and when we finish examining its adjacency list. Time stamps are useful in reasoning about search behavior. Time stamps are used in many graph algorithms.

9 DFS with Time Stamps DFS(G)
for each vertex u in G.V //Initialize vertices u.color = white u.pred = NIL Time = 0 for each vertex u in G.V if u.color == white //If any vertices not discovered DFS-Visit(G, u) //Start a new tree with them DFS-Visit(G, u) time = time //white vertex u has just been discovered u.disc = time u.color = gray for each v in G.Adj[u] if v.color == white v.pred = u DFS-Visit(G, v) u.color = black time = time + 1 u.finish = time

10 Example y z s t x w v u

11 Diagramming time intervals
Plotting the start and finish times of each node in a DFS forest gives a nested structure. s t z v u y w x (s (z (y (x x) y)(w w)z) s) (t (v v) (u u) t)

12 Parenthesis Theorem For two vertices, a and b, in a depth-first forest of G, exactly one of the following three holds: The intervals (a.discovery, a.finish) and (b.discovery, b.finish) are disjoint. The interval (a.discovery, a.finish) is nested within (b.discovery, b.finish). (This is true when a is a descendant of b). The interval (b.discovery, b.finish) is nested within (a.discovery, a.finish). (This is true when b is a descendant of a).


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III"

Similar presentations


Ads by Google