Download presentation

Presentation is loading. Please wait.

Published byAnnice Hines Modified over 2 years ago

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

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

4
**Graphs - Directed Acyclic Graphs (DAG)**

6 1 2 4 3 5

5
**Graphs - Representations - Adjacency matrix**

1 2 3 4 5 6 6 1 2 3 4 5 6 1 1 2 3 4 5

6
**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

7
**Breadth-First Search - Algorithm**

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

8
**Breadth-First Search - Example**

g

9
**Breadth-First Search - Example**

d e f g s Q

10
**Breadth-First Search - Example**

1 1 d e f g e a Q

11
**Breadth-First Search - Example**

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

12
**Breadth-First Search - Example**

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

13
**Breadth-First Search - Example**

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

14
**Breadth-First Search - Example**

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

15
**Breadth-First Search - Example**

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

16
**Breadth-First Search - Example**

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

17
**Breadth-First Search - Example**

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

18
**Breadth-First Search - Complexity**

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

19
**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)

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

21
**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

22
**Depth-First Search - Example**

b c d e

23
**Depth-First Search - Example**

b 1/ c d e

24
**Depth-First Search - Example**

b 1/ 2/ c d e

25
**Depth-First Search - Example**

b 1/ 2/ 3/ c d e

26
**Depth-First Search - Example**

b 1/ 2/ 4/ 3/ c d e

27
**Depth-First Search - Example**

b 1/ 2/ B 4/ 3/ c d e

28
**Depth-First Search - Example**

b 1/ 2/ B 4/5 3/ c d e

29
**Depth-First Search - Example**

b 1/ 2/ B 4/5 3/6 c d e

30
**Depth-First Search - Example**

b 1/ 2/7 B 4/5 3/6 c d e

31
**Depth-First Search - Example**

b 1/ 2/7 B F 4/5 3/6 c d e

32
**Depth-First Search - Example**

b 1/8 2/7 B F 4/5 3/6 c d e

33
**Depth-First Search - Example**

b 1/8 2/7 9/ B F 4/5 3/6 c d e

34
**Depth-First Search - Example**

b 1/8 2/7 9/ B C F 4/5 3/6 c d e

35
**Depth-First Search - Example**

b 1/8 2/7 9/ B C F 4/5 3/6 10/ c d e

36
**Depth-First Search - Example**

b 1/8 2/7 9/ B C F B 4/5 3/6 10/ c d e

37
**Depth-First Search - Example**

b 1/8 2/7 9/ B C F B 4/5 3/6 10/11 c d e

38
**Depth-First Search - Example**

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

39
**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)

40
**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

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

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

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

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

45
**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)

46
**Depth-First Search - Timestamps**

B F b f g C a e C B C d

47
**DFS - Checking for cycles**

[Adapted from M.Golin]

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]

51
**DFS - Topological Sorting**

undershorts socks watch pants shoes belt shirt tie jacket

52
**DFS - Topological Sorting**

[Adapted from M.Golin]

53
**DFS - Topological Sorting**

[Adapted from M.Golin]

54
**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

55
**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

56
**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

57
**DFS - Topological Sorting - Example 2**

[Adapted from M.Golin]

58
**DFS - Topological Sorting**

Theorem TopologicalSort(G) produces a topological sort of a directed acyclic graph G.

59
**DFS - Strongly Connected Components**

60
**DFS - Strongly Connected Components**

61
**DFS - Strongly Connected Components**

[Adapted from L.Joskowicz]

62
**DFS - Strongly Connected Components**

[Adapted from L.Joskowicz]

63
**DFS - Strongly Connected Components**

[Adapted from L.Joskowicz]

64
**DFS - Strongly Connected Components**

[Adapted from L.Joskowicz]

65
**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

66
**DFS - Strongly Connected Components**

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

67
**DFS - Strongly Connected Components**

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

68
**DFS - Strongly Connected Components**

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

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

70
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 xy (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.

71
**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.

72
**DFS - SCC - Correctness Theorem**

In a directed graph G = (V,E) the forefather (u) of any vertex uV 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 uV vertices u and (u) lie in the same strongly connected component.

73
**DFS - SCC - Correctness Theorem**

In a directed graph G = (V,E) two vertices u,vV 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.

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

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

76
**DFS - SCC - Applications**

[Adapted from L.Joskowicz]

Similar presentations

Presentation is loading. Please wait....

OK

Chapter 22: Elementary Graph Algorithms

Chapter 22: Elementary Graph Algorithms

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Free download ppt on alternative sources of energy Ppt on standing order activated Ppt online compressors Ppt on 2nd world war countries Ppt on solar power plant Ppt on indian textile industries in india Ppt on solids in maths Ppt on information technology industry in india Disaster management ppt on uttarakhand Ppt on cloud computing security