# Graphs - Definition G(V,E) - graph with vertex set V and edge set E

## Presentation on theme: "Graphs - Definition G(V,E) - graph with vertex set V and edge set E"— Presentation transcript:

Graphs - Definition G(V,E) - graph with vertex set V and edge set E
E  {(a,b)| aV and bV} - for directed graphs E  {{a,b}| aV and bV} - for undirected graphs w: E  R - weight function |V| - number of vertices |E| - number of edges Often we will assume that V = {1, ,n}

Graphs - Examples 6 1 6 1 2 2 3 4 5 3 4 5

Graphs - Trees 6 1 6 1 2 4 2 4 3 5 3 5

Graphs - Directed Acyclic Graphs (DAG)
6 1 2 4 3 5

Graphs - Representations - Adjacency matrix
1 2 3 4 5 6 6 1 2 3 4 5 6 1 1 2 3 4 5

Graphs - Representations - Adjacency lists
6 1 1 2 3 4 5 6 6 2 6 2 3 3 4 5 1 3 5 2 1

BreadthFirstSearch(graph G, vertex s) for u  V[G]  {s} do colour[u]  white; d[u] ; p[u]  0 colour[s]  gray; d[s]  0; p[s]  0 Q  {s} while Q  0 do u  Head[Q] for v  Adj[u] do if colour[v] = white then colour[v]  gray; d[v]  d[u] + 1; p[v]  u EnQueue(Q,v) DeQueue(Q) colour[u]  black

g

d e f g s Q

1 1 d e f g e a Q

1 2 1 2 d e f g a b f Q

1 2 2 1 2 d e f g b f d Q

1 2 3 2 1 2 d e f g f d c Q

1 2 3 2 1 2 3 d e f g d c g Q

1 2 3 2 1 2 3 d e f g c g Q

1 2 3 2 1 2 3 d e f g g Q

1 2 3 2 1 2 3 d e f g Q = 

BreadthFirstSearch(graph G, vertex s) for u  V[G]  {s} do colour[u]  white; d[u] ; p[u]  0 colour[s]  gray; d[s]  0; p[s]  0 Q  {s} while Q  0 do u  Head[Q] for v  Adj[u] do if colour[v] = white then colour[v]  gray; d[v]  d[u] + 1; p[v]  u EnQueue(Q,v) DeQueue(Q) colour[u]  black (V) Thus T(V,E)=(V+E) (V) without for cycle (E) for all while cycles together

Theorem After BreadthFirstSearch algorithm terminates d[v] is equal with shortest distance from s to v for all vertices v for all vertices v reachable from s the one of the shortest paths from s to v contains edge (p[v], v)

Depth-First Search - Algorithm
DepthFirstSearch(graph G) for u  V[G] do colour[u]  white p[u]  0 time  0 if colour[v] = white then DFSVisit(v)

Depth-First Search - Algorithm
DFSVisit(vertex u) time  time + 1 d[u]  time colour[u]  gray for v  Adj[u] do if colour[v] = white then p[v]  u DFSVisit(v) colour[u]  black f[u]  time

Depth-First Search - Example
b c d e

Depth-First Search - Example
b 1/ c d e

Depth-First Search - Example
b 1/ 2/ c d e

Depth-First Search - Example
b 1/ 2/ 3/ c d e

Depth-First Search - Example
b 1/ 2/ 4/ 3/ c d e

Depth-First Search - Example
b 1/ 2/ B 4/ 3/ c d e

Depth-First Search - Example
b 1/ 2/ B 4/5 3/ c d e

Depth-First Search - Example
b 1/ 2/ B 4/5 3/6 c d e

Depth-First Search - Example
b 1/ 2/7 B 4/5 3/6 c d e

Depth-First Search - Example
b 1/ 2/7 B F 4/5 3/6 c d e

Depth-First Search - Example
b 1/8 2/7 B F 4/5 3/6 c d e

Depth-First Search - Example
b 1/8 2/7 9/ B F 4/5 3/6 c d e

Depth-First Search - Example
b 1/8 2/7 9/ B C F 4/5 3/6 c d e

Depth-First Search - Example
b 1/8 2/7 9/ B C F 4/5 3/6 10/ c d e

Depth-First Search - Example
b 1/8 2/7 9/ B C F B 4/5 3/6 10/ c d e

Depth-First Search - Example
b 1/8 2/7 9/ B C F B 4/5 3/6 10/11 c d e

Depth-First Search - Example
b 1/8 2/7 9/12 B C F B 4/5 3/6 10/11 c d e

