Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II

2 Depth First Search Idea: Explore the graph from a vertex by searching as far (deep) as you can along a given path. When you have moved as far as you can go, back up until you find a vertex with unexplored neighbors. Follow these paths to the end. Continue until all vertices reachable from the original have been explored. If any undiscovered vertices remain (e.g. if the graph is unconnected), one of them is selected as a new source and the search is repeated. Repeat until all vertices are discovered. Note that DFS may generate multiple trees (i.e. a forest).

3 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

4 Example r s t u v w x y We will apply DFS to this graph in class.

5 Analysis of DFS Initialization takes: Q(v)
DFS-Visit called once on each vertex:Q (v) DFS-Visit explores each edge exactly once: Q(E) Total: Q(V+E)

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

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

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

9 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

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


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II"

Similar presentations


Ads by Google