You are on page 1of 40

1

Graph Traversals
We can traverse graphs just like we traverse binary trees!
There are two types of graph traversals:
Depth-first and Breadth-first

A Depth-first Traversal keeps moving A Breadth-first Traversal explores


forward until it hits a dead end or a the graph in growing concentric circles,
previously-visited vertex… then it exploring all vertices 1 away from the
backtrack sand tries another path start, then 2 away, then 3 away, etc.

v (Dead end)
v
v (Previously visited!) v
start v v start v
v v
v v v v v
(Previously visited!)
v v v
v
1 step away
2 steps away v
3 steps away
2

Depth-first Traversals
Let’s learn the Depth-first Traversal algorithm first:

Depth-First-Traversal(curVertex)
{
If we’ve already visited the current vertex
Return
Otherwise
Mark the current vertex as visited
Process the current vertex (e.g., print it out)
For each edge leaving the current vertex
Determine which vertex the edge takes us to
Call Depth-First-Traversal on that vertex
}

(Notice that it’s recursive!)


3

Depth-first Traversal Demo curVertex

curVertex curVertex

1
v
0 2
Depth-First-Traversal(curVertex)
Depth-First-Traversal(curVertex)
{ v 3 v
Depth-First-Traversal(curVertex)
{ If we’ve already visited the current vertex
{ If we’ve already visited the current vertex
Return
If we’ve already visited the current vertex
Return
Otherwise
Return Processed vertex 0!
Otherwise
Mark the current vertex as visited Processed vertex 1!
Otherwise
Mark the current
Process vertex
the current as visited
vertex (e.g., print it out) Processed vertex 2!
Mark the current
Process vertex
the current as visited
vertex (e.g., print it out)
For the
Process eachcurrent
edge leaving
vertexthe current
(e.g., print vertex
it out)
For each edge leaving the current vertex
Determine which vertex the edge takes us to
For each
Calledge
Determine leaving the current
which vertex vertex
the edge
Depth-First-Traversal on takes
that us to
vertex
} Determine which vertex the edge
Call Depth-First-Traversal on thattakes us to
vertex
} Call Depth-First-Traversal on that vertex
}
4

Depth-first Traversal Demo curVertex

curVertex

1
v
0 2
Depth-First-Traversal(curVertex)
Depth-First-Traversal(curVertex)
{ v 3 v
Depth-First-Traversal(curVertex)
{ If we’ve already visited the current vertex v
{ If we’ve already visited the current vertex
Return curVertex
If we’ve already visited the current vertex
Return
Otherwise
Return Processed vertex 0!
Otherwise
Mark the current vertex as visited Processed vertex 1!
Otherwise
Mark the current
Process vertex
the current as visited
vertex (e.g., print it out) Processed vertex 2!
Mark the current
Process vertex
the current as visited
vertex (e.g., print it out) Processed vertex 3!
For each edge leaving the current vertex
Process the current vertex (e.g., print it out)
For each edge leaving
Determine which the current
vertex vertex
the edge takes us to
For each
Calledge
Determine leaving the current
which vertex vertex
the edge
Depth-First-Traversal on takes
that us to
vertex
} Determine which vertex the edge
Call Depth-First-Traversal on thattakes us to
vertex
} Call Depth-First-Traversal on that vertex
}
5

Depth-first Traversal Demo curVertex

curVertex

1
v
0 2
Depth-First-Traversal(curVertex) v 3 v
Depth-First-Traversal(curVertex)
{ v
{ If we’ve already visited the current vertex
If we’ve already visited the current vertex
Return
Return Processed vertex 0!
Otherwise Processed vertex 1!
Otherwise
Mark the current vertex as visited Processed vertex 2!
Mark the current
Process vertex
the current as visited
vertex (e.g., print it out) Processed vertex 3!
Process the current vertex (e.g., print it out)
For each edge leaving the current vertex
For each edge leaving
Determine the current
which vertex vertex
the edge takes us to
Determine which vertex the edge
Call Depth-First-Traversal takes
on that us to
vertex
} Call Depth-First-Traversal on that vertex
}
6

Depth-first Traversal Demo


curVertex

