Presentation is loading. Please wait.

Presentation is loading. Please wait.

Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms

Similar presentations


Presentation on theme: "Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms"— Presentation transcript:

1 Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms
Christian Scheideler SS 2019

2 Basic Graph Algorithms
Overview: Basic notation Graph representations Breadth-First-Search Depth-First-Search Minimum spanning trees SS 2019 Chapter 5

3 Basic Notation A directed graph G is a pair (V,E), where V is a finite set and E⊆VV. Elements in V are called nodes or vertices, elements in E are called edges. Correspondingly, V is the node set and E the edge set of G. Edges are ordered pairs of nodes. Edges of the form (u,u), uV, are allowed and called loops. If (u,v)E, we say that the edge leads from u to v. We also say that u and v are adjacent. The degree of a node v is the number of edges (v,w) in E. SS 2019 Chapter 5

4 Directed Graph 1 2 5 3 4 6 SS 2019 Chapter 5

5 Basic Notation An undirected graph G is a pair (V,E), where V is a finite set and E is a set of subsets of V of size two. Elements in V are called nodes or vertices, elements in E are called edges. Correspondingly, V is the node set and E the edge set of G. Edges have the form {u,v}. Edges of the form {u,u} are usually not allowed. If {u,v}E, then we say that u and v are adjacent. The degree of u is the number of edges {u,v} in E. SS 2019 Chapter 5

6 Undirected Graph V={1,2,3,4,5,6} E={ {1,2}, {1,5}, {2,5}, {3,6} } 1 2
SS 2019 Chapter 5

7 Graph Theory D=4 D B C A E n: number of nodes, m: number of edges
(v,w): distance of w to v in G - directed graph: number of edges of a shortest directed path from v to w - undirected draph: number of edges of a shortest path from v to w D=maxv,w (v,w): diameter of G D B C A E D=4 SS 2019 Chapter 5

8 Graph Theory G undirected:
G is connected: diameter D is finite (i.e., there is a path from every node to every other node in G) Example: graph that is not connected D B C A E SS 2019 Chapter 5

9 Graph Theory G directed:
G is weakly connected: diameter D is finite, if all edges are seen as undirected G is strongly connected: D is finite D B C A E SS 2019 Chapter 5

10 large diameter (n-1 for n nodes)  bad for data structures
Graph Theory Linear list: degree 2 large diameter (n-1 for n nodes)  bad for data structures SS 2019 Chapter 5

11 Diameter is 2d ~ 2 log2 n  good for data structures
Graph Theory Complete binary tree: n=2d+1-1 nodes, degree 3 Diameter is 2d ~ 2 log2 n  good for data structures depth d d SS 2019 Chapter 5

12 n = k2 nodes, maximum degree 4 diameter is 2(k-1) ~ 2 n
Graph Theory 2-dimensional grid: n = k2 nodes, maximum degree 4 diameter is 2(k-1) ~ 2 n 1 side length k k SS 2019 Chapter 5

13 Graph Representations
1: Sequence of edges 1 2 Dummy 4 3 / (1,2) (2,3) (3,4) (4,1) SS 2019 Chapter 5

14 Graph Representations
2: Adjacency list 1 n 1 2 V 2 3 4 1 4 3 3 4 SS 2019 Chapter 5

15 Graphrepräsentationen
3: Adjacency matrix A[i,j]{0,1} A[i,j]=1 if and only if (i,j)E 1 2 A = 4 3 SS 2019 Chapter 5

16 Graph Traversal Central question: How can we traverse the nodes of the graph so that every node is visited at least once? SS 2019 Chapter 5

17 Graph Traversal Central question: How can we traverse the nodes of the graph so that every node is visited at least once? Basic strategies: Breadth-first search Depth-first search SS 2019 Chapter 5

18 Explore graph distance by distance from s
Breadth-First Search Start at some node s Explore graph distance by distance from s s SS 2019 Chapter 5

19 Depth-First Search Start from some node s
Explorate graph in depth-first manner ( : current, : still active, : done) s SS 2019 Chapter 5

20 Breadth-First Search Breadth-first search algorithm forms the base of many other graph algorithms. Given a graph G=(V,E) and a source sV, the goal of breadth-first search is to find all nodes vV that are reachable from s. A node v is reachable from s if there is a (directed) path in G from s to v. The breadth-first search algorithm can also be used to compute the distance d(s,v) of v from s. SS 2019 Chapter 5

