Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs...

31
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first search (BFS) depth-first search(DFS) topological sort strongly connected components

Transcript of Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs...

Page 1: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Chapter 22: Elementary Graph Algorithms

Overview:Definition of a graphRepresentation of graphs

adjacency listmatrix

Elementary search algorithmsbreadth-first search (BFS)depth-first search(DFS)

topological sortstrongly connected components

Page 2: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Notation

Graph G(V,E) is a data structure defined by a set of vertices Va set of eges E

|V| = # of vertices

|E| = # of edges

Usually omit | | in asymptotic notationQ(V, E) means Q(|V|, |E|)

Page 3: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Adj = adjacency-list representation of G(V,E) is an array of |V| lists

Adj[u] contains all vertices adjacent to vertex u

For a directed graph, edge (uv) represented by v in Adj[u]. sum of lengths of adjacency lists = |E|

For an undirected graph, edge (u,v) appears as v in Adj[u] and as u in Adj[v].

\sum of lengths of adjacency lists = 2|E|

Memory requirement is Q(V+E)

A weighted graph has a function w(u,v) defined on the domain E.

The weight of edge (u,v) can be stored as property of vertex v in Adj[u].

Page 4: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

adjacency-matrix representation:

Number the vertices 1,2,...|V|

Define |V| x |V| matrix A such that aij = 1 if (i, j) E, aij = 0 otherwise

For a weighted graph, set aij = w(i,j) if (i, j) E

Disadvantage is requires Q(V2) memory regardless of |E|

Advantage is speed of determining if edge (u,v) is in graph

For an undirected graph A is symmetric

A sparse graphs mean |E| << |V|2 adjacency-list representation preferred (saves space)

A dense graph means |E| ~ |V|2

adjacency-matrix representation preferred (speed)

Page 5: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Graph representations

1

1

Page 6: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Breadth-first search: Given G(V,E) and a source vertex s,

Breadth-first search does the following:(1) finds every vertex v reachable from s

(2) calculates distance (minimum number of edges) between s and v

(3) produces a Breadth-first tree with s as the root and every reachable vertex as a node

The path from s to v in the Breadth-first tree corresponds to the shortest path in G. “Breadth-first” finds all vertices at distance k from s before searching for any vertices at distance k+1.

Page 7: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

BFS(G,s) pseudo code for each u V(G) – {s}

do color[u] white, d[u] , p[u] NIL

(Initialization: color all vertices white for “undiscovered” set distance from s as infinite, set predecessor in Breadth-first tree to NIL)

color[s] gray, d[s] 0, p[s] NIL

(initialize s as gray for “discovered” i.e. reachable from sA gray vertex has been discovered but its adjacency list has not been searched for links to other vertices. After adjacency list is searched, color is changed to black)

Q 0, Enqueue(Q,s)

(A “first-in first-out” queue holds a list of the current gray vertices Gray vertices with the smallest distance from s are processed firstResolve ambiguity by some rule like alphabetical order)

Page 8: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

BFS(G,s) algorithm continued while Q 0

do u Dequeue(Q)for each v Adj[u]

do if color[v] = white then color[v] = gray d[v]d[u] + 1p[v] uEnqueue(Q,v)

color[u] black (adj list searched)

Runtime analysis:initialization requires O(V).each vertex is discovered at most once queue operations require O(V).searching adjacency lists requires O(E) total runtime O(V+E)

Page 9: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Example of BFS p596

Page 10: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Predecessor of v: p(v) is the vertex in whose adjacency list v was discovered

Predecessor sub-graph Gp(Vp,Ep)Vp = {v V : p[v] NIL} {s}Ep = {(p[v],v) : v Vp - {s}}

Predecessor sub-graph of BFS is a single tree

Page 11: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Finding shortest path is most common application of Breadth-first search

d(s,u) = shortest path between s and any u Vd(s,u) = if u is not reachable from s

Weight of a path is the sum of the weights of its edges

For an unweighted graph,weight of path = number of edges = length of path

For weighted graph, least-weight is not necessarily shortest path

Page 12: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Theorem 22.5: On termination of BFS(G,s), (1)all reachable vertices have been found

