/
Graph Searching Algorithms Graph Searching Algorithms

Graph Searching Algorithms - PowerPoint Presentation

calandra-battersby
calandra-battersby . @calandra-battersby
Follow
379 views
Uploaded On 2018-01-18

Graph Searching Algorithms - PPT Presentation

Tree BreadthFirst Search BFS BreadthFirst Search BFS 0 u x v y z w white gray black Not discovered Discovered adjacent white nodes Discovered no adjacent white nodes ID: 624768

search dfs color white dfs search white color bfs depth visit black time breadth vertices discovered initialization sorting topological connected setting gray

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Graph Searching Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Graph Searching AlgorithmsSlide2

TreeSlide3

Breadth-First Search (BFS)Slide4

Breadth-First Search (BFS)

0

u

x

v

y

z

w

white

gray

black

Not discovered

Discovered,

adjacent white nodes

Discovered,

no adjacent white nodesSlide5

Breadth-First Search (BFS)

0

u

x

v

y

z

w

BFS(G,

u

):

1. Initialize the graph

color[u

]

gray

π[u

]  Nil

d[u]  0 for each other vertex color[u]

 whiteSlide6

Breadth-First Search (BFS)

0

u

x

v

y

z

w

Q

u

BFS(G,

u

):

2. Initialize the queue

Q

Ø

Enqueue(Q

,

u

)Slide7

Breadth-First Search (BFS)

Q

BFS(G,

u

): 3. While Q ≠ Ø 1)

t  Dequeue(Q)

0

u

x

v

y

z

w

t =

uSlide8

Breadth-First Search (BFS)

0

u

1

x

1

v

y

z

w

Q

v

BFS(G,

u

):

3. While Q ≠ Ø

2) for each

r

adj

to

t

if

color[r] = white color[r]  gray

π[r]  t d[r

]  d[t] + 1 Enqueue(Q, r

)

t =

u

r =

x

,

v

xSlide9

Breadth-First Search (BFS)

0

u

1

x

1

v

y

z

w

Q

v

BFS(G,

u

):

3. While Q ≠ Ø

3)

color[t

]

black

t =

u

r =

x

, v

xSlide10

Breadth-First Search (BFS)

0

u

1

x

1

v

y

z

w

Q

x

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

)

2) for each r adj to t … 3) color[t

]  blackt =

vSlide11

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

z

w

Q

x

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

)

2) for each r adj to t … 3) color[t

]  blackt =

vr = y

ySlide12

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

z

w

Q

x

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

) 2) for each

r adj to t … 3) color[t]

 blackt = v

r = y

ySlide13

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

z

w

Q

y

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

) 2) for each r

adj to t … 3) color[t]

 blackt = xr = Slide14

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

z

3

w

Q

w

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

) 2) for each r

adj to t … 3) color[t] 

blackt = yr = wSlide15

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

Q

z

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

) 2) for each r adj to t

… 3) color[t]  black

t = wr = zSlide16

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

Q

BFS(G,

u

):

3. While Q ≠ Ø

1)

t

Dequeue(Q

) 2) for each r adj to t …

3) color[t]  black

t = zr = Slide17

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

BFS(G,

u

):

- the shortest-path distance

from

uSlide18

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

BFS(G,

u

):

- the shortest-path distance

from

u

- construct a treeSlide19

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

BFS(G,

u

):

- Initialization: |V|

-

Enqueuing/dequeuing

:

|V|

- Scanning

adj

vertices: |E|Slide20

Breadth-First Search (BFS)

0

u

1

x

1

v

2

y

4

z

3

w

BFS(G,

u

):

- Initialization: O(|V|)

-

Enqueuing/dequeuing

:

O(|V|)

- Scanning adjacent vertices:

O(|E|)

=> total running time: O(|V| + |E|)Slide21

Depth-First Search (DFS)Slide22

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

v

wSlide23

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp:

t

d[u

] = t

v

wSlide24

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp: t+1

d[u

] =

t

v

w

d[v] = t+1Slide25

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp: t+2

d[u

] =

t

v

w

d[v] = t+1f[v] = t+2Slide26

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp: t+3

d[u

] =

t

v

w

d[v] = t+1f[v] = t+2

