Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph...

19
1/8/2016 1 Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called edges. V={1, 2, 3,4} E = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)} Graphs 2

Transcript of Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph...

Page 1: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

1

Data Structures

Elementary Graph Algorithms

BFS, DFS & Topological Sort

1

• A graph, G = (V, E), consists of two sets:

– V is a finite non-empty set of vertices.

– E is a set of pairs of vertices, called edges.

V={1, 2, 3,4}

E = {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}

Graphs

2

Page 2: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

2

3

Graphs

• In an undirected graph

– The pair of vertices are unordered pairs.

– Thus, the pairs (v1, v2) and (v2, v1) are the same.

– No reflective (self) edges.

• In a directed graph

– The edges are represented by a directed pair (v1, v2).

– Therefore (v2, v1) and (v1, v2) are two different edges

– v1 is the tail and v2 the head of the edge.

4

Page 3: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

3

Graphs

• G1 and G2 are undirected. • G3 is a directed graph.

• G1 = ({1,2,3,4}, {(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)} ) • G2 = ({1,2,3,4,5,6,7}, {(1,2),(1,3),(2,4),(2,5),(3,6),(3,7)} ) • G3 = ({1,2,3}, {<1,2>, <2,1>, <2,3>})

5

Graphs

• The maximum number edges in

– An undirected graph with n vertices is n(n - 1)/2

– A directed graph with n vertices is n2

• An undirected graph with n vertices and exactly n(n-1)/2 edges is said to be complete

6

Page 4: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

4

Graphs

7

• If (v1,v2) is an edge of a graph G, then – We shall say that the vertices v1 and v2 are adjacent – And that the edge (v1,v2) is incident on vertices v1 and v2

• If (v1,v2) is a directed edge, then vertex v1 will be said to be adjacent to v2, while v2 is adjacent from v1.

• The degree of a vertex in undirected graph is the number of edges incident to that vertex. – Example: In G1, degree(2) = 2.

• In case G is a directed graph, we define – The in-degree of a vertex v is the number of edges for which v is the head. – The out-degree is the number of edges for which v is the tail. – Example: In G3, in-degree(3) = 1; out-degree(3) = 0.

Graphs

A subgraph of a graph G is a graph G' = (V’, E’) such that V' V and E' E.

8

Page 5: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

5

Graphs

• A path from vertex vp to vertex vq (vp ⇝ vq) in graph G is a sequence of vertices vp,vi1,vi2, ...,vin,vq such that (vp,vi1),(vi1,vi2), ...,(vin,vq) are edges

• The length of a path is the number of edges on it • A simple path is a path in which

– All vertices except possibly the first and last are distinct

(1,2,3,4) – simple path (1,4,2,3,4,5) – not simple

9

1

2 4

3

5

Graphs

• A cycle is a path in which

– The first and last vertices are the same

• A simple cycle – same with simple path

• When the graph is directed, we add the prefix "directed" to the terms

10

1

2 4

3

5

Page 6: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

6

Graphs

• In an undirected graph, G, two vertices v1 and v2 are said to be connected – If there is a path in G from v1 to v2. – Since G is undirected, this means there must also be a

path from v2 to v1.

• An undirected graph is said to be connected – If for every pair of distinct vertices vi, vj in there is a path

from vi to vj in G

11

1

2 4

3

5

1

2 4

3

5

connected graph not connected graph

Graphs

• A (connected) component of an undirected graph is a maximal connected subgraph

• A tree is a connected acyclic undirected graph

12

1

2 4

3

5

1

2 4

3

5

tree Two connected components

Page 7: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

7

Graphs

A forest is a acyclic undirected graph (not have to be connected)

13

Graphs

14

Page 8: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

8

Graph Representation

• Two common ways to represent a graph (either directed or undirected):

– Adjacency lists.

– Adjacency matrix.

15

Adjacency Lists

• Array Adj of |V| lists, – One per vertex. – The list Adj[u] contains all vertices v such that (u, v) E. – Works for both directed and undirected graphs.

• Space: Θ(V + E). – When expressing space/running time, we’ll drop the cardinality.

