Dfs presentation

41

description

 

Transcript of Dfs presentation

Page 1: Dfs presentation
Page 2: Dfs presentation

PRESETA

TION

DATA S

TRUCTU

RE

Page 3: Dfs presentation

Click icon to add picture

GROUP

MEM

BERS

WAJIH

A IMTI

AZ

AMMARA SID

DIQUI

SAMIA JA

VED

HAMNA AFZ

AL

RUBAB ZAHRA

Page 4: Dfs presentation

DEPTH F

IRST

SEARCH

Page 5: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 6: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 7: Dfs presentation

Definition:The aim of the DFS algorithm is travers the graph in such a way that is try to go

for from the root node. Stack is use in the implementation of the depth first search. Lets see how depth first search work with

respect to the following graph.

Page 8: Dfs presentation

a d

cf

e

b

Un Directed graph

Page 9: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 10: Dfs presentation

ProcessAs stated before in DFS nodes are visited by going through the depth of the tree from the starting node if we do the depth first traversal of the above graph and print the visited node it will be “ A B C D E F CD “ DFS visited the root node then its children nodes until it reach the end node E and F them moves up to the parents nodes

Page 11: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 12: Dfs presentation

Algorithm stepsStep:1 Push the root node in stack.Step:2 Loop until stack is empty.Step:3 Peek the node of the stack.Step:4 If the node has unvisited child nodes get the unvisited child node mark it has travers and push it on stack.

Page 13: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 14: Dfs presentation

Directed graph

Example

Page 15: Dfs presentation
Page 16: Dfs presentation

16

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

Page 17: Dfs presentation

17

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

visit(F)F-E

Page 18: Dfs presentation

18

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

Page 19: Dfs presentation

19

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

Page 20: Dfs presentation

20

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

Page 21: Dfs presentation

21

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

dfs(D)D-C D-F

Page 22: Dfs presentation

22

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

dfs(D)D-C D-F

Page 23: Dfs presentation

23

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-GFunction call

stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

dfs(D)D-C D-F

Page 24: Dfs presentation

24

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(C)C-A C-D

Page 25: Dfs presentation

25

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

Page 26: Dfs presentation

26

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

Page 27: Dfs presentation

27

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

dfs(G)

Page 28: Dfs presentation

28

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

dfs(E)E-C E-D E-G

Page 29: Dfs presentation

29

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

dfs(F)F-E

Page 30: Dfs presentation

30

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

Page 31: Dfs presentation

31

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

dfs(A)A-F A-G

Function call stack:

Page 32: Dfs presentation

32

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

Nodes reachable from A: A, C, D, E, F, G

Page 33: Dfs presentation

33

DIRECTED DEPTH FIRST SEARCH

F

A

B C G

D

E

H

I

Nodes reachable from A: A, C, D, E, F, G

Page 34: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 35: Dfs presentation

Assume that graph is connected. Depth-first search visits every vertex in the graph and checks every edge its edge. Therefore, DFS complexity is O(V + E). As it was mentioned before, if an adjacency matrix is used for a graph representation, then all edges, adjacent to a vertex can't be found efficiently, that results in O(V2) complexity.

Time Complexity

Page 36: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 37: Dfs presentation

Advantage of depth first search

• The advantage of depth-first Search is that memory requirement is only linear with respect to the search graph. This is in contrast with breadth-first search which requires more space. The reason is that the algorithm only needs to store a stack of nodes on the path from the root to the current node.

• The time complexity of a depth-first Search to depth d is O(b^d) since it generates the same set of nodes as breadth-first search, but simply in a different order. Thus practically depth-first search is time-limited rather than space-limited.

• If depth-first search finds solution without exploring much in a path then the time and space it takes will be very less.

Page 38: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 39: Dfs presentation

Disadvantages • The disadvantage of Depth-First Search is that there is a possibility that it may go down the left-most path forever. Even a finite graph can generate an infinite tree. One solution to this problem is to impose a cutoff depth on the search. Although the ideal cutoff is the solution depth d and this value is rarely known in advance of actually solving the problem. If the chosen cutoff depth is less than d, the algorithm will fail to find a solution, whereas if the cutoff depth is greater than d, a large price is paid in execution time, and the first solution found may not be an optimal one. • Depth-First Search is not guaranteed to find the solution. • And there is no guarantee to find a minimal solution, if more than one solution exists.

Page 40: Dfs presentation

OUT LIN

ES

DefinitionProcess Algorithmic stepsExampleCodeTime ComplexityAdvantagesDisadvantages

Page 41: Dfs presentation