(2) d[v] = d(s,v) for v V (some may be infinite)

(3) for any reachable v s, a shortest path includes p[v] followed by edge (p[v],v).

Page 13: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Depth-first search: Explores all edges leaving a given vertex, v, before “backtracking” to explore edges leaving the vertex from which v was discovered

Continues until all vertices reachable from a given source are discovered If the graph still contains undiscovered vertices, choose a new source

p[v] = u if v was discovered in a search of the adjacency list of u

As in BFS, vertices initialized to white, colored gray when discovered, colored back after their adjacency list has been examined

d[v] is the timestamp field when v was discovered

f[v] is the timestamp field when v was blackened

range of timestamps is 1 to 2|V| d[v] < f[v]

Page 14: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Classification of edges:

Tree edge connects vertex to its predecessor

Back edge connects vertex to an ancestor in the same tree(also self loops in directed graph)

Forward edge connects vertex to descendant in the same tree

Cross edge: all othersIf in the same tree, then one vertex cannot be an ancestor of the other

Edges (u,v) can be classified by the color v when it is first exploredwhite v tree edgegray v back edgeblack v forward or cross edge

Page 15: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

DFS Pseudocodes:

DFS(G)for each u V (initialization)

do color[u] white, p[u] NILtime 0for each u V (choose a new source)

do if color[u] = white then DFS-Visit(u)

DFG-Visit(u) (DFS from source u)color[u] gray, time time +1, d[u] time

(vertex u discovered)for each v adj[u]

do if color[v] = whitethen p[v] u, DFG-Visit(v)

color[u] black, f[u] time, time time +1(finished with vertex u)

Page 16: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

DFS: Fig 22.4 p605

Page 17: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Predecessor subgraph Gp = (V,Ep); Ep = {(p[v],v) ; v V and p[v] NIL}Gp contains all the vertices of G; hence, may be multiple trees

Predecessor subgraph showing “parenthesis” structure

Predecessor subgraph showing Non-tree edgesB-edge: head () contains tail ()F-edge: tail () contains head ()C-edge: connects disjoint ()Note C-edges inside trees.

Fig 22.5 p607

Page 18: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Properties of DFS:

Gp of DFS is a forest of trees, each of which reflects the pattern of recursive calls to DFS-Visit (slide 15)

Pattern of discovery and finishing times has “parenthesis structure” (slide 18)For any 2 vertices u and v, exactly one of the following is true(1) intervals [d[u], f[u]] and [d[v], f[v]] are disjoint and

neither u nor v is a descendent of the other(2) [d[u], f[u]] is contained entirely in [d[v], f[v]] and

u is a descendent of v(3) [d[v], f[v]] is contained entirely in [d[u], f[u]] and

v is a descendent of u.

Corollary of the Parenthesis Theorem:

Vertex v is a descendant of u in the depth-first forest of G if and only if d[u] < d[v] < f[v] < f[u]

Page 19: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Theorem 22.10: Only tree and back edges occur in a DFS of an undirected graph

In DFS of undirected graph, edge type is determined by direction of search when edge is encountered.

Consider edge(u,v) with v in u’s adjacency list.

If edge(u,v) is traversed from u to v, then v is discovered on the traversal and edge(u,v) is a tree edge.

The only remaining option for traversal of edge(u,v) is from v to u = p(v), which we call a “back” edge in an undirected graph.

Predecessor graph is a single tree

Page 20: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

CptS 450 Spring 2015[All problems are from Cormen et al, 3rd Edition]

Homework Assignment 11: due 4/15/151. ex 22.2-2 p 601 Draw Gp Show all edges2. ex 22.3-2 p 610 Draw Gp Show all edges

Page 21: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

White-Path Theorem: Vertex v is a descendant of u in the depth-first forest of G (directed or undirected) if and only if at the time d[u] when u is discovered, v can be reached from u by a path consisting entirely of white vertices.

Page 22: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Lemma 22:11 A directed graph is acyclic if and only if a DFS yields no back edges

Proof: If G has a back edge (u,v) then v is an ancestor of u and (u,v) completes a cycle in G.

