introduction to algorithms

49
Introduction to Algorithms Elementary Graph Algorithms My T. Thai @ UF

Upload: raphael-park

Post on 01-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

Introduction to Algorithms. Elementary Graph Algorithms My T. Thai @ UF. Outline. Graph representation Graph-searching algorithms Breadth-first search Depth-first search Topological sort Strongly connected components. Graphs. Given a graph G=(V, E) V : set of vertices E : set of edges - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to Algorithms

Introduction to Algorithms

Elementary Graph Algorithms

My T. Thai @ UF

Page 2: Introduction to Algorithms

Outline

Graph representation Graph-searching algorithms

Breadth-first search Depth-first search

Topological sort Strongly connected components

My T. [email protected]

2

Page 3: Introduction to Algorithms

Graphs

Given a graph G=(V, E) V: set of vertices E: set of edges

Types of graphs: Undirected: Directed: Weighted: each edge e has a weight w(e) Dense: Sparse: |E| = o(V2)

|E| = O(|V|2) My T. [email protected]

3

Page 4: Introduction to Algorithms

Representations of graphs

Two common ways to represent for algorithms: Adjacency lists

Adjacency matrix

My T. [email protected]

4

Page 5: Introduction to Algorithms

Adjacency lists

Array Adj of |V| lists, one per vertex List of vertex u, Adj[u], include all vertices v

such that If edges have weights, can put the weights in

the lists

My T. [email protected]

5

Page 6: Introduction to Algorithms

Adjacency matrix

Store adjacent matrix |V| × |V| matrix A = (aij )

If edges have weights, aij is the weight of edge (i, j)

My T. [email protected]

6

Page 7: Introduction to Algorithms

Adjacency lists vs Adjacency matrix

Adjacency lists Adjacency matrix Space:

Time: to list all vertices adjacent to u:

Time: to determine if (u, v) ∈ E:

My T. [email protected]

7

Page 8: Introduction to Algorithms

Graph searching algorithms

Visiting all the nodes in a graph in a particular manner following the edges of the graph

Used to discover the structure of the graph Two common searching algorithms:

Breadth-first search (BFS): visit the sibling nodes before visiting the child nodes (visit node layer by layer based on the distance to the root)

Depth-first search (DFS):  visit the child nodes before visiting the sibling nodes (follow the “depth” of the graph)

My T. [email protected]

8

Page 9: Introduction to Algorithms

Breadth-first search

Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V