1
v
0 2
Depth-First-Traversal(curVertex) v 3 v
Depth-First-Traversal(curVertex)
{ v
{ If we’ve already visited the current vertex
curVertex
Return
If we’ve already visited the current vertex
Return But we’ve already visited Processed vertex 0!
Otherwise Vertex #3! Processed vertex 1!
Otherwise
Mark the current vertex as visited Processed vertex 2!
So we don’t want to do so
Process the current vertex (e.g., print it out)
Mark the current vertex as visited Processed vertex 3!
again!
Process the edge
For each current vertex
leaving the(e.g., print
current it out)
vertex
Determine
For each which the
edge leaving vertex the edge
current takes us to
vertex
Call Depth-First-Traversal
Determine on that
which vertex the edge vertex
takes us to
} Call Depth-First-Traversal on that vertex
}
7

Depth-firstVertex
Traversal
#0 has
Demo
no MORE outgoing edges...
curVertex
So there’s nothing to do!
1
v
0 2
v 3 v
Depth-First-Traversal(curVertex) v
{
If we’ve already visited the current vertex
Return Processed vertex 0!
Processed vertex 1!
Otherwise Processed vertex 2!
Mark the current vertex as visited Processed vertex 3!
Process the current vertex (e.g., print it out)
For each edge leaving the current vertex
Determine which vertex the edge takes us to And we’re done!
Call Depth-First-Traversal on that vertex
}
8

Depth-first Traversal Challenge


What does a Depth-first Traversal look like on this graph?

Teacher’s
Lounge

Treasure
Room

Goblin Witch Bats


Room Hangout Bathroom
Start
Room

Ghoul Warlock
Study Lounge
Ghost David’s
Galley Den
Carey’s
Corner
9

Implementing Depth-first Traversal w/Stack!


You can also implement your Depth-first Traversal with a
stack if you like! (What’s not to like???)

Depth-First-Search-With-Stack(start_room)
Push start_room on the stack
While the stack is not empty
Pop the top item off the stack and put it in variable c
If c hasn’t been visited yet
Drop a breadcrumb (we’ve visited the current room)
For each door d leaving the room
If the room r behind door d hasn’t been visited
Push r onto the stack.

Basically, the stack allows you to simulate recursion…


Or does the recursion allow you to simulate a stack?
Hmmmmmmm!
10

Breadth-first Graph Traversal


Idea:
Process all of the vertices that are 1 edge away
from the start vertex,
then process all vertices that are two edges away,
then process all vertices that are three edges away,
etc…

Question:
What data structure could we use to implement this?

Answer:
Not a P, but a ?
11

Breadth-first Graph Traversal


Breadth-First-Search (startVertex)
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Process vertex c (e.g., print its contents out)
For each vertex v directly reachable from c
If v has not yet been “discovered”
Mark v as “discovered”
Insert vertex v into the queue
}
Hmmm. Does this algorithm look familiar?

It’s a-maze-ingly similar to our queue-based


maze-solving algorithm!!!
12
Processed vertex 0!
Breadth-first Traversal Demo
Breadth-First-Search (startVertex)
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Process vertex c (e.g., print its contents out)
We haven’t
For each vertex v directly reachable from c discovered this this
If v has not yet been “discovered” Vertex yet!
Mark v as “discovered”
Insert vertex v into the queue
} v

c c
startVertex
Queue d 4
1
0
1
0 2
d
3
13
Processed vertex 0!
Breadth-first Traversal Demo
Breadth-First-Search (startVertex)
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Process vertex c (e.g., print its contents out) Vertex c has no
For each vertex v directly reachable from c other edges, so
If v has not yet been “discovered” we’re done with it.
Mark v as “discovered” We haven’t
Insert vertex v into the queue discovered this this
} Vertex yet! v

c c
Queue d 4
1
0 1 3
0 2
d
3 v
d
14
Processed vertex 0!
Breadth-first Traversal Demo Processed vertex 1!

Breadth-First-Search (startVertex)
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Process vertex c (e.g., print its contents out)#1 has no
Vertex
For each vertex v directly reachable from
morecoutgoing
We haven’t
If v has not yet been “discovered” edges…
discovered this this
Mark v as “discovered” Vertex
Ah ha! We haveyet!
Insert vertex v into the queue already discovered
} this this Vertex! c
v
c Queue d 4
1
1 3
2
0 2
d
3 d
d
15
Processed vertex 0!
Breadth-first Traversal Demo Processed vertex 1!
Processed vertex 3!