21 Breadth-First Search If a new node v is discovered while searching for new nodes in Adj[u], then u is called the predecessor of v. (Adj[u]: set of neighbors or adjacency list of u, i.e., the nodes vV with {u,v}E resp. (u,v)E) In the algorithm: nodes are white, gray, or black. White nodes are nodes that have not been discovered yet. Gray nodes are nodes that have already been discovered but whose adjacency list has not completely been explored yet. Black nodes are nodes that have already been discovered and fully processed. SS 2019 Chapter 5

22 Pseudo-code for Breadth-First Search
BFS(G,S) 1 for each node uV\{s} do color[u]WHITE d[u]   p[u]  NIL color[s]  GRAY d[s]  0 p[s]  NIL Q  { } Enqueue(Q,s) while Q  { } do u  Dequeue(Q) for each vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1 p[v]  u Enqueue(Q,v) color[u]  BLACK SS 2019 Chapter 5

23 Pseudo-code for Breadth-First Search
BFS(G,S) 1 for each node uV\{s} do color[u]WHITE d[u]   p[u]  NIL color[s]  GRAY d[s]  0 p[s]  NIL Q  { } Enqueue(Q,s) while Q  { } do u  Dequeue(Q) for each vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1 p[v]  u Enqueue(Q,v) color[u]  BLACK initialize BFS SS 2019 Chapter 5

24 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s b f i Q: s SS 2019 Chapter 5 24

25 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s b f i Q: s SS 2019 Chapter 5 25

26 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s u b f i Q: SS 2019 Chapter 5 26

27 Breadth-First Search Example
v=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s u b f i Q: SS 2019 Chapter 5 27

28 Breadth-First Search Example
v=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s u b f i Q: SS 2019 Chapter 5 28

29 Breadth-First Search Example
v=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK c g s u b f i Q: SS 2019 Chapter 5 29

30 Breadth-First Search Example
v=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u b f i Q: SS 2019 Chapter 5 30

31 Breadth-First Search Example
v=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u b f i Q: a SS 2019 Chapter 5 31

32 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u v=b f i Q: a SS 2019 Chapter 5 32

33 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u v=b f i Q: a SS 2019 Chapter 5 33

34 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u v=b f i Q: a SS 2019 Chapter 5 34

35 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u v=b f i 1 Q: a SS 2019 Chapter 5 35

36 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u v=b f i 1 Q: a, b SS 2019 Chapter 5 36

37 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u b f i 1 Q: a, b SS 2019 Chapter 5 37

38 Breadth-First Search Example
BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s u b f i 1 Q: a, b SS 2019 Chapter 5 38

39 Breadth-First Search Example
u=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b SS 2019 Chapter 5 39

40 Breadth-First Search Example
v=d u=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b SS 2019 Chapter 5 40

41 Breadth-First Search Example
v=d u=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b SS 2019 Chapter 5 41

42 Breadth-First Search Example
v=d u=a e h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b SS 2019 Chapter 5 42

43 Breadth-First Search Example
v=d u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b SS 2019 Chapter 5 43

44 Breadth-First Search Example
v=d u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s b f i 1 Q: b, d SS 2019 Chapter 5 44

45 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s b f i 1 Q: b, d SS 2019 Chapter 5 45

46 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s b f i 1 Q: b, d SS 2019 Chapter 5 46

47 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s b f i 1 Q: b, d SS 2019 Chapter 5 47

48 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 b f i 1 Q: b, d SS 2019 Chapter 5 48

49 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 b f i 1 Q: b, d, c SS 2019 Chapter 5 49

50 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 b f i 1 Q: b, d, c SS 2019 Chapter 5 50

51 Breadth-First Search Example
u=a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 b f i 1 Q: b, d, c SS 2019 Chapter 5 51

52 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 u=b f i 1 Q: d, c SS 2019 Chapter 5 52

53 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 u=b f i 1 Q: d, c SS 2019 Chapter 5 53

54 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 u=b f i 1 Q: d, c SS 2019 Chapter 5 54

55 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 u=b f i 1 Q: d, c SS 2019 Chapter 5 55

56 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 v=c g s 2 b f i 1 Q: c SS 2019 Chapter 5 56