• Time to list all vertices adjacent to u: Θ(degree(u)). • Time to determine if (u, v) E: O(degree(u)).

16

Page 9: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

9

Adjacency Matrix

• Space: Θ(V2). • Time to list all vertices adjacent to u: Θ(V). • Time to determine if (u, v) E: O(1).

17

Breadth-First Search

• Given a graph G = (V, E) and a vertex s V

– Which vertices are reachable from s

– What is the shortest distance to each reachable vertex

– What are the shortest paths

18

Page 10: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

10

Breadth-First Search

• Algorithm Idea: – Send a wave out from s:

• First hits all vertices 1 edge from s.

• From there, hits all vertices 2 edges from s.

• Etc.

– Use FIFO queue Q to maintain wave front. • v Q if and only if wave has hit v but has not come out of v

yet.

19

Breadth-First Search

• Input: – Graph G = (V, E), either directed or undirected.

– Source vertex s V.

• Output: – v.d = distance to all v V from s.

• If v V is not reachable from s, v.d = ∞.

– v.π is the predecessor u of v on shortest path s ⇝ v. • If v V is not reachable from s, v.π will be null. • The set of edges {(v.π, v) : v ≠ s} forms a tree, such that u is v’s predecessor.

• Auxiliary Means: – every vertex has a color:

White - undiscovered Gray - discovered, but not finished (not done exploring from it) Black - finished (have found everything reachable from it)

20

Page 11: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

11

Breadth-First Search

21

r s t u v w x y

t y w w s

r t w t x

r

x

v

x u

y u

s

22

BFS(G, s) 1 for each vertex u G.V−{s} 2 u.color ← WHITE 3 u.d ← ∞ 4 u.π ← NULL 5 s.color ← GRAY 6 s.d ← 0 7 s.π ← NULL 8 Q ← ∅ 9 ENQUEUE(Q, s)

10 while Q ∅ 11 u ← DEQUEUE(Q) 12 for each v G.Adj[u] 13 if v.color = WHITE 14 v.color ← GRAY 15 v.d ← u.d + 1 16 v.π← u 17 ENQUEUE(Q, v) 18 u.color ← BLACK

Complexity: O(V + E)

Page 12: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

12

Printing The Shortest Path

1. PrintPath (G, s, v)

2. if (v = s) then

3. print s

4. else if (v.π = null) then

5. print "no path from" s "to" v "exists"

6. else

7. printPath (G, s, v.π)

8. print v

23

Depth-First Search

• Given a graph G = (V, E)

– Draw G as a forest of sub graphs

– Determine which vertex is a descendant of another in the forest

– Detect cycles

– Discover connected components

– Topological Sort

24

Page 13: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

13

Depth-First Search

• Algorithm Idea:

– Search deeper in the graph whenever possible:

• Start from an arbitrary unvisited vertex.

• Explore out one of the undiscovered edge of the most recently discovered vertex v.

• When all of v's edges have been explored, backtracks, and continue.

• Start all over again.

25

Depth-First Search

• Input: – Graph G = (V, E), either directed or undirected.

• Output: – v.d = discovery time & v.f= finishing time.

• A unique integer from 1 to 2|V| such that 1 ≤ v.d < v.f ≤ 2|V|.

– v.π = u such that u is the predecessor to v in the visit order. • The predecessor subgraph Gπ = (V, Eπ), where

Eπ={(v.π,v) : v.π ≠ null} forms a forest composed of several trees.

• Auxiliary Means: – every vertex has a color:

• White - undiscovered • Gray - discovered, but not finished (not done exploring from it) • Black - finished (have found everything reachable from it)

26

Page 14: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

14

27

28

DFS(G)

1 for each vertex u G.V

2 u.color ← WHITE

3 u.π← NULL

4 time ← 0

5 for each vertex u G.V

6 if u.color = WHITE

7 DFS-VISIT(G,u)

DFS-VISIT(G,u)

// white vertex u has just been discovered

1 u.color ← GRAY

2 time ← time+1

3 u.d ← time

4 for each v G.Adj[u] // explore edge (u, v)

5 if v.color = WHITE