Output: d[v] = distance (smallest # of edges, or shortest path)

from s to v, for all v V such that (u, v) is last edge on shortest path s

to v u is v’s predecessor

Builds breadth-first tree with root s that contains all reachable vertices

My T. [email protected]

9

Page 10: Introduction to Algorithms

Breadth-first search Discover vertices in a wave manner from the

source s First hit all vertices 1 edge from s, then vertices 2 edges

from s … A vertex is discovered at the first time that it is encountered during

the search. A vertex is finished if all vertices adjacent to it have

been discovered. Colors the vertices to keep track of progress.

White: Undiscovered. Gray: Discovered but not finished. Black: Finished.

My T. [email protected]

10

Page 11: Introduction to Algorithms

My T. [email protected]

11

Queue Q contains all discovered but not finished vertices – gray vertices

Page 12: Introduction to Algorithms

Example

My T. [email protected]

12

Page 13: Introduction to Algorithms

My T. [email protected]

13

Page 14: Introduction to Algorithms

My T. [email protected]

14

Page 15: Introduction to Algorithms

My T. [email protected]

15

Page 16: Introduction to Algorithms

My T. [email protected]

16

Page 17: Introduction to Algorithms

Analysis of BFS Initialization takes O(V)

In while loop Each vertex is enqueued and dequeued at most once =>

total time for queuing is O(V) The adjacency list of each vertex is scanned at most

once. The sum of lengths of all adjacency lists is (E)

=>Total running time: O(V+E) linear in the size of the adjacency list representation of the graph

My T. [email protected]

17

Page 18: Introduction to Algorithms

Correctness of BFS

Where is the shortest distance from s to v

Proof (sketched): : If t is the sequence of vertices in the

queue, then If v is reached from u where d.u =

=>My T. Thai

[email protected]

18

Page 19: Introduction to Algorithms

Depth-first search

Input: G = (V, E), directed or undirected. No source vertex given.

Output: 2 timestamps on each vertex: d[v] = discovery time f [v] = finishing time [v] : predecessor of v = u, such that v was

discovered during the scan of u’s adjacency list Build depth-first forest comprising several

depth-first trees

My T. [email protected]

19

Page 20: Introduction to Algorithms

Depth-first search

From a vertex v Follow an outgoing edge to explore Keep searching as deep as possible When all vertices reachable through the edge is

explored, follow another outgoing edge of v

When all vertices reachable from the original source are discovered, choose an undiscovered vertex as a new source then repeat the process

My T. [email protected]

20

Page 21: Introduction to Algorithms

Depth-first search

My T. [email protected]

21

white: undiscoveredgray: discoveredblack: finished

Page 22: Introduction to Algorithms

Example

My T. [email protected]

22

Page 23: Introduction to Algorithms

My T. [email protected]

23

Page 24: Introduction to Algorithms

My T. [email protected]

24

Page 25: Introduction to Algorithms

My T. [email protected]

25

Page 26: Introduction to Algorithms

Analysis of DFS

In DFS: loops on lines 1-2 & 5-7 take (V) time, excluding time to execute DFS-Visit

DFS-Visit is called once for each white vertex vV when it’s colored gray the first time. Lines 3-6 of DFS-Visit is executed |Adj[v]| times. The total cost of executing all DFS-Visit is vV|Adj[v]| = (E)

Total running time of DFS is (V+E)My T. Thai

[email protected]

26

Page 27: Introduction to Algorithms

Parenthesis TheoremTheorem 22.7 For all u, v, exactly one of the following holds:

1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and neither u nor v is a descendant of the other.

2. d[u] < d[v] < f [v] < f [u] and v is a descendant of u.

3. d[v] < d[u] < f [u] < f [v] and u is a descendant of v d[u] < d[v] < f [u] < f [v] cannot happen Like parentheses:

OK: ( ) [ ] ( [ ] ) [ ( ) ] Not OK: ( [ ) ] [ ( ] )

Corollary 22.8

v is a proper descendant of u if and only if d[u]<d[v]<f [v]<f [u]My T. Thai

[email protected]

27

Page 28: Introduction to Algorithms

Example of parenthesis theorem

My T. [email protected]

28

Page 29: Introduction to Algorithms

Proof of Parenthesis Theorem

W.l.g suppose d[u] < d[v], there are two cases: d[v] < f[u]

v is discovered while u is gray v is descendant of u v is discovered more recently than u v is finished

before the algorithm comes back and finishes u Interval [d[v], f[v]] is included in [d[u], f[u]]

d[v] > f[u] d[v] < f[v] Two intervals [d[u], f[u]] and [d[v], f[v]] are disjoint

My T. [email protected]

29

Page 30: Introduction to Algorithms

White-path theoremTheorem 22.9 In a depth-first forest of a (directed or

undirected) graph G =(V,E), vertex v is a descendant of vertex u if and only if at the time d[u] that the search discovers u, there is a path from u to v consisting entirely of white vertices.

Proof:

=> at time d[u], all vertices on the path from u to v, including v are descendant of u Apply corollary 22.8, d[u] < d[v], these vertices are white at time d[u]

<= Suppose there is a white path from u to v but v is not a descendant of u Let v’ the nearest vertex to u on the path s.t. v’ is not descendant of u w is the prior vertex of v’ on the path w is descendant of u d[u]

< d[w] < d[v’] < f[v’] < f[w]< f[u] v’ is the descendant of uMy T. [email protected]

30

Page 31: Introduction to Algorithms

Classification of edges

Tree edge: in the depth-first forest. Found by exploring (u, v)

Back edge: (u, v), where u is a descendant of v Forward edge: (u, v), where v is a descendant of u, but

not a tree edge Cross edge: any other edge. Can go between vertices in

same depth-first tree or in different depth-first trees

Theorem 22.10 In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

My T. [email protected]

31

Page 32: Introduction to Algorithms

Topological sort

Directed acyclic graph (DAG) A directed graph with no cycles Good for modeling processes and structures that

have a partial order, edge (u, v) represent order u>v

Topology sort: make a total order of a given DAG such that vertex u appears before v if there is edge (u, v)

My T. [email protected]

32

Page 33: Introduction to Algorithms

Example

My T. [email protected]

33

Order for getting dressed

Page 34: Introduction to Algorithms

Characterizing DAGs

Lemma 2.11. A directed graph G is acyclic if and only if a depth-first search of G yields no back edges.

Proof: ⇒: Show that back edge cycle⇒

Suppose there is a back edge (u, v) => v is ancestor of u in depth-first forest => there is a path from v to u => there is a cycle

⇐: Show that cycle back edge⇒ Suppose G contains cycle c. Let v be the first vertex discovered in

c, and let (u, v) be the preceding edge in c. At time d[v], vertices of c form a white path v to u By white-path theorem, u is descendant of v in depth-first forest

=>(u, v) is a back edge.

My T. [email protected]

34

Page 35: Introduction to Algorithms

TOPOLOGICAL-SORT

Running time: (V + E)

My T. [email protected]

35

Page 36: Introduction to Algorithms

Strongly connected components

Given directed graph G = (V, E), a strongly connected component (SCC) of G is a maximal set of vertices C V ⊆ such that for all u, v C∈ , there are paths both from u to v and from v to u

My T. [email protected]

36

Page 37: Introduction to Algorithms

Component Graph

GSCC = (VSCC, ESCC). VSCC has one vertex for each SCC in G ESCC has an edge if there’s an edge between the

corresponding SCC’s in G

My T. [email protected]

37

Page 38: Introduction to Algorithms

GSCC is a DAG

Proof Suppose there is a path v v in G there are paths u u v and v v u in G u and v are reachable from each other, so they

are not in separate SCC’s.

My T. [email protected]

38

Page 39: Introduction to Algorithms

Transpose of a Directed Graph

GT = transpose of directed graph G GT = (V, ET), ET = {(u, v) : (v, u) E}. GT is G with all edges reversed.

Can create GT in Θ(V + E) time if using adjacency lists.

Observation: G and GT have the same SCC’s. (u and v are reachable from each other in G if and only if reachable from each other in GT.)

My T. [email protected]

39

Page 40: Introduction to Algorithms

Algorithm to compute SCCs

Time: (V + E)

My T. [email protected]

40

Page 41: Introduction to Algorithms

Example

My T. [email protected]

41

Page 42: Introduction to Algorithms

How does it work? Idea:

By considering vertices in second DFS in decreasing order of finishing times from first DFS, we are visiting vertices of the component graph in topologically sorted order.

Because we are running DFS on GT, we will not be visiting any v from u, where v and u are in different components.

Notation: d[u] and f [u] always refer to first DFS. Extend notation for d and f to sets of vertices U V: d(U) = minuU{d[u]} (earliest discovery time)

f (U) = maxuU{ f [u]} (latest finishing time)

My T. [email protected]

42

Page 43: Introduction to Algorithms

SCCs and DFS finishing times

Proof: Case 1: d(C) < d(C)

Let x be the first vertex discovered in C. At time d[x], all vertices in C and C are white.

=> there exist paths of white vertices from x to all vertices in C and C.

By the white-path theorem, all vertices in C and C are descendants of x in depth-first tree.

By the parenthesis theorem, f [x] = f (C) > f(C).

My T. [email protected]

43

Page 44: Introduction to Algorithms

SCCs and DFS finishing times

Proof: Case 2: d(C) > d(C)

Let y be the first vertex discovered in C At time d[y], all vertices in C are white and

there is a white path from y to each vertex in C all vertices in C become descendants of y Again, f [y] = f (C).

At time d[y], all vertices in C are also white. By earlier lemma, since there is an edge (u, v),

we cannot have a path from C to C => no vertex in C is reachable from y => at time f [y], all vertices in C are still white

=> for all w C, f [w] > f [y] => f (C) > f (C)

My T. [email protected]

44

Page 45: Introduction to Algorithms

SCCs and DFS finishing times

Proof: (u, v) ET (v, u) E Since SCC’s of G and GT are the same, f(C) >

f (C), by Lemma 22.14.

My T. [email protected]

45

Page 46: Introduction to Algorithms

Correctness

When we do the second DFS, on GT, start with SCC C such that f(C) is maximum. The second DFS starts from some x C, and it

visits all vertices in C. Corollary 22.15 says that since f(C) > f (C) for all

C C, there are no edges from C to C in GT.

=> DFS will visit only vertices in C.

=> the depth-first tree rooted at x contains exactly the vertices of C.

My T. [email protected]

46

Page 47: Introduction to Algorithms

Correctness The next root chosen in the second DFS is in SCC C such

that f (C) is maximum over all SCC’s other than C. DFS visits all vertices in C, but the only edges out of C go to C,

which we’ve already visited. Therefore, the only tree edges will be to vertices in C.

Each time we choose a root for the second DFS, it can reach only vertices in its SCC—get tree edges to these, vertices in SCC’s already visited in second DFS—get no tree edges

to these.

My T. [email protected]

47

Page 48: Introduction to Algorithms

Summary

Two common ways to represent a graph: adjacency lists and adjacency matrix. Choosing representation depends on: Type of graph: dense or sparse Type of operation that is used frequently in the problem adjacency lists saves more space than adjacency matrix

BFS starts from a given source and computes the shortest paths from the source to other vertices. Some vertices may be not discovered

BFS starts from an arbitrary vertex and discovers the whole graph to capture the graph’s structure

My T. [email protected]

48

Page 49: Introduction to Algorithms

Both BFS and DFS can provide a set of reachable vertices from a given vertex

Topology sort is used to form the total order from partial orders

Strongly connected components list all strongly connected components in their topology order

BFS, DFS, Topology Sort, Strongly Connected Component run in linear time in term of the graph’s size

My T. [email protected]

49