Professional Documents
Culture Documents
Graph Traversals
We can traverse graphs just like we traverse binary trees!
There are two types of graph traversals:
Depth-first and Breadth-first
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
}
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
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
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
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
Teacher’s
Lounge
Treasure
Room
Ghoul Warlock
Study Lounge
Ghost David’s
Galley Den
Carey’s
Corner
9
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.
Question:
What data structure could we use to implement this?
Answer:
Not a P, but a ?
11
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
Ghoul Warlock
Study Treasure
Lounge Room
Ghost
Galley
Carey’s
Corner
18
WA $900
$300
LA $400 NY
$140
$180 $220 FL
TX
19
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
Dist A B C D
0 6 2 4
22
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
DA 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.
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 acost
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
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…
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:
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