1 algorithms csci 235, fall 2015 lecture 35 graphs iv

12
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

Upload: chad-sutton

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

DFS with Time Stamps DFS(G) for v in vertices[G] do{Initialize vertices} color[v]

TRANSCRIPT

Page 1: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

1

Algorithms

CSCI 235, Fall 2015Lecture 35

Graphs IV

Page 2: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

2

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

Page 3: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

DFS with Time StampsDFS(G)

for v in vertices[G] do {Initialize vertices}color[v] <- whitepred[v] <- nil {predecessor => parent}

time <- 0 {time is a global variable}for v in vertices[G] do

if color[v] = white then {If any vertices not discovered}DFS-Visit(v) {Start a new tree with them}

DFS-Visit(v)color[v] <- graytime <- time + 1discovery[v] <- timefor a in Adj[v] do

if color[a] = white thenpred[a] = vDFS-Visit[a]

color[v] <- blacktime <- time + 1finish[v] <- time

Page 4: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

4

Exampley z s t

uvwx

v d[v] f[v]

Page 5: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

5

Diagramming time intervalsPlotting the start and finish times of each node in a DFS forest gives a nested structure.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16(s (z (y (x x) y)(w w)z) s) (t (v v) (u u) t)

s

z

yx

w

t

v u

Page 6: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

6

Parenthesis TheoremFor two vertices, a and b, in a depth-first forest of G, exactly one of the following three holds:

• The intervals (discovery[a], finish[a]) and (discovery[b], finish[b]) are disjoint.• The interval (discovery[a], finish[a]) is nested within (discovery[b], finish[b]). (This is true when a is a descendant of b).• The interval (discovery[b], finish[b]) is nested within (discovery[a], finish[a]). (This is true when b is a descendant of a).

Page 7: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

7

Topological SortA directed acyclic graph (DAG) is a directed graph without cycles.

A topological sort of a DAG = (V, E) is a linear ordering of vertices in V consistent with the partial order a < b if (a, b) is in E.In other words, each vertex in a topological sort must precede all its descendants in the DAG and must follow all its ancestors.

Another way to look at it: A topological sort lines up all the vertices in the DAG so that all the edges point from left to right.

Page 8: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

8

Algorithm for Topological sortTopological-Sort(G)

1) Call DFS(G) to compute the finish times f[v] for each vertex in v.

2) As each vertex is finished, insert it into the front of a linked list.

3) Return the linked list of vertices.

Because each vertex is finished after all its descendants are finished, each vertex precedes all its descendants in the list.

Running time = Running time of DFS + time to insert into list = (V+E) + (V) = (V + E)

Page 9: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

9

Example: Professor Bumstead gets dressed

Professor Bumstead must put on certain items before others (e.g. socks before shoes).

undershorts

pants

beltshirt

tie

jacket

socks

shoeswatch

We will work out the topological sort in class

Page 10: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

10

Connected and Strongly Connected components

A connected component of a graph is a maximal set of vertices such that for any two vertices, a and b, in the set, there is a path from a to b or from b to a.In other words: two vertices are in the same connected component if there is a path from one to the other.

A strongly connected component of a graph is a maximal set of vertices such that for any two vertices, a and b, in the set, there is a path from a to b and from b to a.In other words: In a strongly connected component there is a path from every member of the set to every other member of the set.

Importance: Decomposing a graph into its strongly connected components is the first step in many graph algorithms.

Page 11: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

11

Algorithm for strongly connected components

Definition: The transpose of G, written GT, is a graph with the same vertices as G in which the directions of the edges have been reversed.

Strongly-Connected-Components(G)1. Call DFS(G) to compute finish[v] for each vertex in G.2. Call Modified-DFS(GT), where the main loop of Modified-

DFS(GT) processes vertices in order of decreasing finish[v].3. Each tree in the depth-first forest of Modified-DFS(GT) is a

strongly connected component of G.

Running time = running time of DFS = (V + E)

Page 12: 1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV

12

Example

a b c d

hgfe