57 Breadth-First Search Example
u=d a v=e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 c g s 2 b f i 1 Q: c SS 2019 Chapter 5 57

58 Breadth-First Search Example
u=d a v=e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g s 2 b f i 1 Q: c, e SS 2019 Chapter 5 58

59 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g s 2 b v=f i 1 Q: c, e SS 2019 Chapter 5 59

60 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g s 2 b v=f i 1 3 Q: c, e, f SS 2019 Chapter 5 60

61 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 b f i 1 3 Q: c, e, f SS 2019 Chapter 5 61

62 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 b f i 1 3 Q: c, e, f SS 2019 Chapter 5 62

63 Breadth-First Search Example
u=d a e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 b f i 1 3 Q: c, e, f SS 2019 Chapter 5 63

64 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 u=c g v=s 2 b f i 1 3 Q: e, f SS 2019 Chapter 5 64

65 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 u=c g v=s 2 b f i 1 3 Q: e, f SS 2019 Chapter 5 65

66 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 u=c g v=s 2 b f i 1 3 Q: e, f SS 2019 Chapter 5 66

67 Breadth-First Search Example
u=e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 b f i 1 3 Q: f SS 2019 Chapter 5 67

68 Breadth-First Search Example
u=e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 4 b f i 1 3 Q: f, g SS 2019 Chapter 5 68

69 Breadth-First Search Example
u=e 2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 4 b f i 1 3 Q: f, g SS 2019 Chapter 5 69

70 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 4 b u=f i 1 3 Q: g SS 2019 Chapter 5 70

71 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 4 b u=f i 1 3 4 Q: g, i SS 2019 Chapter 5 71

72 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c g v=s 2 4 b u=f i 1 3 4 Q: g, i SS 2019 Chapter 5 72

73 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c u=g v=s 2 4 b f i 1 3 4 Q: i SS 2019 Chapter 5 73

74 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 u=g v=s 2 4 b f i 1 3 4 Q: i, h SS 2019 Chapter 5 74

75 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 u=g v=s 2 4 b f i 1 3 4 Q: i, h SS 2019 Chapter 5 75

76 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f u=i 1 3 4 Q: h SS 2019 Chapter 5 76

77 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f u=i 1 3 4 Q: h SS 2019 Chapter 5 77

78 Breadth-First Search Example
2 h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f u=i 1 3 4 Q: h SS 2019 Chapter 5 78

79 Breadth-First Search Example
2 u=h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f i 1 3 4 Q: SS 2019 Chapter 5 79

80 Breadth-First Search Example
2 u=h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f i 1 3 4 Q: SS 2019 Chapter 5 80

81 Breadth-First Search Example
2 u=h BFS(G,s) 1. „initialize BFS“ 2. while Q do u  Dequeue(Q) for vAdj[u] do if color[v]=WHITE then color[v]  GRAY d[v]  d[u]+1; p[v]  u Enqueue(Q,v) color[u]  BLACK 1 3 c 5 g v=s 2 4 b f i 1 3 4 Q: SS 2019 Chapter 5 81

82 Breadth-First Search Runtime
Theorem 5.1: Given a graph G=(V,E) and a source s, algorithm BFS has a runtime of SS 2019 Chapter 5

83 Depth-First Search BFS(G,S) 1 for each node uV\{s} do color[u]WHITE
p[u]  NIL color[s]  GRAY p[s]  NIL Q  { } Enqueue(Q,s) while Q  { } do u  Dequeue(Q) for each vAdj[u] do if color[v]=WHITE then color[v]  GRAY p[v]  u Enqueue(Q,v) color[u]  BLACK DFS(G,S) 1 for each node uV\{s} do color[u]WHITE p[u]  NIL color[s]  GRAY p[s]  NIL Q  { } Push(Q,s) while Q  { } do u  Pop(Q) for each vAdj[u] do if color[v]=WHITE then color[v]  GRAY p[v]  u Push(Q,v) color[u]  BLACK SS 2019 Chapter 5

84 Depth-First Search Instead of an iterative approach we will look at a recursive approach of realizing depth-first search. Recursive approach: Initially: all nodes are white Discovered nodes become gray Fully processed nodes become black Two time stamps: d[v] and f[v] (lie between 1 and 2|V|) d[v]: time at which v is discovered f[v]: time at which v has been processed SS 2019 Chapter 5

85 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time SS 2019 Chapter 5 85

