1 views

Uploaded by Sam

bfsDfs

- MCQ for IES GATE PSUs Practice Test Workbook Booklet
- A Course in Combinatorics Second Edition
- Prepare _ CodeChef
- AI Search.docx
- Dw 23733738
- Experiment 04
- SBC URI EscolaDeInverno Workshop 10-08-2014
- L13 Graph Part02
- Tennis Player and Ball
- anomalies in firewall policy
- Bfs
- ALgorithm Full First Ctvcxvcxv Syllabus
- Maths - Ijmcar - Some Isomorphism - Sanjay Sharma
- NEWS03
- KL Partitioning
- BackTrack
- consensus
- Algo Solution
- grfthyex1
- Grap

You are on page 1of 4

Given a directed graph G and a start vertex s ∈ V [G], a visitation algorithm for (G, s) is an

algorithm which visits all vertices of G that are reachable from s. In principle, the visitation of a

vertex u could consist of any kind of action (such as computing the credit score of the customer

represented by the vertex u).

Every vertex reachable from s must be visited exactly once. In order to enforce this rule, we use

a Boolean array visited on V [G]. Initially, visited[u] is false for all u ∈ V [G], and at the end

of the algorithm, visited[u] is true for all vertices reachable from s, and false for all vertices

unreachable from s. The actual act of visiting a vertex u is represented by the assignment

statement visited[u] := true.

Let n be the number of vertices of G and m the number of edges. We write Adj[v] to be the set

of out-neighbors of v. A vertex u is an out-neighbor of v if there is a directed edge from v to u.

We write In[v] to be the set of in-neighbors of v. A vertex u is an in-neighbor of v if there is a

directed edge from u to v, that is, if v is an out-neighbor of u.

We say that a visitation algorithm is a depth first search or DFS, algorithm, if vertices are visited

in depth first order. The requirements of depth first search are as follows:

1. s is visited first.

2. If v is reachable from s, and v 6= s, there is some vertex u ∈ In[v] such that u is visited

before v is visited. (In some algorithms, one such vertex is labeled the predecessor of v.)

3. If u, v ∈ V [G] are both reachable from s and if v is not reachable from u, and if u is visited

before v is visited, then all vertices reachable from u are visited before v is visited.

For a given directed graph and start vertex s, the order of DFS visitation is not necessarily unique.

All DFS algorithms, as far as I know, use a stack. It is possible to write a DFS algorithm without

an explicit stack data structure by using recursion, but that’s “cheating,” since you are actually

1

making use of the run-time stack. Since the purpose of this section is to show how to use a stack

for DFS search, we will not allow any of our algorithms to use recursion.

Each of our algorithms uses a stack S of vertices. A vertex can be pushed onto the stack more

than once, in fact, the same vertex could be on the stack in several places simultaneously. Being

pushed onto the stack does not necessarily imply visitation; we must indicate the visitation of v

by explicitly writing visited[v] := true, and that assignment may not occur more than once.

Here is a stack algorithm for depth first search, which we call DFS-A(G, s).

DFS-A(G,s)

for all v in V[G] do

visited[v] := false

end for

S := EmptyStack

Push(S,s)

while not Empty(S) do

u := Pop(S)

if not visited[u] then

visted[u] := true

for all w in Adj[u] do

if not visited[w] then

Push(S,w)

end if

end for

end if

end while

DFS-A can have multiple copies on the stack at the same time. However, the total number of

iterations of the innermost loop of DFS-A cannot exceed the number of edges of G, and thus the

size of S cannot exceed m. The running time of DFS-A is O(n + m).

It is possible to write a DFS algorithm where no vertex is ever in the stack in more than one

place, but it is somewhat trickier. We give such an algorithm, DFS-B, below.

DFS-B(G,s)

for all v in V[G] do

visited[v] := false

end for

S := EmptyStack

visited[s] := true

Push(S,s)

while not Empty(S) do

u := Pop(S)

if there is at least one unvisited vertex in Adj[u] then

Pick w to be any unvisited vertex in Adj[u]

Push(S,u)

visited[w] := true

Push(S,w)

2

end if

end while

No vertex can be on the stack in more than one place. The size of S is thus not more than n.

The time complexity of DFS-B is O(n + m).

Questions about DFS-B:

2. Prove that the number of times Push is executed is O(n). In fact, Push is executed exactly

2n − 1 times if all vertices are reachable from s. Hint: use amortized analysis.

3. All the statements of the code of DFS-B are executed O(n) times. Why can’t we say that

the time complexity of DFS-B is O(n)? Hint: Execution of Line 10 or 11 takes more than

one time step. How would you actually implement those lines?

We say that a visitation algorithm is a breadth first search or BFS, algorithm, if vertices are

visited in breadth first order. If v is reachable from s, let d[v] be the length of the shortest path

from s to v. Breadth first search must satisfy the following requirements:

1. If v is reachable from s, and v 6= s, there is some vertex u ∈ In[v] such that u is visited

before v is visited. (In some algorithms, one such vertex is labeled the predecessor of v.)

2. If u and v are both reachable from s, and if d[u] < d[v], then u must be visited before v is

visited.

For a given directed graph and start vertex s, the order of a BFS is not necessarily unique.