6 v.π ← u

7 DFS-VISIT(G, v)

8 u.color ← BLACK

// blacken u; it is finished.

9 time ← time+1

10 u.f ← time

Page 15: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

15

Depth-First Search

• When one vertex is a descendant of another in the forest that was constructed by DFS?

– Parenthesis Theorem

– White-path Theorem

29

Parenthesis Theorem

• Theorem (Parenthesis theorem): – For all u, v, exactly one of the following holds:

1. u.d < u.f < v.d < v.f or v.d < v.f < u.d < u.f and neither of u and v is a descendant of the other.

2. u.d < v.d < v.f < u.f and v is a descendant of u. 3. v.d < u.d < u.f < v.f and u is a descendant of v.

– So u.d < v.d < u.f < v.f cannot happen.

• Like parentheses:

– OK: ( ) [ ] ( [ ] ) [ ( ) ] – Not OK: ( [ ) ] [ ( ] )

• Corollary (Nesting of descendants’ intervals): – v is a proper descendant of u if and only if u.d < v.d < v.f < u.f.

30

Page 16: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

16

White-path Theorem

• Theorem (White-path theorem):

– v is a descendant of u if and only if at time u.d, there is a path u ⇝ v consisting of only white vertices (except for u, which was just colored gray)

31

Classification of Edges

• Tree edge: – In the constructed forest. – Found by exploring (u, v).

• Back edge: – (u, v), where u is a descendant of v.

• Forward edge: – (u, v), where v is a descendant of u, but not a tree edge.

• Cross edge: – any other edge. – Can go between vertices in same tree or in different trees.

• In an undirected graph, there may be some ambiguity

since (u, v) & (v, u) are the same edge. – Classify by the first type above that matches

• Theorem:

– In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges.

32

Page 17: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

17

Classification of Edges

• Edge (u, v) can be classified by the color of v when the edge is first explored:

– WHITE - indicates a tree edge

– GRAY - indicates a back edge

– BLACK indicates a forward or cross edge.

• (u, v) is a forward edge if u.d < v.d

• (u, v) is a cross edge if u.d > v.d.

33

Detection of Cycles

• Lemma:

– A directed graph G is acyclic if and only if a DFS of G yields no back edges.

• Proof:

– Back edge Cycle

• Suppose there is a back edge (u, v)

• Then v is ancestor of u in the constructed forest

• Therefore, there is a path v ⇝ u, so v ⇝ u → v is a cycle

34

Page 18: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

18

Detection of Cycles

• Cycle back edge. – Suppose G contains cycle C.

– Let v be the first vertex discovered in C, and let (u, v) be the preceding edge in C.

– At time v.d, vertices of C form a white path v ⇝ u • since v is the first vertex discovered in c.

– By white-path theorem, u is descendant of v in depth-first forest.

– Therefore, (u, v) is a back edge.

• Similar for undirected graph.

35

Topological Sort

• Directed acyclic graph (DAG) is a directed graph with no cycles

• Good for modeling a partial order: – a > b and b > c a > c. – May have a and b such that

neither a > b nor b > c.

• Topological sort of a DAG: a

linear ordering of vertices such that if (u, v) E, then u appears somewhere before v.

36

X U V W Z Y

X U

V

W

Z

Y

Page 19: Data Structures - BGUfds161/wiki.files/Graphs_DFS_BFS.pdf · Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 •A graph, G = (V, E), consists of two sets:

1/8/2016

19

Topological Sort topologicalSort (G) // Assume G is a DAG

Call DFS(G) to compute finishing times v.f for all v V

as each vertex is finished, insert it onto the front of a linked list

return the linked list of vertices

Complexity: Θ(V + E)

37

Topological Sort

• Correctness: – Just need to show if (u, v) E, then v.f < u.f.

• When we explore (u, v), what are the colors of u and v? – u is gray. – v can’t be gray.

• Because then (u, v) is a back edge, but G is a dag.

– If v is white. • By parenthesis theorem, u.d < v.d < v.f < u.f.

– If v is black. • Then v is already finished, but u doesn't, therefore, v.f < u.f.

38