86 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time SS 2019 Chapter 5 86

87 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time SS 2019 Chapter 5 87

88 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=0 SS 2019 Chapter 5 88

89 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=0 u SS 2019 Chapter 5 89

90 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=0 u SS 2019 Chapter 5 90

91 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=0 u SS 2019 Chapter 5 91

92 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=1 u 1 SS 2019 Chapter 5 92

93 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=1 v u 1 SS 2019 Chapter 5 93

94 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=1 v u 1 SS 2019 Chapter 5 94

95 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=1 u 1 SS 2019 Chapter 5 95

96 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=2 u 2 1 SS 2019 Chapter 5 96

97 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=2 v u 2 1 SS 2019 Chapter 5 97

98 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=2 v u 2 1 SS 2019 Chapter 5 98

99 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=2 u 2 1 SS 2019 Chapter 5 99

100 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=3 u 3 2 1 SS 2019 Chapter 5 100

101 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=3 u 3 2 1 SS 2019 Chapter 5 101

102 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=3 u 3 2 1 SS 2019 Chapter 5 102

103 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=4 4 3 2 1 SS 2019 Chapter 5 103

104 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=4 4 u 3 2 1 v SS 2019 Chapter 5 104 104

105 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=4 4 u 3 2 1 v SS 2019 Chapter 5 105 105

106 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=4 4 3 2 1 u SS 2019 Chapter 5 106 106

107 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=5 4 3 2 1 u 5 SS 2019 Chapter 5 107 107

108 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=5 4 3 2 v 1 u 5 SS 2019 Chapter 5 108 108

109 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=5 4 3 2 v 1 u 5 SS 2019 Chapter 5 109 109

110 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=5 4 3 2 v 1 u 5 SS 2019 Chapter 5 110 110

111 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 3 2 u 6 1 5 SS 2019 Chapter 5 111 111

112 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 v 3 2 u 6 1 5 SS 2019 Chapter 5 112 112

113 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 v 3 2 u 6 1 5 SS 2019 Chapter 5 113 113

114 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 3 2 u v 6 1 5 SS 2019 Chapter 5 114 114

115 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 3 2 u v 6 1 5 SS 2019 Chapter 5 115 115

116 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=6 4 3 2 u v 6 1 5 SS 2019 Chapter 5 116 116

117 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=7 4 3 2 7 v 6 1 5 SS 2019 Chapter 5 117 117

118 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=7 4 3 2 7 6 1 u 5 SS 2019 Chapter 5 118 118

119 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=8 4 3 2 7 6 1 8 5 SS 2019 Chapter 5 119 119

120 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=8 4 u 3 2 7 6 1 8 5 SS 2019 Chapter 5 120 120

121 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=9 4 9 3 2 7 6 1 8 5 SS 2019 Chapter 5 121 121

122 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=9 4 9 3 2 7 u 6 1 v 8 5 SS 2019 Chapter 5 122 122

123 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=9 4 9 3 2 7 u 6 1 v 8 5 SS 2019 Chapter 5 123 123

124 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=9 4 9 3 2 7 6 1 u 8 5 SS 2019 Chapter 5 124 124

125 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 9 3 2 7 6 1 u 8 10 5 SS 2019 Chapter 5 125 125

126 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 v 9 3 2 7 6 1 u 8 10 5 SS 2019 Chapter 5 126 126

127 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 v 9 3 2 7 6 1 u 8 10 5 SS 2019 Chapter 5 127 127

128 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 9 3 2 7 6 1 u 8 v 10 5 SS 2019 Chapter 5 128 128

129 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 9 3 2 7 6 1 u 8 v 10 5 SS 2019 Chapter 5 129 129

130 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=10 4 9 3 2 7 6 1 u 8 v 10 5 SS 2019 Chapter 5 130 130

131 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=11 4 9 3 2 7 6 1 11 8 v 10 5 SS 2019 Chapter 5 131 131

132 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=11 4 9 3 2 7 6 1 11 8 10 5 SS 2019 Chapter 5 132 132

133 Depth-First Search DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=12 4 9 3 2 7 12 6 1 11 8 10 5 SS 2019 Chapter 5 133 133