We will use a queue of vertices in our breadth first search algorithms. Here is one such algorithm.

BFS-A(G,s)

for all v in V[G] do

visited[v] := false

end for

Q := EmptyQueue

Enqueue(Q,s)

while not Empty(Q) do

u := Dequeue(Q)

if not visited[u] then

visted[u] := true

for all w in Adj[u] do

if not visited[w] then

3

Enqueue(Q,w)

end if

end for

end if

end while

the innermost loop of BFS-A does not exceed m, and therefore the size of Q never exceeds m The

running time of BFS-A is O(n + m).

On the other hand, algorithm BFS-B never has multiple copies of a vertex in Q.

BFS-B(G,s)

for all v in V[G] do

visited[v] := false

end for

Q := EmptyQueue

visited[s] := true

Enqueue(Q,s)

while not Empty(Q) do

u := Dequeue(Q)

for all w in Adj[u] do

if not visited[w] then

visited[w] := true

Enqueue(Q,w)

end if

end while

In BFS-B, the size of Q is never larger than n. The running time of BFS-B is O(n + m).

- MCQ for IES GATE PSUs Practice Test Workbook BookletUploaded byPravendraSingh
- A Course in Combinatorics Second EditionUploaded byMạnh Hiệp
- Prepare _ CodeChefUploaded byJOYDIP SARKAR
- AI Search.docxUploaded byLuke
- Dw 23733738Uploaded byAnonymous 7VPPkWS8O
- Experiment 04Uploaded byANTBLE
- SBC URI EscolaDeInverno Workshop 10-08-2014Uploaded byMarcel Santana
- L13 Graph Part02Uploaded byEnmusk
- Tennis Player and BallUploaded byLau Yeowhong
- anomalies in firewall policyUploaded byLeoKuriakose
- BfsUploaded byAlex Solomon A
- ALgorithm Full First Ctvcxvcxv SyllabusUploaded byChanel Hooper
- Maths - Ijmcar - Some Isomorphism - Sanjay SharmaUploaded byglobal-marketin8184
- NEWS03Uploaded byaliscribd46
- KL PartitioningUploaded byjakk234
- BackTrackUploaded bytrọng_hồ_1
- consensusUploaded byMohamad Ali Ansari
- Algo SolutionUploaded bySiddhArth JAin
- grfthyex1Uploaded byLuk TseMan
- GrapUploaded byNicolas Salcedo
- 05_Centrality.pdfUploaded byMaulinni'am MA
- Graph-Theoretic ScagnosticsUploaded bylgallindo
- dynamicProgramminggeneralUploaded byAnu G Nair
- 1617 Level MS-NA Mathematics Exam Related Materials T1 Wk8 UpdatedUploaded byMikhael
- Chapter 10 Review Sheet-QuestionUploaded byEamon Barkhordarian
- CoRALUploaded bylasfter
- Peter Komjath and Saharon Shelah- Finite subgraphs of uncountably chromatic graphsUploaded bySakoMC
- DM_chap7Uploaded byb9902233
- A note on competitive diffusion through social networksUploaded bymareyi
- 01StableMatching-2x2Uploaded byJohn Le Tourneux

- Place ValuesUploaded byFamediemame Oppong
- dcna-1Uploaded bysjslall
- FAS-115DG, Data Sheet 4921240116 UKUploaded byDario Guzman
- LEVEL B1Uploaded byVaidehi Deshpande
- OBE Advanced Database SystemsUploaded byRaul Gutierrez
- Art of Lapis LazuliUploaded bydeeptheonlyone
- AVR Instruction SetUploaded byPourya Eskandari
- Guia SoldaduraUploaded byotazmayta
- Recursive Automata 2Uploaded bybbscribed
- Event Management System Vb Net ReportUploaded bySikkandhar Jabbar
- STEP 7 - Function Block Diagram for S7-300 and S7-400Uploaded byKalind Singh
- Prelims 2016Uploaded byTarun Garg
- urdu_romanization.pdfUploaded byPoppi HS
- 9-12 eld standardsUploaded byapi-331215565
- master making connectionsUploaded byapi-290076952
- Actualizar PDMS 12.0 to 12.1Uploaded byMx Trailers
- Norroena - Embracing the History and Romance of Northern Europe - Vol 11Uploaded byDana Koop
- G. Richard Shell-Bargaining for Advantage-PENGUIN GroupUploaded byAaron Martin
- Which BibleUploaded byLakshmiprakash Rajagopalan
- a12 StandardUploaded byarachman297988
- An Introduction to Japanese - Syntax Grammar LanguageUploaded bymkahulhet017
- Mcs-012 Solved Assignment 2017-18Uploaded byVeeresh MSV
- About Private BrowsinUploaded byAnonymous 7DKWnOhru
- Coding MPP ProgramsUploaded byMahesh Ketkar
- The language problemUploaded bymaqsood hasni
- 5.Components of .NET FrameworkUploaded byRamesh Kumar
- POSAV V5 Ethernet Disk Logging ICD Rev18Uploaded byFilip Skultety
- Design and Implementation of Truncated Multiplier in FIR FilterUploaded byIJARTET
- Plflow 2 UserguideUploaded bysajitkalidas
- IglesiasUploaded byPallikaras Evangelos