Breadth-First-Search (startVertex)
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Process vertex c (e.g., print its contents out)
For each vertex v directly reachable from c
If v has not yet been “discovered”
Mark v as “discovered” Vertex #3 has NO
Insert vertex v into the queue outgoing edges at
} all! So we’re done.

c Queue d 4
1
3 2
0 2
d
3 d
c d
16
Processed vertex 0!
Breadth-first Traversal
And finally we’re Demo Processed vertex 1!
done! Processed vertex 3!
Processed vertex 2!
Breadth-First-Search (startVertex) Processed vertex 4!
{
Add the starting vertex to our queue
Mark the starting vertex as “discovered”
While the queue is not empty
Dequeue the top vertex from the queue and place in c
Vertex #4 has NO
Process vertex c (e.g., print its contents out) outgoing edges…
For each vertex v directly reachable from c
Vertex #2 has no
If v has not yet been “discovered”
more outgoing
Mark v as “discovered” edges…
Insert vertex v into the queue v
}
c
c Queue d 4
1
4
2 d
0 2
d
3 d
d
17

Breadth-first Traversal Challenge


What does a Breadth-first Traversal look like on this graph?

Goblin Witch Bats


Room Hangout Bathroom
Start
Room

Ghoul Warlock
Study Treasure
Lounge Room
Ghost
Galley
Carey’s
Corner
18

Graphs With Weighted Edges


What does it mean for a graph to have weighted edges?

Definition: Each edge connecting vertex u with vertex v


has a weight or cost associated with it.

Question: Why would we want to have weighted edges?

WA $900
$300
LA $400 NY
$140
$180 $220 FL
TX
19

Graphs With Weighted Edges


Definition: The weight of a path from vertex u to vertex
v is the sum of the weights of the edges between the
two vertices.

WA $900 Question: What’s the cost of


$300 traveling from LA to NY to
WA?
LA $400 NY
$140 Question: What’s the
$180 $220 FL shortest path from LA to
TX
WA?
Definition: The shortest path between two vertices is
the path with the lowest total cost of edges between the
two vertices. (The shortest path is a set of vertices)
20

Finding the Shortest Path


Question: How can we find the shortest path between
any two nodes in a graph?
Answer: Dijkstra’s Algorithm (the dorm guy?)

Dijkstra’s Algorithm:

the length of
This algorithm determines the shortest path (i.e.
set of vertices) from a start vertex s to all other
vertices in the graph.
A 10 B So Dijkstra(A) would give
7
2 2 us a value of 6 for A to B,
C D a value of 2 for A to C,
2 6 and 4 for A to D.
21

Dijkstra’s Algorithm
Input: A graph G, and a starting vertex s

s G may not have any


A 10 negative edge values.
B
7
2 2
C D
2 6

Output: An array called Dist of optimal distances from s


to every other node in the graph.

Dist A B C D
0 6 2 4
22

Dijkstra’s Algorithm: Basic Idea


Dijkstra's algorithm splits vertices in two distinct sets: the
set of unsettled vertices and the set of settled vertices.

Unsettled vertex: A vertex v is unsettled if we don’t know


the optimal distance to it from the starting vertex s.

Settled vertex: A vertex v is settled if we have learned


the optimal distance to it from the starting vertex
s.
Initially all vertices Start vertex
are unsettled.
A
Unsettled
10 B
Unsettled
The algorithm ends 7
once all vertices are 2 2
in the settled set. C
Unsettled D
Unsettled
2 6
23

Dijkstra on a Graph
Assume that all vertices are
infinitely far away to start… 10
A
A B
Since we start at vertex A, 7
2 2
we know it’s the closest C D
vertex to us! How far is it? 2 6
Zero steps away! We can Distance 0 10 2 7
settle it immediately! (Best known so far)