134 Depth-First Search runtime: O(|V|+|E|) DFS(G)
1. for each vertex uV do color[u]  WHITE ; p[u]  nil 2. time  0 3. for each vertex uV do if color[u]=WHITE then DFS-Visit(u) DFS-Visit(u) 1. color[u]  GRAY 2. time  time +1; d[u]  time 4. for each vAdj[u] do if color[v] = WHITE then p[v]  u ; DFS-Visit(v) 6. color[u]  BLACK 7. time  time+1; f[u]  time time=12 4 9 3 2 7 12 6 1 11 8 10 5 runtime: O(|V|+|E|) SS 2019 Chapter 5 134 134

135 Depth-First Search Runtime
SS 2019 Chapter 5

136 Classification of edges:
Depth-First Search Classification of edges: Tree edges are edges of the DFS-forest in G Back edges are edges (u,v) that connect node u with one of its ancestors in the DFS-tree Forward edges are non-tree edges (u,v) that connect node u with one of its descendents in the DFS-tree Cross edges are the other edges 4 9 e b 3 2 7 12 a 6 1 11 8 c d 10 5 SS 2019 Chapter 5

137 Classification of edges:
Depth-First Search Classification of edges: Tree edges are edges of the DFS-forest in G Back edges are edges (u,v) that connect node u with one of its ancestors in the DFS-tree Forward edges are non-tree edges (u,v) that connect node u with one of its descendents in the DFS-tree Cross edges are the other edges back edge 4 9 e b 3 2 7 12 a 6 1 11 8 c d 10 5 SS 2019 Chapter 5

138 Classification of edges:
Depth-First Search Classification of edges: Tree edges are edges of the DFS-forest in G Back edges are edges (u,v) that connect node u with one of its ancestors in the DFS-tree Forward edges are non-tree edges (u,v) that connect node u with one of its descendents in the DFS-tree Cross edges are the other edges 4 forward edge 9 e b 3 2 7 12 a 6 1 11 8 c d 10 5 SS 2019 Chapter 5

139 Classification of edges:
Depth-First Search Classification of edges: Tree edges are edges of the DFS-forest in G Back edges are edges (u,v) that connect node u with one of its ancestors in the DFS-tree Forward edges are non-tree edges (u,v) that connect node u with one of its descendents in the DFS-tree Cross edges are the other edges 4 9 e b 3 2 7 cross edge 12 a 6 1 11 8 c d 10 5 SS 2019 Chapter 5

140 Classification of edges (v,w):
Depth-First Search Classification of edges (v,w): Edge type d[v]< d[w] f[v]>f[w] tree & forward yes back no cross SS 2019 Chapter 5

141 Recognition of acyclic directed graphs (DAG)
Depth-First Search Application: Recognition of acyclic directed graphs (DAG) Property: no directed cycles SS 2019 Chapter 5

142 Depth-First Search Theorem 5.3: The following statements are
equivalent: G is a DAG The DFS-tree does not contain a back edge (v,w)∈E: f[v]>f[w] Topological sort: Assign numbers s(v) to the nodes v so that for all edges (v,w)E, s(v)<s(w). Theorem 5.3 implies: When sorting the nodes in decreasing order of their finishing times, we obtain a topological sort. SS 2019 Chapter 5

143 Minimum Spanning Trees
Definition 5.4: A weighted undirected graph (G,w) is an undirected graph G=(V,E) with a weight function w:Eℝ For any subgraph H=(U,F) (i.e., U⊆V, F⊆E) of G, the weight w(H) of H is defined as w(H) = S w(e) e∈F SS 2019 Chapter 5

144 Minimum Spanning Trees
Definition 5.5: A subgraph H of an undirected graph G is called a spanning tree of G=(V,E) if H is a tree over all nodes of G. A spanning tree S of a weighted undirected graph G is called a minimum spanning tree (MST) of G if the weight of S is minimal among all spanning trees of G. SS 2019 Chapter 5

145 Minimum Spanning Trees
d c b a 1 4 3 2 Graph G=(V,E) d c b a 1 3 2 Spanning tree of G (minimal) (not minimal) d c b a 1 3 2 Spanning tree of G 3 d c b a 2 Spanning tree of G SS 2019 Chapter 5

146 Minimum Spanning Trees
8 7 b c d 4 9 2 4 a 11 i 14 e 6 7 8 10 h g f 1 2 SS 2019 Chapter 5