If G contains cycle c and v is the first vertex discovered in c, then a white path exist to vertex u the last vertex in c By white-path theorem, u is a decendent of v (u,v) must be a back edge

Page 23: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

DAG = directed acyclic graphPrecedence among events: common use of DFS on DAGs

Example: precedence of events getting dressed

Page 24: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

topological sort:linear ordering of vertices in a dag such that if G contains directed edge (u,v), then u appears before v in the ordering

Pseudocode for topological sort:Topological-Sort(G)

DFS(G) yields f[v] for all verticesAt each f[v], insert the vertex into the front of a linked listreturn list

New graph topology: all edges point left-to-rightf(v) decreases left-to-right

Page 25: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Theorem 22.12: Proof of the correctness of Topological-Sort(G)

When edge (u,v) is explored in the DFS of a dag, v must be white or black because

if v is gray, then (u,v) is a back edge (we would be exploring the adjacency list of v) and DFS of a dag cannot yield a back edge.

If v is white, then v is a descendant of u and f[v] < f[u] (Corollary of the Parenthesis Theorem in slide #19)

If v is black, then f[v] has already been assigned and, since we are still exploring the adj[u], f[v] < f[u].

Thus, for any edge(u,v) in the dag, f[v] < f[u]

\in topological sort, v will be on the right of u

\ all edges point to the right

Page 26: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Strongly Connected Components (SCCs) Strongly connected components (SCCs) of directed graph G(V,E) are a set of vertices VSCC V such that for every pair of vertices u and v, u ~> v and v ~> u (u reachable from v and v reachable from u)

Component graph GSCC = (VSCC, ESCC)Let C1, C2, ..., Ck denote the SCCs of directed graph G

VSCC = {v1, v2, ..., vk} contains a vertex from each SCC

edge (vi,vj) ESCC if G contains edge (x,y) for some x Ci and y Cj

(i.e. edges the component graph connect the SCCs of G)

Transpose of directed G(V,E) = GT(V,ET) where ET = {(u,v) : (v,u) E}(i.e. GT has same vertices a G, but its edges are reversed)

Page 27: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices

Construct the transpose of G

Call DFS(GT) with vertices in order of decreasing f[u] of DFS(G)

The vertices of each tree of the DFS(GT) forest are a SCC of G

Example: Fig 22.9 p616

Page 28: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

SCC(G) Pseudocode: Call DFS(G) to get f[u] for all vertices

Construct the transpose of G

Call DFS(GT) with vertices in order of decreasing f[u] of DFS(G)

The vertices of each tree of the DFS(GT) forest are a SCC of G

Page 29: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

CptS 450 Spring 2015[All problems are from Cormen et al, 3rd Edition]

Homework Assignment 12: due 4/22/151. ex 22.4-1 p 614 Topo sort on DAG of Fig 22.8 p6152. ex 22.5-2 p 620 SCCs on graph in Fig 22.6 p611

Page 30: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

Lemma 22.14Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v) E be an edge with u in C and v in C’, then f(C) > f(C’)

Case 1: d(C) < d(C’) use white path theoremCase 2: d(C) > d(C’) use parenthesis structure

Corollary 22.15Let C and C’ be distinct SCCs of directed G(V,E). Let (u,v) ET be an edge with u in C and v in C’, then f(C) < f(C’)

Theorems behind the SCC pseudo-code

Page 31: Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.

How Strongly-Connected-Components(G) works:

In the DFS of GT, start with a vertex in Cmax, the SCC that has the largest finish time.

The search will visit all of the vertices of Cmax but will not find an edge that connects Cmax to any other SCC.

If such an edge were found to C’, then by Corollary 22.15 of Lemma 22.14 f(Cmax) < f(C’), which violates the choice of Cmax as the SCC with the largest finish time

As the source of the next DFS in GT, chose a vertex in C’, the SCC with f(C’) larger than all other finishing time except those in Cmax.

The search will visit all the vertices of C’ and may find some edges that connect it to Cmax but not to any other SCC.

Since the vertices of Cmax have already been discovered, the only new vertices visited from the second source are those in C’.