A B C D
Now let’s see which unsettled vertices
we can reach directly from A.
• B is 10 units away.
• C is 2 units away.
• D is 7 units away.
And
Going going
directly
Ok, let’s directly
from
compare from
A toA
these B
C to
is D
costs only
is our
to only
10
2 units
units
7 units
away,
away,
current away,
which
which
best which
isis in
costs
less
is less
our than
than
table…infinity,
infinity,
so so
I’llI’ll
update
updatethisthis
entry…
entry
entry
too…
too…
24

Dijkstra on a Graph
Ok, so now we know the costs to
travel to all vertices directly 10
A
A B
reachable from A.
7
2 2
Which unsettled vertex is closest C D
to A? 2 6
Distance 0 10 2 7
Right! C is closest to A. (Best known so far)

A B C D
If we go directly to C (A  C), it costs us 2 units. Is
there any possible way I can travel to C cheaper by going
through B or through D first?
No Iway!
So
And if
know Ifthat
I travel
I travel through
through
if I travel B (e.g.
D (e.g.
directly A
A from
DA B
… …
to C,
C),IC),
at I
aknow
cost
know it’llme
it’ll2cost
of units,costatme
that’s at least
least
the 10(plus
7fastest
units units (plusroute.
possible
whateverwhatever
it costsit to
Thereforecosts
to travel
travel
we through
canthrough
settle atthe
Cthe other
2other
units. vertices
vertices to to
C). C).
25

Dijkstra on a Graph
At this point, we know the
shortest path from A to C. Now A
A 10 B
let’s see if we can travel 7
through C to reach one of our 2 2
other unsettled vertices faster. C D
2 6
Ok, which unsettled vertices can
be reached directly from C?
Distance 0 10
8 2 74
(Best known so far)
• B is 6 units away. A B C D
• D is 2 units away.

Let’s do B first. We know we can get from A to C in 2


Let’s do D next. We know we can get from A to C in 2
units, and we can directly go from C to B in 6 units, so we
units, and we can directly go from C to D in 2 units, so
can reach B in just 8 units!
we can reach D in just 4 units!
Is our new distance to B better than our old one?
Is our new distance to D better than our old one?
You bet!! Let’s update our table!
Yup!! Let’s update our table again!
26

Dijkstra on a Graph
Ok, so now we know the best
cost to get to all unsettled 10
A
A B
vertices, assuming we travel
7
through C. 2 2
C D
Which unsettled vertex is closest 2 6
to A now?
Distance 0 10
8 2 4
74
Right! D is closest.
(Best known so far)

A B C D
If we take the path A  C  D, it costs us 4 units. Is
there any possible way I can travel to D cheaper by going
through B first?
No Iway!
So knowIfthat
I travel through
if I travel B (e.g.
from A AC CD, B acost
at … ofD),
4
I knowthat’s
units, it’ll cost
the me at least
fastest 8 units.
possible That’s
route. much longer!
Therefore we can
settle D at 4 units.
27

Dijkstra on a Graph
At this point, we know the
shortest path from A to D. Now A
A 10 B
let’s see if we can travel 7
through D to reach one of our 2 2
other unsettled vertices faster. C D
2 6
Ok, which unsettled vertices can
be reached directly from D?
Distance 0 10
8 2 4
6 74
(Best known so far)
• B is 2 units away. A B C D

Let’s check B. We know we can get from A to D in 4


units, and we can directly go from D to B in 2 units, so
we can reach B in just 6 units!
Is our new distance to B better than our old one?
You bet!! Let’s update our table!
28

Dijkstra on a Graph
Ok, so now we know the best
cost to get to all unsettled 10
A
A B
vertices, assuming we travel
7
through D. 2 2
C D
Which unsettled vertex is closest 2 6
to A now?
Distance 0 10
6 2 474
Right! B is closest.
(Best known so far)

A B C D
If we take the path A  C  D  B it costs us 6 units.
Is there any possible way I can travel to B cheaper?
And now that all of our vertices are settled, we are
Noguaranteed
way! Theretoare no found
have other the minimum
vertices we can go through
travel distances
that will make our to
path from
each of A tovertices!
our B shorter.
Therefore we can settle B at 6 units.
29

Dijkstra
And now I’ll give you the more formal algorithm…

Born: 11 May 1930, Rotterdam, Netherlands