Depth-First Search - Complexity
DepthFirstSearch(graph G) for u  V[G] do colour[u]  white p[u]  0 time  0 if colour[v] = white then DFSVisit(v) (V) executed (V) times DFSVisit(vertex u) time  time + 1 d[u]  time colour[u]  gray for v  Adj[u] do if colour[v] = white then p[v]  u DFSVisit(v) colour[u]  black f[u]  time (E) for all DFSVisit calls together Thus T(V,E)=(V+E)

Depth-First Search - Classification of Edges
Trees edges - edges in depth-first forest Back edges - edges (u, v) connecting vertex u to an v in a depth-first tree (including self-loops) Forward edges - edges (u, v) connecting vertex u to a descendant v in a depth-first tree Cross edges - all other edges

Depth-First Search - Classification of Edges
Theorem In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

Depth-First Search - White Path Theorem
If during depth-first search a “white” vertex u is reachable from a “grey” vertex v via path that contains only “white” vertices, then vertex u will be a descendant on v in depth-first search forest.

Depth-First Search - Timestamps
Parenthesis Theorem After DepthFirstSearch algorithm terminates for any two vertices u and v exactly one from the following three conditions holds the intervals [d[u],f[u]] and [d[v],f[v]] are entirely disjoint the intervals [d[u],f[u]] is contained entirely within the interval [d[v],f[v]] and u is a descendant of v in depth- first tree the intervals [d[v],f[v]] is contained entirely within the interval [d[u],f[u]] and v is a descendant of u in depth-

Depth-First Search - Timestamps
b s c 3/6 2/9 1/10 11/16 B F C B 4/5 7/8 12/13 14/15 C C C d e f g

Depth-First Search - Timestamps
b f g e a d (s (b (a (d d) a) (e e) b) s) (c (f f) (g g) c)

Depth-First Search - Timestamps
B F b f g C a e C B C d

DFS - Checking for cycles

DFS - Checking for cycles

DFS - Checking for cycles

DFS - Checking for cycles

DFS - Topological Sorting
undershorts socks watch pants shoes belt shirt tie jacket

DFS - Topological Sorting

DFS - Topological Sorting

DFS - Topological Sorting
TopologicalSort(graph G) call DFS(G) to compute f[v] for all vertices v as f[v] for vertex v is computed, insert onto the front of a linked list return the linked list of vertices

DFS - Topological Sorting - Example 1
undershorts 11/16 socks 17/18 watch 9/10 pants 12/15 shoes 13/14 belt shirt 6/7 1/8 tie 2/5 jacket 3/4

DFS - Topological Sorting - Example 1
socks undershorts pants shoes watch 17/18 11/16 12/15 13/14 9/10 shirt belt tie jacket 1/8 6/7 2/5 3/4

DFS - Topological Sorting - Example 2

DFS - Topological Sorting
Theorem TopologicalSort(G) produces a topological sort of a directed acyclic graph G.

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components

DFS - Strongly Connected Components
StronglyConnectedComponents(graph G) call DFS(G) to compute f[v] for all vertices v compute GT call DFS(GT) consider vertices in order of decreasing of f[v] output the vertices of each tree in the depth-first forest as a separate strongly connected component

DFS - Strongly Connected Components
13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - Strongly Connected Components
13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - Strongly Connected Components
13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - SCC - Correctness 13/14 11/16 1/10 8/9 12/15 3/4 2/7 5/6

DFS - SCC - Correctness y' y x C(x) Assume that y preceded by y' is the closest vertex to x outside C(x). Then: - d(y)<f(y)<d(x)<d(y) (otherwise we will have xy (in G). - for all x'C(x): d(x)<d(x')<f(x')<f(x) (the largest value of f(x) will have the vertex first "discovered" in C(x)). - thus we have d(y)<f(y)<d(y')<f(y'), however there is and edge (y,y') in G, implying f(y)<d(y') d(y')<y(y). Contradiction.

DFS - SCC - Correctness Lemma
If two vertices are in the same strongly connected, then no path between them leaves this strongly connected component. Theorem In any depth-first search, all vertices in the same strongly connected component are placed in the same depth-first tree.

DFS - SCC - Correctness Theorem
In a directed graph G = (V,E) the forefather (u) of any vertex uV in any depth-first search of G is an ancestor of u. Corollary In any depth-first search of a directed graph G = (V,E) for all uV vertices u and (u) lie in the same strongly connected component.

DFS - SCC - Correctness Theorem
In a directed graph G = (V,E) two vertices u,vV lie in the same strongly connected component if and only if they have the same forefather in a depth-first search of G. StronglyConnectedComponents(G) correctly computes the strongly connected components of a directed graph G.

DFS - SCC - Correctness 2 [Adapted from S.Whitesides]

DFS - SCC - Correctness 2 [Adapted from S.Whitesides]

DFS - SCC - Applications