Docsity

Depth-First Search (DFS) Algorithm: Implementation and Running Time, Study notes of Computer Science

Partial preview of the text

Download Depth-First Search (DFS) Algorithm: Implementation and Running Time and more Study notes Computer Science in PDF only on Docsity!

Implementing Depth-First

Search

Section 3.

DFS Implementation

Explored nodes (shown in red) Stack

DFS Execution

Explored nodes (shown in red) Stack

Explored nodes (in red) Stack

DFS Execution

Explored nodes (in red) Stack

Explored nodes (in red) Stack

DFS Execution

Explored nodes (in red) Stack

Explored nodes (in red) Stack

DFS Execution

Explored nodes (in red) Stack

Explored nodes (in red) Stack

DFS Execution

Explored nodes (in red) Stack

Explored nodes (in red) Stack

DFS Execution

Explored nodes (in red) Stack

Explored nodes (in red) Stack

DFS Running Time p Running Time of DFS = O(m + n)

n Same as BFS

DFS Running Time p Main steps in the Algo.

n Adding and Deleting nodes to stack S

p Count node additions and deletions p Number of additions=Number of deletions

n Enough to count number of node additions

DFS Running Time p Main steps in the Algo.

n Adding and Deleting nodes to stack S

p Count node additions and deletions p Number of additions=Number of deletions

n Enough to count number of node additions

p Number of times node u gets added

= du (the degree of u)

DFS Running Time p Main steps in the Algo.

n Adding and Deleting nodes to stack S

p Count node additions and deletions p Number of additions=Number of deletions

n Enough to count number of node additions

p Number of times node u gets added

= du (the degree of u)

p Total Running time = O(Σu du) = O(m+n)