Died: 6 August 2002, Nuenen, Netherlands
30

Dijkstra’s Algorithm
Dijkstra’s Algorithm uses 2 data structures:

1. An array called Dist that holds the the current best known
cost to get from s to every other vertex in the graph.
For each vertex i, Dist[i] starts out with a value of:
• 0 for vertex s
• Infinity for all other vertices

Dist from
G A B C D
s vertex s
10 to… 0
A B
2 7 Idea: We start at node A so we’re
2
C D 0 steps away from node A. We
2 assume the other vertices are
infinitely far away from A.
31

Dijkstra’s Algorithm
Dijkstra’s Algorithm uses 2 data structures:

2. An array called Done that holds true for each vertex


that has been fully processed, and false otherwise.
For each vertex i, Done[i] starts out with a value of false.

Dist from
G A B C D
s vertex s
10 to… 0
A B
2 7 Done A B C D
2
C D false false false false
2
32

Dijkstra’s Algorithm
u v
While there are still unprocessed vertices: s 10
A B
Set u = the closest unprocessed vertex to 7
2
the start vertex s C D 2
v 2
Mark vertex u as processed: Done[u] = true.
We now know how to reach u optimally from s Previous cost:
Loop through all unprocessed vertices: New cost:
New cost: 0
0 ++ 10
2 ==210
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare: from
A B C D
a. the previously computed path from s to vertex
v (i.e. Dist[v]) OR s to… 0 10 2
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v))
Done A B C D
true false false false
false
If the new cost is less than old cost then u v v
Set Dist[v] = Dist[u] + weight(u,v)
33

Dijkstra’s Algorithm
u v
While there are still unprocessed vertices: s 10
A B
Set u = the closest unprocessed vertex to 7
2
the start vertex s C D 2
u2 vv
Mark vertex u as processed: Done[u] = true.
We now know how to reach u optimally from s Previous cost: 7
Loop through all unprocessed vertices: New cost: 0
2+7 2=74
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare: from
A B C D
a. the previously computed path from s to vertex
s to… 0 10 2 4
7
v (i.e. Dist[v]) OR
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v))
Done A B C D
true false
true false false
false
If the new cost is less than old cost then u v u vv
Set Dist[v] = Dist[u] + weight(u,v)
34

Dijkstra’s Algorithm
s uv
While there are still unprocessed vertices: 10 B
A
Set u = the closest unprocessed vertex to 7
2 2
the start vertex s C D
2 u
Mark vertex u as processed: Done[u] = true.
We now know how to reach u optimally from s Previous cost: 10
Loop through all unprocessed vertices: New cost: 4 + 2 = 6
Set v = the next unprocessed vertex
Dist
If there’s an edge from u to v then compare: from
A B C D
a. the previously computed path from s to vertex
s to… 0 6 2
10 4
v (i.e. Dist[v]) OR
b. the path from s to u, and then from u
to v (I.e. Dist[u] + weight(u,v))
Done A B C D
true false
true false
true false
false true
If the new cost is less than old cost then uv u
Set Dist[v] = Dist[u] + weight(u,v)
And we’re done! The Dist array contains the results.
35

Old slides 
36
Processed Vertex 0

Depth-First Traversal Processed Vertex 1


Processed Vertex 2
Andbool
here’s some more C++-like {false};
visited[GRAPH_SIZE]
We
code…
use a “visited” array to =track
 
where we’ve been – this Vertex
is like0 has two outgoing
bool visited[GRAPH_SIZE] = edges. For Vertex
{false};
checking for a digital breadcrumb! simplicity, ourNO outgoing
2 has
 
bool visited[GRAPH_SIZE] = {false}; 2
algorithm will pick theedges…
edge
 
