tirgul 7 review of graphs graph algorithms: –dfs –properties of dfs –topological sort

25
Tirgul 7 •Review of graphs •Graph algorithms: –DFS –Properties of DFS –Topological sort

Upload: kameron-hearing

Post on 16-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Tirgul 7

•Review of graphs

•Graph algorithms:–DFS–Properties of DFS–Topological sort

Review of graphs

Graphs are a very useful tool in Computer Science. Many problems can be reduced to problems on graphs, and there exists many efficient algorithms that solves graph problems.

Today we will examine two of those algorithms.

Graphs - definition

A directed graph, G, is a couple (V,E) such that V is a finite set and E is a subset of VV. The set V is denoted as the vertex set of G and the set E is denoted as the edge set of G. Note that a directed graph may contain self loops (and edge from a vertex to itself).

In an undirected graph, the edges in E are not ordered, in the sense of that an edge is a set {u,v}m instead of an ordered couple (u,v).

Graph representations: adjacency matrix

One way to represent a graph in the computer is to use an adjacency matrix. This is a matrix of size |V||V|, we will denote it by T. The vertices are enumerated, v1,…,v|V|. Now, Ti,j=1 there is an edge between the vertices vi and vj (vi,vj)E.

If the graph is undirected: Ti,j=1 Tj,i=1

*Note: what is the meaning of T2, T3, etc.

Graph representations: adjacency lists

Another way is to use adjacency lists. For each vertex v there is a linked list of his neighbors. This is a better representation for sparse graphs, since we use only |V| lists and in a sparse graph, each list is short.

Some important definitionsSub-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’EV’V’

Path: Let u,v be vertices in the graph. A path of length k between u and v is a sequence of vertices, v0,…,vk, such that v0=v, vk=u, and for each i{0..k-1}, (vi, vi+1)E. We say that vi is the predecessor vi+1 on the path

If there is a path from v to u we say that v is an ancestor of u and u is a descendant of v.

Cycle: In a directed graph, a cycle is a path v0,..,vk, such that v0=vk. If the vertices v1,…,vk are also pair wise disjoint, the cycle is called simple.

In an undirected graph, a (simple) cycle is a path v1,…,vk such that v0=vk, k3 and v1,…,vk are pair wise disjoint.

more important definitions…Connected graph: An undirected graph G is said to be connected if for each two vertices u,v in the graph, there is a path between u and v.

Strongly Connected graph: A directed graph G is said to be strongly connected if for each two vertices u,v in the graph, there is a path between u and v.

Tree: A tree is an undirected, connected, a-cyclic graph.

Rooted Tree: A directed graph G is called a rooted tree if there exists sV s.t. for each vV, there is exactly one path between s and v.

Forest: A forest (rooted forest) is a set of disjoint trees (rooted trees).

Depth First Search (DFS)

A traversal through a graph is going through the graph vertices. We will now see an interesting traversal method, DFS.

The strategy that we use in DFS is to go as “deep” as we can in the graph.

We check the edges that expands from the last vertex we checked, and that wasn’t checked yet.

DFS – cont.In DFS, we put several flags on the vertices. In the beginning of the algorithm, all the vertices are white. In the first time that the algorithm sees a vertex, it is painted in gray. When the algorithms finishes dealing with the vertex, it is painted in black.

In addition, each vertex v has two time stamps. The first, v.d, is the time when it was painted in gray. The second, v.f, is the time when it was painted in black.

DFS – pseudo codeDFS(G)

//initializing.

for each vertex uV[G] {

u.color = white;

u.prev = nil;

}

time = 0;

for each vertex u V[G] {

if (u.color == white)

DFS-VISIT(u)

}

DFS – pseudo code (cont.)DFS-VISIT(u)

u.color = gray;

u.d = ++time;

for each vertex vadj[u] {

if (v.color == white) {

v.prev = u;

DFS-VISIT(v);

}

}

u.color = black;

u.f = ++time;

A short exampleu v

w

u v

w

1/

u v

w

1/ 2/

u v

w

1/ 2/

3/

A short example (cont.)u v

w

1/ 2/

3/

u v

w

1/ 2/

3/4

u v

w

1/ 2/5

3/4

u v

w

1/6 2/5

3/4

