770 likes | 783 Views
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
E N D
Graphs - Definition G(V,E) - graph with vertex set V and edge set E E {(a,b)| aV and bV} - for directed graphs E {{a,b}| aV and bV} - 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 OK ?
Graphs - Trees 6 1 6 1 2 4 2 4 3 5 3 5 OK “Rooted tree”
Graphs - Directed Acyclic Graphs (DAG) 6 1 2 4 3 5
Graphs - Representations - Adjacency matrix 6 1 2 3 4 5
Graphs - Representations - Adjacency lists 6 1 2 3 4 5
Breadth-First Search - Algorithm BreadthFirstSearch(graph G, vertex s) foru 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
Breadth-First Search - Example a s b c d e f g
Breadth-First Search - Example a s b c 0 d e f g Q
Breadth-First Search - Example a s b c 1 0 1 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 1 2 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 2 1 2 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q
Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q =
Breadth-First Search - Complexity BreadthFirstSearch(graph G, vertex s) foru 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
Breadth-First Search - Shortest Distances • 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) foru V[G] do colour[u] white p[u] 0 time 0 foru V[G] do 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 time time + 1 f[u] time
Depth-First Search - Example s a b c d e
Depth-First Search - Example s a b 1/ c d e
Depth-First Search - Example s a b 1/ 2/ c d e
Depth-First Search - Example s a b 1/ 2/ 3/ c d e
Depth-First Search - Example s a b 1/ 2/ 4/ 3/ c d e
Depth-First Search - Example s a b 1/ 2/ B 4/ 3/ c d e
Depth-First Search - Example s a b 1/ 2/ B 4/5 3/ c d e
Depth-First Search - Example s a b 1/ 2/ B 4/5 3/6 c d e
Depth-First Search - Example s a b 1/ 2/7 B 4/5 3/6 c d e
Depth-First Search - Example s a b 1/ 2/7 B F 4/5 3/6 c d e
Depth-First Search - Example s a b 1/8 2/7 B F 4/5 3/6 c d e
Depth-First Search - Example s a b 1/8 2/7 9/ B F 4/5 3/6 c d e
Depth-First Search - Example s a b 1/8 2/7 9/ B C F 4/5 3/6 c d e
Depth-First Search - Example s a b 1/8 2/7 9/ B C F 4/5 3/6 10/ c d e
Depth-First Search - Example s a b 1/8 2/7 9/ B C F B 4/5 3/6 10/ c d e
Depth-First Search - Example s a b 1/8 2/7 9/ B C F B 4/5 3/6 10/11 c d e
Depth-First Search - Example s a 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) foru V[G] do colour[u] white p[u] 0 time 0 foru V[G] do 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 time time + 1 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 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- • first tree
Depth-First Search - Timestamps a 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 s c b f g e a d 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (s (b (a (d d) a) (e e) b) s) (c (f f) (g g) c)
Depth-First Search - Timestamps s c C B F b f g C a e C B C d
DFS - Checking for cycles [Adapted from M.Golin]
DFS - Checking for cycles [Adapted from M.Golin]
DFS - Checking for cycles [Adapted from M.Golin]