147 Minimum Spanning Trees
Goal: Given a weighted undirected graph (G,w) with G=(V,E), find a minimum spanning tree of (G,w). Approach: We successively extend the edge set A⊆E to a minimum spanning tree. Initially, A={ }. In each step, we extend A to A∪{ {u,v} }, where {u,v} is an A-safe edge, until |A|=|V|-1. Definition 5.6: {u,v} is called A-safe if under the assumption that A can be extended to an MST, also A∪{ {u,v} } can be extended to an MST. SS 2019 Chapter 5

148 Generic MST-Algorithm
Generic-MST(G,w) 1. A  { } 2. while A is not a spanning tree do find an A-safe edge {u,v} A  A ∪ { {u,v} } 5. return A Correctness: results from the definition of A-safe edges SS 2019 Chapter 5

149 Cuts in Graphs V\C C A Definition 5.7:
A cut (C,V\C) in a graph G=(V,E) is a partition of the node set V of the graph. An edge of G is crossing a cut (V,V\C) if one node of it is in C and the other node in V\C. A cut (C,V\C) respects a subset A⊆E if no edge in A crosses the cut. An edge crossing the cut (C,V\C) is called light if it has a minimal weight among all edges crossing (C,V\C). V\C C A SS 2019 Chapter 5

150 Cuts in Graphs 8 7 b c d 4 9 2 4 a 11 i 14 e 6 7 8 10 h g f 1 2 light
V-C V-C h g f 1 2 crossing edges SS 2019 Chapter 5

151 Characterization of Safe Edges
Theorem 5.8: Let (G,w) with G=(V,E) be a weighted undirected graph, and let A⊆E be contained in a minimum spanning tree of (G,w). Moreover, let (C,V\C) be a cut respecting A and {u,v} be a light edge crossing (C,V\C). Then {u,v} is an A-safe edge. Corollary 5.9: Let (G,w) with G=(V,E) be a weighted undirected graph, and let A⊆E be contained in a minimum spanning tree of (G,w). If {u,v} is an edge of mimimal weight that connects a connected component C of GA=(V,A) with the rest of the graph GA, then {u,v} is an A-safe edge. SS 2019 Chapter 5

152 Prim‘s Algorithm At any time of the algorithm, GA=(V,A) consists of a tree TA and a set of isolated nodes IA. A edge of minimal weight that connects IA with TA is added to A. TA u Node v with minimum w(u,v) over all edges crossing the cut SS 2019 Chapter 5

153 Prim‘s Algorithm At any time of the algorithm, GA=(V,A) consists of a tree TA and a set of isolated nodes IA. A edge of minimal weight that connects IA with TA is added to A. The nodes in IA are organized in a Min-Heap. The key d[v] of a node vIA is given by the minimal weight of an edge that connects v with TA. SS 2019 Chapter 5

154 Prim‘s Algorithm Prim(G,w,s)
1. for each vertex vV do d[v]; p[v]nil 2. d[s]0; QBuildHeap(V) 3. while Q do udeleteMin(Q) for each vertex vAdj[u] do if vQ and d[v]>w(u,v) then DecreaseKey(Q,v,w(u,v)); p[v]u DecreaseKey(Q,v,d) operation: Reduces d[v] of v in Q to d and performs heapifyUp from the current position of v to repair heap property. SS 2019 Chapter 5

155 Prim‘s Algorithm 8 7 b c d 4 9 2 4 a 11 i 14 e 6 7 8 10 h g f 1 2 4 d
SS 2019 Chapter 5

156 Prim‘s Algorithm 4 d e g c f b h a i 8 7 9 10 2 1 6 14 11 4 d e g c f
SS 2019 Chapter 5

157 Prim‘s Algorithm 4 d e g c f b h a i 8 7 9 10 2 1 6 14 11 4 d e g c f
SS 2019 Chapter 5

158 Prim‘s Algorithm 4 d e g c f b h a i 8 7 9 10 2 1 6 14 11 4 d e g c f
SS 2019 Chapter 5

159 Prim‘s Algorithm Correctness of Prim‘s algorithm for general instances
4 d e g c f b h a i 8 7 9 10 2 1 6 14 11 Correctness of Prim‘s algorithm for general instances (G,w): results from correctness of Generic-MST and Corollary 5.9. SS 2019 Chapter 5


Download ppt "Premaster Course Algorithms 1 Chapter 5: Basic Graph Algorithms"

Similar presentations


Ads by Google