Elementary Graph Algorithms

1 / 19

# Elementary Graph Algorithms - PowerPoint PPT Presentation

Elementary Graph Algorithms. Peter Schröder. Representations. For graphs G=(V,E) adjacency list for every vertex v list Adj[v] better for sparse graphs: size of O(max(E,V)) adjacency matrix A: a ij =1 if (v i ,v j ) in E single bit entry for each edge in |V|x|V| matrix

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Elementary Graph Algorithms' - hans

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Elementary Graph Algorithms

Peter Schröder

Representations
• For graphs G=(V,E)
• for every vertex v list Adj[v]
• better for sparse graphs: size of O(max(E,V))
• adjacency matrix A: aij=1 if (vi,vj) in E
• single bit entry for each edge in |V|x|V| matrix
• better for dense graphs: size is O(|V|2)
• meaning of A2?
• Given a graph G=(V,E) and a distinguished vertex s in V
• discover every vertex reachable from s
• find shortest path from s
• the name reflects the fact that all vertices at distance k-1 are discovered before any at distance k are discovered
• 3 colors
• white: undiscovered; gray: discovered; black: all neighbors have been discovered
• Incremental buildup of a tree with a predecessor relationship

BFS(G,s)

for( u in V[G]-{s} )

color[u] = white; d[u] = infinity; p[u] = nil;

color[s] = gray; d[s] = 0; p[s] = nil; Q = {s}

while( Q != {} )

if( color[v] == white )

color[v] = gray; d[v] = d[u]+1; p[v] = u;

Enqueue(Q,v)

Dequeue(Q)

color[u] = black

Analysis
• Running time?
• Shortest paths
• Lemma 1: Let G=(V,E) be (un-)directed, s in V arbitrary. Then for any (u,v) in E: ∂(s,v)≤ ∂(s,u)+1
• Lemma 2: Let G=(V,E) be (un-)directed, suppose BFS(G,s) is run. Then upon termination d[v]≥ ∂(s,v) for all v in V
Analysis
• Shortest paths
• Lemma 3: Suppose during execution of BFS(G,s) Q contains (v1,v2,…vr). Then d[vr] ≤d[v1]+1 and d[vi] ≤d[vi+1] for i=1,2,…r-1
• Theorem: Correctness of BFS. Suppose BFS(G,s) is run then every v in V reachable from s is discovered and d[v]= ∂(s,v) upon termination. One of the shortest paths from s to v is given by the shortest path from s to p[v] and the edge (p[v],v)
• BFS builds a breadth first tree in the p[] field
• predecessor subgraph of G=(V,E)
• Gp(Vp,Ep) where Vp contains all v in V with non-nil p[] field plus {s} and Ep contains all predecessor edges for all v in Vp
• this is a tree. Why?
• Lemma 5: BFS(G,s) produces a predecessor subgraph which is a breadth first tree
Depth First Search
• Different strategy
• always go as deep as possible before going on
• not just a single tree, but a forest results
• Gp=(V,Ep) where Ep consists of all edges (p[v],v) for any v in V with predecessor field not empty
• contrast with BFS tree: Gp(Vp,Ep), where Ep depends on Vp not V!
• there will be no distinguished start vertex
Depth First Search

DFS(G)

for( u in V[G] ) color[u] = white; p[u] = nil;

time = 0

for( u in V[G] ) if( color[u] == white ) DFS-visit(u)

DFS-visit(u)

color[u] = gray; d[u] = time = time+1;

if( color[v] == white )

p[v] = u; DFS-visit(v);

color[u] = black; f[u] = time = time + 1;

Analysis
• Running time
• Correctness
• Theorem 6: (Parenthesis theorem) In any DFS of a (un-)directed graph G=(V,E) for any pair u,v one of the following holds
• [d[u],f[u]] and [d[v],f[v]] are entirely disjoint
• [d[u],f[u]] is contained in [d[v],f[v]] and u is a descencent of v
• vice versa
• Corollary 7: descendent intervals are nested
Analysis
• Correctness
• Theorem 8: (White path theorem) In a DFF of a (un-)directed G=(V,E) vertex v is a descendent of u iff at the time d[u] v can be reached from u along a path of white vertices
Classification
• DFS admits classification of edges
• Tree edges: members of DFF of Gp. (u,v) is a tree edge if v was discovered exploring u
• Back edges: edges (u,v) connecting a vertex u to an ancestor v (self loops are back edges)
• Forward edges: non-tree edges (u,v) connecting u to a descendent v
• Cross edges: all others (c0uld be same tree, could be different trees)
Edge Types
• Undirected graphs
• Theorem 9: in a DFS of an undirected graph G=(V,E) every edge of G is either a tree edge or a back edge
Topological Sort
• For a directed acyclic graph (DAG)
• a topological sort is a linear ordering of all its vertices such that if G contains (u,v) then u appears before v in the ordering

Topological-Sort(G)

call DFS(G) to compute finish times

enter vertices onto front of list

according to their finish times

Analysis
• Running time
• Correctness
• Lemma 10: a directed graph is acyclic iff DFS(G) yields no back edges
• Theorem 11: Topological-Sort(G) produces a topological sort of a DAG G
A Classic Problem
• Strongly connected components
• a strongly connected component of a directed graph G=(V,E) is a maximal subset U of V such that for any u,v in U there is a path from u to v and a path from v to u
• form the transpose GT (runtime?)
• u is reachable from v in G iff v is reachable from u in GT
Algorithm

Strongly-Connected-Components(G)

call DFS(G) to compute finish times

compute GT

call DFS(GT) and consider vertices

decreasing finish time order

output vertices of each tree in DFS(GT)

as separate strongly connected component

Analysis
• Running time
• Correctness
• Lemma 12: if two vertices are in the same SCC then no path between them ever leaves the SCC
• Theorem 13: in any DFS all vertices in the same SCC are placed in the same DFT
Analysis
• Correctness
• Theorem 14: in a directed graph G=(V,E) the forefather phi(u) of any u in V in any DFS of G is an ancestor of u
• Corollary 15: in any DFS of a directed graph G=(V,E) vertices u and phi(u) for all u in V lie in the same SCC
• Theorem 16: in a directed graph G=(V,E) two vertices u,v in V lies in the same SCC iff they have the same forefather in DFS(G)