Download presentation
Presentation is loading. Please wait.
Published byChad Sutton Modified over 8 years ago
1
1 Algorithms CSCI 235, Fall 2015 Lecture 35 Graphs IV
2
2 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.
3
DFS with Time Stamps DFS(G) for v in vertices[G] do{Initialize vertices} color[v] <- white pred[v] 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] <- gray time <- time + 1 discovery[v] <- time for a in Adj[v] do if color[a] = white then pred[a] = v DFS-Visit[a] color[v] <- black time <- time + 1 finish[v] <- time
4
4 Example yzst uvwx vd[v]f[v]
5
5 Diagramming time intervals Plotting 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 y x w t vu
6
6 Parenthesis Theorem For 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).
7
7 Topological Sort A 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.
8
8 Algorithm for Topological sort Topological-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)
9
9 Example: Professor Bumstead gets dressed Professor Bumstead must put on certain items before others (e.g. socks before shoes). undershorts pants belt shirt tie jacket socks shoes watch We will work out the topological sort in class
10
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.
11
11 Algorithm for strongly connected components Definition: The transpose of G, written G T, 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(G T ), where the main loop of Modified- DFS(G T ) processes vertices in order of decreasing finish[v]. 3.Each tree in the depth-first forest of Modified-DFS(G T ) is a strongly connected component of G. Running time = running time of DFS = (V + E)
12
12 Example abcd hgfe
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.