Running time of DFS: What is the running time of DFS ?

Both loops in the DFS procedure takes O(|V|) time, not including the calls to DFS-VISIT.

The algorithm calls DFS-VISIT exactly once for each vertex, because it is only called on white vertices. Each DFS-VISIT takes |adj[v]| to finish. Thus, the running time of the second loop is:

vV|adj[v]| = Θ(E).

And the total running time is:

Θ(E+V).

predecessor subgraph of DFSDefinition: the predecessor subgraph of DFS is the graph Gπ(V,Eπ) when Eπ={(v.prev, v) | vV} (v.prev is defined during the run of DFS).

The predecessor subgraph of DFS creates a depth-first rooted forest, which consists of several depth-first rooted trees. The coloring of vertices and the fact that we update the prev field only when we reach a white vertex ensures that the trees in the first-depth forest are disjoint.

Properties of DFS: The parenthesis theorem:

Let G be a graph (directed or undirected) then after DFS on the graph:

For each two vertices u, v exactly one of the following is true:•[u.d, u.f] and [v.d, v.f] are disjoint.•[u.d, u.f] [v.d, v.f] and u is a descendant of v.•[v.d, v.f] [u.d, u.f] and v is a descendant of u.

Immediate conclusion: a vertex v is a descendant of a vertex u in the first-depth forest iff [v.d, v.f] [u.d, u.f] .

Proof of the parenthesis theorem:

We will consider the case where u.d < v.d

If u.f > v.d this means that we first encountered v when u was still gray. Therefore, v is a descendant of u. Furthermore, since v was discovered after u, all the edges that expand from v are checked and it is painted black before u is painted in black. Thus, v.f < u.f and [v.d, v.f] [u.d, u.f].

If u.f < v.d then [u.d, u.f] and [v.d, v.f] are disjoint.

The case which v.d < u.d is symmetrical, only switch u and v in the above argument.

The result of DFS:After DFS on directed graph. Each vertex has a time stamp. The edges in gray are the edges in the depth-first forest.

The first-depth forest of the above graph. There is a correspondence between the discover and finish times of each vertex and the parenthesis structure below.

edge classification:

We define between four types of edges:

1. tree edges are edges in Gπ.

2. back edges are edges which connects a vertex to it’s ancestor in a first-depth tree.

3. forward edges are edges which are not tree edges but connects a vertex u to a descendant v in a first-depth tree.

4. cross edges are all the other edges.

Another interesting property of depth-first search is that it can be used to classify the edges of the graph. This classification can give important information on the graph.

example: full DFS with edge classification

The white path theorem: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

Proof:

•w.l.o.g. each other vertex along the path becomes a descendant of u in the tree. •Let w be the predecessor of u in the path.

According to the parenthesis theorem:•w.fu.f (they might be the same vertex). •v.d>u.d and v.d<w.f. •Thus, u.d<v.d<w.f u.f. According to the parenthesis theorem, [v.d, v.f] [u.d, u.f], and v must be a descendant of u.

The white path theorem (cont.)

uv

w

A-cyclic graphs and DFS:A directed a-cyclic graph is denoted DAG.

Theorem: A graph DAG iff during a DFS run on the graph, there are no back edges.

Proof: Assume that there is a back edge, (u,v). So v is an ancestor of u in the depth first tree and the edge (u,v) completes a cycle.

Assume that G contains a cycle c. Let v be the first 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 u. According to the white path theorem, u is a descendant of v in the depth first tree. Thus, the edge (u,v) is a back edge.

Topological SortA topological sort of a DAG G is a linear ordering of the vertices in G, such that if G contains an edge (u,v), then u appears before v in the ordering. If G contains a cycle, no such ordering exists.

Topological-Sort(G)

call DFS on G. As each vertex is finished, insert it onto the front of a linked list.

What is topological sort good for ?

DAG’s are used in many applications to denote precedence order in a set of events. A Topological Sort of such a graph suggests an order to the events.

How to dress in the morning ?

After using DFS in order to compute the finish times we have the vertices, ordered according to the finish times and now we can dress…

socks --- shirt --- tie --- pants --- shoes --- belts

Topological Sort - example

pants

beltshirt

tie

shoes socks1/6

2/3

4/5

7/10

8/9

11/12