d[w

] = t+3Slide27

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp: t+4

d[u

] =

t

v

w

d[v] = t+1f[v] = t+2

d[w

] = t+3

f[v] = t+4Slide28

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

timestamp: t+5

d[u

] =

t

f[u] = t+5

vw

d[v] = t+1

f[v] = t+2

d[w

] = t+3f[w] = t+4Slide29

Depth-First Search (DFS)

u

d[u

]: when

u

is discovered

f[u

]: when searching

adj

of

u

is finished

d[u

] =

t

f[u] = t+5

v

w

d[v] = t+1f[v] = t+2

d[w

] = t+3

f[w] = t+4

d[u

] < f[u]

[ d[u], f[u] ] entirely contains [ d[v], f[v] ]

[ d[v], f[v] ] and [ d[w], f[w] ] are entirely disjointSlide30

Depth-First Search (DFS)

u

x

v

y

z

w

white

gray

black

Not discovered

Discovered,

adjacent white nodes

Discovered,

no adjacent white nodesSlide31

Depth-First Search (DFS)

u

x

v

y

z

w

d

/

d

/

f

Not discovered

Discovered,

adjacent white nodes

Discovered,

no adjacent white nodesSlide32

Depth-First Search (DFS)

u

x

v

y

z

w

DFS(G):

1. Initialization

for each

u

V[G],

color[u

]

white

π[u

]

Nil

time

0Slide33

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting color[u]

 gray d[u] 

time  time + 1

1/

u

x

v

y

z

wSlide34

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. for each adj v

of white π[v] 

u DFS-Visit[v]

1/

u

x

2/

v

y

z

wSlide35

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. for each adj v

of white π[v] 

u DFS-Visit[v]

1/

u

x

2/

v

3/

y

z

wSlide36

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. for each adj v

of white π[v] 

u DFS-Visit[v]

1/

u

4/

x

2/

v

3/

y

z

wSlide37

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/

u

4/5

x

2/

v

3/

y

z

wSlide38

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/

u

4/5

x

2/

v

3/6

y

z

wSlide39

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/

u

4/5

x

2/7

v

3/6

y

z

wSlide40

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/8

u

4/5

x

2/7

v

3/6

y

z

wSlide41

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/8

u

4/5

x

2/7

v

3/6

y

z

9/

wSlide42

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/8

u

4/5

x

2/7

v

3/6

y

10/

z

9/

wSlide43

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/8

u

4/5

x

2/7

v

3/6

y

10/11

z

9/

wSlide44

Depth-First Search (DFS)

DFS(G):

1. Initialization

2. For each

u V[G] if color[u] = white DFS-Visit(u

)DFS-

Visit(u): 1. Initial Setting 2. Handling adj vertices 3.

color[u]  black f[u

]  time  time + 1

1/8

u

4/5

x

2/7

v

3/6

y

10/11

z

9/12

wSlide45

Depth-First Search (DFS)

DFS(G):

- construct a forest

1/8

u

4/5

x

2/7

v

3/6

y

10/11

z

9/12

wSlide46

Depth-First Search (DFS)

1/8

u

4/5

x

2/7

v

3/6

y

10/11

z

9/12

w

DFS(G):

- Initialization: O(|V|)

- Traversing vertices: O(|V|)

- Scanning adjacent vertices:

O(|E|)

=> total running time:

O(|V| + |E|)Slide47

Topological Sorting

m

n

o

q

t

u

r

s

m

s

q

u

o

n

t

rSlide48

Topological Sorting

Brute-Force way

1. Find a vertex

without edges.

2. Put it onto the front of the list, and remove it from G. 3. Remove all edges to the removed edge.

4. Repeat 1~3.

n

o

q

t

u

r

s

m

O(|V|

2

+ |V||E|)

Or

O(|V|

2

)Slide49

Topological Sorting

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto the front of the list.

n

o

q

t

u

r

s

m

O(|V| + |E|)Slide50

Topological Sorting

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto the front of the list.

u

v

1)

v

is white:

d[u

] <

d[v

] <

f[u

]

2) v is black: f[v

] < d[u]3) v is gray: d[v] < d[u] <

f[v]At d[u]:

v enters the list before u?Slide51

Topological Sorting

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto the front of the list.

u

v

1)

v

is white:

d[u

] <

d[v

]

At

d[u

]:

v enters the list before u?

t

t is gray: d[t] < d[u

] < f[t]t is black: f[t] < d[u]Slide52

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] <

f[u] < d[v] < f[v]

u

v

wSlide53

Depth-First Search (DFS)

u

v

w

d[u

] <

f[u

]

[

d[u

],

f[u

] ] entirely contains [

d[v

],

f[v

] ]

[

d[v

],

f[v] ] and [ d[w

], f[w] ] are entirely disjointIn a depth-first forest,

v is a descendant of u if and only if d[u] <

d[v] < f[v] < f[u]Slide54

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

At

d[u

]:

1)

v

is white:

d[u

] <

d[v

]

t

is gray:

d[t

] <

d[u

] <

f[t

]

t is black:

f[t

] <

d[u

]

t

Contradiction:

G is not acyclic.Slide55

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

At

d[u

]:

1)

v

is white:

d[u

] <

d[v

]

t

is gray:

d[t

] <

d[u

] <

f[t

]

t is black:

f[t

] <

d[u

]

t

Contradiction:

v should be black.Slide56

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

At

d[u

]:

1)

v

is white:

d[u

] <

d[v

]

t

is white.

tSlide57

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

At

d[u

]:

1)

v

is white:

d[u

] <

d[v

] <

f[u

]

t

is white.

tSlide58

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

1)

v

is white:

d[u

] <

d[v

] <

f[u

]

2)

v

is black:

f[v

] <

d[u

]

3)

v

is gray:

d[v

] <

d[u

] <

f[v

]

At

d[u

]:

v

will enter the list before

u

.Slide59

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

1)

v

is white:

d[u

] <

d[v

] <

f[u

]

2)

v

is black:

f[v

] <

d[u

]

3)

v

is gray:

d[v

] <

d[u

] <

f[v

]

At

d[u

]:

v

is already in the list.Slide60

Topological Sorting

If

v

is a descendant,

d[u] < d[v] < f[v

] < f[u]If v is an

anscestor, d[v] < d[u] <

f[u] < f[v] Otherwise,

d[v] < f[v] < d[u] <

f[u] or d[u] < f[u] < d[v

] < f[v]

u

v

1)

v

is white:

d[u

] <

d[v

] <

f[u

]

2)

v

is black:

f[v

] <

d[u

]

3)

v

is gray:

d[v

] <

d[u

] <

f[v

]

At

d[u

]:

Contradiction:

G is not acyclic.Slide61

Strongly Connected ComponentSlide62

Strongly Connected ComponentSlide63

Strongly Connected ComponentSlide64

Strongly Connected Component

Call DFS(G)

Arrange the vertices

in order of decreasing

f(u

)

1

2

3

4

5

8

9

6

7Slide65

Strongly Connected Component

Call DFS(G)

Arrange the vertices

in order of decreasing

f(u

)

Compute G

T

1

2

3

4

5

8

9

6

7Slide66

Strongly Connected Component

Call DFS(G)

Arrange the vertices

in order of decreasing

f(u

)

Compute G

T

Run DFS(G

T

)

1

2

3

4

5

8

9

6

7Slide67

Strongly Connected Component

Call DFS(G)

Arrange the vertices

in order of decreasing

f(u

)

Compute G

T

Run DFS(G

T

)

1

2

3

4

5

8

9

6

7Slide68

Topological Sorting

n

o

r

s

s

o

n

rSlide69

Strongly Connected ComponentSlide70

Strongly Connected Component

Last

f[u

]

Last

f[u

]

>Slide71

Strongly Connected Component

Call DFS(G)

Arrange the vertices

in order of decreasing

f(u

)

1

2

3

4

5

8

9

6

7Slide72

Strongly Connected Component

4

3

1

2Slide73

Strongly Connected Component

4

3

1

2Slide74

Adjacency Matrix of Graphs

1

2

3

4

5

Directed graph

Adjacency-Matrix

1 2 3 4 5

5

3

4

2

1Slide75

Adjacency Matrix of Graphs

A =

A

T

=