DSA1 | Week 6

Navigating Graphs using BFS

Try them all

Explore one thoroughly

What do you do when you have many different options?

Navigating Graphs

Navigating Graphs

Try them all

Explore one thoroughly

What do you do when you have many different options?

Phase 0.

Identify a

starting point \(s\).

Phase 1.

Collect

all friends of \(s\).

Phase 0.

Identify a

starting point \(s\).

Phase 1.

Collect

all friends of \(s\).

Phase 0.

Identify a

starting point \(s\).

Phase k.

Collect
anyone who is a
friend of
anyone seen before.

\(\cdots\)

\(s\)

\(x\)

\(y\)

\(w\)

\(z\)

\(s\)

\(x\)

\(y\)

\(w\)

\(z\)

\(s\)

\(x\)

\(y\)

\(w\)

\(z\)

\(s\)

\(x\)

\(y\)

\(w\)

\(z\)

Phase 1.

Collect

all friends of \(s\).

Phase 0.

Identify a

starting point \(s\).

Phase k.

Collect
anyone NEW
who is a

friend of
anyone seen before.

\(\cdots\)

Implementation?

ask every unvisited node if

they are adjacent to

any visited node.

Phase #\(k\).

execute_one_round():

    for all v in V(G):

          if v is not in visited:

for all u in N(v):

                 if u is in visited:

                      mark v    
    add all marked vertices to visited
execute_one_round():

    for all v in boundary(G):
        for all u in N(v):
            
               if u is unvisited &
               has unvisited neighbors:

                 mark u RED

               else if u is unvisited:

                 mark u BLUE 
       move boundary to deadzone
    
    add all RED vertices to boundary + visited

add all BLUE vertices to deadzone + visited

visited = [s]

repeat n times:
 
    execute_one_round()
visited[s] = true

add s to head of Q

while Q is non-empty:

    v = pop(Q) //remove the head of Q

    for u in N(v):

         if visited[u] is false:

              add u to visited and push(Q,u)

0

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

0

01

03

04

05

07

08

09

11

12

13

14

15

16

17

18

19

20

21

22

23

24

0

01

04

05

08

09

12

13

14

16

17

18

19

20

21

22

23

24