void DFS(Graph G, int v)that goes to the lowest
{ 1 numbered vertex first.
So we’ve hit a dead end!
void DFS(Graph 0 v)
G, int
if ( visited[v] == true)
void{ DFS(Graph G, int v)// already been here
return;
{ if (else
visited[v]
{ == true)
if ( visited[v]
return; ==//true)
visited[v] =already
true; // been
drophere visited
breadcrumb 0 true
false
return; // already been
else {print(G[v]); here
else {visited[v] = true;
// process
// drop
vertex v
breadcrumb 1 true
false
}
visited[v] = true; ////drop
print(G[v]); breadcrumb
process vertex v true
2 false
} for (each edge e
print(G[v]); //leaving
processv) {
vertex v 3 false
} u = destination_vertex(e);
for (each
DFS(G,edge
u);e leaving v) { vu uv
for (each edge e leaving v)
u }= destination_vertex(e); { 1
u =}DFS(G,
destination_vertex(e);
u);
v
DFS(G, u); 0 2
}
}} 3
}
37
Processed Vertex 0

Depth-First Traversal Processed Vertex 1


Processed Vertex 2
Andbool
here’s some more C++-like
visited[GRAPH_SIZE] code…
= {false};
Processed Vertex 3
 

bool visited[GRAPH_SIZE] = {false};


bool  visited[GRAPH_SIZE] = {false}; 3
 
void DFS(Graph G, int v) Vertex 3 has NO outgoing
{ 1 edges…
void DFS(Graph G,
if ( visited[v]0 == true)
int v)
void{ DFS(Graph G, int v)// already been
return; So we’ve hit another
here
{ if (else
visited[v]
{ == true) dead end!
if ( visited[v]
return; ==//true)
visited[v] =already
true; //been
drophere
breadcrumbvisited 0 true
false
return; // already been
else {print(G[v]); here
else {visited[v] = true;
// process
// drop
vertex v
breadcrumb 1 true
false
}
visited[v] = true; ////drop
print(G[v]); breadcrumb
process vertex v true
2 false
} for (each edge e
print(G[v]); //leaving
processv) {
vertex v true
3 false
} u = destination_vertex(e);
for (each
DFS(G, edge
u);e leaving v) { vu
for (each edge e leaving v) {
u }= destination_vertex(e); 1
u =}DFS(G,
destination_vertex(e);
u);
DFS(G, u); 0 2
}
}} 3 u v
}
38
Processed Vertex 0

Depth-First Traversal Processed Vertex 1


Processed Vertex 2
And here’s some more C++-like code… Processed Vertex 3
We’ve now finished
processing all of Vertex 1’s
bool visited[GRAPH_SIZE] = {false};
outgoing edges so we’re done
bool  visited[GRAPH_SIZE] = {false};
  with it…
1
void DFS(Graph G, int0 v)
void{ DFS(Graph G, int v)
{ if ( visited[v] == true)
if ( visited[v]
return; ==//true)
already been here visited 0 true
false
return;
else { // already been here
else {visited[v] = true; // drop breadcrumb true
1 false
visited[v] = true; ////drop
print(G[v]); breadcrumb
process vertex v true
2 false
print(G[v]);
} // process vertex v true
3 false
}
for (each edge e leaving v) { vu
for (each edge e leaving v) {
u = destination_vertex(e); v 1
u = DFS(G,
destination_vertex(e);
u);
DFS(G, u); 0 2
}
}} 3
}
39
Processed Vertex 0

Depth-First Traversal Woot! Processed Vertex 1


Processed Vertex 2
And we’re done - we’ve finished
And here’s some more C++-like
processing code…
the entire graph!
Processed Vertex 3
We’ve already visited
Vertex
We’ve
bool visited[GRAPH_SIZE] = {false}; now 3, so there’s no
finished
reasonall
bool  visited[GRAPH_SIZE] = {false};
processing to of
visit it again.
Vertex 0’s Let’s
  return…
outgoing edges so we’re done
3 with it…
void DFS(Graph G, int0 v)
void{ DFS(Graph G, int v)
{ if ( visited[v] == true)
if ( visited[v]
return; ==//true)
already been here visited 0 true
false
return; // already been here
else {
else {visited[v] = true; // drop breadcrumb true
1 false
visited[v] = true; ////drop
print(G[v]); breadcrumb
process vertex v true
2 false
print(G[v]);
} // process vertex v true
3 false
}
for (each edge e leaving v) {
for (each edge e leaving v) {
u = destination_vertex(e); v 1
u = DFS(G,
destination_vertex(e);
u);
DFS(G, u); 0 2
}
}} 3 uv
}
40

You might also like