SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Click to add Text
1
CSE 373
Graphs 1: Concepts,
Depth/Breadth-First Search
reading: Weiss Ch. 9
slides created by Marty Stepp
http://www.cs.washington.edu/373/
© University of Washington, all rights reserved.
2
What is a graph?
Seattle
New York
L.A.
Tokyo
Sydney
Seoul 128
140
181
30
16
56
3
Graphs
• graph: A data structure containing:
 a set of vertices V, (sometimes called nodes)
 a set of edges E, where an edge
represents a connection between 2 vertices.
• Graph G = (V, E)
• an edge is a pair (v, w) where v, w are in V
• the graph at right:
 V = {a, b, c, d}
 E = {(a, c), (b, c), (b, d), (c, d)}
• degree: number of edges touching a given vertex.
 at right: a=1, b=2, c=3, d=2
a
c
b
d
4
Graph examples
• For each, what are the vertices and what are the edges?
 Web pages with links
 Methods in a program that call each other
 Road maps (e.g., Google maps)
 Airline routes
 Facebook friends
 Course pre-requisites
 Family trees
 Paths through a maze
5
Paths
• path: A path from vertex a to b is a sequence of edges that can be
followed starting from a to reach b.
 can be represented as vertices visited, or edges taken
 example, one path from V to Z: {b, h} or {V, X, Z}
 What are two paths from U to Y?
• path length: Number of vertices
or edges contained in the path.
• neighbor or adjacent: Two vertices
connected directly by an edge.
 example: V and X
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
6
Reachability, connectedness
• reachable: Vertex a is reachable from b
if a path exists from a to b.
• connected: A graph is connected if every
vertex is reachable from any other.
 Is the graph at top right connected?
• strongly connected: When every vertex
has an edge to every other vertex.
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
a
c
b
d
a
c
b
d
e
7
Loops and cycles
• cycle: A path that begins and ends at the same node.
 example: {b, g, f, c, a} or {V, X, Y, W, U, V}.
 example: {c, d, a} or {U, W, V, U}.
 acyclic graph: One that does
not contain any cycles.
• loop: An edge directly from
a node to itself.
 Many graphs don't allow loops.
X
U
V
W
Z
Y
a
c
b
e
d
f
g
h
8
Weighted graphs
• weight: Cost associated with a given edge.
 Some graphs have weighted edges, and some are unweighted.
 Edges in an unweighted graph can be thought of as having equal weight
(e.g. all 0, or all 1, etc.)
 Most graphs do not allow negative weights.
• example: graph of airline flights, weighted by miles between cities:
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
9
Directed graphs
• directed graph ("digraph"): One where edges are one-way
connections between vertices.
 If graph is directed, a vertex has a separate in/out degree.
 A digraph can be weighted or unweighted.
 Is the graph below connected? Why or why not?
a
d
b
e
g
f
c
10
Digraph example
• Vertices = UW CSE courses (incomplete list)
• Edge (a, b) = a is a prerequisite for b
142
143 154
140
311
312
331
351
333
341
344
403
352
373
120
410
332
374
131
421
431 440
415
413
417
414
444
446
450
451
452
11
Linked Lists, Trees, Graphs
• A binary tree is a graph with some restrictions:
 The tree is an unweighted, directed, acyclic graph (DAG).
 Each node's in-degree is at most 1, and out-degree is at most 2.
 There is exactly one path from the root to every node.
• A linked list is also a graph:
 Unweighted DAG.
 In/out degree of at most 1 for all nodes.
