1 / 77

Graphs - Definition

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

Download Presentation

Graphs - Definition

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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}

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

  3. Graphs - Trees 6 1 6 1 2 4 2 4 3 5 3 5 OK ?

  4. Graphs - Trees 6 1 6 1 2 4 2 4 3 5 3 5 OK “Rooted tree”

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

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

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

  8. 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

  9. Breadth-First Search - Example a s b c d e f g

  10. Breadth-First Search - Example a s b c  0       d e f g Q

  11. Breadth-First Search - Example a s b c 1 0    1   d e f g Q

  12. Breadth-First Search - Example a s b c 1 0 2   1 2  d e f g Q

  13. Breadth-First Search - Example a s b c 1 0 2  2 1 2  d e f g Q

  14. Breadth-First Search - Example a s b c 1 0 2 3 2 1 2  d e f g Q

  15. Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q

  16. Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q

  17. Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q

  18. Breadth-First Search - Example a s b c 1 0 2 3 2 1 2 3 d e f g Q = 

  19. 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

  20. 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)

  21. 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)

  22. 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

  23. Depth-First Search - Example s a b c d e

  24. Depth-First Search - Example s a b 1/ c d e

  25. Depth-First Search - Example s a b 1/ 2/ c d e

  26. Depth-First Search - Example s a b 1/ 2/ 3/ c d e

  27. Depth-First Search - Example s a b 1/ 2/ 4/ 3/ c d e

  28. Depth-First Search - Example s a b 1/ 2/ B 4/ 3/ c d e

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

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

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

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

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

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

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

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

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

  38. 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

  39. 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

  40. 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)

  41. 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

  42. 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.

  43. 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.

  44. 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

  45. 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

  46. 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)

  47. Depth-First Search - Timestamps s c C B F b f g C a e C B C d

  48. DFS - Checking for cycles [Adapted from M.Golin]

  49. DFS - Checking for cycles [Adapted from M.Golin]

  50. DFS - Checking for cycles [Adapted from M.Golin]

More Related