Download presentation

Presentation is loading. Please wait.

Published byAnnice Hines Modified about 1 year ago

1
Graph Algorithms 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
Graph Algorithms Graphs - Examples

3
Graph Algorithms Graphs - Trees

4
Graph Algorithms Graphs - Directed Acyclic Graphs (DAG)

5
Graph Algorithms Graphs - Representations - Adjacency matrix

6
Graph Algorithms Graphs - Representations - Adjacency lists

7
Graph Algorithms 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
Graph Algorithms Breadth-First Search - Example sabc defg

9
Graph Algorithms Breadth-First Search - Example 0 s Q s abc defg

10
Graph Algorithms Breadth-First Search - Example 01 1 s Q ea abc defg

11
Graph Algorithms Breadth-First Search - Example 01 2 1 2 s Q abf abc defg

12
Graph Algorithms Breadth-First Search - Example 01 2 12 2 s Q bfd abc defg

13
Graph Algorithms Breadth-First Search - Example 2 s Q fdc abc defg

14
Graph Algorithms Breadth-First Search - Example s Q dcg abc defg

15
Graph Algorithms Breadth-First Search - Example s Q cg abc defg

16
Graph Algorithms Breadth-First Search - Example s Q g abc defg

17
Graph Algorithms Breadth-First Search - Example s Q = abc defg

18
Graph Algorithms 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) (V) without for cycle (E) for all while cycles together Thus T(V,E)= (V+E)

19
Graph Algorithms 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
Graph Algorithms Depth-First Search - Algorithm DepthFirstSearch(graph G) for u V[G] do colour[u] white p[u] 0 time 0 for u V[G] do if colour[v] = white then DFSVisit(v)

21
Graph Algorithms 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

22
Graph Algorithms Depth-First Search - Example sab cde

23
Graph Algorithms Depth-First Search - Example 1/ sab cde

24
Graph Algorithms Depth-First Search - Example 1/2/ sab cde

25
Graph Algorithms Depth-First Search - Example 1/ 3/ 2/ sab cde

26
Graph Algorithms Depth-First Search - Example 1/ 3/ 2/ 4/ sab cde

27
Graph Algorithms Depth-First Search - Example 1/ 3/ 2/ 4/ sab cde B

28
Graph Algorithms Depth-First Search - Example 1/ 3/ 2/ 4/5 sab cde B

29
Graph Algorithms Depth-First Search - Example 1/ 3/6 2/ 4/5 sab cde B

30
Graph Algorithms Depth-First Search - Example 1/ 3/6 2/7 4/5 sab cde B

31
Graph Algorithms Depth-First Search - Example 1/ 3/6 2/7 4/5 sab cde B F

32
Graph Algorithms Depth-First Search - Example 1/8 3/6 2/7 4/5 sab cde B F

33
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/2/7 4/5 sab cde B F

34
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/2/7 4/5 sab cde B F C

35
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/2/7 10/4/5 sab cde B F C

36
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/2/7 10/4/5 sab cde B F C B

37
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/2/7 10/114/5 sab cde B F C B

38
Graph Algorithms Depth-First Search - Example 1/8 3/6 9/122/7 10/114/5 sab cde B F C B

39
Graph Algorithms Depth-First Search - Complexity 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 DepthFirstSearch(graph G) for u V[G] do colour[u] white p[u] 0 time 0 for u V[G] do if colour[v] = white then DFSVisit(v) (V) executed (V) times Thus T(V,E)= (V+E) (E) for all DFSVisit calls together

40
Graph Algorithms 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
Graph Algorithms 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
Graph Algorithms 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.

43
Graph Algorithms 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

44
Graph Algorithms Depth-First Search - Timestamps 3/6 7/8 1/102/9 12/134/5 abs def BF 11/16 14/15 c g C C C C B

45
Graph Algorithms Depth-First Search - Timestamps (s (b (a (d d) a) (e e) b) s) (c (f f) (g g) c) s b c a e d fg

46
Graph Algorithms Depth-First Search - Timestamps ae s bf d B F c g C C C C B

47
Graph Algorithms DFS - Checking for cycles [Adapted from M.Golin]

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

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

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

51
Graph Algorithms DFS - Topological Sorting shoes socks watc h jacke t tie shirt pants belt undershorts

52
Graph Algorithms DFS - Topological Sorting [Adapted from M.Golin]

53
Graph Algorithms DFS - Topological Sorting [Adapted from M.Golin]

54
Graph Algorithms 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
Graph Algorithms DFS - Topological Sorting - Example 1 shoes socks watc h jacke t tie shirt pants belt undershorts 11/16 12/15 6/71/8 2/5 3/4 17/18 13/14 9/10

56
Graph Algorithms DFS - Topological Sorting - Example 1 shoessocks watc h jacke t tieshirt pants belt undershorts 11/1612/15 6/71/82/53/4 17/1813/149/10

57
Graph Algorithms DFS - Topological Sorting - Example 2 [Adapted from M.Golin]

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

59
Graph Algorithms DFS - Strongly Connected Components

60
Graph Algorithms DFS - Strongly Connected Components

61
Graph Algorithms DFS - Strongly Connected Components [Adapted from L.Joskowicz]

62
Graph Algorithms DFS - Strongly Connected Components [Adapted from L.Joskowicz]

63
Graph Algorithms DFS - Strongly Connected Components [Adapted from L.Joskowicz]

64
Graph Algorithms DFS - Strongly Connected Components [Adapted from L.Joskowicz]

65
Graph Algorithms DFS - Strongly Connected Components StronglyConnectedComponents(graph G) call DFS(G) to compute f[v] for all vertices v compute G T call DFS(G T ) 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
Graph Algorithms DFS - Strongly Connected Components 13/14 3/4 1/1011/16 2/712/15 8/9 5/6

67
Graph Algorithms DFS - Strongly Connected Components 13/14 3/4 1/1011/16 2/712/15 8/9 5/6

68
Graph Algorithms DFS - Strongly Connected Components 13/14 3/4 1/1011/16 2/712/15 8/9 5/6

69
Graph Algorithms DFS - SCC - Correctness 13/14 3/4 1/1011/16 2/712/15 8/9 5/6

70
Graph Algorithms 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)

71
Graph Algorithms 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
Graph Algorithms 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.

73
Graph Algorithms 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. Theorem StronglyConnectedComponents(G) correctly computes the strongly connected components of a directed graph G.

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

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

76
Graph Algorithms DFS - SCC - Applications [Adapted from L.Joskowicz]

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google