F
B
A E
K
H
J
G
A B D
C
12
Searching for paths
• Searching for a path from one vertex to another:
 Sometimes, we just want any path (or want to know there is a path).
 Sometimes, we want to minimize path length (# of edges).
 Sometimes, we want to minimize path cost (sum of edge weights).
• What is the shortest path from MIA to SFO?
Which path has the minimum cost?
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
$500
13
Depth-first search
• depth-first search (DFS): Finds a path between two vertices by
exploring each possible path as far as possible before backtracking.
 Often implemented recursively.
 Many graph algorithms involve visiting or marking vertices.
• Depth-first paths from a to all vertices (assuming ABC edge order):
 to b: {a, b}
 to c: {a, b, e, f, c}
 to d: {a, d}
 to e: {a, b, e}
 to f: {a, b, e, f}
 to g: {a, d, g}
 to h: {a, d, g, h}
a
e
b c
h
g
d f
14
DFS pseudocode
function dfs(v1, v2):
dfs(v1, v2, { }).
function dfs(v1, v2, path):
path += v1.
mark v1 as visited.
if v1 is v2:
a path is found!
for each unvisited neighbor n of v1:
if dfs(n, v2, path) finds a path: a path is found!
path -= v1. // path is not found.
• The path param above is used if you want to have the
path available as a list once you are done.
 Trace dfs(a, f) in the above graph.
a
e
b c
h
g
d f
15
DFS observations
• discovery: DFS is guaranteed to
find a path if one exists.
• retrieval: It is easy to retrieve exactly
what the path is (the sequence of
edges taken) if we find it
• optimality: not optimal. DFS is guaranteed to find a path, not
necessarily the best/shortest path
 Example: dfs(a, f) returns {a, d, c, f} rather than {a, d, f}.
a
e
b c
h
g
d f
16
Breadth-first search
• breadth-first search (BFS): Finds a path between two nodes by
taking one step down all paths and then immediately backtracking.
 Often implemented by maintaining a queue of vertices to visit.
• BFS always returns the shortest path (the one with the fewest edges)
between the start and the end vertices.
 to b: {a, b}
 to c: {a, e, f, c}
 to d: {a, d}
 to e: {a, e}
 to f: {a, e, f}
 to g: {a, d, g}
 to h: {a, d, h}
a
e
b c
h
g
d f
17
BFS pseudocode
function bfs(v1, v2):
queue := {v1}.
mark v1 as visited.
while queue is not empty:
v := queue.removeFirst().
if v is v2:
a path is found!
for each unvisited neighbor n of v:
mark n as visited.
queue.addLast(n).
// path is not found.
• Trace bfs(a, f) in the above graph.
a
e
b c
h
g
d f
18
BFS observations
• optimality:
 always finds the shortest path (fewest edges).
 in unweighted graphs, finds optimal cost path.
 In weighted graphs, not always optimal cost.
• retrieval: harder to reconstruct the actual sequence of vertices or
edges in the path once you find it
 conceptually, BFS is exploring many possible paths in parallel, so it's not
easy to store a path array/list in progress
 solution: We can keep track of the path by storing predecessors for
each vertex (each vertex can store a reference to a previous vertex).
• DFS uses less memory than BFS, easier to reconstruct the path once
found; but DFS does not always find shortest path. BFS does.
a
e
b c
h
g
d f
19
DFS, BFS runtime
• What is the expected runtime of DFS and BFS, in terms of the
number of vertices V and the number of edges E ?
• Answer: O(|V| + |E|)
 where |V| = number of vertices, |E| = number of edges
 Must potentially visit every node and/or examine every edge once.
 why not O(|V| * |E|) ?
• What is the space complexity of each algorithm?
 (How much memory does each algorithm require?)
20
BFS that finds path
function bfs(v1, v2):
queue := {v1}.
mark v1 as visited.
while queue is not empty:
v := queue.removeFirst().
if v is v2:
a path is found! (reconstruct it by following .prev back to v1.)
for each unvisited neighbor n of v:
mark n as visited. (set n.prev = v.)
queue.addLast(n).
// path is not found.
 By storing some kind of "previous" reference associated with each
vertex, you can reconstruct your path back once you find v2.
a
e
b c
h
g
d f
prev

Weitere ähnliche Inhalte

Ähnlich wie 22-graphs1-dfs-bfs.ppt

Ähnlich wie 22-graphs1-dfs-bfs.ppt (20)

Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
Graphical reprsentation dsa (DATA STRUCTURE ALGORITHM)
 
Lecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdfLecture 16 - Dijkstra's Algorithm.pdf
Lecture 16 - Dijkstra's Algorithm.pdf
 
Graphs
GraphsGraphs
Graphs
 
LAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptxLAB7_FILS_DSA_graphs_datastructures.pptx
LAB7_FILS_DSA_graphs_datastructures.pptx
 
Graphs (1)
Graphs (1)Graphs (1)
Graphs (1)
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Graphs
GraphsGraphs
Graphs
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Bfs & dfs application
Bfs & dfs applicationBfs & dfs application
Bfs & dfs application
 
Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)Depth first traversal(data structure algorithms)
Depth first traversal(data structure algorithms)
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
Graphs
GraphsGraphs
Graphs
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 

