100% found this document useful (1 vote)
26 views

Depth First Search

Depth first search (DFS) is an algorithm for traversing or searching trees or graphs. It starts at the root node and explores as far as possible along each branch before backtracking. The key steps are: (1) mark the current node and add it to a list, (2) if the current node has unexplored neighbors, select the first neighbor as the new current node, (3) else backtrack to the most recent node with unexplored neighbors and select the next neighbor. The algorithm terminates when the list is empty.

Uploaded by

jeanneta olivia
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
26 views

Depth First Search

Depth first search (DFS) is an algorithm for traversing or searching trees or graphs. It starts at the root node and explores as far as possible along each branch before backtracking. The key steps are: (1) mark the current node and add it to a list, (2) if the current node has unexplored neighbors, select the first neighbor as the new current node, (3) else backtrack to the most recent node with unexplored neighbors and select the next neighbor. The algorithm terminates when the list is empty.

Uploaded by

jeanneta olivia
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

15.082 and 6.

855J

Depth First Search

Depth First Search animation 1


Initialize

2 4 8

1 1 5 7

pred(1) =all
Unmark 0
nodes in N; 3 6 9
next := 1
Mark node s= 1
order(next)
LIST:= {1}

LIST 1

next 1
Depth First Search animation 2
Select a node i in LIST

2 4 8

1 1 5 7

In depth first
search, i is 3 6 9
the last node
in LIST

LIST 1

next 1
Depth First Search animation 3
If node i is incident to an admissible arc…
2 2 4 8

1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j)
add j to LIST

LIST 1 2

next 2
1
Depth First Search animation 4
Select the last node on LIST
2 2 4 8

1 1 5 7

3 6 9
Node 2 gets
selected

LIST 1 2

next 2
1
Depth First Search animation 5
If node i is incident to an admissible arc…
2 2 4 3 8

1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j)
add j to LIST

LIST 1 2 4

next 2
1
3
Depth First Search animation 6
Select
2 2 4 3 8

1 1 5 7

Select the
last node on 3 6 9
LIST

LIST 1 2 4

next 2
1
3
Depth First Search animation 7
If node i is incident to an admissible arc…
2 2 4 3 8 4

1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j)
add j to LIST

LIST 1 2 4 8

next 2
1
3
4
Depth First Search animation 8
Select
2 2 4 3 8 4

1 1 5 7

Select the
last node on 3 6 9
LIST

LIST 1 2 4 8

next 2
1
3
4
Depth First Search animation 9
If node i is not incident to an admissible arc…
2 2 4 3 8 4

1 1 5 7

Delete node i
from LIST 3 6 9

LIST 1 2 4 8

next 2
1
3
4
Depth First Search animation 10
Select
2 2 4 3 8 4

1 1 5 7

Select the
last node on 3 6 9
LIST

LIST 1 2 4 8

next 2
1
3
4
Depth First Search animation 11
If node i is incident to an admissible arc…
2 2 4 3 8 4

5
1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j)
add j to LIST

LIST 1 2 4 8
5

next 2
1
3
4
5
Depth First Search animation 12
Select
2 2 4 3 8 4

5
1 1 5 7

Select the
last node on 3 6 9
LIST

LIST 1 2 4 8
5

next 2
1
3
4
5
Depth First Search animation 13
If node i is incident to an admissible arc…
2 2 4 3 8 4

5
1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j) 6
add j to LIST

LIST 1 2 4 8
5 6

next 2
1
3
4
5
6
Depth First Search animation 14
Select the last node on LIST
2 2 4 3 8 4

5
1 1 5 7

Select node 6
3 6 9
6

LIST 1 2 4 8
5 6

next 2
1
3
4
5
6
Depth First Search animation 15
If node i is incident to an admissible arc…
2 2 4 3 8 4

5
1 1 5 7
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j) 6 7
add j to LIST

LIST 1 2 4 8
5 6 9

next 2
1
3
4
5
6
7
Depth First Search animation 16
Select the last node on LIST
2 2 4 3 8 4

5
1 1 5 7

Select node 9
3 6 9
6 7

LIST 1 2 4 8
5 6 9

next 2
1
3
4
5
6
7
Depth First Search animation 17
If node i is incident to an admissible arc…
2 2 4 3 8 4

5
1 1 5 7 8
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j) 6 7
add j to LIST

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 18
Select the last node on LIST
2 2 4 3 8 4

5
1 1 5 7 8

Select node 7
3 6 9
6 7

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 19
If node i is not incident to an admissible arc…
2 2 4 3 8 4

5
1 1 5 7 8

Delete node 7
from LIST 3 6 9
6 7

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 20
Select node 9
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
9 is9
not incident
from LIST 3 6 9
to an 6 7
admissible
arc.

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 21
Select node 6
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
6 is6
not incident
from LIST 3 6 9
to an 6 7
admissible
arc.

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 22
Select node 5
2 2 4 3 8 4

5
1 1 5 7 8

But node
Delete 5 is5
node
not
fromincident
LIST 3 6 9
to an 6 7
admissible
arc.

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 23
Select node 4
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
4 is4
not incident
from LIST 3 6 9
to an 6 7
admissible
arc.

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 24
Select node 2
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
2 is2
not incident
from LIST 3 6 9
to an 6 7
admissible
arc.

LIST 1 2 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
Depth First Search animation 25
Select node 1
2 2 4 3 8 4

5
1 1 5 7 8
Next :=Node
Select
Mark an j
Next + 1
admissible 9
pred(j) := i 3 6
arc (i,j):= next
order(j) 9 6 7
add j to LIST

LIST 1 2
3 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
9
Depth First Search animation 26
Select node 3
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
3 is3
not incident
from LIST 3 6 9
to an 9 6 7
admissible
arc.

LIST 1 2
3 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
9
Depth First Search animation 27
Select node 1
2 2 4 3 8 4

5
1 1 5 7 8

Delete
But node
node
1 is1
not incident
from LIST 3 6 9
to an 9 6 7
admissible
arc.

LIST 1 2
3 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
9
Depth First Search animation 28
LIST is empty
2 2 4 3 8 4

5
1 1 5 7 8

The algorithm
ends! 3 6 9
9 6 7

LIST 1 2
3 4 8
5 6 9 7

next 2
1
3
4
5
6
7
8
9
Depth First Search animation 29
The depth first search tree

1 1
Note that each
induced
22 3 9
subtree has
consecutively
labeled nodes 4 3

4 8 5 5

6 6

9 7

7 8
Depth First Search animation 30

You might also like