Graph searching algorithms
This presentation is the property of its rightful owner.
Sponsored Links
1 / 75

Graph Searching Algorithms PowerPoint PPT Presentation


  • 36 Views
  • Uploaded on
  • Presentation posted in: General

Graph Searching Algorithms. Tree. Breadth-First Search (BFS). Breadth-First Search (BFS). Not discovered. white. u. ∞. 0. x. Discovered, adjacent white nodes. gray. v. y. ∞. ∞. Discovered, no adjacent white nodes. black. w. ∞. ∞. z. Breadth-First Search (BFS). u. ∞. 0.

Download Presentation

Graph Searching Algorithms

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Graph searching algorithms

Graph Searching Algorithms


Graph searching algorithms

Tree


Breadth first search bfs

Breadth-First Search (BFS)


Breadth first search bfs1

Breadth-First Search (BFS)

Not discovered

white

u

0

x

Discovered,

adjacent white nodes

gray

v

y

Discovered,

no adjacent white nodes

black

w

z


Breadth first search bfs2

Breadth-First Search (BFS)

u

0

x

BFS(G, u):

1. Initialize the graph

color[u]  gray

π[u]  Nil

d[u]  0

for each other vertex

color[u]  white

v

y

w

z


Breadth first search bfs3

Breadth-First Search (BFS)

Q

u

0

x

u

BFS(G, u):

2. Initialize the queue

Q  Ø

Enqueue(Q, u)

v

y

w

z


Breadth first search bfs4

Breadth-First Search (BFS)

t = u

Q

u

0

x

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

v

y

w

z


Breadth first search bfs5

Breadth-First Search (BFS)

t = u

r = x, v

Q

u

1

0

x

v

x

BFS(G, u):

3. While Q ≠ Ø

2) for each radj to t

if color[r] = white

color[r]  gray

π[r] t

d[r] d[t] + 1

Enqueue(Q, r)

v

y

1

w

z


Breadth first search bfs6

Breadth-First Search (BFS)

t = u

r = x, v

Q

u

1

0

x

v

x

BFS(G, u):

3. While Q ≠ Ø

3) color[t]  black

v

y

1

w

z


Breadth first search bfs7

Breadth-First Search (BFS)

t = v

Q

u

1

0

x

x

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

1

w

z


Breadth first search bfs8

Breadth-First Search (BFS)

t = v

r = y

Q

u

1

0

x

x

y

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

z


Breadth first search bfs9

Breadth-First Search (BFS)

t = v

r = y

Q

u

1

0

x

x

y

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

z


Breadth first search bfs10

Breadth-First Search (BFS)

t = x

r =

Q

u

1

0

x

y

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

z


Breadth first search bfs11

Breadth-First Search (BFS)

t = y

r = w

Q

u

1

0

x

w

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

3

z


Breadth first search bfs12

Breadth-First Search (BFS)

t = w

r = z

Q

u

1

0

x

z

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

3

4

z


Breadth first search bfs13

Breadth-First Search (BFS)

t = z

r =

Q

u

1

0

x

BFS(G, u):

3. While Q ≠ Ø

1) tDequeue(Q)

2) for each radj to t

3) color[t]  black

v

y

2

1

w

3

4

z


Breadth first search bfs14

Breadth-First Search (BFS)

u

1

0

x

BFS(G, u):

- the shortest-path distance

from u

v

y

2

1

w

3

4

z


Breadth first search bfs15

Breadth-First Search (BFS)

u

1

0

x

BFS(G, u):

- the shortest-path distance

from u

- construct a tree

v

y

2

1

w

3

4

z


Breadth first search bfs16

Breadth-First Search (BFS)

u

1

0

x

BFS(G, u):

- Initialization: |V|

- Enqueuing/dequeuing:

|V|

- Scanning adj vertices: |E|

v

y

2

1

w

3

4

z


Breadth first search bfs17

Breadth-First Search (BFS)

u

1

0

x

BFS(G, u):

- Initialization: O(|V|)

- Enqueuing/dequeuing:

O(|V|)

- Scanning adjacent vertices:

O(|E|)

=> total running time:

O(|V| + |E|)

v

y

2

1

w

3

4

z


Depth first search dfs

Depth-First Search (DFS)


Depth first search dfs1

Depth-First Search (DFS)

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

u

v

w


Depth first search dfs2

Depth-First Search (DFS)

timestamp: t

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

u

v

w


Depth first search dfs3

Depth-First Search (DFS)

timestamp: t+1

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

u

v

w

d[v] = t+1


Depth first search dfs4

Depth-First Search (DFS)

timestamp: t+2

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

u

v

w

d[v] = t+1

f[v] = t+2


Depth first search dfs5

Depth-First Search (DFS)

timestamp: t+3

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

u

v

w

d[v] = t+1

f[v] = t+2

d[w] = t+3


Depth first search dfs6

Depth-First Search (DFS)

timestamp: t+4

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

u

v

w

d[v] = t+1

f[v] = t+2

d[w] = t+3

f[v] = t+4


Depth first search dfs7

Depth-First Search (DFS)

timestamp: t+5

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

f[u] = t+5

u

v

w

d[v] = t+1

f[v] = t+2

d[w] = t+3

f[w] = t+4


Depth first search dfs8

Depth-First Search (DFS)

d[u]: when u is discovered

f[u]: when searching adj of u

is finished

d[u] = t

f[u] = t+5

u

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 disjoint

v

w

d[v] = t+1

f[v] = t+2

d[w] = t+3

f[w] = t+4


Depth first search dfs9

Depth-First Search (DFS)

Not discovered

white

u

x

Discovered,

adjacent white nodes

gray

v

y

Discovered,

no adjacent white nodes

black

w

z


Depth first search dfs10

Depth-First Search (DFS)