Kürzlich hochgeladen

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 

Kürzlich hochgeladen (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 

22-graphs1-dfs-bfs.ppt

  • 1. Click to add Text 1 CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9 slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved.
  • 2. 2 What is a graph? Seattle New York L.A. Tokyo Sydney Seoul 128 140 181 30 16 56
  • 3. 3 Graphs • graph: A data structure containing:  a set of vertices V, (sometimes called nodes)  a set of edges E, where an edge represents a connection between 2 vertices. • Graph G = (V, E) • an edge is a pair (v, w) where v, w are in V • the graph at right:  V = {a, b, c, d}  E = {(a, c), (b, c), (b, d), (c, d)} • degree: number of edges touching a given vertex.  at right: a=1, b=2, c=3, d=2 a c b d
  • 4. 4 Graph examples • For each, what are the vertices and what are the edges?  Web pages with links  Methods in a program that call each other  Road maps (e.g., Google maps)  Airline routes  Facebook friends  Course pre-requisites  Family trees  Paths through a maze
  • 5. 5 Paths • path: A path from vertex a to b is a sequence of edges that can be followed starting from a to reach b.  can be represented as vertices visited, or edges taken  example, one path from V to Z: {b, h} or {V, X, Z}  What are two paths from U to Y? • path length: Number of vertices or edges contained in the path. • neighbor or adjacent: Two vertices connected directly by an edge.  example: V and X X U V W Z Y a c b e d f g h
  • 6. 6 Reachability, connectedness • reachable: Vertex a is reachable from b if a path exists from a to b. • connected: A graph is connected if every vertex is reachable from any other.  Is the graph at top right connected? • strongly connected: When every vertex has an edge to every other vertex. X U V W Z Y a c b e d f g h a c b d a c b d e
  • 7. 7 Loops and cycles • cycle: A path that begins and ends at the same node.  example: {b, g, f, c, a} or {V, X, Y, W, U, V}.  example: {c, d, a} or {U, W, V, U}.  acyclic graph: One that does not contain any cycles. • loop: An edge directly from a node to itself.  Many graphs don't allow loops. X U V W Z Y a c b e d f g h
  • 8. 8 Weighted graphs • weight: Cost associated with a given edge.  Some graphs have weighted edges, and some are unweighted.  Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1, etc.)  Most graphs do not allow negative weights. • example: graph of airline flights, weighted by miles between cities: ORD PVD MIA DFW SFO LAX LGA HNL
  • 9. 9 Directed graphs • directed graph ("digraph"): One where edges are one-way connections between vertices.  If graph is directed, a vertex has a separate in/out degree.  A digraph can be weighted or unweighted.  Is the graph below connected? Why or why not? a d b e g f c
  • 10. 10 Digraph example • Vertices = UW CSE courses (incomplete list) • Edge (a, b) = a is a prerequisite for b 142 143 154 140 311 312 331 351 333 341 344 403 352 373 120 410 332 374 131 421 431 440 415 413 417 414 444 446 450 451 452
  • 11. 11 Linked Lists, Trees, Graphs • A binary tree is a graph with some restrictions:  The tree is an unweighted, directed, acyclic graph (DAG).  Each node's in-degree is at most 1, and out-degree is at most 2.  There is exactly one path from the root to every node. • A linked list is also a graph:  Unweighted DAG.  In/out degree of at most 1 for all nodes. F B A E K H J G A B D C
  • 12. 12 Searching for paths • Searching for a path from one vertex to another:  Sometimes, we just want any path (or want to know there is a path).  Sometimes, we want to minimize path length (# of edges).  Sometimes, we want to minimize path cost (sum of edge weights). • What is the shortest path from MIA to SFO? Which path has the minimum cost? ORD PVD MIA DFW SFO LAX LGA HNL $500
  • 13. 13 Depth-first search • depth-first search (DFS): Finds a path between two vertices by exploring each possible path as far as possible before backtracking.  Often implemented recursively.  Many graph algorithms involve visiting or marking vertices. • Depth-first paths from a to all vertices (assuming ABC edge order):  to b: {a, b}  to c: {a, b, e, f, c}  to d: {a, d}  to e: {a, b, e}  to f: {a, b, e, f}  to g: {a, d, g}  to h: {a, d, g, h} a e b c h g d f
  • 14. 14 DFS pseudocode function dfs(v1, v2): dfs(v1, v2, { }). function dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: a path is found! for each unvisited neighbor n of v1: if dfs(n, v2, path) finds a path: a path is found! path -= v1. // path is not found. • The path param above is used if you want to have the path available as a list once you are done.  Trace dfs(a, f) in the above graph. a e b c h g d f
  • 15. 15 DFS observations • discovery: DFS is guaranteed to find a path if one exists. • retrieval: It is easy to retrieve exactly what the path is (the sequence of edges taken) if we find it • optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path  Example: dfs(a, f) returns {a, d, c, f} rather than {a, d, f}. a e b c h g d f
  • 16. 16 Breadth-first search • breadth-first search (BFS): Finds a path between two nodes by taking one step down all paths and then immediately backtracking.  Often implemented by maintaining a queue of vertices to visit. • BFS always returns the shortest path (the one with the fewest edges) between the start and the end vertices.  to b: {a, b}  to c: {a, e, f, c}  to d: {a, d}  to e: {a, e}  to f: {a, e, f}  to g: {a, d, g}  to h: {a, d, h} a e b c h g d f
  • 17. 17 BFS pseudocode function bfs(v1, v2): queue := {v1}. mark v1 as visited. while queue is not empty: v := queue.removeFirst(). if v is v2: a path is found! for each unvisited neighbor n of v: mark n as visited. queue.addLast(n). // path is not found. • Trace bfs(a, f) in the above graph. a e b c h g d f
  • 18. 18 BFS observations • optimality:  always finds the shortest path (fewest edges).  in unweighted graphs, finds optimal cost path.  In weighted graphs, not always optimal cost. • retrieval: harder to reconstruct the actual sequence of vertices or edges in the path once you find it  conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store a path array/list in progress  solution: We can keep track of the path by storing predecessors for each vertex (each vertex can store a reference to a previous vertex). • DFS uses less memory than BFS, easier to reconstruct the path once found; but DFS does not always find shortest path. BFS does. a e b c h g d f
  • 19. 19 DFS, BFS runtime • What is the expected runtime of DFS and BFS, in terms of the number of vertices V and the number of edges E ? • Answer: O(|V| + |E|)  where |V| = number of vertices, |E| = number of edges  Must potentially visit every node and/or examine every edge once.  why not O(|V| * |E|) ? • What is the space complexity of each algorithm?  (How much memory does each algorithm require?)
  • 20. 20 BFS that finds path function bfs(v1, v2): queue := {v1}. mark v1 as visited. while queue is not empty: v := queue.removeFirst(). if v is v2: a path is found! (reconstruct it by following .prev back to v1.) for each unvisited neighbor n of v: mark n as visited. (set n.prev = v.) queue.addLast(n). // path is not found.  By storing some kind of "previous" reference associated with each vertex, you can reconstruct your path back once you find v2. a e b c h g d f prev