Graphs - definition Tirgul 7 A directed graph, G, is a couple (V,E) such that V is a finite set and E is a subset of V × V . The set V is •Review of graphs denoted as the vertex set of G and the set E is denoted as the edge set of G. Note that a directed •Graph algorithms: graph may contain self loops (and edge from a vertex to itself). –DFS In an undirected graph, the edges in E are not –Properties of DFS ordered, in the sense of that an edge is a set {u,v}m –Topological sort instead of an ordered couple (u,v). Graph representations: Graph representations: adjacency lists adjacency matrix One way to represent a graph in the Another way is to use adjacency lists. For computer is to use an adjacency matrix. each vertex v there is a linked list of his This is a matrix of size |V| × |V|, we will neighbors. This is a better representation denote it by T. The vertices are enumerated, for sparse graphs, since we use only |V| lists v 1 ,…,v |V| . Now, T i,j =1 ⇔ there is an edge and in a sparse graph, each list is short. between the vertices v i and v j ⇔ (v i ,v j ) ∈ E. If the graph is undirected: T i,j =1 ⇔ T j,i =1 *Note: what is the meaning of T 2 , T 3 , etc. more important definitions… Some important definitions Connected graph : An undirected graph G is said to be Sub-graph : Let G(V,E) be a graph. We say that G’(E’,V’) is a sub-graph of G if V’ ⊆ V and E’ ⊆ E ∩ V’ × V’ connected if for each two vertices u,v in the graph, there Path : Let u,v be vertices in the graph. A path of length k is a path between u and v. Strongly Connected graph : A directed graph G is said between u and v is a sequence of vertices, v 0 ,…,v k , such that v 0 =v, v k =u, and for each i ∈ {0..k-1}, (v i , v i+1 ) ∈ E. We say that to be strongly connected if for each two vertices u,v in v i is the predecessor v i+1 on the path the graph, there is a path between u and v. If there is a path from v to u we say that v is an ancestor of u Tree : A tree is an undirected, connected, a-cyclic graph. and u is a descendant of v. Rooted Tree : A directed graph G is called a rooted tree Cycle : In a directed graph, a cycle is a path v 0 ,..,v k, such that if there exists s ∈ V s.t. for each v ∈ V, there is exactly v 0 =v k . If the vertices v 1 ,…,v k are also pair wise disjoint, the one path between s and v. cycle is called simple . Forest : A forest ( rooted forest ) is a set of disjoint trees In an undirected graph, a (simple) cycle is a path v 1 ,…,v k such that v 0 =v k , k ≥ 3 and v 1 ,…,v k are pair wise disjoint. (rooted trees). 1
DFS – cont. Depth First Search (DFS) In DFS, we put several flags on the vertices. A traversal through a graph is going through In the beginning of the algorithm, all the the graph vertices. We will now see an vertices are white. In the first time that the interesting traversal method, DFS. algorithm sees a vertex, it is painted in gray. When the algorithms finishes dealing with The strategy that we use in DFS is to go as the vertex, it is painted in black. “deep” as we can in the graph. In addition, each vertex v has two time We check the edges that expands from the stamps. The first, v.d, is the time when it last vertex we checked, and that wasn’t was painted in gray. The second, v.f, is the checked yet. time when it was painted in black. DFS – pseudo code (cont.) DFS – pseudo code DFS-VISIT(u) DFS(G) u.color = gray; //initializing. for each vertex u ∈ V[G] { u.d = ++time; for each vertex v ∈ adj[u] { u.color = white; if (v.color == white) { u.prev = nil; v.prev = u; } DFS-VISIT(v); time = 0; for each vertex u ∈ V[G] { } } if (u.color == white) u.color = black; DFS-VISIT(u) u.f = ++time; } A short example (cont.) A short example u v u v u v u v 1/ 2/ 1/ 2/ 1/ 3/ 3/4 w w w w u v u v u v u v 1/ 2/5 1/ 2/ 1/ 2/ 1/6 2/5 3/4 3/ 3/4 w w w w 2
predecessor subgraph of DFS Running time of DFS : Definition : the predecessor subgraph of DFS is the What is the running time of DFS ? ✁ ={(v.prev, v) | v ∈ V} (v.prev graph G ✁ (V,E ✁ ) when E Both loops in the DFS procedure takes O(|V|) is defined during the run of DFS). time, not including the calls to DFS-VISIT. The predecessor subgraph of DFS creates a depth-first The algorithm calls DFS-VISIT exactly once for rooted forest, which consists of several depth-first each vertex, because it is only called on white rooted trees. The coloring of vertices and the fact that vertices. Each DFS-VISIT takes |adj[v]| to finish. we update the prev field only when we reach a white Thus, the running time of the second loop is: � (E). vertex ensures that the trees in the first-depth forest � v ∈ V |adj[v]| = are disjoint. And the total running time is: � (E+V). Proof of the parenthesis theorem : Properties of DFS : We will consider the case where u.d < v.d The parenthesis theorem: If u.f > v.d this means that we first encountered v when Let G be a graph (directed or undirected) then after DFS u was still gray. Therefore, v is a descendant of u. on the graph: Furthermore, since v was discovered after u, all the For each two vertices u, v exactly one of the following is edges that expand from v are checked and it is painted true: black before u is painted in black. Thus, v.f < u.f and •[u.d, u.f] and [v.d, v.f] are disjoint. [v.d, v.f] ⊆ [u.d, u.f]. •[u.d, u.f] ⊆ [v.d, v.f] and u is a descendant of v. If u.f < v.d then [u.d, u.f] and [v.d, v.f] are disjoint. •[v.d, v.f] ⊆ [u.d, u.f] and v is a descendant of u. The case which v.d < u.d is symmetrical, only switch u Immediate conclusion: a vertex v is a descendant of a and v in the above argument. vertex u in the first-depth forest iff [v.d, v.f] ⊆ [u.d, u.f] . The result of DFS: edge classification: After DFS on directed Another interesting property of depth-first search is that it graph. Each vertex has a can be used to classify the edges of the graph. This time stamp. The edges in classification can give important information on the graph. gray are the edges in the depth-first forest. We define between four types of edges: tree edges are edges in G 1. ✁ . The first-depth forest of the back edges are edges which connects a vertex to it’s 2. above graph. There is a ancestor in a first-depth tree. correspondence between the forward edges are edges which are not tree edges discover and finish times of 3. each vertex and the but connects a vertex u to a descendant v in a first- parenthesis structure below. depth tree. cross edges are all the other edges. 4. 3
The white path theorem: example: full DFS with edge classification Theorem: in a depth-first forest of a graph G, a vertex v is a descendant of a vertex u iff in the time u.d, which the algorithm discovers u, there is a path from u to v which consists only of white vertices. Proof: � Assume that u is a descendant of v, let w be a vertex on the path from u to v in the depth- first tree. The conclusion from the parenthesis theorem implies that u.d<w.d and thus w is white in time u.d A-cyclic graphs and DFS: The white path theorem (cont.) A directed a-cyclic graph is denoted DAG . Proof: ⇐ w u Theorem: A graph DAG iff during a DFS run on the v graph, there are no back edges. •w.l.o.g. each other vertex along the path becomes a Proof: ⇐ Assume that there is a back edge, (u,v). So v descendant of u in the tree. is an ancestor of u in the depth first tree and the edge •Let w be the predecessor of u in the path. (u,v) completes a cycle. � Assume that G contains a cycle c. Let v be the first According to the parenthesis theorem: •w.f ≤ u.f (they might be the same vertex). vertex that DFS discovers. Let u be the predecessor of v in the cycle. In time v.d, there is a white path from v to •v.d>u.d and v.d<w.f. u. According to the white path theorem, u is a •Thus, u.d<v.d<w.f ≤ u.f. According to the parenthesis descendant of v in the depth first tree. Thus, the edge theorem, [v.d, v.f] ⊆ [u.d, u.f], and v must be a descendant (u,v) is a back edge. of u. Topological Sort - example Topological Sort How to dress in the morning ? A topological sort of a DAG G is a linear ordering of the vertices in G, such that if G contains an edge (u,v), 4/5 11/12 then u appears before v in the ordering. If G contains a 1/6 pants shoes socks 7/10 cycle, no such ordering exists. shirt 2/3 belt Topological-Sort(G) 8/9 tie call DFS on G. As each vertex is finished, insert it onto the front of a linked list. After using DFS in order to compute the finish times What is topological sort good for ? we have the vertices, ordered according to the finish DAG’s are used in many applications to denote times and now we can dress… precedence order in a set of events. A Topological socks --- shirt --- tie --- pants --- shoes --- belts Sort of such a graph suggests an order to the events. 4
Recommend
More recommend