Not discovered

u

x

Discovered,

adjacent white nodes

d /

v

y

Discovered,

no adjacent white nodes

d / f

w

z


Depth first search dfs11

Depth-First Search (DFS)

u

x

DFS(G):

1. Initialization

for each u V[G],

color[u]  white

π[u]  Nil

time  0

v

y

w

z


Depth first search dfs12

Depth-First Search (DFS)

u

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

DFS-Visit(u):

1. Initial Setting

color[u]  gray

d[u]  time  time + 1

w

z


Depth first search dfs13

Depth-First Search (DFS)

u

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

2/

DFS-Visit(u):

1. Initial Setting

2. for each adjv of white

π[v] u

DFS-Visit[v]

w

z


Depth first search dfs14

Depth-First Search (DFS)

u

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/

2/

DFS-Visit(u):

1. Initial Setting

2. for each adjv of white

π[v] u

DFS-Visit[v]

w

z


Depth first search dfs15

Depth-First Search (DFS)

u

4/

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/

2/

DFS-Visit(u):

1. Initial Setting

2. for each adjv of white

π[v] u

DFS-Visit[v]

w

z


Depth first search dfs16

Depth-First Search (DFS)

u

4/5

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/

2/

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

z


Depth first search dfs17

Depth-First Search (DFS)

u

4/5

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

z


Depth first search dfs18

Depth-First Search (DFS)

u

4/5

1/

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

z


Depth first search dfs19

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

z


Depth first search dfs20

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

9/

z


Depth first search dfs21

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

9/

10/

z


Depth first search dfs22

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

9/

10/11

z


Depth first search dfs23

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

1. Initialization

2. For each u V[G]

if color[u] = white

DFS-Visit(u)

v

y

3/6

2/7

DFS-Visit(u):

1. Initial Setting

2. Handling adj vertices

3. color[u]  black

f[u]  time  time + 1

w

9/12

10/11

z


Depth first search dfs24

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

- construct a forest

v

y

3/6

2/7

w

9/12

10/11

z


Depth first search dfs25

Depth-First Search (DFS)

u

4/5

1/8

x

DFS(G):

- Initialization: O(|V|)

- Traversing vertices: O(|V|)

- Scanning adjacent vertices:

O(|E|)

=> total running time:

O(|V| + |E|)

v

y

3/6

2/7

w

9/12

10/11

z


Topological sorting

Topological Sorting

m

n

o

s

q

r

t

u

m

n

q

o

s

r

u

t


Topological sorting1

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.

m

n

o

s

q

r

t

u

O(|V|2 + |V||E|)

Or

O(|V|2)


Topological sorting2

Topological Sorting

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto

the front of the list.

m

n

o

s

q

r

t

u

O(|V| + |E|)


Topological sorting3

Topological Sorting

v enters the list before u?

u

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto

the front of the list.

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]:


Topological sorting4

Topological Sorting

v enters the list before u?

u

Using DFS

1. Call DFS(G)

2. When a vertex is

finished, put it onto

the front of the list.

t

v

1) v is white: d[u] < d[v]

At d[u]:

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

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


Topological sorting5

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

w


Depth first search dfs26

Depth-First Search (DFS)

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 disjoint

u

In a depth-first forest,

v is a descendant of u

if and only if

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

v

w


Topological sorting6

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]

Contradiction:

G is not acyclic.

At d[u]:

u

t

1) v is white: d[u] < d[v]

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

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

v


Topological sorting7

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]

Contradiction:

v should be black.

At d[u]:

u

t

1) v is white: d[u] < d[v]

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

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

v


Topological sorting8

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]

At d[u]:

u

t

1) v is white: d[u] < d[v]

t is white.

v


Topological sorting9

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]

At d[u]:

u

t

1) v is white: d[u] < d[v] < f[u]

t is white.

v


Topological sorting10

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]

v will enter the list before u.

At d[u]:

u

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]

v


Topological sorting11

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]

v is already in the list.

At d[u]:

u

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]

v


Topological sorting12

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]

Contradiction:

G is not acyclic.

At d[u]:

u

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]

v


Strongly connected component

Strongly Connected Component


Strongly connected component1

Strongly Connected Component


Strongly connected component2

Strongly Connected Component


Strongly connected component3

Strongly Connected Component

1

2

3

4

5

Call DFS(G)

Arrange the vertices

in order of decreasing f(u)

6

7

9

8


Strongly connected component4

Strongly Connected Component

1

2

3

4

5

Call DFS(G)

Arrange the vertices

in order of decreasing f(u)

Compute GT

6

7

9

8


Strongly connected component5

Strongly Connected Component

1

2

3

4

5

Call DFS(G)

Arrange the vertices

in order of decreasing f(u)

Compute GT

Run DFS(GT)

6

7

9

8


Strongly connected component6

Strongly Connected Component

1

2

3

4

5

Call DFS(G)

Arrange the vertices

in order of decreasing f(u)

Compute GT

Run DFS(GT)

6

7

9

8


Topological sorting13

Topological Sorting

n

o

s

r

n

o

r

s


Strongly connected component7

Strongly Connected Component


Strongly connected component8

Strongly Connected Component

Last f[u]

>

Last f[u]


Strongly connected component9

Strongly Connected Component

1

2

3

4

5

Call DFS(G)

Arrange the vertices

in order of decreasing f(u)

6

7

9

8


Strongly connected component10

Strongly Connected Component

2

1

3

4


Strongly connected component11

Strongly Connected Component

2

1

3

4


Adjacency matrix of graphs

Adjacency Matrix of Graphs

Directed graph

Adjacency-Matrix

1 2 3 4 5

1

2

1

2

5

3

3

4

4

5


Adjacency matrix of graphs1

Adjacency Matrix of Graphs

A =

AT =


  • Login